Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -4,19 +4,21 @@ from generate_prompts import generate_prompt
|
|
4 |
from diffusers import AutoPipelineForText2Image
|
5 |
from io import BytesIO
|
6 |
import gradio as gr
|
|
|
7 |
|
8 |
-
|
9 |
-
print("Loading
|
10 |
-
model = AutoPipelineForText2Image.from_pretrained("stabilityai/sdxl-turbo")
|
11 |
-
print("Model loaded
|
|
|
12 |
|
13 |
-
|
14 |
try:
|
|
|
15 |
print(f"Generating response for {prompt_name} with prompt: {prompt}")
|
16 |
-
output =
|
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:
|
21 |
image = output.images[0]
|
22 |
buffered = BytesIO()
|
@@ -38,7 +40,6 @@ 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}")
|
@@ -46,11 +47,11 @@ async def queue_api_calls(sentence_mapping, character_dict, selected_style):
|
|
46 |
prompts.append((paragraph_number, prompt))
|
47 |
print(f"Generated prompt for paragraph {paragraph_number}: {prompt}")
|
48 |
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
|
55 |
images = {paragraph_number: response for (paragraph_number, _), response in zip(prompts, responses)}
|
56 |
print(f"Images generated: {images}")
|
@@ -59,20 +60,16 @@ async def queue_api_calls(sentence_mapping, character_dict, selected_style):
|
|
59 |
def process_prompt(sentence_mapping, character_dict, selected_style):
|
60 |
print(f"process_prompt called with sentence_mapping: {sentence_mapping}, character_dict: {character_dict}, selected_style: {selected_style}")
|
61 |
try:
|
62 |
-
# See if there is a loop already running. If there is, reuse it.
|
63 |
loop = asyncio.get_running_loop()
|
64 |
except RuntimeError:
|
65 |
-
# Create new event loop if one is not running
|
66 |
loop = asyncio.new_event_loop()
|
67 |
asyncio.set_event_loop(loop)
|
68 |
print("Event loop created.")
|
69 |
|
70 |
-
# 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.
|
71 |
cmpt_return = loop.run_until_complete(queue_api_calls(sentence_mapping, character_dict, selected_style))
|
72 |
print(f"process_prompt completed with return value: {cmpt_return}")
|
73 |
return cmpt_return
|
74 |
|
75 |
-
# Gradio interface with high concurrency limit
|
76 |
gradio_interface = gr.Interface(
|
77 |
fn=process_prompt,
|
78 |
inputs=[
|
|
|
4 |
from diffusers import AutoPipelineForText2Image
|
5 |
from io import BytesIO
|
6 |
import gradio as gr
|
7 |
+
from multiprocessing import Pool, current_process
|
8 |
|
9 |
+
def load_model():
|
10 |
+
print(f"Loading model in process {current_process().name}")
|
11 |
+
model = AutoPipelineForText2Image.from_pretrained("stabilityai/sdxl-turbo")
|
12 |
+
print(f"Model loaded in process {current_process().name}")
|
13 |
+
return model
|
14 |
|
15 |
+
def generate_image(prompt, prompt_name):
|
16 |
try:
|
17 |
+
model = load_model()
|
18 |
print(f"Generating response for {prompt_name} with prompt: {prompt}")
|
19 |
+
output = model(prompt=prompt, num_inference_steps=1, guidance_scale=0.0)
|
20 |
print(f"Output for {prompt_name}: {output}")
|
21 |
|
|
|
22 |
if isinstance(output.images, list) and len(output.images) > 0:
|
23 |
image = output.images[0]
|
24 |
buffered = BytesIO()
|
|
|
40 |
print(f"queue_api_calls invoked with sentence_mapping: {sentence_mapping}, character_dict: {character_dict}, selected_style: {selected_style}")
|
41 |
prompts = []
|
42 |
|
|
|
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 |
prompts.append((paragraph_number, prompt))
|
48 |
print(f"Generated prompt for paragraph {paragraph_number}: {prompt}")
|
49 |
|
50 |
+
with Pool() as pool:
|
51 |
+
tasks = [(prompt, f"Prompt {paragraph_number}") for paragraph_number, prompt in prompts]
|
52 |
+
print("Tasks created for image generation.")
|
53 |
+
responses = pool.starmap(generate_image, 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}")
|
|
|
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 |
loop = asyncio.get_running_loop()
|
64 |
except RuntimeError:
|
|
|
65 |
loop = asyncio.new_event_loop()
|
66 |
asyncio.set_event_loop(loop)
|
67 |
print("Event loop created.")
|
68 |
|
|
|
69 |
cmpt_return = loop.run_until_complete(queue_api_calls(sentence_mapping, character_dict, selected_style))
|
70 |
print(f"process_prompt completed with return value: {cmpt_return}")
|
71 |
return cmpt_return
|
72 |
|
|
|
73 |
gradio_interface = gr.Interface(
|
74 |
fn=process_prompt,
|
75 |
inputs=[
|