RanM commited on
Commit
cfeca25
·
verified ·
1 Parent(s): 94bdfb6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -18
app.py CHANGED
@@ -4,17 +4,16 @@ from generate_prompts import generate_prompt
4
  from diffusers import AutoPipelineForText2Image
5
  from io import BytesIO
6
  import gradio as gr
7
- from concurrent.futures import ThreadPoolExecutor
8
 
9
  # Load the model once outside of the function
10
  print("Loading the model...")
11
  model = AutoPipelineForText2Image.from_pretrained("stabilityai/sdxl-turbo")
12
  print("Model loaded successfully.")
13
 
14
- def generate_image(prompt, prompt_name):
15
  try:
16
  print(f"Generating response for {prompt_name} with prompt: {prompt}")
17
- output = model(prompt=prompt, num_inference_steps=1, guidance_scale=0.0)
18
  print(f"Output for {prompt_name}: {output}")
19
 
20
  # Check if the model returned images
@@ -35,7 +34,7 @@ def generate_image(prompt, prompt_name):
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, batch_size=5):
39
  print(f"queue_api_calls invoked with sentence_mapping: {sentence_mapping}, character_dict: {character_dict}, selected_style: {selected_style}")
40
  prompts = []
41
 
@@ -47,21 +46,13 @@ async def queue_api_calls(sentence_mapping, character_dict, selected_style, batc
47
  prompts.append((paragraph_number, prompt))
48
  print(f"Generated prompt for paragraph {paragraph_number}: {prompt}")
49
 
50
- # Set max_workers to the total number of prompts
51
- max_workers = min(batch_size, len(prompts))
52
-
53
- # Generate images for each prompt in parallel using threading
54
- images = {}
55
- with ThreadPoolExecutor(max_workers=max_workers) as executor:
56
- loop = asyncio.get_running_loop()
57
- for i in range(0, len(prompts), batch_size):
58
- batch_prompts = prompts[i:i+batch_size]
59
- tasks = [loop.run_in_executor(executor, generate_image, prompt, f"Prompt {paragraph_number}") for paragraph_number, prompt in batch_prompts]
60
- print("Tasks created for image generation.")
61
- responses = await asyncio.gather(*tasks)
62
- print("Responses received from image generation tasks.")
63
- images.update({paragraph_number: response for (paragraph_number, _), response in zip(batch_prompts, responses)})
64
 
 
65
  print(f"Images generated: {images}")
66
  return images
67
 
 
4
  from diffusers import AutoPipelineForText2Image
5
  from io import BytesIO
6
  import gradio as gr
 
7
 
8
  # Load the model once outside of the function
9
  print("Loading the model...")
10
  model = AutoPipelineForText2Image.from_pretrained("stabilityai/sdxl-turbo")
11
  print("Model loaded successfully.")
12
 
13
+ async def generate_image(prompt, prompt_name):
14
  try:
15
  print(f"Generating response for {prompt_name} with prompt: {prompt}")
16
+ output = await asyncio.to_thread(model, prompt=prompt, num_inference_steps=1, guidance_scale=0.0)
17
  print(f"Output for {prompt_name}: {output}")
18
 
19
  # Check if the model returned images
 
34
  print(f"Error generating image for {prompt_name}: {e}")
35
  return None
36
 
37
+ 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
 
 
46
  prompts.append((paragraph_number, prompt))
47
  print(f"Generated prompt for paragraph {paragraph_number}: {prompt}")
48
 
49
+ # Generate images for each prompt in parallel using asyncio
50
+ tasks = [generate_image(prompt, f"Prompt {paragraph_number}") for paragraph_number, prompt in prompts]
51
+ print("Tasks created for image generation.")
52
+ responses = await asyncio.gather(*tasks)
53
+ print("Responses received from image generation tasks.")
 
 
 
 
 
 
 
 
 
54
 
55
+ images = {paragraph_number: response for (paragraph_number, _), response in zip(prompts, responses)}
56
  print(f"Images generated: {images}")
57
  return images
58