Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,12 @@
|
|
| 1 |
import os
|
| 2 |
-
import
|
| 3 |
from generate_prompts import generate_prompt
|
| 4 |
from diffusers import AutoPipelineForText2Image
|
| 5 |
from io import BytesIO
|
| 6 |
import gradio as gr
|
| 7 |
-
|
| 8 |
|
| 9 |
-
# Load the model once
|
| 10 |
print("Loading the model...")
|
| 11 |
model = AutoPipelineForText2Image.from_pretrained("stabilityai/sdxl-turbo")
|
| 12 |
print("Model loaded successfully.")
|
|
@@ -25,21 +25,20 @@ def generate_image(prompt, prompt_name):
|
|
| 25 |
image.save(buffered, format="JPEG")
|
| 26 |
image_bytes = buffered.getvalue()
|
| 27 |
print(f"Image bytes length for {prompt_name}: {len(image_bytes)}")
|
| 28 |
-
return image_bytes
|
| 29 |
except Exception as e:
|
| 30 |
print(f"Error saving image for {prompt_name}: {e}")
|
| 31 |
-
return None
|
| 32 |
else:
|
| 33 |
raise Exception(f"No images returned by the model for {prompt_name}.")
|
| 34 |
except Exception as e:
|
| 35 |
print(f"Error generating image for {prompt_name}: {e}")
|
| 36 |
-
return None
|
| 37 |
|
| 38 |
-
|
| 39 |
-
print(f"
|
|
|
|
| 40 |
prompts = []
|
| 41 |
-
|
| 42 |
-
# Generate prompts for each paragraph
|
| 43 |
for paragraph_number, sentences in sentence_mapping.items():
|
| 44 |
combined_sentence = " ".join(sentences)
|
| 45 |
print(f"combined_sentence for paragraph {paragraph_number}: {combined_sentence}")
|
|
@@ -47,38 +46,26 @@ async def queue_api_calls(sentence_mapping, character_dict, selected_style):
|
|
| 47 |
prompts.append((paragraph_number, prompt))
|
| 48 |
print(f"Generated prompt for paragraph {paragraph_number}: {prompt}")
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
tasks = [loop.run_in_executor(executor, generate_image, prompt, f"Prompt {paragraph_number}") for paragraph_number, prompt in prompts]
|
| 57 |
-
print("Tasks created for image generation.")
|
| 58 |
-
responses = await asyncio.gather(*tasks)
|
| 59 |
-
print("Responses received from image generation tasks.")
|
| 60 |
|
| 61 |
-
images = {
|
| 62 |
print(f"Images generated: {images}")
|
| 63 |
return images
|
| 64 |
|
| 65 |
def process_prompt(sentence_mapping, character_dict, selected_style):
|
| 66 |
print(f"process_prompt called with sentence_mapping: {sentence_mapping}, character_dict: {character_dict}, selected_style: {selected_style}")
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
asyncio.set_event_loop(loop)
|
| 74 |
-
print("Event loop created.")
|
| 75 |
-
|
| 76 |
-
# This sends the prompts to function that sets up the async calls. Once all the calls to the API complete, it returns a list of the gr.Textbox with value= set.
|
| 77 |
-
cmpt_return = loop.run_until_complete(queue_api_calls(sentence_mapping, character_dict, selected_style))
|
| 78 |
-
print(f"process_prompt completed with return value: {cmpt_return}")
|
| 79 |
-
return cmpt_return
|
| 80 |
|
| 81 |
-
# Gradio interface with high concurrency limit
|
| 82 |
gradio_interface = gr.Interface(
|
| 83 |
fn=process_prompt,
|
| 84 |
inputs=[
|
|
|
|
| 1 |
import os
|
| 2 |
+
import concurrent.futures
|
| 3 |
from generate_prompts import generate_prompt
|
| 4 |
from diffusers import AutoPipelineForText2Image
|
| 5 |
from io import BytesIO
|
| 6 |
import gradio as gr
|
| 7 |
+
import json
|
| 8 |
|
| 9 |
+
# Load the model once globally
|
| 10 |
print("Loading the model...")
|
| 11 |
model = AutoPipelineForText2Image.from_pretrained("stabilityai/sdxl-turbo")
|
| 12 |
print("Model loaded successfully.")
|
|
|
|
| 25 |
image.save(buffered, format="JPEG")
|
| 26 |
image_bytes = buffered.getvalue()
|
| 27 |
print(f"Image bytes length for {prompt_name}: {len(image_bytes)}")
|
| 28 |
+
return prompt_name, image_bytes
|
| 29 |
except Exception as e:
|
| 30 |
print(f"Error saving image for {prompt_name}: {e}")
|
| 31 |
+
return prompt_name, None
|
| 32 |
else:
|
| 33 |
raise Exception(f"No images returned by the model for {prompt_name}.")
|
| 34 |
except Exception as e:
|
| 35 |
print(f"Error generating image for {prompt_name}: {e}")
|
| 36 |
+
return prompt_name, None
|
| 37 |
|
| 38 |
+
def process_prompts(sentence_mapping, character_dict, selected_style):
|
| 39 |
+
print(f"process_prompts called with sentence_mapping: {sentence_mapping}, character_dict: {character_dict}, selected_style: {selected_style}")
|
| 40 |
+
|
| 41 |
prompts = []
|
|
|
|
|
|
|
| 42 |
for paragraph_number, sentences in sentence_mapping.items():
|
| 43 |
combined_sentence = " ".join(sentences)
|
| 44 |
print(f"combined_sentence for paragraph {paragraph_number}: {combined_sentence}")
|
|
|
|
| 46 |
prompts.append((paragraph_number, prompt))
|
| 47 |
print(f"Generated prompt for paragraph {paragraph_number}: {prompt}")
|
| 48 |
|
| 49 |
+
num_prompts = len(prompts)
|
| 50 |
+
print(f"Number of prompts: {num_prompts}")
|
| 51 |
|
| 52 |
+
with concurrent.futures.ThreadPoolExecutor(max_workers=num_prompts) as executor:
|
| 53 |
+
tasks = {executor.submit(generate_image, prompt, f"Prompt {paragraph_number}"): paragraph_number for paragraph_number, prompt in prompts}
|
| 54 |
+
results = {tasks[future]: future.result() for future in concurrent.futures.as_completed(tasks)}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
+
images = {prompt_name: image for prompt_name, (prompt_name_key, image) in results.items()}
|
| 57 |
print(f"Images generated: {images}")
|
| 58 |
return images
|
| 59 |
|
| 60 |
def process_prompt(sentence_mapping, character_dict, selected_style):
|
| 61 |
print(f"process_prompt called with sentence_mapping: {sentence_mapping}, character_dict: {character_dict}, selected_style: {selected_style}")
|
| 62 |
+
# Check if inputs are already in dict form
|
| 63 |
+
if isinstance(sentence_mapping, str):
|
| 64 |
+
sentence_mapping = json.loads(sentence_mapping)
|
| 65 |
+
if isinstance(character_dict, str):
|
| 66 |
+
character_dict = json.loads(character_dict)
|
| 67 |
+
return process_prompts(sentence_mapping, character_dict, selected_style)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
|
|
|
| 69 |
gradio_interface = gr.Interface(
|
| 70 |
fn=process_prompt,
|
| 71 |
inputs=[
|