Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,25 +1,46 @@
|
|
1 |
import gradio as gr
|
2 |
-
from diffusers import AutoPipelineForText2Image
|
3 |
from generate_propmts import generate_prompt
|
4 |
from PIL import Image
|
5 |
import asyncio
|
|
|
6 |
import traceback
|
7 |
|
8 |
-
# Load the model
|
9 |
model = AutoPipelineForText2Image.from_pretrained("stabilityai/sdxl-turbo")
|
10 |
-
model.scheduler = DPMSolverMultistepScheduler.from_config(model.scheduler.config) # Changed scheduler
|
11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
async def generate_image(prompt):
|
14 |
try:
|
15 |
-
|
|
|
|
|
|
|
16 |
output = await asyncio.to_thread(
|
17 |
model,
|
18 |
prompt=prompt,
|
19 |
-
num_inference_steps=num_inference_steps,
|
20 |
-
guidance_scale=0.0,
|
21 |
-
output_type="pil"
|
22 |
)
|
|
|
|
|
23 |
if output.images:
|
24 |
return output.images[0]
|
25 |
else:
|
@@ -27,8 +48,7 @@ async def generate_image(prompt):
|
|
27 |
except Exception as e:
|
28 |
print(f"Error generating image: {e}")
|
29 |
traceback.print_exc()
|
30 |
-
return None
|
31 |
-
|
32 |
|
33 |
async def inference(sentence_mapping, character_dict, selected_style):
|
34 |
images = []
|
|
|
1 |
import gradio as gr
|
2 |
+
from diffusers import AutoPipelineForText2Image
|
3 |
from generate_propmts import generate_prompt
|
4 |
from PIL import Image
|
5 |
import asyncio
|
6 |
+
import threading
|
7 |
import traceback
|
8 |
|
9 |
+
# Load the model once outside of the function
|
10 |
model = AutoPipelineForText2Image.from_pretrained("stabilityai/sdxl-turbo")
|
|
|
11 |
|
12 |
+
class SchedulerWrapper:
|
13 |
+
def __init__(self, scheduler):
|
14 |
+
self.scheduler = scheduler
|
15 |
+
self._step = threading.local()
|
16 |
+
self._step.step = 0
|
17 |
+
|
18 |
+
def step(self, *args, **kwargs):
|
19 |
+
try:
|
20 |
+
self._step.step += 1
|
21 |
+
return self.scheduler.step(*args, **kwargs)
|
22 |
+
except IndexError:
|
23 |
+
self._step.step = 0
|
24 |
+
return self.scheduler.step(*args, **kwargs)
|
25 |
+
|
26 |
+
# Wrap the scheduler
|
27 |
+
model.scheduler = SchedulerWrapper(model.scheduler)
|
28 |
|
29 |
async def generate_image(prompt):
|
30 |
try:
|
31 |
+
# Set a higher value for num_inference_steps
|
32 |
+
num_inference_steps = 5 # Adjust this value as needed
|
33 |
+
|
34 |
+
# Use the model to generate an image
|
35 |
output = await asyncio.to_thread(
|
36 |
model,
|
37 |
prompt=prompt,
|
38 |
+
num_inference_steps=num_inference_steps,
|
39 |
+
guidance_scale=0.0, # Typical value for guidance scale in image generation
|
40 |
+
output_type="pil" # Directly get PIL Image objects
|
41 |
)
|
42 |
+
|
43 |
+
# Check for output validity and return
|
44 |
if output.images:
|
45 |
return output.images[0]
|
46 |
else:
|
|
|
48 |
except Exception as e:
|
49 |
print(f"Error generating image: {e}")
|
50 |
traceback.print_exc()
|
51 |
+
return None # Return None on error to handle it gracefully in the UI
|
|
|
52 |
|
53 |
async def inference(sentence_mapping, character_dict, selected_style):
|
54 |
images = []
|