Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,473 +1,373 @@
|
|
1 |
-
|
2 |
|
3 |
-
from
|
4 |
-
import torch
|
5 |
|
6 |
-
import
|
7 |
-
from io import BytesIO
|
8 |
import os
|
9 |
-
import
|
10 |
-
import warnings
|
11 |
-
|
12 |
-
# Only used when MULTI_GPU set to True
|
13 |
-
from helper import UNetDataParallel
|
14 |
-
from share_btn import community_icon_html, loading_icon_html, share_js
|
15 |
-
|
16 |
-
# SDXL code: https://github.com/huggingface/diffusers/pull/3859
|
17 |
-
|
18 |
-
# Process environment variables
|
19 |
-
# Use `segmind/SSD-1B` (distilled SDXL) for faster generation.
|
20 |
-
use_ssd = os.getenv("USE_SSD", "false").lower() == "true"
|
21 |
-
if use_ssd:
|
22 |
-
model_key_base = "segmind/SSD-1B"
|
23 |
-
model_key_refiner = "stabilityai/stable-diffusion-xl-refiner-1.0"
|
24 |
-
lcm_lora_id = "latent-consistency/lcm-lora-ssd-1b"
|
25 |
-
else:
|
26 |
-
model_key_base = "stabilityai/stable-diffusion-xl-base-1.0"
|
27 |
-
model_key_refiner = "stabilityai/stable-diffusion-xl-refiner-1.0"
|
28 |
-
lcm_lora_id = "latent-consistency/lcm-lora-sdxl"
|
29 |
-
|
30 |
-
# Use LCM LoRA (enabled by default)
|
31 |
-
if "ENABLE_LCM" not in os.environ:
|
32 |
-
warnings.warn("`ENABLE_LCM` environment variable is not set. LCM LoRA will be loaded by default and refiner will be disabled by default. You can set it to `False` to turn off LCM LoRA.")
|
33 |
-
|
34 |
-
enable_lcm = os.getenv("ENABLE_LCM", "true").lower() == "true"
|
35 |
-
# Use refiner (disabled by default if LCM is enabled)
|
36 |
-
enable_refiner = os.getenv("ENABLE_REFINER", "false" if enable_lcm or use_ssd else "true").lower() == "true"
|
37 |
-
# Output images before the refiner and after the refiner
|
38 |
-
output_images_before_refiner = os.getenv("OUTPUT_IMAGES_BEFORE_REFINER", "false").lower() == "true"
|
39 |
-
|
40 |
-
offload_base = os.getenv("OFFLOAD_BASE", "false").lower() == "true"
|
41 |
-
offload_refiner = os.getenv("OFFLOAD_REFINER", "true").lower() == "true"
|
42 |
-
|
43 |
-
# Generate how many images by default
|
44 |
-
default_num_images = int(os.getenv("DEFAULT_NUM_IMAGES", "4"))
|
45 |
-
if default_num_images < 1:
|
46 |
-
default_num_images = 1
|
47 |
-
|
48 |
-
# Create public link
|
49 |
-
share = os.getenv("SHARE", "false").lower() == "true"
|
50 |
-
|
51 |
-
print("Loading model", model_key_base)
|
52 |
-
pipe = DiffusionPipeline.from_pretrained(model_key_base, torch_dtype=torch.float16, use_safetensors=True, variant="fp16")
|
53 |
-
|
54 |
-
if enable_lcm:
|
55 |
-
pipe.load_lora_weights(lcm_lora_id)
|
56 |
-
pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)
|
57 |
-
|
58 |
-
multi_gpu = os.getenv("MULTI_GPU", "false").lower() == "true"
|
59 |
-
|
60 |
-
if multi_gpu:
|
61 |
-
pipe.unet = UNetDataParallel(pipe.unet)
|
62 |
-
pipe.unet.config, pipe.unet.dtype, pipe.unet.add_embedding = pipe.unet.module.config, pipe.unet.module.dtype, pipe.unet.module.add_embedding
|
63 |
-
pipe.to("cuda")
|
64 |
-
else:
|
65 |
-
if offload_base:
|
66 |
-
pipe.enable_model_cpu_offload()
|
67 |
-
else:
|
68 |
-
pipe.to("cuda")
|
69 |
-
|
70 |
-
# if using torch < 2.0
|
71 |
-
# pipe.enable_xformers_memory_efficient_attention()
|
72 |
-
|
73 |
-
# pipe.unet = torch.compile(pipe.unet, mode="reduce-overhead", fullgraph=True)
|
74 |
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
if
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
122 |
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
130 |
|
131 |
-
|
132 |
-
|
133 |
-
image.save(buffered, format="JPEG")
|
134 |
-
img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
135 |
-
|
136 |
-
image_b64 = (f"data:image/jpeg;base64,{img_str}")
|
137 |
-
images_b64_list.append(image_b64)
|
138 |
-
|
139 |
-
return images_b64_list
|
140 |
-
|
141 |
-
# Reference: https://huggingface.co/spaces/google/sdxl/blob/main/app.py#L139
|
142 |
-
css = """
|
143 |
-
.gradio-container {
|
144 |
-
font-family: 'IBM Plex Sans', sans-serif;
|
145 |
-
}
|
146 |
-
.gr-button {
|
147 |
-
color: white;
|
148 |
-
border-color: black;
|
149 |
-
background: black;
|
150 |
-
}
|
151 |
-
input[type='range'] {
|
152 |
-
accent-color: black;
|
153 |
-
}
|
154 |
-
.dark input[type='range'] {
|
155 |
-
accent-color: #dfdfdf;
|
156 |
-
}
|
157 |
-
.gradio-container {
|
158 |
-
max-width: 730px !important;
|
159 |
-
margin: auto;
|
160 |
-
padding-top: 1.5rem;
|
161 |
-
}
|
162 |
-
#gallery {
|
163 |
-
min-height: 22rem;
|
164 |
-
margin-bottom: 15px;
|
165 |
-
margin-left: auto;
|
166 |
-
margin-right: auto;
|
167 |
-
border-bottom-right-radius: .5rem !important;
|
168 |
-
border-bottom-left-radius: .5rem !important;
|
169 |
-
}
|
170 |
-
#gallery>div>.h-full {
|
171 |
-
min-height: 20rem;
|
172 |
-
}
|
173 |
-
.details:hover {
|
174 |
-
text-decoration: underline;
|
175 |
-
}
|
176 |
-
.gr-button {
|
177 |
-
white-space: nowrap;
|
178 |
-
}
|
179 |
-
.gr-button:focus {
|
180 |
-
border-color: rgb(147 197 253 / var(--tw-border-opacity));
|
181 |
-
outline: none;
|
182 |
-
box-shadow: var(--tw-ring-offset-shadow), var(--tw-ring-shadow), var(--tw-shadow, 0 0 #0000);
|
183 |
-
--tw-border-opacity: 1;
|
184 |
-
--tw-ring-offset-shadow: var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color);
|
185 |
-
--tw-ring-shadow: var(--tw-ring-inset) 0 0 0 calc(3px var(--tw-ring-offset-width)) var(--tw-ring-color);
|
186 |
-
--tw-ring-color: rgb(191 219 254 / var(--tw-ring-opacity));
|
187 |
-
--tw-ring-opacity: .5;
|
188 |
-
}
|
189 |
-
#advanced-btn {
|
190 |
-
font-size: .7rem !important;
|
191 |
-
line-height: 19px;
|
192 |
-
margin-top: 12px;
|
193 |
-
margin-bottom: 12px;
|
194 |
-
padding: 2px 8px;
|
195 |
-
border-radius: 14px !important;
|
196 |
-
}
|
197 |
-
#advanced-options {
|
198 |
-
display: none;
|
199 |
-
margin-bottom: 20px;
|
200 |
-
}
|
201 |
-
.footer {
|
202 |
-
margin-bottom: 45px;
|
203 |
-
margin-top: 35px;
|
204 |
-
text-align: center;
|
205 |
-
border-bottom: 1px solid #e5e5e5;
|
206 |
-
}
|
207 |
-
.footer>p {
|
208 |
-
font-size: .8rem;
|
209 |
-
display: inline-block;
|
210 |
-
padding: 10px 10px;
|
211 |
-
transform: translateY(10px);
|
212 |
-
background: white;
|
213 |
-
}
|
214 |
-
.dark .footer {
|
215 |
-
border-color: #303030;
|
216 |
-
}
|
217 |
-
.dark .footer>p {
|
218 |
-
background: #0b0f19;
|
219 |
-
}
|
220 |
-
.acknowledgments h4{
|
221 |
-
margin: 1.25em 0 .25em 0;
|
222 |
-
font-weight: bold;
|
223 |
-
font-size: 115%;
|
224 |
-
}
|
225 |
-
.animate-spin {
|
226 |
-
animation: spin 1s linear infinite;
|
227 |
-
}
|
228 |
-
@keyframes spin {
|
229 |
-
from {
|
230 |
-
transform: rotate(0deg);
|
231 |
-
}
|
232 |
-
to {
|
233 |
-
transform: rotate(360deg);
|
234 |
-
}
|
235 |
-
}
|
236 |
-
#share-btn-container {
|
237 |
-
display: flex; padding-left: 0.5rem !important; padding-right: 0.5rem !important; background-color: #000000; justify-content: center; align-items: center; border-radius: 9999px !important; width: 13rem;
|
238 |
-
margin-top: 10px;
|
239 |
-
margin-left: auto;
|
240 |
-
}
|
241 |
-
#share-btn {
|
242 |
-
all: initial; color: #ffffff;font-weight: 600; cursor:pointer; font-family: 'IBM Plex Sans', sans-serif; margin-left: 0.5rem !important; padding-top: 0.25rem !important; padding-bottom: 0.25rem !important;right:0;
|
243 |
-
}
|
244 |
-
#share-btn * {
|
245 |
-
all: unset;
|
246 |
-
}
|
247 |
-
#share-btn-container div:nth-child(-n+2){
|
248 |
-
width: auto !important;
|
249 |
-
min-height: 0px !important;
|
250 |
-
}
|
251 |
-
#share-btn-container .wrap {
|
252 |
-
display: none !important;
|
253 |
-
}
|
254 |
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
#
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
#
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
|
284 |
-
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
|
295 |
-
|
296 |
-
|
297 |
-
|
298 |
-
|
299 |
-
|
300 |
-
|
301 |
-
|
302 |
-
|
303 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
304 |
gr.HTML(
|
305 |
-
|
306 |
-
<div style="text-align: center; margin: 0 auto;">
|
307 |
-
<div
|
308 |
-
style="
|
309 |
-
display: inline-flex;
|
310 |
-
align-items: center;
|
311 |
-
gap: 0.8rem;
|
312 |
-
font-size: 1.75rem;
|
313 |
-
"
|
314 |
-
>
|
315 |
-
<svg
|
316 |
-
width="0.65em"
|
317 |
-
height="0.65em"
|
318 |
-
viewBox="0 0 115 115"
|
319 |
-
fill="none"
|
320 |
-
xmlns="http://www.w3.org/2000/svg"
|
321 |
-
>
|
322 |
-
<rect width="23" height="23" fill="white"></rect>
|
323 |
-
<rect y="69" width="23" height="23" fill="white"></rect>
|
324 |
-
<rect x="23" width="23" height="23" fill="#AEAEAE"></rect>
|
325 |
-
<rect x="23" y="69" width="23" height="23" fill="#AEAEAE"></rect>
|
326 |
-
<rect x="46" width="23" height="23" fill="white"></rect>
|
327 |
-
<rect x="46" y="69" width="23" height="23" fill="white"></rect>
|
328 |
-
<rect x="69" width="23" height="23" fill="black"></rect>
|
329 |
-
<rect x="69" y="69" width="23" height="23" fill="black"></rect>
|
330 |
-
<rect x="92" width="23" height="23" fill="#D9D9D9"></rect>
|
331 |
-
<rect x="92" y="69" width="23" height="23" fill="#AEAEAE"></rect>
|
332 |
-
<rect x="115" y="46" width="23" height="23" fill="white"></rect>
|
333 |
-
<rect x="115" y="115" width="23" height="23" fill="white"></rect>
|
334 |
-
<rect x="115" y="69" width="23" height="23" fill="#D9D9D9"></rect>
|
335 |
-
<rect x="92" y="46" width="23" height="23" fill="#AEAEAE"></rect>
|
336 |
-
<rect x="92" y="115" width="23" height="23" fill="#AEAEAE"></rect>
|
337 |
-
<rect x="92" y="69" width="23" height="23" fill="white"></rect>
|
338 |
-
<rect x="69" y="46" width="23" height="23" fill="white"></rect>
|
339 |
-
<rect x="69" y="115" width="23" height="23" fill="white"></rect>
|
340 |
-
<rect x="69" y="69" width="23" height="23" fill="#D9D9D9"></rect>
|
341 |
-
<rect x="46" y="46" width="23" height="23" fill="black"></rect>
|
342 |
-
<rect x="46" y="115" width="23" height="23" fill="black"></rect>
|
343 |
-
<rect x="46" y="69" width="23" height="23" fill="black"></rect>
|
344 |
-
<rect x="23" y="46" width="23" height="23" fill="#D9D9D9"></rect>
|
345 |
-
<rect x="23" y="115" width="23" height="23" fill="#AEAEAE"></rect>
|
346 |
-
<rect x="23" y="69" width="23" height="23" fill="black"></rect>
|
347 |
-
</svg>
|
348 |
-
<h1 style="font-weight: 900; margin-bottom: 7px;margin-top:5px">
|
349 |
-
Stable Diffusion XL 1.0 Demo
|
350 |
-
</h1>
|
351 |
-
</div>
|
352 |
-
<p style="margin-bottom: 10px; font-size: 94%; line-height: 23px;">
|
353 |
-
Stable Diffusion XL 1.0 is the latest text-to-image model from StabilityAI.
|
354 |
-
<br/>
|
355 |
-
Source code of this space is on
|
356 |
-
<a
|
357 |
-
href="https://github.com/TonyLianLong/stable-diffusion-xl-demo"
|
358 |
-
style="text-decoration: underline;"
|
359 |
-
target="_blank"
|
360 |
-
>TonyLianLong/stable-diffusion-xl-demo</a>.
|
361 |
-
</p>
|
362 |
-
</div>
|
363 |
-
"""
|
364 |
)
|
|
|
365 |
with gr.Group():
|
366 |
-
|
367 |
-
|
368 |
-
|
369 |
-
|
370 |
-
|
371 |
-
|
372 |
-
|
373 |
-
|
374 |
-
|
375 |
-
|
376 |
-
border=(True, False, True, True),
|
377 |
-
rounded=(True, False, False, True),
|
378 |
-
container=False,
|
379 |
-
)
|
380 |
-
negative = gr.Textbox(
|
381 |
-
label="Enter your negative prompt",
|
382 |
-
show_label=False,
|
383 |
-
max_lines=1,
|
384 |
-
placeholder="Enter a negative prompt",
|
385 |
-
elem_id="negative-prompt-text-input",
|
386 |
-
).style(
|
387 |
-
border=(True, False, True, True),
|
388 |
-
rounded=(True, False, False, True),
|
389 |
-
container=False,
|
390 |
-
)
|
391 |
-
btn = gr.Button("Generate image", elem_id="generate-image-btn").style(
|
392 |
-
rounded=(False, True, True, False),
|
393 |
-
full_width=False,
|
394 |
-
)
|
395 |
-
|
396 |
-
gallery = gr.Gallery(
|
397 |
-
label="Generated images", show_label=False, elem_id="gallery"
|
398 |
-
).style(grid=[2], height="auto")
|
399 |
-
|
400 |
-
with gr.Group(elem_id="container-advanced-btns"):
|
401 |
-
#advanced_button = gr.Button("Advanced options", elem_id="advanced-btn")
|
402 |
-
with gr.Group(elem_id="share-btn-container"):
|
403 |
-
community_icon = gr.HTML(community_icon_html)
|
404 |
-
loading_icon = gr.HTML(loading_icon_html)
|
405 |
-
share_button = gr.Button("Share to community", elem_id="share-btn")
|
406 |
-
|
407 |
-
with gr.Accordion("Advanced settings", open=False):
|
408 |
-
# gr.Markdown("Advanced settings are temporarily unavailable")
|
409 |
-
samples = gr.Slider(label="Images", minimum=1, maximum=max(16 if enable_lcm else 4, default_num_images), value=default_num_images, step=1)
|
410 |
-
if enable_lcm:
|
411 |
-
steps = gr.Slider(label="Steps", minimum=1, maximum=10, value=4, step=1)
|
412 |
-
else:
|
413 |
-
steps = gr.Slider(label="Steps", minimum=1, maximum=250, value=50, step=1)
|
414 |
-
|
415 |
-
if enable_refiner:
|
416 |
-
refiner_strength = gr.Slider(label="Refiner Strength", minimum=0, maximum=1.0, value=0.3, step=0.1)
|
417 |
-
else:
|
418 |
-
refiner_strength = gr.Slider(label="Refiner Strength (refiner not enabled)", minimum=0, maximum=0, value=0, step=0)
|
419 |
-
guidance_scale = gr.Slider(
|
420 |
-
label="Guidance Scale", minimum=0, maximum=50, value=default_guidance_scale, step=0.1
|
421 |
)
|
422 |
-
|
423 |
-
|
424 |
-
|
425 |
-
|
426 |
-
|
427 |
-
|
428 |
-
|
|
|
|
|
|
|
429 |
)
|
430 |
-
|
431 |
-
|
432 |
-
|
433 |
-
|
434 |
-
|
435 |
-
|
436 |
-
|
437 |
-
|
438 |
-
|
439 |
-
|
440 |
-
|
441 |
-
|
442 |
-
|
443 |
-
|
444 |
-
|
445 |
-
|
446 |
-
#)
|
447 |
-
share_button.click(
|
448 |
-
None,
|
449 |
-
[],
|
450 |
-
[],
|
451 |
-
_js=share_js,
|
452 |
)
|
453 |
-
gr.
|
454 |
-
|
455 |
-
|
456 |
-
|
457 |
-
|
458 |
-
</p>
|
459 |
-
</div>
|
460 |
-
"""
|
461 |
)
|
462 |
-
|
463 |
-
|
464 |
-
|
465 |
-
|
466 |
-
|
467 |
-
|
468 |
-
|
469 |
-
|
470 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
471 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
472 |
|
473 |
-
|
|
|
|
1 |
+
#!/usr/bin/env python
|
2 |
|
3 |
+
from __future__ import annotations
|
|
|
4 |
|
5 |
+
import requests
|
|
|
6 |
import os
|
7 |
+
import random
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
+
import gradio as gr
|
10 |
+
import numpy as np
|
11 |
+
import spaces
|
12 |
+
import torch
|
13 |
+
from PIL import Image
|
14 |
+
from io import BytesIO
|
15 |
+
from diffusers.utils import load_image
|
16 |
+
from diffusers import AutoencoderKL, DiffusionPipeline, AutoPipelineForImage2Image, AutoPipelineForInpainting
|
17 |
+
|
18 |
+
DESCRIPTION = "# Run any LoRA or SD Model"
|
19 |
+
if not torch.cuda.is_available():
|
20 |
+
DESCRIPTION += "\n<p>⚠️ This space is running on the CPU. This demo doesn't work on CPU 😞! Run on a GPU by duplicating this space or test our website for free and unlimited by <a href='https://squaadai.com'>clicking here</a>, which provides these and more options.</p>"
|
21 |
+
|
22 |
+
MAX_SEED = np.iinfo(np.int32).max
|
23 |
+
MAX_IMAGE_SIZE = int(os.getenv("MAX_IMAGE_SIZE", "1824"))
|
24 |
+
USE_TORCH_COMPILE = os.getenv("USE_TORCH_COMPILE") == "1"
|
25 |
+
ENABLE_CPU_OFFLOAD = os.getenv("ENABLE_CPU_OFFLOAD") == "1"
|
26 |
+
ENABLE_USE_LORA = os.getenv("ENABLE_USE_LORA", "1") == "1"
|
27 |
+
ENABLE_USE_VAE = os.getenv("ENABLE_USE_VAE", "1") == "1"
|
28 |
+
ENABLE_USE_IMG2IMG = os.getenv("ENABLE_USE_IMG2IMG", "1") == "1"
|
29 |
+
ENABLE_USE_INPAINTING = os.getenv("ENABLE_USE_INPAINTING", "1") == "1"
|
30 |
+
|
31 |
+
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
32 |
+
|
33 |
+
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
34 |
+
if randomize_seed:
|
35 |
+
seed = random.randint(0, MAX_SEED)
|
36 |
+
return seed
|
37 |
+
|
38 |
+
|
39 |
+
@spaces.GPU
|
40 |
+
def generate(
|
41 |
+
prompt: str,
|
42 |
+
negative_prompt: str = "",
|
43 |
+
prompt_2: str = "",
|
44 |
+
negative_prompt_2: str = "",
|
45 |
+
use_negative_prompt: bool = False,
|
46 |
+
use_prompt_2: bool = False,
|
47 |
+
use_negative_prompt_2: bool = False,
|
48 |
+
seed: int = 0,
|
49 |
+
width: int = 1024,
|
50 |
+
height: int = 1024,
|
51 |
+
guidance_scale_base: float = 5.0,
|
52 |
+
num_inference_steps_base: int = 25,
|
53 |
+
strength_img2img: float = 0.7,
|
54 |
+
use_vae: bool = False,
|
55 |
+
use_lora: bool = False,
|
56 |
+
model = 'stabilityai/stable-diffusion-xl-base-1.0',
|
57 |
+
vaecall = 'madebyollin/sdxl-vae-fp16-fix',
|
58 |
+
lora = '',
|
59 |
+
lora_scale: float = 0.7,
|
60 |
+
use_img2img: bool = False,
|
61 |
+
use_inpainting: bool = False,
|
62 |
+
url = '',
|
63 |
+
img_url = '',
|
64 |
+
mask_url = '',
|
65 |
+
):
|
66 |
+
if torch.cuda.is_available():
|
67 |
+
|
68 |
+
if not use_img2img:
|
69 |
+
pipe = DiffusionPipeline.from_pretrained(model, torch_dtype=torch.float16)
|
70 |
+
|
71 |
+
if use_vae:
|
72 |
+
vae = AutoencoderKL.from_pretrained(vaecall, torch_dtype=torch.float16)
|
73 |
+
pipe = DiffusionPipeline.from_pretrained(model, vae=vae, torch_dtype=torch.float16)
|
74 |
+
|
75 |
+
if use_img2img:
|
76 |
+
pipe = AutoPipelineForImage2Image.from_pretrained(model, torch_dtype=torch.float16)
|
77 |
+
|
78 |
+
if use_vae:
|
79 |
+
vae = AutoencoderKL.from_pretrained(vaecall, torch_dtype=torch.float16)
|
80 |
+
pipe = AutoPipelineForImage2Image.from_pretrained(model, vae=vae, torch_dtype=torch.float16)
|
81 |
|
82 |
+
if use_inpainting:
|
83 |
+
pipe = AutoPipelineForInpainting.from_pretrained(model, torch_dtype=torch.float16)
|
84 |
+
|
85 |
+
response = requests.get(url)
|
86 |
+
init_image = Image.open(BytesIO(response.content)).convert("RGB")
|
87 |
+
init_image = init_image.resize((width, height))
|
88 |
+
|
89 |
+
image_init = load_image(img_url)
|
90 |
+
mask_image = load_image(mask_url)
|
91 |
+
|
92 |
+
if use_lora:
|
93 |
+
pipe.load_lora_weights(lora)
|
94 |
+
pipe.fuse_lora(lora_scale)
|
95 |
+
|
96 |
+
if ENABLE_CPU_OFFLOAD:
|
97 |
+
pipe.enable_model_cpu_offload()
|
98 |
+
|
99 |
+
else:
|
100 |
+
pipe.to(device)
|
101 |
|
102 |
+
if USE_TORCH_COMPILE:
|
103 |
+
pipe.unet = torch.compile(pipe.unet, mode="reduce-overhead", fullgraph=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
104 |
|
105 |
+
generator = torch.Generator().manual_seed(seed)
|
106 |
+
|
107 |
+
if not use_negative_prompt:
|
108 |
+
negative_prompt = None # type: ignore
|
109 |
+
if not use_prompt_2:
|
110 |
+
prompt_2 = None # type: ignore
|
111 |
+
if not use_negative_prompt_2:
|
112 |
+
negative_prompt_2 = None # type: ignore
|
113 |
+
|
114 |
+
if use_inpainting:
|
115 |
+
image = pipe(
|
116 |
+
prompt=prompt,
|
117 |
+
image=image_init,
|
118 |
+
mask_image=mask_image,
|
119 |
+
strength=strength_img2img,
|
120 |
+
negative_prompt=negative_prompt,
|
121 |
+
prompt_2=prompt_2,
|
122 |
+
width=width,
|
123 |
+
height=height,
|
124 |
+
negative_prompt_2=negative_prompt_2,
|
125 |
+
guidance_scale=guidance_scale_base,
|
126 |
+
num_inference_steps=num_inference_steps_base,
|
127 |
+
generator=generator,
|
128 |
+
).images[0]
|
129 |
+
return image
|
130 |
+
elif use_img2img:
|
131 |
+
images = pipe(
|
132 |
+
prompt=prompt,
|
133 |
+
image=init_image,
|
134 |
+
strength=strength_img2img,
|
135 |
+
negative_prompt=negative_prompt,
|
136 |
+
prompt_2=prompt_2,
|
137 |
+
negative_prompt_2=negative_prompt_2,
|
138 |
+
width=width,
|
139 |
+
height=height,
|
140 |
+
guidance_scale=guidance_scale_base,
|
141 |
+
num_inference_steps=num_inference_steps_base,
|
142 |
+
generator=generator,
|
143 |
+
output_type="pil",
|
144 |
+
).images[0]
|
145 |
+
return images
|
146 |
+
else:
|
147 |
+
return pipe(
|
148 |
+
prompt=prompt,
|
149 |
+
negative_prompt=negative_prompt,
|
150 |
+
prompt_2=prompt_2,
|
151 |
+
negative_prompt_2=negative_prompt_2,
|
152 |
+
width=width,
|
153 |
+
height=height,
|
154 |
+
guidance_scale=guidance_scale_base,
|
155 |
+
num_inference_steps=num_inference_steps_base,
|
156 |
+
generator=generator,
|
157 |
+
output_type="pil",
|
158 |
+
).images[0]
|
159 |
+
|
160 |
+
with gr.Blocks(theme=gr.themes.Soft(), css="style.css") as demo:
|
161 |
gr.HTML(
|
162 |
+
"<p><center>📙 For any additional support, join our <a href='https://discord.gg/JprjXpjt9K'>Discord</a></center></p>"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
163 |
)
|
164 |
+
gr.Markdown(DESCRIPTION, elem_id="description")
|
165 |
with gr.Group():
|
166 |
+
model = gr.Text(label='Model', placeholder='e.g. stabilityai/stable-diffusion-xl-base-1.0')
|
167 |
+
vaecall = gr.Text(label='VAE', placeholder='e.g. madebyollin/sdxl-vae-fp16-fix')
|
168 |
+
lora = gr.Text(label='LoRA', placeholder='e.g. nerijs/pixel-art-xl')
|
169 |
+
lora_scale = gr.Slider(
|
170 |
+
info="The closer to 1, the more it will resemble LoRA, but errors may be visible.",
|
171 |
+
label="Lora Scale",
|
172 |
+
minimum=0.01,
|
173 |
+
maximum=1,
|
174 |
+
step=0.01,
|
175 |
+
value=0.7,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
176 |
)
|
177 |
+
url = gr.Text(label='URL (Img2Img)', placeholder='e.g https://example.com/image.png')
|
178 |
+
img_url = gr.Text(label='URL (Image Inpainting)', placeholder='e.g https://example.com/image.png')
|
179 |
+
mask_url = gr.Text(label='URL (Mask Image Inpainting)', placeholder='e.g https://example.com/image.png')
|
180 |
+
with gr.Row():
|
181 |
+
prompt = gr.Text(
|
182 |
+
placeholder="Input prompt",
|
183 |
+
label="Prompt",
|
184 |
+
show_label=False,
|
185 |
+
max_lines=1,
|
186 |
+
container=False,
|
187 |
)
|
188 |
+
run_button = gr.Button("Run", scale=0)
|
189 |
+
result = gr.Image(label="Result", show_label=False)
|
190 |
+
with gr.Accordion("Advanced options", open=False):
|
191 |
+
with gr.Row():
|
192 |
+
use_inpainting = gr.Checkbox(label='Use Inpainting', value=False, visible=ENABLE_USE_INPAINTING)
|
193 |
+
use_img2img = gr.Checkbox(label='Use Img2Img', value=False, visible=ENABLE_USE_IMG2IMG)
|
194 |
+
use_vae = gr.Checkbox(label='Use VAE', value=False, visible=ENABLE_USE_VAE)
|
195 |
+
use_lora = gr.Checkbox(label='Use Lora', value=False, visible=ENABLE_USE_LORA)
|
196 |
+
use_negative_prompt = gr.Checkbox(label="Use negative prompt", value=False)
|
197 |
+
use_prompt_2 = gr.Checkbox(label="Use prompt 2", value=False)
|
198 |
+
use_negative_prompt_2 = gr.Checkbox(label="Use negative prompt 2", value=False)
|
199 |
+
negative_prompt = gr.Text(
|
200 |
+
placeholder="Input Negative Prompt",
|
201 |
+
label="Negative prompt",
|
202 |
+
max_lines=1,
|
203 |
+
visible=False,
|
|
|
|
|
|
|
|
|
|
|
|
|
204 |
)
|
205 |
+
prompt_2 = gr.Text(
|
206 |
+
placeholder="Input Prompt 2",
|
207 |
+
label="Prompt 2",
|
208 |
+
max_lines=1,
|
209 |
+
visible=False,
|
|
|
|
|
|
|
210 |
)
|
211 |
+
negative_prompt_2 = gr.Text(
|
212 |
+
placeholder="Input Negative Prompt 2",
|
213 |
+
label="Negative prompt 2",
|
214 |
+
max_lines=1,
|
215 |
+
visible=False,
|
216 |
+
)
|
217 |
+
|
218 |
+
seed = gr.Slider(
|
219 |
+
label="Seed",
|
220 |
+
minimum=0,
|
221 |
+
maximum=MAX_SEED,
|
222 |
+
step=1,
|
223 |
+
value=0,
|
224 |
+
)
|
225 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
226 |
+
with gr.Row():
|
227 |
+
width = gr.Slider(
|
228 |
+
label="Width",
|
229 |
+
minimum=256,
|
230 |
+
maximum=MAX_IMAGE_SIZE,
|
231 |
+
step=32,
|
232 |
+
value=1024,
|
233 |
+
)
|
234 |
+
height = gr.Slider(
|
235 |
+
label="Height",
|
236 |
+
minimum=256,
|
237 |
+
maximum=MAX_IMAGE_SIZE,
|
238 |
+
step=32,
|
239 |
+
value=1024,
|
240 |
+
)
|
241 |
+
|
242 |
+
with gr.Row():
|
243 |
+
guidance_scale_base = gr.Slider(
|
244 |
+
info="Scale for classifier-free guidance",
|
245 |
+
label="Guidance scale",
|
246 |
+
minimum=1,
|
247 |
+
maximum=20,
|
248 |
+
step=0.1,
|
249 |
+
value=5.0,
|
250 |
+
)
|
251 |
+
with gr.Row():
|
252 |
+
num_inference_steps_base = gr.Slider(
|
253 |
+
info="Number of denoising steps",
|
254 |
+
label="Number of inference steps",
|
255 |
+
minimum=10,
|
256 |
+
maximum=100,
|
257 |
+
step=1,
|
258 |
+
value=25,
|
259 |
)
|
260 |
+
with gr.Row():
|
261 |
+
strength_img2img = gr.Slider(
|
262 |
+
info="Strength for Img2Img",
|
263 |
+
label="Strength",
|
264 |
+
minimum=0,
|
265 |
+
maximum=1,
|
266 |
+
step=0.01,
|
267 |
+
value=0.7,
|
268 |
+
)
|
269 |
+
|
270 |
+
use_negative_prompt.change(
|
271 |
+
fn=lambda x: gr.update(visible=x),
|
272 |
+
inputs=use_negative_prompt,
|
273 |
+
outputs=negative_prompt,
|
274 |
+
queue=False,
|
275 |
+
api_name=False,
|
276 |
+
)
|
277 |
+
use_prompt_2.change(
|
278 |
+
fn=lambda x: gr.update(visible=x),
|
279 |
+
inputs=use_prompt_2,
|
280 |
+
outputs=prompt_2,
|
281 |
+
queue=False,
|
282 |
+
api_name=False,
|
283 |
+
)
|
284 |
+
use_negative_prompt_2.change(
|
285 |
+
fn=lambda x: gr.update(visible=x),
|
286 |
+
inputs=use_negative_prompt_2,
|
287 |
+
outputs=negative_prompt_2,
|
288 |
+
queue=False,
|
289 |
+
api_name=False,
|
290 |
+
)
|
291 |
+
use_vae.change(
|
292 |
+
fn=lambda x: gr.update(visible=x),
|
293 |
+
inputs=use_vae,
|
294 |
+
outputs=vaecall,
|
295 |
+
queue=False,
|
296 |
+
api_name=False,
|
297 |
+
)
|
298 |
+
use_lora.change(
|
299 |
+
fn=lambda x: gr.update(visible=x),
|
300 |
+
inputs=use_lora,
|
301 |
+
outputs=lora,
|
302 |
+
queue=False,
|
303 |
+
api_name=False,
|
304 |
+
)
|
305 |
+
use_img2img.change(
|
306 |
+
fn=lambda x: gr.update(visible=x),
|
307 |
+
inputs=use_img2img,
|
308 |
+
outputs=url,
|
309 |
+
queue=False,
|
310 |
+
api_name=False,
|
311 |
+
)
|
312 |
+
use_inpainting.change(
|
313 |
+
fn=lambda x: gr.update(visible=x),
|
314 |
+
inputs=use_inpainting,
|
315 |
+
outputs=img_url,
|
316 |
+
queue=False,
|
317 |
+
api_name=False,
|
318 |
+
)
|
319 |
+
use_inpainting.change(
|
320 |
+
fn=lambda x: gr.update(visible=x),
|
321 |
+
inputs=use_inpainting,
|
322 |
+
outputs=mask_url,
|
323 |
+
queue=False,
|
324 |
+
api_name=False,
|
325 |
+
)
|
326 |
+
|
327 |
+
gr.on(
|
328 |
+
triggers=[
|
329 |
+
prompt.submit,
|
330 |
+
negative_prompt.submit,
|
331 |
+
prompt_2.submit,
|
332 |
+
negative_prompt_2.submit,
|
333 |
+
run_button.click,
|
334 |
+
],
|
335 |
+
fn=randomize_seed_fn,
|
336 |
+
inputs=[seed, randomize_seed],
|
337 |
+
outputs=seed,
|
338 |
+
queue=False,
|
339 |
+
api_name=False,
|
340 |
+
).then(
|
341 |
+
fn=generate,
|
342 |
+
inputs=[
|
343 |
+
prompt,
|
344 |
+
negative_prompt,
|
345 |
+
prompt_2,
|
346 |
+
negative_prompt_2,
|
347 |
+
use_negative_prompt,
|
348 |
+
use_prompt_2,
|
349 |
+
use_negative_prompt_2,
|
350 |
+
seed,
|
351 |
+
width,
|
352 |
+
height,
|
353 |
+
guidance_scale_base,
|
354 |
+
num_inference_steps_base,
|
355 |
+
strength_img2img,
|
356 |
+
use_vae,
|
357 |
+
use_lora,
|
358 |
+
model,
|
359 |
+
vaecall,
|
360 |
+
lora,
|
361 |
+
lora_scale,
|
362 |
+
use_img2img,
|
363 |
+
use_inpainting,
|
364 |
+
url,
|
365 |
+
img_url,
|
366 |
+
mask_url,
|
367 |
+
],
|
368 |
+
outputs=result,
|
369 |
+
api_name="run",
|
370 |
+
)
|
371 |
|
372 |
+
if __name__ == "__main__":
|
373 |
+
demo.queue(max_size=20).launch()
|