RanM commited on
Commit
b47d25d
·
verified ·
1 Parent(s): 069fef5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -5
app.py CHANGED
@@ -5,16 +5,24 @@ from diffusers import AutoPipelineForText2Image
5
  from io import BytesIO
6
  import gradio as gr
7
  from concurrent.futures import ProcessPoolExecutor
 
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
@@ -47,11 +55,11 @@ 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
- # Set max_workers to the total number of prompts
51
- max_workers = len(prompts)
52
 
53
  # Generate images for each prompt in parallel using multiprocessing
54
- with ProcessPoolExecutor(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.")
 
5
  from io import BytesIO
6
  import gradio as gr
7
  from concurrent.futures import ProcessPoolExecutor
8
+ import multiprocessing
9
 
10
  # Load the model once outside of the function
11
  print("Loading the model...")
12
  model = AutoPipelineForText2Image.from_pretrained("stabilityai/sdxl-turbo")
13
  print("Model loaded successfully.")
14
 
15
+ def initialize_model():
16
+ global model
17
+ if 'model' not in globals():
18
+ print("Loading the model in worker...")
19
+ model = AutoPipelineForText2Image.from_pretrained("stabilityai/sdxl-turbo")
20
+ print("Model loaded in worker successfully.")
21
+
22
+ def generate_image(prompt, prompt_name, num_inference_steps=1):
23
  try:
24
  print(f"Generating response for {prompt_name} with prompt: {prompt}")
25
+ output = model(prompt=prompt, num_inference_steps=num_inference_steps, guidance_scale=0.0)
26
  print(f"Output for {prompt_name}: {output}")
27
 
28
  # Check if the model returned images
 
55
  prompts.append((paragraph_number, prompt))
56
  print(f"Generated prompt for paragraph {paragraph_number}: {prompt}")
57
 
58
+ # Set max_workers to the minimum of the number of prompts and available CPU cores
59
+ max_workers = min(len(prompts), multiprocessing.cpu_count())
60
 
61
  # Generate images for each prompt in parallel using multiprocessing
62
+ with ProcessPoolExecutor(max_workers=max_workers, initializer=initialize_model) as executor:
63
  loop = asyncio.get_running_loop()
64
  tasks = [loop.run_in_executor(executor, generate_image, prompt, f"Prompt {paragraph_number}") for paragraph_number, prompt in prompts]
65
  print("Tasks created for image generation.")