Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,53 +1,24 @@
|
|
1 |
-
import os
|
2 |
import asyncio
|
3 |
from generate_prompts import generate_prompt
|
4 |
from diffusers import AutoPipelineForText2Image
|
5 |
from io import BytesIO
|
6 |
import gradio as gr
|
7 |
-
import threading
|
8 |
|
9 |
-
#
|
10 |
-
|
11 |
-
model = AutoPipelineForText2Image.from_pretrained("stabilityai/sdxl-turbo")
|
12 |
-
print("Model loaded successfully.")
|
13 |
|
14 |
-
|
15 |
-
|
|
|
|
|
|
|
16 |
|
17 |
-
|
18 |
-
def __init__(self):
|
19 |
-
self._step = threading.local()
|
20 |
-
self._init_step_index()
|
21 |
|
22 |
-
|
23 |
-
self._step.step = 0
|
24 |
-
print(f"Initialized step index: {self._step.step}")
|
25 |
-
|
26 |
-
@property
|
27 |
-
def step(self):
|
28 |
-
return self._step.step
|
29 |
-
|
30 |
-
def step_process(self):
|
31 |
-
self._step.step += 1
|
32 |
-
print(f"Step index updated to: {self._step.step}")
|
33 |
-
|
34 |
-
scheduler = Scheduler()
|
35 |
-
|
36 |
-
def generate_image(prompt, prompt_name):
|
37 |
try:
|
38 |
-
# Initialize step index for the current thread if not already done
|
39 |
-
if not hasattr(scheduler._step, 'step'):
|
40 |
-
scheduler._init_step_index()
|
41 |
-
|
42 |
-
print(f"Initial step index for {prompt_name}: {scheduler.step}")
|
43 |
print(f"Generating response for {prompt_name} with prompt: {prompt}")
|
44 |
-
|
45 |
output = model(prompt=prompt, num_inference_steps=1, guidance_scale=0.0)
|
46 |
-
|
47 |
-
# Update and print step index
|
48 |
-
scheduler.step_process()
|
49 |
-
print(f"Updated step index for {prompt_name}: {scheduler.step}")
|
50 |
-
|
51 |
print(f"Output for {prompt_name}: {output}")
|
52 |
|
53 |
# Check if the model returned images
|
@@ -76,13 +47,12 @@ async def queue_api_calls(sentence_mapping, character_dict, selected_style):
|
|
76 |
for paragraph_number, sentences in sentence_mapping.items():
|
77 |
combined_sentence = " ".join(sentences)
|
78 |
print(f"combined_sentence for paragraph {paragraph_number}: {combined_sentence}")
|
79 |
-
prompt = generate_prompt(combined_sentence,
|
80 |
prompts.append((paragraph_number, prompt))
|
81 |
print(f"Generated prompt for paragraph {paragraph_number}: {prompt}")
|
82 |
|
83 |
# Generate images for each prompt in parallel
|
84 |
-
|
85 |
-
tasks = [loop.run_in_executor(None, generate_image, prompt, f"Prompt {paragraph_number}") for paragraph_number, prompt in prompts]
|
86 |
print("Tasks created for image generation.")
|
87 |
responses = await asyncio.gather(*tasks)
|
88 |
print("Responses received from image generation tasks.")
|
@@ -102,9 +72,6 @@ def process_prompt(sentence_mapping, character_dict, selected_style):
|
|
102 |
asyncio.set_event_loop(loop)
|
103 |
print("Event loop created.")
|
104 |
|
105 |
-
# Initialize thread-local variables
|
106 |
-
scheduler._init_step_index()
|
107 |
-
|
108 |
# 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.
|
109 |
cmpt_return = loop.run_until_complete(queue_api_calls(sentence_mapping, character_dict, selected_style))
|
110 |
print(f"process_prompt completed with return value: {cmpt_return}")
|
|
|
|
|
1 |
import asyncio
|
2 |
from generate_prompts import generate_prompt
|
3 |
from diffusers import AutoPipelineForText2Image
|
4 |
from io import BytesIO
|
5 |
import gradio as gr
|
|
|
6 |
|
7 |
+
# Asynchronously load the model once outside of the function
|
8 |
+
model = None
|
|
|
|
|
9 |
|
10 |
+
async def load_model():
|
11 |
+
global model
|
12 |
+
print("Loading the model...")
|
13 |
+
model = AutoPipelineForText2Image.from_pretrained("stabilityai/sdxl-turbo")
|
14 |
+
print("Model loaded successfully.")
|
15 |
|
16 |
+
asyncio.run(load_model())
|
|
|
|
|
|
|
17 |
|
18 |
+
async def generate_image(prompt, prompt_name):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
try:
|
|
|
|
|
|
|
|
|
|
|
20 |
print(f"Generating response for {prompt_name} with prompt: {prompt}")
|
|
|
21 |
output = model(prompt=prompt, num_inference_steps=1, guidance_scale=0.0)
|
|
|
|
|
|
|
|
|
|
|
22 |
print(f"Output for {prompt_name}: {output}")
|
23 |
|
24 |
# Check if the model returned images
|
|
|
47 |
for paragraph_number, sentences in sentence_mapping.items():
|
48 |
combined_sentence = " ".join(sentences)
|
49 |
print(f"combined_sentence for paragraph {paragraph_number}: {combined_sentence}")
|
50 |
+
prompt = generate_prompt(combined_sentence, character_dict, selected_style) # Correct prompt generation
|
51 |
prompts.append((paragraph_number, prompt))
|
52 |
print(f"Generated prompt for paragraph {paragraph_number}: {prompt}")
|
53 |
|
54 |
# Generate images for each prompt in parallel
|
55 |
+
tasks = [generate_image(prompt, f"Prompt {paragraph_number}") for paragraph_number, prompt in prompts]
|
|
|
56 |
print("Tasks created for image generation.")
|
57 |
responses = await asyncio.gather(*tasks)
|
58 |
print("Responses received from image generation tasks.")
|
|
|
72 |
asyncio.set_event_loop(loop)
|
73 |
print("Event loop created.")
|
74 |
|
|
|
|
|
|
|
75 |
# 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.
|
76 |
cmpt_return = loop.run_until_complete(queue_api_calls(sentence_mapping, character_dict, selected_style))
|
77 |
print(f"process_prompt completed with return value: {cmpt_return}")
|