Spaces:
Running
Running
prithivMLmods
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,5 @@
|
|
|
|
|
|
1 |
import os
|
2 |
import random
|
3 |
import uuid
|
@@ -8,34 +10,127 @@ import numpy as np
|
|
8 |
from PIL import Image
|
9 |
import spaces
|
10 |
import torch
|
11 |
-
from diffusers import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
-
|
14 |
-
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
USE_TORCH_COMPILE = os.getenv("USE_TORCH_COMPILE", "0") == "1"
|
17 |
ENABLE_CPU_OFFLOAD = os.getenv("ENABLE_CPU_OFFLOAD", "0") == "1"
|
18 |
-
BATCH_SIZE = int(os.getenv("BATCH_SIZE", "1")) # Allow generating multiple images at once
|
19 |
|
20 |
-
# Determine device and load model outside of function for efficiency
|
21 |
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
22 |
-
pipe = StableDiffusionXLPipeline.from_pretrained(
|
23 |
-
MODEL_ID,
|
24 |
-
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
25 |
-
use_safetensors=True,
|
26 |
-
add_watermarker=False,
|
27 |
-
).to(device)
|
28 |
-
pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
|
29 |
-
|
30 |
-
# Torch compile for potential speedup (experimental)
|
31 |
-
if USE_TORCH_COMPILE:
|
32 |
-
pipe.compile()
|
33 |
-
|
34 |
-
# CPU offloading for larger RAM capacity (experimental)
|
35 |
-
if ENABLE_CPU_OFFLOAD:
|
36 |
-
pipe.enable_model_cpu_offload()
|
37 |
|
38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
def save_image(img):
|
41 |
unique_name = str(uuid.uuid4()) + ".png"
|
@@ -47,553 +142,180 @@ def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
|
47 |
seed = random.randint(0, MAX_SEED)
|
48 |
return seed
|
49 |
|
50 |
-
@spaces.GPU(
|
51 |
def generate(
|
52 |
prompt: str,
|
53 |
negative_prompt: str = "",
|
54 |
use_negative_prompt: bool = False,
|
55 |
-
|
|
|
56 |
width: int = 1024,
|
57 |
height: int = 1024,
|
58 |
guidance_scale: float = 3,
|
59 |
-
num_inference_steps: int = 30,
|
60 |
randomize_seed: bool = False,
|
61 |
-
use_resolution_binning: bool = True,
|
62 |
-
num_images: int = 1, # Number of images to generate
|
63 |
progress=gr.Progress(track_tqdm=True),
|
64 |
):
|
|
|
|
|
|
|
|
|
65 |
seed = int(randomize_seed_fn(seed, randomize_seed))
|
66 |
-
generator = torch.Generator(
|
|
|
|
|
|
|
|
|
67 |
|
68 |
-
# Improved options handling
|
69 |
options = {
|
70 |
-
"prompt":
|
71 |
-
"negative_prompt":
|
72 |
"width": width,
|
73 |
"height": height,
|
74 |
"guidance_scale": guidance_scale,
|
75 |
-
"num_inference_steps":
|
76 |
"generator": generator,
|
|
|
|
|
77 |
"output_type": "pil",
|
78 |
}
|
79 |
-
|
80 |
-
|
81 |
-
if use_resolution_binning:
|
82 |
-
options["use_resolution_binning"] = True
|
83 |
-
|
84 |
-
# Generate images potentially in batches
|
85 |
-
images = []
|
86 |
-
for i in range(0, num_images, BATCH_SIZE):
|
87 |
-
batch_options = options.copy()
|
88 |
-
batch_options["prompt"] = options["prompt"][i:i+BATCH_SIZE]
|
89 |
-
if "negative_prompt" in batch_options:
|
90 |
-
batch_options["negative_prompt"] = options["negative_prompt"][i:i+BATCH_SIZE]
|
91 |
-
images.extend(pipe(**batch_options).images)
|
92 |
|
93 |
image_paths = [save_image(img) for img in images]
|
94 |
return image_paths, seed
|
95 |
|
96 |
examples = [
|
97 |
-
"a cat
|
98 |
-
"
|
99 |
-
"
|
100 |
-
"
|
101 |
-
"An alien holding a sign board containing the word 'Flash', futuristic, neonpunk",
|
102 |
-
"Kids going to school, Anime style"
|
103 |
]
|
104 |
|
105 |
css = '''
|
106 |
.gradio-container{max-width: 700px !important}
|
107 |
h1{text-align:center}
|
108 |
-
footer {
|
109 |
-
visibility: hidden
|
110 |
-
}
|
111 |
-
.wheel-and-hamster {
|
112 |
-
--dur: 1s;
|
113 |
-
position: relative;
|
114 |
-
width: 12em;
|
115 |
-
height: 12em;
|
116 |
-
font-size: 14px;
|
117 |
-
}
|
118 |
-
.wheel,
|
119 |
-
.hamster,
|
120 |
-
.hamster div,
|
121 |
-
.spoke {
|
122 |
-
position: absolute;
|
123 |
-
}
|
124 |
-
.wheel,
|
125 |
-
.spoke {
|
126 |
-
border-radius: 50%;
|
127 |
-
top: 0;
|
128 |
-
left: 0;
|
129 |
-
width: 100%;
|
130 |
-
height: 100%;
|
131 |
-
}
|
132 |
-
.wheel {
|
133 |
-
background: radial-gradient(100% 100% at center,hsla(0,0%,60%,0) 47.8%,hsl(0,0%,60%) 48%);
|
134 |
-
z-index: 2;
|
135 |
-
}
|
136 |
-
.hamster {
|
137 |
-
animation: hamster var(--dur) ease-in-out infinite;
|
138 |
-
top: 50%;
|
139 |
-
left: calc(50% - 3.5em);
|
140 |
-
width: 7em;
|
141 |
-
height: 3.75em;
|
142 |
-
transform: rotate(4deg) translate(-0.8em,1.85em);
|
143 |
-
transform-origin: 50% 0;
|
144 |
-
z-index: 1;
|
145 |
-
}
|
146 |
-
.hamster__head {
|
147 |
-
animation: hamsterHead var(--dur) ease-in-out infinite;
|
148 |
-
background: hsl(30,90%,55%);
|
149 |
-
border-radius: 70% 30% 0 100% / 40% 25% 25% 60%;
|
150 |
-
box-shadow: 0 -0.25em 0 hsl(30,90%,80%) inset,
|
151 |
-
0.75em -1.55em 0 hsl(30,90%,90%) inset;
|
152 |
-
top: 0;
|
153 |
-
left: -2em;
|
154 |
-
width: 2.75em;
|
155 |
-
height: 2.5em;
|
156 |
-
transform-origin: 100% 50%;
|
157 |
-
}
|
158 |
-
.hamster__ear {
|
159 |
-
animation: hamsterEar var(--dur) ease-in-out infinite;
|
160 |
-
background: hsl(0,90%,85%);
|
161 |
-
border-radius: 50%;
|
162 |
-
box-shadow: -0.25em 0 hsl(30,90%,55%) inset;
|
163 |
-
top: -0.25em;
|
164 |
-
right: -0.25em;
|
165 |
-
width: 0.75em;
|
166 |
-
height: 0.75em;
|
167 |
-
transform-origin: 50% 75%;
|
168 |
-
}
|
169 |
-
.hamster__eye {
|
170 |
-
animation: hamsterEye var(--dur) linear infinite;
|
171 |
-
background-color: hsl(0,0%,0%);
|
172 |
-
border-radius: 50%;
|
173 |
-
top: 0.375em;
|
174 |
-
left: 1.25em;
|
175 |
-
width: 0.5em;
|
176 |
-
height: 0.5em;
|
177 |
-
}
|
178 |
-
.hamster__nose {
|
179 |
-
background: hsl(0,90%,75%);
|
180 |
-
border-radius: 35% 65% 85% 15% / 70% 50% 50% 30%;
|
181 |
-
top: 0.75em;
|
182 |
-
left: 0;
|
183 |
-
width: 0.2em;
|
184 |
-
height: 0.25em;
|
185 |
-
}
|
186 |
-
.hamster__body {
|
187 |
-
animation: hamsterBody var(--dur) ease-in-out infinite;
|
188 |
-
background: hsl(30,90%,90%);
|
189 |
-
border-radius: 50% 30% 50% 30% / 15% 60% 40% 40%;
|
190 |
-
box-shadow: 0.1em 0.75em 0 hsl(30,90%,55%) inset,
|
191 |
-
0.15em -0.5em 0 hsl(30,90%,80%) inset;
|
192 |
-
top: 0.25em;
|
193 |
-
left: 2em;
|
194 |
-
width: 4.5em;
|
195 |
-
height: 3em;
|
196 |
-
transform-origin: 17% 50%;
|
197 |
-
transform-style: preserve-3d;
|
198 |
-
}
|
199 |
-
.hamster__limb--fr,
|
200 |
-
.hamster__limb--fl {
|
201 |
-
clip-path: polygon(0 0,100% 0,70% 80%,60% 100%,0% 100%,40% 80%);
|
202 |
-
top: 2em;
|
203 |
-
left: 0.5em;
|
204 |
-
width: 1em;
|
205 |
-
height: 1.5em;
|
206 |
-
transform-origin: 50% 0;
|
207 |
-
}
|
208 |
-
.hamster__limb--fr {
|
209 |
-
animation: hamsterFRLimb var(--dur) linear infinite;
|
210 |
-
background: linear-gradient(hsl(30,90%,80%) 80%,hsl(0,90%,75%) 80%);
|
211 |
-
transform: rotate(15deg) translateZ(-1px);
|
212 |
-
}
|
213 |
-
.hamster__limb--fl {
|
214 |
-
animation: hamsterFLLimb var(--dur) linear infinite;
|
215 |
-
background: linear-gradient(hsl(30,90%,80%) 80%,hsl(0,90%,75%) 80%);
|
216 |
-
transform: rotate(-60deg) translateZ(-1px);
|
217 |
-
}
|
218 |
-
.hamster__limb--br,
|
219 |
-
.hamster__limb--bl {
|
220 |
-
clip-path: polygon(0 0,100% 0,100% 30%,70% 80%,60% 100%,40% 100%,30% 80%);
|
221 |
-
top: 2.3em;
|
222 |
-
left: 2.8em;
|
223 |
-
width: 1.25em;
|
224 |
-
height: 2.5em;
|
225 |
-
transform-origin: 50% 10%;
|
226 |
-
}
|
227 |
-
.hamster__limb--br {
|
228 |
-
animation: hamsterBRLimb var(--dur) linear infinite;
|
229 |
-
background: linear-gradient(hsl(0,90%,75%) 40%,hsl(30,90%,80%) 40%);
|
230 |
-
transform: rotate(45deg) translateZ(-1px);
|
231 |
-
}
|
232 |
-
.hamster__limb--bl {
|
233 |
-
animation: hamsterBLLimb var(--dur) linear infinite;
|
234 |
-
background: linear-gradient(hsl(0,90%,75%) 40%,hsl(30,90%,80%) 40%);
|
235 |
-
transform: rotate(-30deg) translateZ(-1px);
|
236 |
-
}
|
237 |
-
.hamster__tail {
|
238 |
-
animation: hamsterTail var(--dur) linear infinite;
|
239 |
-
background: hsl(0,90%,85%);
|
240 |
-
border-radius: 0.25em 50% 50% 0.25em;
|
241 |
-
box-shadow: 0.1em 0.5em 0 hsl(30,90%,55%) inset,
|
242 |
-
0.1em -0.25em 0 hsl(30,90%,90%) inset;
|
243 |
-
top: 3em;
|
244 |
-
left: 6em;
|
245 |
-
width: 0.75em;
|
246 |
-
height: 0.75em;
|
247 |
-
transform: rotate(30deg) translateZ(-1px);
|
248 |
-
}
|
249 |
-
.spoke {
|
250 |
-
--s: 0.2;
|
251 |
-
background: hsl(0,0%,100%);
|
252 |
-
box-shadow: 0 0 0 0.2em hsl(0,0%,0%);
|
253 |
-
left: calc(50% - var(--s)/2);
|
254 |
-
width: var(--s);
|
255 |
-
height: var(--s);
|
256 |
-
}
|
257 |
-
.spoke:nth-child(1) {
|
258 |
-
--rotation: 15deg;
|
259 |
-
transform: rotate(var(--rotation));
|
260 |
-
}
|
261 |
-
.spoke:nth-child(2) {
|
262 |
-
--rotation: 45deg;
|
263 |
-
transform: rotate(var(--rotation));
|
264 |
-
}
|
265 |
-
.spoke:nth-child(3) {
|
266 |
-
--rotation: 75deg;
|
267 |
-
transform: rotate(var(--rotation));
|
268 |
-
}
|
269 |
-
.spoke:nth-child(4) {
|
270 |
-
--rotation: 105deg;
|
271 |
-
transform: rotate(var(--rotation));
|
272 |
-
}
|
273 |
-
.spoke:nth-child(5) {
|
274 |
-
--rotation: 135deg;
|
275 |
-
transform: rotate(var(--rotation));
|
276 |
-
}
|
277 |
-
.spoke:nth-child(6) {
|
278 |
-
--rotation: 165deg;
|
279 |
-
transform: rotate(var(--rotation));
|
280 |
-
}
|
281 |
-
.spoke:nth-child(7) {
|
282 |
-
--rotation: 195deg;
|
283 |
-
transform: rotate(var(--rotation));
|
284 |
-
}
|
285 |
-
.spoke:nth-child(8) {
|
286 |
-
--rotation: 225deg;
|
287 |
-
transform: rotate(var(--rotation));
|
288 |
-
}
|
289 |
-
.spoke:nth-child(9) {
|
290 |
-
--rotation: 255deg;
|
291 |
-
transform: rotate(var(--rotation));
|
292 |
-
}
|
293 |
-
.spoke:nth-child(10) {
|
294 |
-
--rotation: 285deg;
|
295 |
-
transform: rotate(var(--rotation));
|
296 |
-
}
|
297 |
-
.spoke:nth-child(11) {
|
298 |
-
--rotation: 315deg;
|
299 |
-
transform: rotate(var(--rotation));
|
300 |
-
}
|
301 |
-
.spoke:nth-child(12) {
|
302 |
-
--rotation: 345deg;
|
303 |
-
transform: rotate(var(--rotation));
|
304 |
-
}
|
305 |
-
|
306 |
-
@keyframes hamster {
|
307 |
-
50% {
|
308 |
-
transform: rotate(-4deg) translate(-0.8em,1.85em);
|
309 |
-
}
|
310 |
-
}
|
311 |
-
|
312 |
-
@keyframes hamsterHead {
|
313 |
-
50% {
|
314 |
-
transform: rotate(-8deg);
|
315 |
-
}
|
316 |
-
}
|
317 |
-
|
318 |
-
@keyframes hamsterEye {
|
319 |
-
50% {
|
320 |
-
transform: translateY(0.1em);
|
321 |
-
}
|
322 |
-
}
|
323 |
-
|
324 |
-
@keyframes hamsterEar {
|
325 |
-
50% {
|
326 |
-
transform: rotate(8deg);
|
327 |
-
}
|
328 |
-
}
|
329 |
-
|
330 |
-
@keyframes hamsterBody {
|
331 |
-
50% {
|
332 |
-
transform: rotate(2deg);
|
333 |
-
}
|
334 |
-
}
|
335 |
-
|
336 |
-
@keyframes hamsterFRLimb {
|
337 |
-
8%,70% {
|
338 |
-
transform: rotate(15deg) translateZ(-1px);
|
339 |
-
}
|
340 |
-
33% {
|
341 |
-
transform: rotate(-60deg) translateZ(-1px);
|
342 |
-
}
|
343 |
-
83% {
|
344 |
-
transform: rotate(45deg) translateZ(-1px);
|
345 |
-
}
|
346 |
-
}
|
347 |
-
|
348 |
-
@keyframes hamsterFLLimb {
|
349 |
-
8%,70% {
|
350 |
-
transform: rotate(-60deg) translateZ(-1px);
|
351 |
-
}
|
352 |
-
33% {
|
353 |
-
transform: rotate(15deg) translateZ(-1px);
|
354 |
-
}
|
355 |
-
83% {
|
356 |
-
transform: rotate(-45deg) translateZ(-1px);
|
357 |
-
}
|
358 |
-
}
|
359 |
-
|
360 |
-
@keyframes hamsterBRLimb {
|
361 |
-
0%,50% {
|
362 |
-
transform: rotate(45deg) translateZ(-1px);
|
363 |
-
}
|
364 |
-
25% {
|
365 |
-
transform: rotate(-30deg) translateZ(-1px);
|
366 |
-
}
|
367 |
-
75% {
|
368 |
-
transform: rotate(60deg) translateZ(-1px);
|
369 |
-
}
|
370 |
-
}
|
371 |
-
|
372 |
-
@keyframes hamsterBLLimb {
|
373 |
-
0%,50% {
|
374 |
-
transform: rotate(-30deg) translateZ(-1px);
|
375 |
-
}
|
376 |
-
25% {
|
377 |
-
transform: rotate(45deg) translateZ(-1px);
|
378 |
-
}
|
379 |
-
75% {
|
380 |
-
transform: rotate(-45deg) translateZ(-1px);
|
381 |
-
}
|
382 |
-
}
|
383 |
-
|
384 |
-
@keyframes hamsterTail {
|
385 |
-
50% {
|
386 |
-
transform: rotate(-30deg) translateZ(-1px);
|
387 |
-
}
|
388 |
-
}
|
389 |
-
|
390 |
-
#wrapper {
|
391 |
-
position: relative;
|
392 |
-
height: 100vh;
|
393 |
-
display: flex;
|
394 |
-
justify-content: center;
|
395 |
-
align-items: center;
|
396 |
-
}
|
397 |
-
.loading-container {
|
398 |
-
position: relative;
|
399 |
-
display: flex;
|
400 |
-
justify-content: center;
|
401 |
-
align-items: center;
|
402 |
-
width: 100%;
|
403 |
-
height: 100%;
|
404 |
-
top: 50%;
|
405 |
-
transform: translateY(-50%);
|
406 |
-
}
|
407 |
-
.hidden {
|
408 |
-
display: none;
|
409 |
-
}
|
410 |
'''
|
411 |
-
|
412 |
-
|
413 |
-
|
414 |
-
|
415 |
-
|
416 |
-
|
417 |
-
|
418 |
-
|
419 |
-
|
420 |
-
|
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 |
-
|
448 |
-
|
449 |
-
|
450 |
-
|
451 |
-
|
452 |
-
|
453 |
-
|
454 |
-
|
455 |
-
|
456 |
-
|
457 |
-
|
458 |
-
|
459 |
-
|
460 |
-
|
461 |
-
}
|
462 |
-
});
|
463 |
-
|
464 |
-
// Start observing the body for changes
|
465 |
-
observer.observe(document.body, { childList: true, subtree: true });
|
466 |
-
});
|
467 |
-
</script>
|
468 |
-
'''
|
469 |
-
|
470 |
-
block = gr.Blocks(css=css)
|
471 |
-
|
472 |
-
with block as demo:
|
473 |
-
gr.HTML(html)
|
474 |
-
with gr.Column(elem_id="main-app"):
|
475 |
-
gr.Markdown(
|
476 |
-
"""
|
477 |
-
# Generate images with Stable Diffusion XL
|
478 |
-
"""
|
479 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
480 |
with gr.Row():
|
481 |
-
|
482 |
-
|
483 |
-
|
484 |
-
|
485 |
-
|
486 |
-
|
487 |
-
|
488 |
-
|
489 |
-
|
490 |
-
|
491 |
-
|
492 |
-
|
493 |
-
|
494 |
-
|
495 |
-
|
496 |
-
seed = gr.Number(
|
497 |
-
label="Seed",
|
498 |
-
value=1,
|
499 |
-
precision=0
|
500 |
-
)
|
501 |
-
randomize_seed = gr.Checkbox(
|
502 |
-
label="Randomize Seed",
|
503 |
-
value=False
|
504 |
-
)
|
505 |
-
width = gr.Slider(
|
506 |
-
label="Width",
|
507 |
-
value=1024,
|
508 |
-
minimum=64,
|
509 |
-
maximum=MAX_IMAGE_SIZE,
|
510 |
-
step=8
|
511 |
-
)
|
512 |
-
height = gr.Slider(
|
513 |
-
label="Height",
|
514 |
-
value=1024,
|
515 |
-
minimum=64,
|
516 |
-
maximum=MAX_IMAGE_SIZE,
|
517 |
-
step=8
|
518 |
-
)
|
519 |
-
guidance_scale = gr.Slider(
|
520 |
-
label="Guidance Scale",
|
521 |
-
value=3,
|
522 |
-
minimum=0,
|
523 |
-
maximum=50,
|
524 |
-
step=0.5
|
525 |
-
)
|
526 |
-
num_inference_steps = gr.Slider(
|
527 |
-
label="Number of Inference Steps",
|
528 |
-
value=30,
|
529 |
-
minimum=1,
|
530 |
-
maximum=100,
|
531 |
-
step=1
|
532 |
-
)
|
533 |
-
use_resolution_binning = gr.Checkbox(
|
534 |
-
label="Use Resolution Binning",
|
535 |
-
value=True
|
536 |
-
)
|
537 |
-
num_images = gr.Number(
|
538 |
-
label="Number of Images to Generate",
|
539 |
-
value=1,
|
540 |
-
minimum=1,
|
541 |
-
maximum=10,
|
542 |
-
step=1
|
543 |
-
)
|
544 |
-
generate_button = gr.Button("Generate Images")
|
545 |
-
with gr.Column():
|
546 |
-
gr.Label("Examples:")
|
547 |
-
for example in examples:
|
548 |
-
gr.Label(f"- {example}")
|
549 |
-
|
550 |
-
generated_images = gr.Image(
|
551 |
-
label="Generated Images",
|
552 |
-
type="pil" # Display the PIL image
|
553 |
)
|
554 |
-
|
555 |
-
|
556 |
-
|
557 |
-
|
558 |
-
|
559 |
-
|
560 |
-
|
561 |
-
|
562 |
-
|
563 |
-
|
564 |
-
|
565 |
-
|
566 |
-
|
567 |
-
|
568 |
-
|
569 |
-
|
570 |
-
|
571 |
-
|
572 |
-
|
573 |
-
|
574 |
-
|
575 |
-
|
576 |
-
|
577 |
-
|
578 |
-
|
579 |
-
|
580 |
-
|
581 |
-
|
582 |
-
|
583 |
-
|
584 |
-
|
585 |
-
|
586 |
-
|
587 |
-
|
588 |
-
|
589 |
-
|
590 |
-
|
591 |
-
|
592 |
-
|
593 |
-
fn=on_generate_click,
|
594 |
-
live=True,
|
595 |
-
title="Diffusion Generator",
|
596 |
-
description="Generate images using Stable Diffusion XL.",
|
597 |
-
layout="vertical",
|
598 |
-
blocking=True
|
599 |
-
).launch()
|
|
|
1 |
+
#!/usr/bin/env python
|
2 |
+
|
3 |
import os
|
4 |
import random
|
5 |
import uuid
|
|
|
10 |
from PIL import Image
|
11 |
import spaces
|
12 |
import torch
|
13 |
+
from diffusers import DiffusionPipeline
|
14 |
+
from typing import Tuple
|
15 |
+
|
16 |
+
#Check for the Model Base..//
|
17 |
+
|
18 |
+
|
19 |
+
|
20 |
+
bad_words = json.loads(os.getenv('BAD_WORDS', "[]"))
|
21 |
+
bad_words_negative = json.loads(os.getenv('BAD_WORDS_NEGATIVE', "[]"))
|
22 |
+
default_negative = os.getenv("default_negative","")
|
23 |
+
|
24 |
+
def check_text(prompt, negative=""):
|
25 |
+
for i in bad_words:
|
26 |
+
if i in prompt:
|
27 |
+
return True
|
28 |
+
for i in bad_words_negative:
|
29 |
+
if i in negative:
|
30 |
+
return True
|
31 |
+
return False
|
32 |
+
|
33 |
+
|
34 |
+
|
35 |
+
style_list = [
|
36 |
+
|
37 |
+
{
|
38 |
+
"name": "2560 x 1440",
|
39 |
+
"prompt": "hyper-realistic 4K image of {prompt}. ultra-detailed, lifelike, high-resolution, sharp, vibrant colors, photorealistic",
|
40 |
+
"negative_prompt": "cartoonish, low resolution, blurry, simplistic, abstract, deformed, ugly",
|
41 |
+
},
|
42 |
+
|
43 |
+
{
|
44 |
+
"name": "Photo",
|
45 |
+
"prompt": "cinematic photo {prompt}. 35mm photograph, film, bokeh, professional, 4k, highly detailed",
|
46 |
+
"negative_prompt": "drawing, painting, crayon, sketch, graphite, impressionist, noisy, blurry, soft, deformed, ugly",
|
47 |
+
},
|
48 |
+
|
49 |
+
{
|
50 |
+
"name": "Cinematic",
|
51 |
+
"prompt": "cinematic still {prompt}. emotional, harmonious, vignette, highly detailed, high budget, bokeh, cinemascope, moody, epic, gorgeous, film grain, grainy",
|
52 |
+
"negative_prompt": "anime, cartoon, graphic, text, painting, crayon, graphite, abstract, glitch, deformed, mutated, ugly, disfigured",
|
53 |
+
},
|
54 |
+
|
55 |
+
{
|
56 |
+
"name": "Anime",
|
57 |
+
"prompt": "anime artwork {prompt}. anime style, key visual, vibrant, studio anime, highly detailed",
|
58 |
+
"negative_prompt": "photo, deformed, black and white, realism, disfigured, low contrast",
|
59 |
+
},
|
60 |
+
{
|
61 |
+
"name": "3D Model",
|
62 |
+
"prompt": "professional 3d model {prompt}. octane render, highly detailed, volumetric, dramatic lighting",
|
63 |
+
"negative_prompt": "ugly, deformed, noisy, low poly, blurry, painting",
|
64 |
+
},
|
65 |
+
{
|
66 |
+
"name": "(No style)",
|
67 |
+
"prompt": "{prompt}",
|
68 |
+
"negative_prompt": "",
|
69 |
+
},
|
70 |
+
]
|
71 |
+
|
72 |
+
styles = {k["name"]: (k["prompt"], k["negative_prompt"]) for k in style_list}
|
73 |
+
STYLE_NAMES = list(styles.keys())
|
74 |
+
DEFAULT_STYLE_NAME = "2560 x 1440"
|
75 |
+
|
76 |
+
def apply_style(style_name: str, positive: str, negative: str = "") -> Tuple[str, str]:
|
77 |
+
p, n = styles.get(style_name, styles[DEFAULT_STYLE_NAME])
|
78 |
+
if not negative:
|
79 |
+
negative = ""
|
80 |
+
return p.replace("{prompt}", positive), n + negative
|
81 |
+
|
82 |
+
|
83 |
+
|
84 |
+
|
85 |
|
86 |
+
DESCRIPTION = """## MidJourney
|
87 |
+
Drop your best results in the community: [rb.gy/klkbs7](http://rb.gy/klkbs7)
|
88 |
+
"""
|
89 |
+
|
90 |
+
|
91 |
+
|
92 |
+
|
93 |
+
|
94 |
+
if not torch.cuda.is_available():
|
95 |
+
DESCRIPTION += "\n<p>⚠️Running on CPU, This may not work on CPU.</p>"
|
96 |
+
|
97 |
+
MAX_SEED = np.iinfo(np.int32).max
|
98 |
+
CACHE_EXAMPLES = torch.cuda.is_available() and os.getenv("CACHE_EXAMPLES", "0") == "1"
|
99 |
+
MAX_IMAGE_SIZE = int(os.getenv("MAX_IMAGE_SIZE", "2048"))
|
100 |
USE_TORCH_COMPILE = os.getenv("USE_TORCH_COMPILE", "0") == "1"
|
101 |
ENABLE_CPU_OFFLOAD = os.getenv("ENABLE_CPU_OFFLOAD", "0") == "1"
|
|
|
102 |
|
|
|
103 |
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
104 |
|
105 |
+
NUM_IMAGES_PER_PROMPT = 1
|
106 |
+
|
107 |
+
if torch.cuda.is_available():
|
108 |
+
pipe = DiffusionPipeline.from_pretrained(
|
109 |
+
"SG161222/RealVisXL_V3.0_Turbo",
|
110 |
+
torch_dtype=torch.float16,
|
111 |
+
use_safetensors=True,
|
112 |
+
add_watermarker=False,
|
113 |
+
variant="fp16"
|
114 |
+
)
|
115 |
+
pipe2 = DiffusionPipeline.from_pretrained(
|
116 |
+
"SG161222/RealVisXL_V2.02_Turbo",
|
117 |
+
torch_dtype=torch.float16,
|
118 |
+
use_safetensors=True,
|
119 |
+
add_watermarker=False,
|
120 |
+
variant="fp16"
|
121 |
+
)
|
122 |
+
if ENABLE_CPU_OFFLOAD:
|
123 |
+
pipe.enable_model_cpu_offload()
|
124 |
+
pipe2.enable_model_cpu_offload()
|
125 |
+
else:
|
126 |
+
pipe.to(device)
|
127 |
+
pipe2.to(device)
|
128 |
+
print("Loaded on Device!")
|
129 |
+
|
130 |
+
if USE_TORCH_COMPILE:
|
131 |
+
pipe.unet = torch.compile(pipe.unet, mode="reduce-overhead", fullgraph=True)
|
132 |
+
pipe2.unet = torch.compile(pipe2.unet, mode="reduce-overhead", fullgraph=True)
|
133 |
+
print("Model Compiled!")
|
134 |
|
135 |
def save_image(img):
|
136 |
unique_name = str(uuid.uuid4()) + ".png"
|
|
|
142 |
seed = random.randint(0, MAX_SEED)
|
143 |
return seed
|
144 |
|
145 |
+
@spaces.GPU(enable_queue=True)
|
146 |
def generate(
|
147 |
prompt: str,
|
148 |
negative_prompt: str = "",
|
149 |
use_negative_prompt: bool = False,
|
150 |
+
style: str = DEFAULT_STYLE_NAME,
|
151 |
+
seed: int = 0,
|
152 |
width: int = 1024,
|
153 |
height: int = 1024,
|
154 |
guidance_scale: float = 3,
|
|
|
155 |
randomize_seed: bool = False,
|
156 |
+
use_resolution_binning: bool = True,
|
|
|
157 |
progress=gr.Progress(track_tqdm=True),
|
158 |
):
|
159 |
+
if check_text(prompt, negative_prompt):
|
160 |
+
raise ValueError("Prompt contains restricted words.")
|
161 |
+
|
162 |
+
prompt, negative_prompt = apply_style(style, prompt, negative_prompt)
|
163 |
seed = int(randomize_seed_fn(seed, randomize_seed))
|
164 |
+
generator = torch.Generator().manual_seed(seed)
|
165 |
+
|
166 |
+
if not use_negative_prompt:
|
167 |
+
negative_prompt = "" # type: ignore
|
168 |
+
negative_prompt += default_negative
|
169 |
|
|
|
170 |
options = {
|
171 |
+
"prompt": prompt,
|
172 |
+
"negative_prompt": negative_prompt,
|
173 |
"width": width,
|
174 |
"height": height,
|
175 |
"guidance_scale": guidance_scale,
|
176 |
+
"num_inference_steps": 25,
|
177 |
"generator": generator,
|
178 |
+
"num_images_per_prompt": NUM_IMAGES_PER_PROMPT,
|
179 |
+
"use_resolution_binning": use_resolution_binning,
|
180 |
"output_type": "pil",
|
181 |
}
|
182 |
+
|
183 |
+
images = pipe(**options).images + pipe2(**options).images
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
184 |
|
185 |
image_paths = [save_image(img) for img in images]
|
186 |
return image_paths, seed
|
187 |
|
188 |
examples = [
|
189 |
+
"A closeup of a cat, a window, in a rustic cabin, close up, with a shallow depth of field, with a vintage film grain, in the style of Annie Leibovitz and in the style of Wes Anderson. --ar 85:128 --v 6.0 --style raw",
|
190 |
+
"Daria Morgendorffer the main character of the animated series Daria, serious expression, very excites sultry look, so hot girl, beautiful charismatic girl, so hot shot, a woman wearing eye glasses, gorgeous figure, interesting shapes, life-size figures",
|
191 |
+
"Dark green large leaves of anthurium, close up, photography, aerial view, in the style of unsplash, hasselblad h6d400c --ar 85:128 --v 6.0 --style raw",
|
192 |
+
"Closeup of blonde woman depth of field, bokeh, shallow focus, minimalism, fujifilm xh2s with Canon EF lens, cinematic --ar 85:128 --v 6.0 --style raw"
|
|
|
|
|
193 |
]
|
194 |
|
195 |
css = '''
|
196 |
.gradio-container{max-width: 700px !important}
|
197 |
h1{text-align:center}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
198 |
'''
|
199 |
+
with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
|
200 |
+
gr.Markdown(DESCRIPTION)
|
201 |
+
gr.DuplicateButton(
|
202 |
+
value="Duplicate Space for private use",
|
203 |
+
elem_id="duplicate-button",
|
204 |
+
visible=os.getenv("SHOW_DUPLICATE_BUTTON") == "1",
|
205 |
+
)
|
206 |
+
with gr.Group():
|
207 |
+
with gr.Row():
|
208 |
+
prompt = gr.Text(
|
209 |
+
label="Prompt",
|
210 |
+
show_label=False,
|
211 |
+
max_lines=1,
|
212 |
+
placeholder="Enter your prompt",
|
213 |
+
container=False,
|
214 |
+
)
|
215 |
+
run_button = gr.Button("Run")
|
216 |
+
result = gr.Gallery(label="Result", columns=1, preview=True)
|
217 |
+
with gr.Accordion("Advanced options", open=False):
|
218 |
+
use_negative_prompt = gr.Checkbox(label="Use negative prompt", value=True, visible=True)
|
219 |
+
negative_prompt = gr.Text(
|
220 |
+
label="Negative prompt",
|
221 |
+
max_lines=1,
|
222 |
+
placeholder="Enter a negative prompt",
|
223 |
+
value="(deformed iris, deformed pupils, semi-realistic, cgi, 3d, render, sketch, cartoon, drawing, anime:1.4), text, close up, cropped, out of frame, worst quality, low quality, jpeg artifacts, ugly, duplicate, morbid, mutilated, extra fingers, mutated hands, poorly drawn hands, poorly drawn face, mutation, deformed, blurry, dehydrated, bad anatomy, bad proportions, extra limbs, cloned face, disfigured, gross proportions, malformed limbs, missing arms, missing legs, extra arms, extra legs, fused fingers, too many fingers, long neck",
|
224 |
+
visible=True,
|
225 |
+
)
|
226 |
+
with gr.Row():
|
227 |
+
num_inference_steps = gr.Slider(
|
228 |
+
label="Steps",
|
229 |
+
minimum=10,
|
230 |
+
maximum=60,
|
231 |
+
step=1,
|
232 |
+
value=30,
|
233 |
+
)
|
234 |
+
with gr.Row():
|
235 |
+
num_images_per_prompt = gr.Slider(
|
236 |
+
label="Images",
|
237 |
+
minimum=1,
|
238 |
+
maximum=5,
|
239 |
+
step=1,
|
240 |
+
value=2,
|
241 |
+
)
|
242 |
+
seed = gr.Slider(
|
243 |
+
label="Seed",
|
244 |
+
minimum=0,
|
245 |
+
maximum=MAX_SEED,
|
246 |
+
step=1,
|
247 |
+
value=0,
|
248 |
+
visible=True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
249 |
)
|
250 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
251 |
+
with gr.Row(visible=True):
|
252 |
+
width = gr.Slider(
|
253 |
+
label="Width",
|
254 |
+
minimum=512,
|
255 |
+
maximum=2048,
|
256 |
+
step=8,
|
257 |
+
value=1024,
|
258 |
+
)
|
259 |
+
height = gr.Slider(
|
260 |
+
label="Height",
|
261 |
+
minimum=512,
|
262 |
+
maximum=2048,
|
263 |
+
step=8,
|
264 |
+
value=1024,
|
265 |
+
)
|
266 |
with gr.Row():
|
267 |
+
guidance_scale = gr.Slider(
|
268 |
+
label="Guidance Scale",
|
269 |
+
minimum=0.1,
|
270 |
+
maximum=20.0,
|
271 |
+
step=0.1,
|
272 |
+
value=6,
|
273 |
+
)
|
274 |
+
with gr.Row(visible=True):
|
275 |
+
style_selection = gr.Radio(
|
276 |
+
show_label=True,
|
277 |
+
container=True,
|
278 |
+
interactive=True,
|
279 |
+
choices=STYLE_NAMES,
|
280 |
+
value=DEFAULT_STYLE_NAME,
|
281 |
+
label="Image Style",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
282 |
)
|
283 |
+
gr.Examples(
|
284 |
+
examples=examples,
|
285 |
+
inputs=prompt,
|
286 |
+
outputs=[result, seed],
|
287 |
+
fn=generate,
|
288 |
+
cache_examples=CACHE_EXAMPLES,
|
289 |
+
)
|
290 |
+
|
291 |
+
use_negative_prompt.change(
|
292 |
+
fn=lambda x: gr.update(visible=x),
|
293 |
+
inputs=use_negative_prompt,
|
294 |
+
outputs=negative_prompt,
|
295 |
+
api_name=False,
|
296 |
+
)
|
297 |
+
|
298 |
+
gr.on(
|
299 |
+
triggers=[
|
300 |
+
prompt.submit,
|
301 |
+
negative_prompt.submit,
|
302 |
+
run_button.click,
|
303 |
+
],
|
304 |
+
fn=generate,
|
305 |
+
inputs=[
|
306 |
+
prompt,
|
307 |
+
negative_prompt,
|
308 |
+
use_negative_prompt,
|
309 |
+
style_selection,
|
310 |
+
seed,
|
311 |
+
width,
|
312 |
+
height,
|
313 |
+
guidance_scale,
|
314 |
+
randomize_seed,
|
315 |
+
],
|
316 |
+
outputs=[result, seed],
|
317 |
+
api_name="run",
|
318 |
+
)
|
319 |
+
|
320 |
+
if __name__ == "__main__":
|
321 |
+
demo.queue(max_size=20).launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|