RanM commited on
Commit
c9b9787
·
verified ·
1 Parent(s): 14e57b0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -11
app.py CHANGED
@@ -1,6 +1,5 @@
1
  from generate_prompts import generate_prompt
2
  import gradio as gr
3
- import torch
4
  from diffusers import AutoPipelineForText2Image
5
  from io import BytesIO
6
  import asyncio
@@ -8,11 +7,10 @@ import asyncio
8
  # Load the model once outside of the function
9
  model = AutoPipelineForText2Image.from_pretrained("stabilityai/sdxl-turbo")
10
 
11
- async def generate_image(prompt):
12
  try:
13
- # Generate an image based on the prompt
14
  output = await asyncio.to_thread(model, prompt=prompt, num_inference_steps=1, guidance_scale=0.0)
15
- print(f"Model output: {output}")
16
 
17
  # Check if the model returned images
18
  if isinstance(output.images, list) and len(output.images) > 0:
@@ -21,16 +19,15 @@ async def generate_image(prompt):
21
  try:
22
  image.save(buffered, format="JPEG")
23
  image_bytes = buffered.getvalue()
24
- # Verify the image bytes
25
- print(f"Image bytes length: {len(image_bytes)}")
26
  return image_bytes
27
  except Exception as e:
28
- print(f"Error saving image: {e}")
29
  return None
30
  else:
31
- raise Exception("No images returned by the model.")
32
  except Exception as e:
33
- print(f"Error generating image: {e}")
34
  return None
35
 
36
  async def process_prompt(sentence_mapping, character_dict, selected_style):
@@ -46,7 +43,7 @@ async def process_prompt(sentence_mapping, character_dict, selected_style):
46
  print(f"Generated prompt for paragraph {paragraph_number}: {prompt}")
47
 
48
  # Create tasks for all prompts and run them concurrently
49
- tasks = [generate_image(prompt) for _, prompt in prompts]
50
  results = await asyncio.gather(*tasks)
51
 
52
  # Map results back to paragraphs
@@ -76,4 +73,4 @@ gradio_interface = gr.Interface(
76
  ).queue(default_concurrency_limit=20)
77
 
78
  if __name__ == "__main__":
79
- gradio_interface.launch() # No need for share=True for local testing
 
1
  from generate_prompts import generate_prompt
2
  import gradio as gr
 
3
  from diffusers import AutoPipelineForText2Image
4
  from io import BytesIO
5
  import asyncio
 
7
  # Load the model once outside of the function
8
  model = AutoPipelineForText2Image.from_pretrained("stabilityai/sdxl-turbo")
9
 
10
+ async def generate_image(prompt, prompt_name):
11
  try:
12
+ print(f"Generating image for {prompt_name}")
13
  output = await asyncio.to_thread(model, prompt=prompt, num_inference_steps=1, guidance_scale=0.0)
 
14
 
15
  # Check if the model returned images
16
  if isinstance(output.images, list) and len(output.images) > 0:
 
19
  try:
20
  image.save(buffered, format="JPEG")
21
  image_bytes = buffered.getvalue()
22
+ print(f"Image bytes length for {prompt_name}: {len(image_bytes)}")
 
23
  return image_bytes
24
  except Exception as e:
25
+ print(f"Error saving image for {prompt_name}: {e}")
26
  return None
27
  else:
28
+ raise Exception(f"No images returned by the model for {prompt_name}.")
29
  except Exception as e:
30
+ print(f"Error generating image for {prompt_name}: {e}")
31
  return None
32
 
33
  async def process_prompt(sentence_mapping, character_dict, selected_style):
 
43
  print(f"Generated prompt for paragraph {paragraph_number}: {prompt}")
44
 
45
  # Create tasks for all prompts and run them concurrently
46
+ tasks = [generate_image(prompt, f"Prompt {paragraph_number}") for paragraph_number, prompt in prompts]
47
  results = await asyncio.gather(*tasks)
48
 
49
  # Map results back to paragraphs
 
73
  ).queue(default_concurrency_limit=20)
74
 
75
  if __name__ == "__main__":
76
+ gradio_interface.launch()