Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -4,42 +4,46 @@ from generate_prompts import generate_prompt
|
|
4 |
from diffusers import AutoPipelineForText2Image
|
5 |
from io import BytesIO
|
6 |
import gradio as gr
|
7 |
-
|
8 |
|
9 |
-
|
10 |
-
print("Loading the model...")
|
11 |
-
model = AutoPipelineForText2Image.from_pretrained("stabilityai/sdxl-turbo")
|
12 |
-
print("Model loaded successfully.")
|
13 |
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
-
|
21 |
-
if isinstance(output.images, list) and len(output.images) > 0:
|
22 |
-
image = output.images[0]
|
23 |
-
buffered = BytesIO()
|
24 |
-
try:
|
25 |
-
image.save(buffered, format="JPEG")
|
26 |
-
image_bytes = buffered.getvalue()
|
27 |
-
print(f"Image bytes length for {prompt_name}: {len(image_bytes)}")
|
28 |
-
return image_bytes
|
29 |
-
except Exception as e:
|
30 |
-
print(f"Error saving image for {prompt_name}: {e}")
|
31 |
-
return None
|
32 |
-
else:
|
33 |
-
raise Exception(f"No images returned by the model for {prompt_name}.")
|
34 |
-
except Exception as e:
|
35 |
-
print(f"Error generating image for {prompt_name}: {e}")
|
36 |
-
return None
|
37 |
|
38 |
async def queue_api_calls(sentence_mapping, character_dict, selected_style):
|
39 |
print(f"queue_api_calls invoked with sentence_mapping: {sentence_mapping}, character_dict: {character_dict}, selected_style: {selected_style}")
|
40 |
prompts = []
|
41 |
|
42 |
-
# Generate prompts for each paragraph
|
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,16 +51,10 @@ async def queue_api_calls(sentence_mapping, character_dict, selected_style):
|
|
47 |
prompts.append((paragraph_number, prompt))
|
48 |
print(f"Generated prompt for paragraph {paragraph_number}: {prompt}")
|
49 |
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
with ThreadPoolExecutor(max_workers=max_workers) as executor:
|
55 |
-
loop = asyncio.get_running_loop()
|
56 |
-
tasks = [loop.run_in_executor(executor, generate_image, prompt, f"Prompt {paragraph_number}") for paragraph_number, prompt in prompts]
|
57 |
-
print("Tasks created for image generation.")
|
58 |
-
responses = await asyncio.gather(*tasks)
|
59 |
-
print("Responses received from image generation tasks.")
|
60 |
|
61 |
images = {paragraph_number: response for (paragraph_number, _), response in zip(prompts, responses)}
|
62 |
print(f"Images generated: {images}")
|
@@ -65,20 +63,16 @@ async def queue_api_calls(sentence_mapping, character_dict, selected_style):
|
|
65 |
def process_prompt(sentence_mapping, character_dict, selected_style):
|
66 |
print(f"process_prompt called with sentence_mapping: {sentence_mapping}, character_dict: {character_dict}, selected_style: {selected_style}")
|
67 |
try:
|
68 |
-
# See if there is a loop already running. If there is, reuse it.
|
69 |
loop = asyncio.get_running_loop()
|
70 |
except RuntimeError:
|
71 |
-
# Create new event loop if one is not running
|
72 |
loop = asyncio.new_event_loop()
|
73 |
asyncio.set_event_loop(loop)
|
74 |
print("Event loop created.")
|
75 |
|
76 |
-
# 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.
|
77 |
cmpt_return = loop.run_until_complete(queue_api_calls(sentence_mapping, character_dict, selected_style))
|
78 |
print(f"process_prompt completed with return value: {cmpt_return}")
|
79 |
return cmpt_return
|
80 |
|
81 |
-
# Gradio interface with high concurrency limit
|
82 |
gradio_interface = gr.Interface(
|
83 |
fn=process_prompt,
|
84 |
inputs=[
|
|
|
4 |
from diffusers import AutoPipelineForText2Image
|
5 |
from io import BytesIO
|
6 |
import gradio as gr
|
7 |
+
import ray
|
8 |
|
9 |
+
ray.init()
|
|
|
|
|
|
|
10 |
|
11 |
+
@ray.remote
|
12 |
+
class ModelActor:
|
13 |
+
def __init__(self):
|
14 |
+
print("Loading the model...")
|
15 |
+
self.model = AutoPipelineForText2Image.from_pretrained("stabilityai/sdxl-turbo")
|
16 |
+
print("Model loaded successfully.")
|
17 |
+
|
18 |
+
def generate_image(self, prompt, prompt_name):
|
19 |
+
try:
|
20 |
+
print(f"Generating response for {prompt_name} with prompt: {prompt}")
|
21 |
+
output = self.model(prompt=prompt, num_inference_steps=1, guidance_scale=0.0)
|
22 |
+
print(f"Output for {prompt_name}: {output}")
|
23 |
+
|
24 |
+
if isinstance(output.images, list) and len(output.images) > 0:
|
25 |
+
image = output.images[0]
|
26 |
+
buffered = BytesIO()
|
27 |
+
try:
|
28 |
+
image.save(buffered, format="JPEG")
|
29 |
+
image_bytes = buffered.getvalue()
|
30 |
+
print(f"Image bytes length for {prompt_name}: {len(image_bytes)}")
|
31 |
+
return image_bytes
|
32 |
+
except Exception as e:
|
33 |
+
print(f"Error saving image for {prompt_name}: {e}")
|
34 |
+
return None
|
35 |
+
else:
|
36 |
+
raise Exception(f"No images returned by the model for {prompt_name}.")
|
37 |
+
except Exception as e:
|
38 |
+
print(f"Error generating image for {prompt_name}: {e}")
|
39 |
+
return None
|
40 |
|
41 |
+
model_actor = ModelActor.remote()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
async def queue_api_calls(sentence_mapping, character_dict, selected_style):
|
44 |
print(f"queue_api_calls invoked with sentence_mapping: {sentence_mapping}, character_dict: {character_dict}, selected_style: {selected_style}")
|
45 |
prompts = []
|
46 |
|
|
|
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}")
|
|
|
51 |
prompts.append((paragraph_number, prompt))
|
52 |
print(f"Generated prompt for paragraph {paragraph_number}: {prompt}")
|
53 |
|
54 |
+
tasks = [model_actor.generate_image.remote(prompt, f"Prompt {paragraph_number}") for paragraph_number, prompt in prompts]
|
55 |
+
print("Tasks created for image generation.")
|
56 |
+
responses = await asyncio.gather(*[ray.get(task) for task in tasks])
|
57 |
+
print("Responses received from image generation tasks.")
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
|
59 |
images = {paragraph_number: response for (paragraph_number, _), response in zip(prompts, responses)}
|
60 |
print(f"Images generated: {images}")
|
|
|
63 |
def process_prompt(sentence_mapping, character_dict, selected_style):
|
64 |
print(f"process_prompt called with sentence_mapping: {sentence_mapping}, character_dict: {character_dict}, selected_style: {selected_style}")
|
65 |
try:
|
|
|
66 |
loop = asyncio.get_running_loop()
|
67 |
except RuntimeError:
|
|
|
68 |
loop = asyncio.new_event_loop()
|
69 |
asyncio.set_event_loop(loop)
|
70 |
print("Event loop created.")
|
71 |
|
|
|
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 = gr.Interface(
|
77 |
fn=process_prompt,
|
78 |
inputs=[
|