Spaces:
Runtime error
Runtime error
Preloaded model and improved UI
Browse files
app.py
CHANGED
@@ -1,3 +1,5 @@
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import spaces
|
3 |
import torch
|
@@ -5,7 +7,7 @@ from diffusers import AutoPipelineForImage2Image, StableDiffusionInstructPix2Pix
|
|
5 |
from loguru import logger
|
6 |
from PIL import Image
|
7 |
|
8 |
-
|
9 |
"stabilityai/sdxl-turbo",
|
10 |
"stabilityai/stable-diffusion-3-medium-diffusers",
|
11 |
"stabilityai/stable-diffusion-xl-refiner-1.0",
|
@@ -13,6 +15,12 @@ models = [
|
|
13 |
]
|
14 |
DEFAULT_MODEL = "stabilityai/stable-diffusion-xl-refiner-1.0"
|
15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
def load_pipeline(model):
|
18 |
pipeline_type = (
|
@@ -22,96 +30,143 @@ def load_pipeline(model):
|
|
22 |
)
|
23 |
|
24 |
return pipeline_type.from_pretrained(
|
25 |
-
model,
|
26 |
-
torch_dtype=torch.float16,
|
27 |
-
use_safetensors=True,
|
28 |
-
variant="fp16"
|
29 |
)
|
30 |
|
31 |
|
32 |
-
load_pipeline(DEFAULT_MODEL).to("cuda")
|
33 |
-
loaded_models = {DEFAULT_MODEL}
|
34 |
|
35 |
|
36 |
-
|
37 |
-
|
|
|
38 |
prompt: str,
|
39 |
init_image: Image.Image,
|
|
|
|
|
|
|
40 |
strength: float,
|
41 |
-
|
|
|
|
|
42 |
):
|
43 |
-
logger.
|
44 |
-
|
|
|
|
|
|
|
|
|
45 |
|
46 |
logger.debug(f"Generating image: {dict(prompt=prompt)}")
|
47 |
additional_args = (
|
48 |
-
{}
|
|
|
|
|
49 |
)
|
50 |
|
51 |
-
def progress_callback(pipe, step_index, timestep, callback_kwargs):
|
52 |
-
logger.trace(
|
53 |
-
f"Callback: {dict(num_timesteps=pipe.num_timesteps, step_index=step_index, timestep=timestep)}"
|
54 |
-
)
|
55 |
-
progress((step_index + 1, pipe.num_timesteps))
|
56 |
-
return callback_kwargs
|
57 |
-
|
58 |
images = pipe(
|
59 |
prompt=prompt,
|
60 |
image=init_image,
|
61 |
-
|
|
|
|
|
|
|
|
|
62 |
**additional_args,
|
63 |
).images
|
64 |
return images[0]
|
65 |
|
66 |
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
)
|
88 |
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
# Cache the model files for the pipeline
|
93 |
-
if model not in loaded_models:
|
94 |
-
logger.debug(f"Caching pipeline: {dict(model=model)}")
|
95 |
-
|
96 |
-
load_pipeline(model)
|
97 |
-
loaded_models.add(model)
|
98 |
-
|
99 |
-
gpu_runner = gpu_3min if model == "timbrooks/instruct-pix2pix" else gpu
|
100 |
-
|
101 |
-
return gpu_runner(model, prompt, init_image, strength, progress)
|
102 |
-
|
103 |
-
|
104 |
-
demo = gr.Interface(
|
105 |
-
fn=generate,
|
106 |
-
inputs=[
|
107 |
-
gr.Dropdown(
|
108 |
-
label="Model", choices=models, value=DEFAULT_MODEL, allow_custom_value=True
|
109 |
-
),
|
110 |
-
gr.Text(label="Prompt"),
|
111 |
-
gr.Image(label="Init image", type="pil"),
|
112 |
-
gr.Slider(label="Strength", minimum=0, maximum=1, value=0.3),
|
113 |
-
],
|
114 |
-
outputs=[gr.Image(label="Output")],
|
115 |
-
)
|
116 |
-
|
117 |
-
demo.launch()
|
|
|
1 |
+
import os
|
2 |
+
|
3 |
import gradio as gr
|
4 |
import spaces
|
5 |
import torch
|
|
|
7 |
from loguru import logger
|
8 |
from PIL import Image
|
9 |
|
10 |
+
SUPPORTED_MODELS = [
|
11 |
"stabilityai/sdxl-turbo",
|
12 |
"stabilityai/stable-diffusion-3-medium-diffusers",
|
13 |
"stabilityai/stable-diffusion-xl-refiner-1.0",
|
|
|
15 |
]
|
16 |
DEFAULT_MODEL = "stabilityai/stable-diffusion-xl-refiner-1.0"
|
17 |
|
18 |
+
MAX_IMAGE_SIZE = 1024
|
19 |
+
|
20 |
+
|
21 |
+
model = os.environ.get("MODEL_ID", DEFAULT_MODEL)
|
22 |
+
gpu_duration = int(os.environ.get("GPU_DURATION", 60))
|
23 |
+
|
24 |
|
25 |
def load_pipeline(model):
|
26 |
pipeline_type = (
|
|
|
30 |
)
|
31 |
|
32 |
return pipeline_type.from_pretrained(
|
33 |
+
model, torch_dtype=torch.float16, use_safetensors=True, variant="fp16"
|
|
|
|
|
|
|
34 |
)
|
35 |
|
36 |
|
37 |
+
pipe = load_pipeline(DEFAULT_MODEL).to("cuda")
|
|
|
38 |
|
39 |
|
40 |
+
@logger.catch(reraise=True)
|
41 |
+
@spaces.GPU(duration=gpu_duration)
|
42 |
+
def infer(
|
43 |
prompt: str,
|
44 |
init_image: Image.Image,
|
45 |
+
negative_prompt: str,
|
46 |
+
width: int,
|
47 |
+
height: int,
|
48 |
strength: float,
|
49 |
+
num_inference_steps: int,
|
50 |
+
guidance_scale: float,
|
51 |
+
progress=gr.Progress(track_tqdm=True),
|
52 |
):
|
53 |
+
logger.info(
|
54 |
+
f"Starting image generation: {dict(model=model, prompt=prompt, image=init_image)}"
|
55 |
+
)
|
56 |
+
|
57 |
+
# Downscale the image
|
58 |
+
init_image.thumbnail((1024, 1024))
|
59 |
|
60 |
logger.debug(f"Generating image: {dict(prompt=prompt)}")
|
61 |
additional_args = (
|
62 |
+
{}
|
63 |
+
if model == "timbrooks/instruct-pix2pix" or strength == 0
|
64 |
+
else dict(strength=strength)
|
65 |
)
|
66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
images = pipe(
|
68 |
prompt=prompt,
|
69 |
image=init_image,
|
70 |
+
negative_prompt=negative_prompt,
|
71 |
+
width=width,
|
72 |
+
height=height,
|
73 |
+
num_inference_steps=num_inference_steps,
|
74 |
+
guidance_scale=guidance_scale,
|
75 |
**additional_args,
|
76 |
).images
|
77 |
return images[0]
|
78 |
|
79 |
|
80 |
+
css = """
|
81 |
+
#col-container {
|
82 |
+
margin: 0 auto;
|
83 |
+
max-width: 640px;
|
84 |
+
}
|
85 |
+
"""
|
86 |
+
|
87 |
+
with gr.Blocks(css=css) as demo:
|
88 |
+
with gr.Column(elem_id="col-container"):
|
89 |
+
gr.Markdown("# Image-to-Image")
|
90 |
+
gr.Markdown(f"## Model: {model}")
|
91 |
+
|
92 |
+
with gr.Row():
|
93 |
+
prompt = gr.Text(
|
94 |
+
label="Prompt",
|
95 |
+
show_label=False,
|
96 |
+
max_lines=1,
|
97 |
+
placeholder="Enter your prompt",
|
98 |
+
container=False,
|
99 |
+
)
|
100 |
+
|
101 |
+
run_button = gr.Button("Run", scale=0, variant="primary")
|
102 |
+
|
103 |
+
init_image = gr.Image(label="Initial image", type="pil")
|
104 |
+
|
105 |
+
result = gr.Image(label="Result", show_label=False)
|
106 |
+
|
107 |
+
with gr.Accordion("Advanced Settings", open=False):
|
108 |
+
negative_prompt = gr.Text(
|
109 |
+
label="Negative prompt",
|
110 |
+
max_lines=1,
|
111 |
+
placeholder="Enter a negative prompt",
|
112 |
+
)
|
113 |
+
|
114 |
+
with gr.Row():
|
115 |
+
width = gr.Slider(
|
116 |
+
label="Width",
|
117 |
+
minimum=256,
|
118 |
+
maximum=MAX_IMAGE_SIZE,
|
119 |
+
step=32,
|
120 |
+
value=1024,
|
121 |
+
)
|
122 |
+
|
123 |
+
height = gr.Slider(
|
124 |
+
label="Height",
|
125 |
+
minimum=256,
|
126 |
+
maximum=MAX_IMAGE_SIZE,
|
127 |
+
step=32,
|
128 |
+
value=1024,
|
129 |
+
)
|
130 |
+
|
131 |
+
with gr.Row():
|
132 |
+
strength = gr.Slider(
|
133 |
+
label="Strength",
|
134 |
+
minimum=0.0,
|
135 |
+
maximum=1.0,
|
136 |
+
step=0.01,
|
137 |
+
value=0.0,
|
138 |
+
)
|
139 |
+
|
140 |
+
num_inference_steps = gr.Slider(
|
141 |
+
label="Number of inference steps",
|
142 |
+
minimum=1,
|
143 |
+
maximum=100,
|
144 |
+
step=1,
|
145 |
+
value=50,
|
146 |
+
)
|
147 |
+
|
148 |
+
guidance_scale = gr.Slider(
|
149 |
+
label="Guidance scale",
|
150 |
+
minimum=0.0,
|
151 |
+
maximum=100.0,
|
152 |
+
step=0.1,
|
153 |
+
value=0.0,
|
154 |
+
)
|
155 |
+
gr.on(
|
156 |
+
triggers=[run_button.click, prompt.submit],
|
157 |
+
fn=infer,
|
158 |
+
inputs=[
|
159 |
+
prompt,
|
160 |
+
init_image,
|
161 |
+
negative_prompt,
|
162 |
+
width,
|
163 |
+
height,
|
164 |
+
strength,
|
165 |
+
num_inference_steps,
|
166 |
+
guidance_scale,
|
167 |
+
],
|
168 |
+
outputs=[result],
|
169 |
)
|
170 |
|
171 |
+
if __name__ == "__main__":
|
172 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|