Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -6,12 +6,15 @@ from io import BytesIO
|
|
6 |
import gradio as gr
|
7 |
|
8 |
# Load the model once outside of the function
|
|
|
9 |
model = AutoPipelineForText2Image.from_pretrained("stabilityai/sdxl-turbo")
|
|
|
10 |
|
11 |
def generate_image(prompt, prompt_name):
|
12 |
try:
|
13 |
-
print(f"Generating response for {prompt_name}")
|
14 |
output = model(prompt=prompt, num_inference_steps=1, guidance_scale=0.0)
|
|
|
15 |
|
16 |
# Check if the model returned images
|
17 |
if isinstance(output.images, list) and len(output.images) > 0:
|
@@ -32,13 +35,13 @@ def generate_image(prompt, prompt_name):
|
|
32 |
return None
|
33 |
|
34 |
async def queue_api_calls(sentence_mapping, character_dict, selected_style):
|
35 |
-
print(f
|
36 |
prompts = []
|
37 |
|
38 |
# Generate prompts for each paragraph
|
39 |
for paragraph_number, sentences in sentence_mapping.items():
|
40 |
combined_sentence = " ".join(sentences)
|
41 |
-
print(f
|
42 |
prompt = generate_prompt(combined_sentence, sentence_mapping, character_dict, selected_style)
|
43 |
prompts.append((paragraph_number, prompt))
|
44 |
print(f"Generated prompt for paragraph {paragraph_number}: {prompt}")
|
@@ -46,12 +49,16 @@ async def queue_api_calls(sentence_mapping, character_dict, selected_style):
|
|
46 |
# Generate images for each prompt in parallel
|
47 |
loop = asyncio.get_running_loop()
|
48 |
tasks = [loop.run_in_executor(None, generate_image, prompt, f"Prompt {paragraph_number}") for paragraph_number, prompt in prompts]
|
|
|
49 |
responses = await asyncio.gather(*tasks)
|
50 |
-
|
|
|
51 |
images = {paragraph_number: response for (paragraph_number, _), response in zip(prompts, responses)}
|
|
|
52 |
return images
|
53 |
|
54 |
def process_prompt(sentence_mapping, character_dict, selected_style):
|
|
|
55 |
try:
|
56 |
# See if there is a loop already running. If there is, reuse it.
|
57 |
loop = asyncio.get_running_loop()
|
@@ -59,9 +66,11 @@ def process_prompt(sentence_mapping, character_dict, selected_style):
|
|
59 |
# Create new event loop if one is not running
|
60 |
loop = asyncio.new_event_loop()
|
61 |
asyncio.set_event_loop(loop)
|
|
|
62 |
|
63 |
# 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.
|
64 |
cmpt_return = loop.run_until_complete(queue_api_calls(sentence_mapping, character_dict, selected_style))
|
|
|
65 |
return cmpt_return
|
66 |
|
67 |
# Gradio interface with high concurrency limit
|
@@ -76,4 +85,6 @@ gradio_interface = gr.Interface(
|
|
76 |
)
|
77 |
|
78 |
if __name__ == "__main__":
|
|
|
79 |
gradio_interface.launch()
|
|
|
|
6 |
import gradio as gr
|
7 |
|
8 |
# Load the model once outside of the function
|
9 |
+
print("Loading the model...")
|
10 |
model = AutoPipelineForText2Image.from_pretrained("stabilityai/sdxl-turbo")
|
11 |
+
print("Model loaded successfully.")
|
12 |
|
13 |
def generate_image(prompt, prompt_name):
|
14 |
try:
|
15 |
+
print(f"Generating response for {prompt_name} with prompt: {prompt}")
|
16 |
output = model(prompt=prompt, num_inference_steps=1, guidance_scale=0.0)
|
17 |
+
print(f"Output for {prompt_name}: {output}")
|
18 |
|
19 |
# Check if the model returned images
|
20 |
if isinstance(output.images, list) and len(output.images) > 0:
|
|
|
35 |
return None
|
36 |
|
37 |
async def queue_api_calls(sentence_mapping, character_dict, selected_style):
|
38 |
+
print(f"queue_api_calls invoked with sentence_mapping: {sentence_mapping}, character_dict: {character_dict}, selected_style: {selected_style}")
|
39 |
prompts = []
|
40 |
|
41 |
# Generate prompts for each paragraph
|
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}")
|
45 |
prompt = generate_prompt(combined_sentence, sentence_mapping, character_dict, selected_style)
|
46 |
prompts.append((paragraph_number, prompt))
|
47 |
print(f"Generated prompt for paragraph {paragraph_number}: {prompt}")
|
|
|
49 |
# Generate images for each prompt in parallel
|
50 |
loop = asyncio.get_running_loop()
|
51 |
tasks = [loop.run_in_executor(None, generate_image, prompt, f"Prompt {paragraph_number}") for paragraph_number, prompt in prompts]
|
52 |
+
print("Tasks created for image generation.")
|
53 |
responses = await asyncio.gather(*tasks)
|
54 |
+
print("Responses received from image generation tasks.")
|
55 |
+
|
56 |
images = {paragraph_number: response for (paragraph_number, _), response in zip(prompts, responses)}
|
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 |
try:
|
63 |
# See if there is a loop already running. If there is, reuse it.
|
64 |
loop = asyncio.get_running_loop()
|
|
|
66 |
# Create new event loop if one is not running
|
67 |
loop = asyncio.new_event_loop()
|
68 |
asyncio.set_event_loop(loop)
|
69 |
+
print("Event loop created.")
|
70 |
|
71 |
# 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.
|
72 |
cmpt_return = loop.run_until_complete(queue_api_calls(sentence_mapping, character_dict, selected_style))
|
73 |
+
print(f"process_prompt completed with return value: {cmpt_return}")
|
74 |
return cmpt_return
|
75 |
|
76 |
# Gradio interface with high concurrency limit
|
|
|
85 |
)
|
86 |
|
87 |
if __name__ == "__main__":
|
88 |
+
print("Launching Gradio interface...")
|
89 |
gradio_interface.launch()
|
90 |
+
print("Gradio interface launched.")
|