Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,76 +1,55 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
from diffusers import AutoPipelineForText2Image
|
3 |
-
from io import BytesIO
|
4 |
import asyncio
|
5 |
-
|
|
|
|
|
|
|
6 |
|
7 |
-
# Load the model
|
8 |
-
|
9 |
|
10 |
async def generate_image(prompt, prompt_name):
|
11 |
try:
|
12 |
print(f"Generating image for {prompt_name}")
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
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
|
34 |
-
|
35 |
-
|
36 |
-
|
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 |
-
# Create tasks for all prompts and run them concurrently
|
46 |
-
tasks = [generate_image(prompt, f"Prompt {paragraph_number}") for paragraph_number, prompt in prompts]
|
47 |
-
results = await asyncio.gather(*tasks)
|
48 |
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
return
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
gr.
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
|
|
|
|
74 |
|
75 |
if __name__ == "__main__":
|
76 |
-
|
|
|
|
|
|
|
|
|
1 |
import asyncio
|
2 |
+
import json
|
3 |
+
import gradio as gr
|
4 |
+
from diffusers import StableDiffusionPipeline
|
5 |
+
from generate_prompts import generate_prompt
|
6 |
|
7 |
+
# Load the model pipeline
|
8 |
+
pipeline = StableDiffusionPipeline.from_pretrained("stabilityai/sdxl-turbo").to("cuda")
|
9 |
|
10 |
async def generate_image(prompt, prompt_name):
|
11 |
try:
|
12 |
print(f"Generating image for {prompt_name}")
|
13 |
+
image = await pipeline(prompt).images[0]
|
14 |
+
img_bytes = image.tobytes()
|
15 |
+
print(f"Image bytes length for {prompt_name}: {len(img_bytes)}")
|
16 |
+
return img_bytes
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
except Exception as e:
|
18 |
print(f"Error generating image for {prompt_name}: {e}")
|
19 |
return None
|
20 |
|
21 |
+
async def queue_image_calls(prompts):
|
22 |
+
tasks = [generate_image(prompts[i], f"Prompt {i}") for i in range(len(prompts))]
|
23 |
+
responses = await asyncio.gather(*tasks)
|
24 |
+
return responses
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
+
def async_image_generation(prompts):
|
27 |
+
try:
|
28 |
+
loop = asyncio.get_running_loop()
|
29 |
+
except RuntimeError:
|
30 |
+
loop = asyncio.new_event_loop()
|
31 |
+
asyncio.set_event_loop(loop)
|
32 |
+
results = loop.run_until_complete(queue_image_calls(prompts))
|
33 |
+
return results
|
34 |
+
|
35 |
+
def gradio_interface(sentence_mapping, character_dict, selected_style):
|
36 |
+
prompts = generate_prompt(sentence_mapping, character_dict, selected_style)
|
37 |
+
image_bytes_list = async_image_generation(prompts)
|
38 |
+
outputs = [gr.Image.update(value=img_bytes) if img_bytes else gr.Image.update(value=None) for img_bytes in image_bytes_list]
|
39 |
+
return outputs
|
40 |
+
|
41 |
+
# Gradio Interface
|
42 |
+
with gr.Blocks() as demo:
|
43 |
+
with gr.Row():
|
44 |
+
with gr.Column():
|
45 |
+
sentence_mapping_input = gr.Textbox(label="Sentence Mapping")
|
46 |
+
character_dict_input = gr.Textbox(label="Character Dictionary")
|
47 |
+
selected_style_input = gr.Textbox(label="Selected Style")
|
48 |
+
submit_btn = gr.Button(value='Submit')
|
49 |
+
prompt_responses = [gr.Image(label=f"Prompt {i} Response") for i in range(4)]
|
50 |
+
submit_btn.click(fn=gradio_interface,
|
51 |
+
inputs=[sentence_mapping_input, character_dict_input, selected_style_input],
|
52 |
+
outputs=prompt_responses)
|
53 |
|
54 |
if __name__ == "__main__":
|
55 |
+
demo.launch()
|