RanM commited on
Commit
d26a101
·
verified ·
1 Parent(s): 1e4ec94

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -11
app.py CHANGED
@@ -8,25 +8,37 @@ import traceback
8
  # Load the model once outside of the function
9
  model = AutoPipelineForText2Image.from_pretrained("stabilityai/sdxl-turbo")
10
 
 
 
 
11
  def generate_image(prompt):
12
  try:
13
- output = model(prompt=prompt, num_inference_steps=1, guidance_scale=0.0)
14
- print(f"Model output: {output}")
 
 
 
 
 
 
 
 
 
15
 
16
- # Check if the model returned images
17
- if isinstance(output.images, list) and len(output.images) > 0:
 
 
 
18
  return output.images[0]
19
  else:
20
  raise Exception("No images returned by the model.")
21
-
22
- except IndexError as e:
23
- print(f"Index error during image generation: {e}")
24
- traceback.print_exc()
25
- return None
26
  except Exception as e:
27
  print(f"Error generating image: {e}")
28
  traceback.print_exc()
29
- return None
 
30
 
31
  def inference(sentence_mapping, character_dict, selected_style):
32
  images = []
@@ -62,7 +74,6 @@ gradio_interface = gr.Interface(
62
  gr.Dropdown(["oil painting", "sketch", "watercolor"], label="Selected Style")
63
  ],
64
  outputs=gr.Gallery(label="Generated Images")
65
- ).queue(default_concurrency_limit=5)
66
 
67
  if __name__ == "__main__":
68
  gradio_interface.launch()
 
8
  # Load the model once outside of the function
9
  model = AutoPipelineForText2Image.from_pretrained("stabilityai/sdxl-turbo")
10
 
11
+ # Create a thread-local storage for step indices
12
+ scheduler_step_storage = threading.local()
13
+
14
  def generate_image(prompt):
15
  try:
16
+ # Initialize step index per thread if not already set
17
+ if not hasattr(scheduler_step_storage, 'step'):
18
+ scheduler_step_storage.step = 0
19
+
20
+ # Use the thread-local step index
21
+ output = model(
22
+ prompt=prompt,
23
+ num_inference_steps=1, # Add a sensible default for inference steps
24
+ guidance_scale=0.0,
25
+ output_type="pil" # Directly get PIL Image objects
26
+ )
27
 
28
+ # Increment the step index after generating the image
29
+ scheduler_step_storage.step += 1
30
+
31
+ # Check for output validity and return
32
+ if output.images:
33
  return output.images[0]
34
  else:
35
  raise Exception("No images returned by the model.")
36
+
 
 
 
 
37
  except Exception as e:
38
  print(f"Error generating image: {e}")
39
  traceback.print_exc()
40
+ return None # Return None on error to handle it gracefully in the UI
41
+
42
 
43
  def inference(sentence_mapping, character_dict, selected_style):
44
  images = []
 
74
  gr.Dropdown(["oil painting", "sketch", "watercolor"], label="Selected Style")
75
  ],
76
  outputs=gr.Gallery(label="Generated Images")
 
77
 
78
  if __name__ == "__main__":
79
  gradio_interface.launch()