Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -17,7 +17,7 @@ class ModelActor:
|
|
17 |
"""
|
18 |
self.model = AutoPipelineForText2Image.from_pretrained("stabilityai/sdxl-turbo")
|
19 |
|
20 |
-
|
21 |
"""
|
22 |
Generates an image based on the provided prompt.
|
23 |
Parameters:
|
@@ -29,7 +29,7 @@ class ModelActor:
|
|
29 |
start_time = time.time()
|
30 |
process_id = os.getpid()
|
31 |
try:
|
32 |
-
output =
|
33 |
if isinstance(output.images, list) and len(output.images) > 0:
|
34 |
image = output.images[0]
|
35 |
buffered = BytesIO()
|
@@ -57,13 +57,14 @@ async def queue_api_calls(sentence_mapping, character_dict, selected_style):
|
|
57 |
combined_sentence = " ".join(sentences)
|
58 |
prompt = generate_prompt(combined_sentence, sentence_mapping, character_dict, selected_style)
|
59 |
prompts.append((paragraph_number, prompt))
|
60 |
-
|
61 |
num_prompts = len(prompts)
|
62 |
num_actors = min(num_prompts, 20)
|
63 |
model_actors = [ModelActor.remote() for _ in range(num_actors)]
|
64 |
tasks = [model_actors[i % num_actors].generate_image.remote(prompt, f"Prompt {paragraph_number}") for i, (paragraph_number, prompt) in enumerate(prompts)]
|
65 |
|
66 |
-
|
|
|
67 |
images = {paragraph_number: response for (paragraph_number, _), response in zip(prompts, responses)}
|
68 |
return images
|
69 |
|
|
|
17 |
"""
|
18 |
self.model = AutoPipelineForText2Image.from_pretrained("stabilityai/sdxl-turbo")
|
19 |
|
20 |
+
def generate_image(self, prompt, prompt_name):
|
21 |
"""
|
22 |
Generates an image based on the provided prompt.
|
23 |
Parameters:
|
|
|
29 |
start_time = time.time()
|
30 |
process_id = os.getpid()
|
31 |
try:
|
32 |
+
output = self.model(prompt=prompt, num_inference_steps=1, guidance_scale=0.0)
|
33 |
if isinstance(output.images, list) and len(output.images) > 0:
|
34 |
image = output.images[0]
|
35 |
buffered = BytesIO()
|
|
|
57 |
combined_sentence = " ".join(sentences)
|
58 |
prompt = generate_prompt(combined_sentence, sentence_mapping, character_dict, selected_style)
|
59 |
prompts.append((paragraph_number, prompt))
|
60 |
+
print(prompts)
|
61 |
num_prompts = len(prompts)
|
62 |
num_actors = min(num_prompts, 20)
|
63 |
model_actors = [ModelActor.remote() for _ in range(num_actors)]
|
64 |
tasks = [model_actors[i % num_actors].generate_image.remote(prompt, f"Prompt {paragraph_number}") for i, (paragraph_number, prompt) in enumerate(prompts)]
|
65 |
|
66 |
+
# Use asyncio.to_thread to call ray.get() synchronously in an async context
|
67 |
+
responses = await asyncio.gather(*[asyncio.to_thread(ray.get, task) for task in tasks])
|
68 |
images = {paragraph_number: response for (paragraph_number, _), response in zip(prompts, responses)}
|
69 |
return images
|
70 |
|