RanM commited on
Commit
3a13c98
·
verified ·
1 Parent(s): e577e0f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -40
app.py CHANGED
@@ -4,48 +4,22 @@ from generate_prompts import generate_prompt
4
  from diffusers import AutoPipelineForText2Image
5
  from io import BytesIO
6
  import gradio as gr
7
- import threading
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
- # Create a thread-local storage object
15
- thread_local = threading.local()
 
 
 
16
 
17
- class Scheduler:
18
- def __init__(self):
19
- self._step = threading.local()
20
- self._step.step = None
21
 
22
- def _init_step_index(self):
23
- self._step.step = 0
24
-
25
- @property
26
- def step(self):
27
- return self._step.step
28
-
29
- def step_process(self):
30
- self._step.step += 1
31
-
32
- scheduler = Scheduler()
33
-
34
- def generate_image(prompt, prompt_name):
35
  try:
36
- # Initialize step index for the current thread
37
- if scheduler.step is None:
38
- scheduler._init_step_index()
39
-
40
- print(f"Initial step index for {prompt_name}: {scheduler.step}")
41
  print(f"Generating response for {prompt_name} with prompt: {prompt}")
42
-
43
  output = model(prompt=prompt, num_inference_steps=1, guidance_scale=0.0)
44
-
45
- # Update and print step index
46
- scheduler.step_process()
47
- print(f"Updated step index for {prompt_name}: {scheduler.step}")
48
-
49
  print(f"Output for {prompt_name}: {output}")
50
 
51
  # Check if the model returned images
@@ -79,8 +53,7 @@ async def queue_api_calls(sentence_mapping, character_dict, selected_style):
79
  print(f"Generated prompt for paragraph {paragraph_number}: {prompt}")
80
 
81
  # Generate images for each prompt in parallel
82
- loop = asyncio.get_running_loop()
83
- tasks = [loop.run_in_executor(None, generate_image, prompt, f"Prompt {paragraph_number}") for paragraph_number, prompt in prompts]
84
  print("Tasks created for image generation.")
85
  responses = await asyncio.gather(*tasks)
86
  print("Responses received from image generation tasks.")
@@ -100,9 +73,6 @@ def process_prompt(sentence_mapping, character_dict, selected_style):
100
  asyncio.set_event_loop(loop)
101
  print("Event loop created.")
102
 
103
- # Initialize thread-local variables
104
- scheduler._init_step_index()
105
-
106
  # This sends the prompts to function that sets up the async calls. Once all the calls to the API complete, it returns a list of the gr.Textbox with value= set.
107
  cmpt_return = loop.run_until_complete(queue_api_calls(sentence_mapping, character_dict, selected_style))
108
  print(f"process_prompt completed with return value: {cmpt_return}")
 
4
  from diffusers import AutoPipelineForText2Image
5
  from io import BytesIO
6
  import gradio as gr
 
7
 
8
+ # Asynchronously load the model once outside of the function
9
+ model = None
 
 
10
 
11
+ async def load_model():
12
+ global model
13
+ print("Loading the model...")
14
+ model = AutoPipelineForText2Image.from_pretrained("stabilityai/sdxl-turbo")
15
+ print("Model loaded successfully.")
16
 
17
+ asyncio.run(load_model())
 
 
 
18
 
19
+ async def generate_image(prompt, prompt_name):
 
 
 
 
 
 
 
 
 
 
 
 
20
  try:
 
 
 
 
 
21
  print(f"Generating response for {prompt_name} with prompt: {prompt}")
 
22
  output = model(prompt=prompt, num_inference_steps=1, guidance_scale=0.0)
 
 
 
 
 
23
  print(f"Output for {prompt_name}: {output}")
24
 
25
  # Check if the model returned images
 
53
  print(f"Generated prompt for paragraph {paragraph_number}: {prompt}")
54
 
55
  # Generate images for each prompt in parallel
56
+ tasks = [generate_image(prompt, f"Prompt {paragraph_number}") for paragraph_number, prompt in prompts]
 
57
  print("Tasks created for image generation.")
58
  responses = await asyncio.gather(*tasks)
59
  print("Responses received from image generation tasks.")
 
73
  asyncio.set_event_loop(loop)
74
  print("Event loop created.")
75
 
 
 
 
76
  # This sends the prompts to function that sets up the async calls. Once all the calls to the API complete, it returns a list of the gr.Textbox with value= set.
77
  cmpt_return = loop.run_until_complete(queue_api_calls(sentence_mapping, character_dict, selected_style))
78
  print(f"process_prompt completed with return value: {cmpt_return}")