Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,8 @@
|
|
1 |
-
import asyncio
|
2 |
import gradio as gr
|
3 |
from diffusers import AutoPipelineForText2Image
|
4 |
-
from
|
|
|
|
|
5 |
|
6 |
# Load the model once outside of the function
|
7 |
model = AutoPipelineForText2Image.from_pretrained("stabilityai/sdxl-turbo")
|
@@ -9,61 +10,69 @@ model = AutoPipelineForText2Image.from_pretrained("stabilityai/sdxl-turbo")
|
|
9 |
async def generate_image(prompt, prompt_name):
|
10 |
try:
|
11 |
print(f"Generating image for {prompt_name}")
|
12 |
-
output = await model
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
except Exception as e:
|
18 |
print(f"Error generating image for {prompt_name}: {e}")
|
19 |
return None
|
20 |
|
21 |
-
async def
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
results = loop.run_until_complete(queue_image_calls(prompts))
|
33 |
-
return results
|
34 |
|
35 |
-
|
36 |
-
prompts
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
|
41 |
-
#
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
|
|
46 |
|
47 |
-
|
48 |
-
sentence_mapping_input = gr.Textbox(label="Sentence Mapping")
|
49 |
-
character_dict_input = gr.Textbox(label="Character Dictionary")
|
50 |
-
selected_style_input = gr.Textbox(label="Selected Style")
|
51 |
-
|
52 |
-
output_images = gr.Gallery(label="Generated Images")
|
53 |
|
54 |
-
|
55 |
-
|
56 |
-
|
|
|
57 |
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
|
|
|
|
67 |
|
68 |
if __name__ == "__main__":
|
69 |
-
|
|
|
|
|
1 |
import gradio as gr
|
2 |
from diffusers import AutoPipelineForText2Image
|
3 |
+
from io import BytesIO
|
4 |
+
import asyncio
|
5 |
+
from generate_propmts import generate_prompt
|
6 |
|
7 |
# Load the model once outside of the function
|
8 |
model = AutoPipelineForText2Image.from_pretrained("stabilityai/sdxl-turbo")
|
|
|
10 |
async def generate_image(prompt, prompt_name):
|
11 |
try:
|
12 |
print(f"Generating image for {prompt_name}")
|
13 |
+
output = await asyncio.to_thread(model, prompt=prompt, num_inference_steps=1, guidance_scale=0.0)
|
14 |
+
|
15 |
+
# Check if the model returned images
|
16 |
+
if isinstance(output.images, list) and len(output.images) > 0:
|
17 |
+
image = output.images[0]
|
18 |
+
buffered = BytesIO()
|
19 |
+
try:
|
20 |
+
image.save(buffered, format="JPEG")
|
21 |
+
image_bytes = buffered.getvalue()
|
22 |
+
print(f"Image bytes length for {prompt_name}: {len(image_bytes)}")
|
23 |
+
return image_bytes
|
24 |
+
except Exception as e:
|
25 |
+
print(f"Error saving image for {prompt_name}: {e}")
|
26 |
+
return None
|
27 |
+
else:
|
28 |
+
raise Exception(f"No images returned by the model for {prompt_name}.")
|
29 |
except Exception as e:
|
30 |
print(f"Error generating image for {prompt_name}: {e}")
|
31 |
return None
|
32 |
|
33 |
+
async def process_prompt(sentence_mapping, character_dict, selected_style):
|
34 |
+
images = {}
|
35 |
+
print(f'sentence_mapping: {sentence_mapping}, character_dict: {character_dict}, selected_style: {selected_style}')
|
36 |
+
prompts = []
|
37 |
|
38 |
+
# Generate prompts for each paragraph
|
39 |
+
for paragraph_number, sentences in sentence_mapping.items():
|
40 |
+
combined_sentence = " ".join(sentences)
|
41 |
+
prompt = generate_prompt(combined_sentence, character_dict, selected_style)
|
42 |
+
prompts.append((paragraph_number, prompt))
|
43 |
+
print(f"Generated prompt for paragraph {paragraph_number}: {prompt}")
|
|
|
|
|
44 |
|
45 |
+
print(f'prompts: {prompts}')
|
46 |
+
# Create tasks for all prompts and run them concurrently
|
47 |
+
tasks = [generate_image(prompt, f"Prompt {paragraph_number}") for paragraph_number, prompt in prompts]
|
48 |
+
print(f'tasks: {tasks}')
|
49 |
+
results = await asyncio.gather(*tasks)
|
50 |
|
51 |
+
# Map results back to paragraphs
|
52 |
+
for i, (paragraph_number, _) in enumerate(prompts):
|
53 |
+
if i < len(results):
|
54 |
+
images[paragraph_number] = results[i]
|
55 |
+
else:
|
56 |
+
print(f"Error: No result for paragraph {paragraph_number}")
|
57 |
|
58 |
+
return images
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
+
# Helper function to generate a prompt based on the input
|
61 |
+
def generate_prompt(combined_sentence, character_dict, selected_style):
|
62 |
+
characters = " ".join([" ".join(character) if isinstance(character, list) else character for character in character_dict.values()])
|
63 |
+
return f"Make an illustration in {selected_style} style from: {characters}. {combined_sentence}"
|
64 |
|
65 |
+
# Gradio interface with high concurrency limit
|
66 |
+
gradio_interface = gr.Interface(
|
67 |
+
fn=process_prompt,
|
68 |
+
inputs=[
|
69 |
+
gr.JSON(label="Sentence Mapping"),
|
70 |
+
gr.JSON(label="Character Dict"),
|
71 |
+
gr.Dropdown(["oil painting", "sketch", "watercolor"], label="Selected Style")
|
72 |
+
],
|
73 |
+
outputs="json",
|
74 |
+
concurrency_limit=20 # Set a high concurrency limit
|
75 |
+
).queue(default_concurrency_limit=20)
|
76 |
|
77 |
if __name__ == "__main__":
|
78 |
+
gradio_interface.launch()
|