Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -17,21 +17,19 @@ 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 |
-
|
24 |
Parameters:
|
25 |
- prompt (str): The input text for image generation.
|
26 |
- prompt_name (str): A name for the prompt, used for logging.
|
27 |
-
|
28 |
Returns:
|
29 |
bytes: The generated image data in bytes format, or None if generation fails.
|
30 |
"""
|
31 |
start_time = time.time()
|
32 |
process_id = os.getpid()
|
33 |
try:
|
34 |
-
output =
|
35 |
if isinstance(output.images, list) and len(output.images) > 0:
|
36 |
image = output.images[0]
|
37 |
buffered = BytesIO()
|
@@ -47,12 +45,10 @@ class ModelActor:
|
|
47 |
async def queue_api_calls(sentence_mapping, character_dict, selected_style):
|
48 |
"""
|
49 |
Generates images for all provided prompts in parallel using Ray actors.
|
50 |
-
|
51 |
Parameters:
|
52 |
- sentence_mapping (dict): Mapping between paragraph numbers and sentences.
|
53 |
- character_dict (dict): Dictionary mapping characters to their descriptions.
|
54 |
- selected_style (str): Selected illustration style.
|
55 |
-
|
56 |
Returns:
|
57 |
dict: A dictionary where keys are paragraph numbers and values are image data in bytes format.
|
58 |
"""
|
@@ -67,19 +63,17 @@ async def queue_api_calls(sentence_mapping, character_dict, selected_style):
|
|
67 |
model_actors = [ModelActor.remote() for _ in range(num_actors)]
|
68 |
tasks = [model_actors[i % num_actors].generate_image.remote(prompt, f"Prompt {paragraph_number}") for i, (paragraph_number, prompt) in enumerate(prompts)]
|
69 |
|
70 |
-
responses = await asyncio.gather(*[
|
71 |
images = {paragraph_number: response for (paragraph_number, _), response in zip(prompts, responses)}
|
72 |
return images
|
73 |
|
74 |
def process_prompt(sentence_mapping, character_dict, selected_style):
|
75 |
"""
|
76 |
Processes the provided prompts and generates images.
|
77 |
-
|
78 |
Parameters:
|
79 |
- sentence_mapping (dict): Mapping between paragraph numbers and sentences.
|
80 |
- character_dict (dict): Dictionary mapping characters to their descriptions.
|
81 |
- selected_style (str): Selected illustration style.
|
82 |
-
|
83 |
Returns:
|
84 |
dict: A dictionary where keys are paragraph numbers and values are image data in bytes format.
|
85 |
"""
|
@@ -99,4 +93,4 @@ gradio_interface = gr.Interface(
|
|
99 |
)
|
100 |
|
101 |
if __name__ == "__main__":
|
102 |
-
gradio_interface.launch()
|
|
|
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:
|
24 |
- prompt (str): The input text for image generation.
|
25 |
- prompt_name (str): A name for the prompt, used for logging.
|
|
|
26 |
Returns:
|
27 |
bytes: The generated image data in bytes format, or None if generation fails.
|
28 |
"""
|
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()
|
|
|
45 |
async def queue_api_calls(sentence_mapping, character_dict, selected_style):
|
46 |
"""
|
47 |
Generates images for all provided prompts in parallel using Ray actors.
|
|
|
48 |
Parameters:
|
49 |
- sentence_mapping (dict): Mapping between paragraph numbers and sentences.
|
50 |
- character_dict (dict): Dictionary mapping characters to their descriptions.
|
51 |
- selected_style (str): Selected illustration style.
|
|
|
52 |
Returns:
|
53 |
dict: A dictionary where keys are paragraph numbers and values are image data in bytes format.
|
54 |
"""
|
|
|
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 |
+
responses = await asyncio.gather(*[ray.get(task) for task in tasks])
|
67 |
images = {paragraph_number: response for (paragraph_number, _), response in zip(prompts, responses)}
|
68 |
return images
|
69 |
|
70 |
def process_prompt(sentence_mapping, character_dict, selected_style):
|
71 |
"""
|
72 |
Processes the provided prompts and generates images.
|
|
|
73 |
Parameters:
|
74 |
- sentence_mapping (dict): Mapping between paragraph numbers and sentences.
|
75 |
- character_dict (dict): Dictionary mapping characters to their descriptions.
|
76 |
- selected_style (str): Selected illustration style.
|
|
|
77 |
Returns:
|
78 |
dict: A dictionary where keys are paragraph numbers and values are image data in bytes format.
|
79 |
"""
|
|
|
93 |
)
|
94 |
|
95 |
if __name__ == "__main__":
|
96 |
+
gradio_interface.launch()
|