Zyrenth commited on
Commit
bcdc3aa
·
verified ·
1 Parent(s): ef3ed31

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +580 -575
app.py CHANGED
@@ -5,597 +5,602 @@ import findfile
5
  import PIL.Image
6
  import autocuda
7
  from pyabsa.utils.pyabsa_utils import fprint
 
8
 
9
- try:
10
- for z_file in findfile.find_cwd_files(and_key=['.zip'],
11
- exclude_key=['.ignore', 'git', 'SuperResolutionAnimeDiffusion'],
12
- recursive=10):
13
- fprint(f"Extracting {z_file}...")
14
- with zipfile.ZipFile(z_file, 'r') as zip_ref:
15
- zip_ref.extractall(os.path.dirname(z_file))
16
- except Exception as e:
17
- os.system('unzip random_examples.zip')
18
-
19
- from diffusers import (
20
- AutoencoderKL,
21
- UNet2DConditionModel,
22
- StableDiffusionPipeline,
23
- StableDiffusionImg2ImgPipeline,
24
- DPMSolverMultistepScheduler,
25
- )
26
- import gradio as gr
27
- import torch
28
- from PIL import Image
29
- import utils
30
- import datetime
31
- import time
32
- import psutil
33
- from Waifu2x.magnify import ImageMagnifier
34
- from RealESRGANv030.interface import realEsrgan
35
-
36
- magnifier = ImageMagnifier()
37
-
38
- start_time = time.time()
39
- is_colab = utils.is_google_colab()
40
-
41
- CUDA_VISIBLE_DEVICES = ""
42
- device = autocuda.auto_cuda()
43
-
44
- dtype = torch.float16 if device != "cpu" else torch.float32
45
-
46
-
47
-
48
- class Model:
49
- def __init__(self, name, path="", prefix=""):
50
- self.name = name
51
- self.path = path
52
- self.prefix = prefix
53
- self.pipe_t2i = None
54
- self.pipe_i2i = None
55
-
56
-
57
- models = [
58
- # Model("anything v3", "Linaqruf/anything-v3.0", "anything v3 style"),
59
- Model("anything v5", "stablediffusionapi/anything-v5", "anything v5 style"),
60
- ]
61
- # Model("Spider-Verse", "nitrosocke/spider-verse-diffusion", "spiderverse style "),
62
- # Model("Balloon Art", "Fictiverse/Stable_Diffusion_BalloonArt_Model", "BalloonArt "),
63
- # Model("Elden Ring", "nitrosocke/elden-ring-diffusion", "elden ring style "),
64
- # Model("Tron Legacy", "dallinmackay/Tron-Legacy-diffusion", "trnlgcy ")
65
- # Model("Pokémon", "lambdalabs/sd-pokemon-diffusers", ""),
66
- # Model("Pony Diffusion", "AstraliteHeart/pony-diffusion", ""),
67
- # Model("Robo Diffusion", "nousr/robo-diffusion", ""),
68
-
69
- scheduler = DPMSolverMultistepScheduler(
70
- beta_start=0.00085,
71
- beta_end=0.012,
72
- beta_schedule="scaled_linear",
73
- num_train_timesteps=1000,
74
- trained_betas=None,
75
- predict_epsilon=True,
76
- thresholding=False,
77
- algorithm_type="dpmsolver++",
78
- solver_type="midpoint",
79
- solver_order=2,
80
- # lower_order_final=True,
81
- )
82
-
83
- custom_model = None
84
- if is_colab:
85
- models.insert(0, Model("Custom model"))
86
- custom_model = models[0]
87
-
88
- last_mode = "txt2img"
89
- current_model = models[1] if is_colab else models[0]
90
- current_model_path = current_model.path
91
-
92
- if is_colab:
93
- pipe = StableDiffusionPipeline.from_pretrained(
94
- current_model.path,
95
- torch_dtype=dtype,
96
- scheduler=scheduler,
97
- safety_checker=lambda images, clip_input: (images, False),
98
  )
99
-
100
- else: # download all models
101
- print(f"{datetime.datetime.now()} Downloading vae...")
102
- vae = AutoencoderKL.from_pretrained(
103
- current_model.path, subfolder="vae", torch_dtype=dtype
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
104
  )
105
- for model in models:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
106
  try:
107
- print(f"{datetime.datetime.now()} Downloading {model.name} model...")
108
- unet = UNet2DConditionModel.from_pretrained(
109
- model.path, subfolder="unet", torch_dtype=dtype
110
- )
111
- model.pipe_t2i = StableDiffusionPipeline.from_pretrained(
112
- model.path,
113
- unet=unet,
114
- vae=vae,
115
- torch_dtype=dtype,
116
- scheduler=scheduler,
117
- # safety_checker=None,
118
- )
119
- model.pipe_i2i = StableDiffusionImg2ImgPipeline.from_pretrained(
120
- model.path,
121
- unet=unet,
122
- vae=vae,
123
- torch_dtype=dtype,
124
- scheduler=scheduler,
125
- # safety_checker=None,
126
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
127
  except Exception as e:
128
- print(
129
- f"{datetime.datetime.now()} Failed to load model "
130
- + model.name
131
- + ": "
132
- + str(e)
133
- )
134
- models.remove(model)
135
- pipe = models[0].pipe_t2i
136
-
137
- # model.pipe_i2i = torch.compile(model.pipe_i2i)
138
- # model.pipe_t2i = torch.compile(model.pipe_t2i)
139
- if torch.cuda.is_available():
140
- pipe = pipe.to(device)
141
-
142
-
143
- # device = "GPU 🔥" if torch.cuda.is_available() else "CPU 🥶"
144
-
145
-
146
- def error_str(error, title="Error"):
147
- return (
148
- f"""#### {title}
149
- {error}"""
150
- if error
151
- else ""
152
- )
153
-
154
-
155
- def custom_model_changed(path):
156
- models[0].path = path
157
- global current_model
158
- current_model = models[0]
159
-
160
-
161
- def on_model_change(model_name):
162
- prefix = (
163
- 'Enter prompt. "'
164
- + next((m.prefix for m in models if m.name == model_name), None)
165
- + '" is prefixed automatically'
166
- if model_name != models[0].name
167
- else "Don't forget to use the custom model prefix in the prompt!"
168
- )
169
-
170
- return (
171
- gr.update(visible=model_name == models[0].name),
172
- gr.update(placeholder=prefix),
173
- )
174
-
175
-
176
- def inference(
177
- model_name,
178
- prompt,
179
- guidance,
180
- steps,
181
- width=512,
182
- height=512,
183
- seed=0,
184
- img=None,
185
- strength=0.5,
186
- neg_prompt="",
187
- scale="ESRGAN4x",
188
- scale_factor=2,
189
- ):
190
- fprint(psutil.virtual_memory()) # print memory usage
191
-
192
- fprint(f"Prompt: {prompt}")
193
- global current_model
194
- for model in models:
195
- if model.name == model_name:
196
- current_model = model
197
- model_path = current_model.path
198
-
199
- generator = torch.Generator(device).manual_seed(seed) if seed != 0 else None
200
-
201
- try:
202
- if img is not None:
203
- return (
204
- img_to_img(
205
- model_path,
206
- prompt,
207
- neg_prompt,
208
- img,
209
- strength,
210
- guidance,
211
- steps,
212
- width,
213
- height,
214
- generator,
215
- scale,
216
- scale_factor,
217
- ),
218
- None,
219
- )
220
- else:
221
- return (
222
- txt_to_img(
223
- model_path,
224
- prompt,
225
- neg_prompt,
226
- guidance,
227
- steps,
228
- width,
229
- height,
230
- generator,
231
- scale,
232
- scale_factor,
233
- ),
234
- None,
235
- )
236
- except Exception as e:
237
- return None, error_str(e)
238
- # if img is not None:
239
- # return img_to_img(model_path, prompt, neg_prompt, img, strength, guidance, steps, width, height,
240
- # generator, scale, scale_factor), None
241
- # else:
242
- # return txt_to_img(model_path, prompt, neg_prompt, guidance, steps, width, height, generator, scale, scale_factor), None
243
-
244
-
245
- def txt_to_img(
246
- model_path,
247
- prompt,
248
- neg_prompt,
249
- guidance,
250
- steps,
251
- width,
252
- height,
253
- generator,
254
- scale,
255
- scale_factor,
256
- ):
257
- print(f"{datetime.datetime.now()} txt_to_img, model: {current_model.name}")
258
-
259
- global last_mode
260
- global pipe
261
- global current_model_path
262
- if model_path != current_model_path or last_mode != "txt2img":
263
- current_model_path = model_path
264
-
265
- if is_colab or current_model == custom_model:
266
- pipe = StableDiffusionPipeline.from_pretrained(
267
- current_model_path,
268
- torch_dtype=dtype,
269
- scheduler=scheduler,
270
- safety_checker=lambda images, clip_input: (images, False),
271
- )
272
- else:
273
- # pipe = pipe.to("cpu")
274
- pipe = current_model.pipe_t2i
275
-
276
- if torch.cuda.is_available():
277
- pipe = pipe.to(device)
278
- last_mode = "txt2img"
279
-
280
- prompt = current_model.prefix + prompt
281
- result = pipe(
282
  prompt,
283
- negative_prompt=neg_prompt,
284
- # num_images_per_prompt=n_images,
285
- num_inference_steps=int(steps),
286
- guidance_scale=guidance,
287
- width=width,
288
- height=height,
289
- generator=generator,
290
- )
291
-
292
- # result.images[0] = magnifier.magnify(result.images[0], scale_factor=scale_factor)
293
- # enhance resolution
294
- if scale_factor > 1:
295
- if scale == "ESRGAN4x":
296
- fp32 = True if device == "cpu" else False
297
- result.images[0] = realEsrgan(
298
- input_dir=result.images[0],
299
- suffix="",
300
- output_dir="imgs",
301
- fp32=fp32,
302
- outscale=scale_factor,
303
- )[0]
304
- else:
305
- result.images[0] = magnifier.magnify(
306
- result.images[0], scale_factor=scale_factor
307
- )
308
- # save image
309
- result.images[0].save(
310
- "imgs/result-{}.png".format(datetime.datetime.now().strftime("%Y%m%d-%H%M%S"))
311
- )
312
- return replace_nsfw_images(result)
313
-
314
-
315
- def img_to_img(
316
- model_path,
317
- prompt,
318
- neg_prompt,
319
- img,
320
- strength,
321
- guidance,
322
- steps,
323
- width,
324
- height,
325
- generator,
326
- scale,
327
- scale_factor,
328
- ):
329
- fprint(f"{datetime.datetime.now()} img_to_img, model: {model_path}")
330
-
331
- global last_mode
332
- global pipe
333
- global current_model_path
334
- if model_path != current_model_path or last_mode != "img2img":
335
- current_model_path = model_path
336
-
337
- if is_colab or current_model == custom_model:
338
- pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
339
- current_model_path,
340
- torch_dtype=dtype,
341
- scheduler=scheduler,
342
- safety_checker=lambda images, clip_input: (images, False),
343
- )
344
- else:
345
- # pipe = pipe.to("cpu")
346
- pipe = current_model.pipe_i2i
347
-
348
- if torch.cuda.is_available():
349
- pipe = pipe.to(device)
350
- last_mode = "img2img"
351
-
352
- prompt = current_model.prefix + prompt
353
- ratio = min(height / img.height, width / img.width)
354
- img = img.resize((int(img.width * ratio), int(img.height * ratio)), Image.LANCZOS)
355
- result = pipe(
356
  prompt,
357
- negative_prompt=neg_prompt,
358
- # num_images_per_prompt=n_images,
359
- image=img,
360
- num_inference_steps=int(steps),
361
- strength=strength,
362
- guidance_scale=guidance,
363
- # width=width,
364
- # height=height,
365
- generator=generator,
366
- )
367
- if scale_factor > 1:
368
- if scale == "ESRGAN4x":
369
- fp32 = True if device == "cpu" else False
370
- result.images[0] = realEsrgan(
371
- input_dir=result.images[0],
372
- suffix="",
373
- output_dir="imgs",
374
- fp32=fp32,
375
- outscale=scale_factor,
376
- )[0]
377
- else:
378
- result.images[0] = magnifier.magnify(
379
- result.images[0], scale_factor=scale_factor
380
- )
381
- # save image
382
- result.images[0].save(
383
- "imgs/result-{}.png".format(datetime.datetime.now().strftime("%Y%m%d-%H%M%S"))
384
- )
385
- return replace_nsfw_images(result)
386
-
387
-
388
- def replace_nsfw_images(results):
389
- if is_colab:
390
- return results.images[0]
391
- if hasattr(results, "nsfw_content_detected") and results.nsfw_content_detected:
392
- for i in range(len(results.images)):
393
- if results.nsfw_content_detected[i]:
394
- results.images[i] = Image.open("nsfw.png")
395
- return results.images[0]
396
-
397
-
398
- css = """.finetuned-diffusion-div div{display:inline-flex;align-items:center;gap:.8rem;font-size:1.75rem}.finetuned-diffusion-div div h1{font-weight:900;margin-bottom:7px}.finetuned-diffusion-div p{margin-bottom:10px;font-size:94%}a{text-decoration:underline}.tabs{margin-top:0;margin-bottom:0}#gallery{min-height:20rem}
399
- """
400
- with gr.Blocks(css=css) as demo:
401
- if not os.path.exists("imgs"):
402
- os.mkdir("imgs")
403
-
404
- gr.Markdown("# Super Resolution Anime Diffusion")
405
- gr.Markdown(
406
- "## Author: [yangheng95](https://github.com/yangheng95) Github:[Github](https://github.com/yangheng95/stable-diffusion-webui)"
407
- )
408
- gr.Markdown(
409
- "### This demo is running on a CPU, so it will take at least 20 minutes. "
410
- "If you have a GPU, you can clone from [Github](https://github.com/yangheng95/SuperResolutionAnimeDiffusion) and run it locally."
411
- )
412
- gr.Markdown(
413
- "### FYI: to generate a 512*512 image and magnify 4x, it only takes 5~8 seconds on a RTX 2080 GPU"
414
- )
415
- gr.Markdown(
416
- "### You can duplicate this demo on HuggingFace Spaces, click [here](https://huggingface.co/spaces/yangheng/Super-Resolution-Anime-Diffusion?duplicate=true)"
417
- )
418
-
419
- with gr.Row():
420
- with gr.Column(scale=55):
421
- with gr.Group():
422
- gr.Markdown("Text to image")
423
-
424
- model_name = gr.Dropdown(
425
- label="Model",
426
- choices=[m.name for m in models],
427
- value=current_model.name,
428
  )
429
-
430
- with gr.Row(visible=False) as custom_model_group:
431
- custom_model_path = gr.Textbox(
432
- label="Custom model path",
433
- placeholder="Path to model, e.g. nitrosocke/Arcane-Diffusion",
434
- interactive=True,
435
- )
436
- gr.HTML(
437
- "<div><font size='2'>Custom models have to be downloaded first, so give it some time.</font></div>"
438
- )
439
-
440
- with gr.Row():
441
- prompt = gr.Textbox(
442
- label="Prompt",
443
- show_label=False,
444
- max_lines=2,
445
- placeholder="Enter prompt. Style applied automatically",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
446
  )
447
- with gr.Row():
448
- generate = gr.Button(value="Generate")
449
-
450
- with gr.Row():
451
- with gr.Group():
452
- neg_prompt = gr.Textbox(
453
- label="Negative prompt",
454
- value="bad result, worst, random, invalid, inaccurate, imperfect, blurry, deformed,"
455
- " disfigured, mutation, mutated, ugly, out of focus, bad anatomy, text, error,"
456
- " extra digit, fewer digits, worst quality, low quality, normal quality, noise, "
457
- "jpeg artifact, compression artifact, signature, watermark, username, logo, "
458
- "low resolution, worst resolution, bad resolution, normal resolution, bad detail,"
459
- " bad details, bad lighting, bad shadow, bad shading, bad background,"
460
- " worst background.",
461
  )
462
-
463
- image_out = gr.Image(height="auto", width="auto")
464
- error_output = gr.Markdown()
465
-
466
- with gr.Row():
467
- gr.Markdown(
468
- "# Random Image Generation Preview (512*768)x4 magnified"
469
- )
470
- for f_img in findfile.find_cwd_files(".png", recursive=2):
471
- with gr.Row():
472
- image = gr.Image(height=512, value=PIL.Image.open(f_img))
473
- # gallery = gr.Gallery(
474
- # label="Generated images", show_label=False, elem_id="gallery"
475
- # ).style(grid=[1], height="auto")
476
-
477
- with gr.Column(scale=45):
478
- with gr.Group():
479
- gr.Markdown("Image to Image")
480
-
481
- with gr.Row():
482
- with gr.Group():
483
- image = gr.Image(
484
- label="Image", height=256, type="pil"
485
  )
486
- strength = gr.Slider(
487
- label="Transformation strength",
488
- minimum=0,
489
- maximum=1,
490
- step=0.01,
491
- value=0.5,
 
492
  )
493
-
494
- with gr.Row():
495
- with gr.Group():
496
- # n_images = gr.Slider(label="Images", value=1, minimum=1, maximum=4, step=1)
497
-
498
- with gr.Row():
499
- guidance = gr.Slider(
500
- label="Guidance scale", value=7.5, maximum=15
501
- )
502
- steps = gr.Slider(
503
- label="Steps", value=15, minimum=2, maximum=75, step=1
 
 
 
504
  )
505
-
 
 
 
 
 
 
 
 
506
  with gr.Row():
507
- width = gr.Slider(
508
- label="Width",
509
- value=512,
510
- minimum=64,
511
- maximum=1024,
512
- step=8,
513
- )
514
- height = gr.Slider(
515
- label="Height",
516
- value=768,
517
- minimum=64,
518
- maximum=1024,
519
- step=8,
520
  )
521
- with gr.Row():
522
- scale = gr.Radio(
523
- label="Scale",
524
- choices=["Waifu2x", "ESRGAN4x"],
525
- value="Waifu2x",
 
526
  )
527
- with gr.Row():
528
- scale_factor = gr.Slider(
529
- 1,
530
- 8,
531
- label="Scale factor (to magnify image) (1, 2, 4, 8)",
532
- value=1,
533
- step=1,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
534
  )
535
-
536
- seed = gr.Slider(
537
- 0, 2147483647, label="Seed (0 = random)", value=0, step=1
538
- )
539
-
540
- if is_colab:
541
- model_name.change(
542
- on_model_change,
543
- inputs=model_name,
544
- outputs=[custom_model_group, prompt],
545
- queue=False,
 
 
 
 
546
  )
547
- custom_model_path.change(
548
- custom_model_changed, inputs=custom_model_path, outputs=None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
549
  )
550
- # n_images.change(lambda n: gr.Gallery().style(grid=[2 if n > 1 else 1], height="auto"), inputs=n_images, outputs=gallery)
551
-
552
- gr.Markdown(
553
- "### based on [Anything V5]"
554
- )
555
-
556
- inputs = [
557
- model_name,
558
- prompt,
559
- guidance,
560
- steps,
561
- width,
562
- height,
563
- seed,
564
- image,
565
- strength,
566
- neg_prompt,
567
- scale,
568
- scale_factor,
569
- ]
570
- outputs = [image_out, error_output]
571
- prompt.submit(inference, inputs=inputs, outputs=outputs)
572
- generate.click(inference, inputs=inputs, outputs=outputs, api_name="generate")
573
-
574
- prompt_keys = [
575
- "girl",
576
- "lovely",
577
- "cute",
578
- "beautiful eyes",
579
- "cumulonimbus clouds",
580
- random.choice(["dress"]),
581
- random.choice(["white hair"]),
582
- random.choice(["blue eyes"]),
583
- random.choice(["flower meadow"]),
584
- random.choice(["Elif", "Angel"]),
585
- ]
586
- prompt.value = ",".join(prompt_keys)
587
- ex = gr.Examples(
588
- [
589
- [models[0].name, prompt.value, 7.5, 15],
590
- ],
591
- inputs=[model_name, prompt, guidance, steps, seed],
592
- outputs=outputs,
593
- fn=inference,
594
- cache_examples=False,
595
- )
596
-
597
- print(f"Space built in {time.time() - start_time:.2f} seconds")
598
-
599
- if not is_colab:
600
- demo.queue()
601
- demo.launch(debug=is_colab, share=is_colab)
 
5
  import PIL.Image
6
  import autocuda
7
  from pyabsa.utils.pyabsa_utils import fprint
8
+ import spaces
9
 
10
+ @spaces.GPU
11
+ def __main:
12
+ try:
13
+ for z_file in findfile.find_cwd_files(and_key=['.zip'],
14
+ exclude_key=['.ignore', 'git', 'SuperResolutionAnimeDiffusion'],
15
+ recursive=10):
16
+ fprint(f"Extracting {z_file}...")
17
+ with zipfile.ZipFile(z_file, 'r') as zip_ref:
18
+ zip_ref.extractall(os.path.dirname(z_file))
19
+ except Exception as e:
20
+ os.system('unzip random_examples.zip')
21
+
22
+ from diffusers import (
23
+ AutoencoderKL,
24
+ UNet2DConditionModel,
25
+ StableDiffusionPipeline,
26
+ StableDiffusionImg2ImgPipeline,
27
+ DPMSolverMultistepScheduler,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  )
29
+ import gradio as gr
30
+ import torch
31
+ from PIL import Image
32
+ import utils
33
+ import datetime
34
+ import time
35
+ import psutil
36
+ from Waifu2x.magnify import ImageMagnifier
37
+ from RealESRGANv030.interface import realEsrgan
38
+
39
+ magnifier = ImageMagnifier()
40
+
41
+ start_time = time.time()
42
+ is_colab = utils.is_google_colab()
43
+
44
+ CUDA_VISIBLE_DEVICES = ""
45
+ device = autocuda.auto_cuda()
46
+
47
+ dtype = torch.float16 if device != "cpu" else torch.float32
48
+
49
+
50
+
51
+ class Model:
52
+ def __init__(self, name, path="", prefix=""):
53
+ self.name = name
54
+ self.path = path
55
+ self.prefix = prefix
56
+ self.pipe_t2i = None
57
+ self.pipe_i2i = None
58
+
59
+
60
+ models = [
61
+ # Model("anything v3", "Linaqruf/anything-v3.0", "anything v3 style"),
62
+ Model("anything v5", "stablediffusionapi/anything-v5", "anything v5 style"),
63
+ ]
64
+ # Model("Spider-Verse", "nitrosocke/spider-verse-diffusion", "spiderverse style "),
65
+ # Model("Balloon Art", "Fictiverse/Stable_Diffusion_BalloonArt_Model", "BalloonArt "),
66
+ # Model("Elden Ring", "nitrosocke/elden-ring-diffusion", "elden ring style "),
67
+ # Model("Tron Legacy", "dallinmackay/Tron-Legacy-diffusion", "trnlgcy ")
68
+ # Model("Pokémon", "lambdalabs/sd-pokemon-diffusers", ""),
69
+ # Model("Pony Diffusion", "AstraliteHeart/pony-diffusion", ""),
70
+ # Model("Robo Diffusion", "nousr/robo-diffusion", ""),
71
+
72
+ scheduler = DPMSolverMultistepScheduler(
73
+ beta_start=0.00085,
74
+ beta_end=0.012,
75
+ beta_schedule="scaled_linear",
76
+ num_train_timesteps=1000,
77
+ trained_betas=None,
78
+ predict_epsilon=True,
79
+ thresholding=False,
80
+ algorithm_type="dpmsolver++",
81
+ solver_type="midpoint",
82
+ solver_order=2,
83
+ # lower_order_final=True,
84
  )
85
+
86
+ custom_model = None
87
+ if is_colab:
88
+ models.insert(0, Model("Custom model"))
89
+ custom_model = models[0]
90
+
91
+ last_mode = "txt2img"
92
+ current_model = models[1] if is_colab else models[0]
93
+ current_model_path = current_model.path
94
+
95
+ if is_colab:
96
+ pipe = StableDiffusionPipeline.from_pretrained(
97
+ current_model.path,
98
+ torch_dtype=dtype,
99
+ scheduler=scheduler,
100
+ safety_checker=lambda images, clip_input: (images, False),
101
+ )
102
+
103
+ else: # download all models
104
+ print(f"{datetime.datetime.now()} Downloading vae...")
105
+ vae = AutoencoderKL.from_pretrained(
106
+ current_model.path, subfolder="vae", torch_dtype=dtype
107
+ )
108
+ for model in models:
109
+ try:
110
+ print(f"{datetime.datetime.now()} Downloading {model.name} model...")
111
+ unet = UNet2DConditionModel.from_pretrained(
112
+ model.path, subfolder="unet", torch_dtype=dtype
113
+ )
114
+ model.pipe_t2i = StableDiffusionPipeline.from_pretrained(
115
+ model.path,
116
+ unet=unet,
117
+ vae=vae,
118
+ torch_dtype=dtype,
119
+ scheduler=scheduler,
120
+ # safety_checker=None,
121
+ )
122
+ model.pipe_i2i = StableDiffusionImg2ImgPipeline.from_pretrained(
123
+ model.path,
124
+ unet=unet,
125
+ vae=vae,
126
+ torch_dtype=dtype,
127
+ scheduler=scheduler,
128
+ # safety_checker=None,
129
+ )
130
+ except Exception as e:
131
+ print(
132
+ f"{datetime.datetime.now()} Failed to load model "
133
+ + model.name
134
+ + ": "
135
+ + str(e)
136
+ )
137
+ models.remove(model)
138
+ pipe = models[0].pipe_t2i
139
+
140
+ # model.pipe_i2i = torch.compile(model.pipe_i2i)
141
+ # model.pipe_t2i = torch.compile(model.pipe_t2i)
142
+ if torch.cuda.is_available():
143
+ pipe = pipe.to(device)
144
+
145
+
146
+ # device = "GPU 🔥" if torch.cuda.is_available() else "CPU 🥶"
147
+
148
+
149
+ def error_str(error, title="Error"):
150
+ return (
151
+ f"""#### {title}
152
+ {error}"""
153
+ if error
154
+ else ""
155
+ )
156
+
157
+
158
+ def custom_model_changed(path):
159
+ models[0].path = path
160
+ global current_model
161
+ current_model = models[0]
162
+
163
+
164
+ def on_model_change(model_name):
165
+ prefix = (
166
+ 'Enter prompt. "'
167
+ + next((m.prefix for m in models if m.name == model_name), None)
168
+ + '" is prefixed automatically'
169
+ if model_name != models[0].name
170
+ else "Don't forget to use the custom model prefix in the prompt!"
171
+ )
172
+
173
+ return (
174
+ gr.update(visible=model_name == models[0].name),
175
+ gr.update(placeholder=prefix),
176
+ )
177
+
178
+
179
+ def inference(
180
+ model_name,
181
+ prompt,
182
+ guidance,
183
+ steps,
184
+ width=512,
185
+ height=512,
186
+ seed=0,
187
+ img=None,
188
+ strength=0.5,
189
+ neg_prompt="",
190
+ scale="ESRGAN4x",
191
+ scale_factor=2,
192
+ ):
193
+ fprint(psutil.virtual_memory()) # print memory usage
194
+
195
+ fprint(f"Prompt: {prompt}")
196
+ global current_model
197
+ for model in models:
198
+ if model.name == model_name:
199
+ current_model = model
200
+ model_path = current_model.path
201
+
202
+ generator = torch.Generator(device).manual_seed(seed) if seed != 0 else None
203
+
204
  try:
205
+ if img is not None:
206
+ return (
207
+ img_to_img(
208
+ model_path,
209
+ prompt,
210
+ neg_prompt,
211
+ img,
212
+ strength,
213
+ guidance,
214
+ steps,
215
+ width,
216
+ height,
217
+ generator,
218
+ scale,
219
+ scale_factor,
220
+ ),
221
+ None,
222
+ )
223
+ else:
224
+ return (
225
+ txt_to_img(
226
+ model_path,
227
+ prompt,
228
+ neg_prompt,
229
+ guidance,
230
+ steps,
231
+ width,
232
+ height,
233
+ generator,
234
+ scale,
235
+ scale_factor,
236
+ ),
237
+ None,
238
+ )
239
  except Exception as e:
240
+ return None, error_str(e)
241
+ # if img is not None:
242
+ # return img_to_img(model_path, prompt, neg_prompt, img, strength, guidance, steps, width, height,
243
+ # generator, scale, scale_factor), None
244
+ # else:
245
+ # return txt_to_img(model_path, prompt, neg_prompt, guidance, steps, width, height, generator, scale, scale_factor), None
246
+
247
+
248
+ def txt_to_img(
249
+ model_path,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
250
  prompt,
251
+ neg_prompt,
252
+ guidance,
253
+ steps,
254
+ width,
255
+ height,
256
+ generator,
257
+ scale,
258
+ scale_factor,
259
+ ):
260
+ print(f"{datetime.datetime.now()} txt_to_img, model: {current_model.name}")
261
+
262
+ global last_mode
263
+ global pipe
264
+ global current_model_path
265
+ if model_path != current_model_path or last_mode != "txt2img":
266
+ current_model_path = model_path
267
+
268
+ if is_colab or current_model == custom_model:
269
+ pipe = StableDiffusionPipeline.from_pretrained(
270
+ current_model_path,
271
+ torch_dtype=dtype,
272
+ scheduler=scheduler,
273
+ safety_checker=lambda images, clip_input: (images, False),
274
+ )
275
+ else:
276
+ # pipe = pipe.to("cpu")
277
+ pipe = current_model.pipe_t2i
278
+
279
+ if torch.cuda.is_available():
280
+ pipe = pipe.to(device)
281
+ last_mode = "txt2img"
282
+
283
+ prompt = current_model.prefix + prompt
284
+ result = pipe(
285
+ prompt,
286
+ negative_prompt=neg_prompt,
287
+ # num_images_per_prompt=n_images,
288
+ num_inference_steps=int(steps),
289
+ guidance_scale=guidance,
290
+ width=width,
291
+ height=height,
292
+ generator=generator,
293
+ )
294
+
295
+ # result.images[0] = magnifier.magnify(result.images[0], scale_factor=scale_factor)
296
+ # enhance resolution
297
+ if scale_factor > 1:
298
+ if scale == "ESRGAN4x":
299
+ fp32 = True if device == "cpu" else False
300
+ result.images[0] = realEsrgan(
301
+ input_dir=result.images[0],
302
+ suffix="",
303
+ output_dir="imgs",
304
+ fp32=fp32,
305
+ outscale=scale_factor,
306
+ )[0]
307
+ else:
308
+ result.images[0] = magnifier.magnify(
309
+ result.images[0], scale_factor=scale_factor
310
+ )
311
+ # save image
312
+ result.images[0].save(
313
+ "imgs/result-{}.png".format(datetime.datetime.now().strftime("%Y%m%d-%H%M%S"))
314
+ )
315
+ return replace_nsfw_images(result)
316
+
317
+
318
+ def img_to_img(
319
+ model_path,
 
 
 
 
320
  prompt,
321
+ neg_prompt,
322
+ img,
323
+ strength,
324
+ guidance,
325
+ steps,
326
+ width,
327
+ height,
328
+ generator,
329
+ scale,
330
+ scale_factor,
331
+ ):
332
+ fprint(f"{datetime.datetime.now()} img_to_img, model: {model_path}")
333
+
334
+ global last_mode
335
+ global pipe
336
+ global current_model_path
337
+ if model_path != current_model_path or last_mode != "img2img":
338
+ current_model_path = model_path
339
+
340
+ if is_colab or current_model == custom_model:
341
+ pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
342
+ current_model_path,
343
+ torch_dtype=dtype,
344
+ scheduler=scheduler,
345
+ safety_checker=lambda images, clip_input: (images, False),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
346
  )
347
+ else:
348
+ # pipe = pipe.to("cpu")
349
+ pipe = current_model.pipe_i2i
350
+
351
+ if torch.cuda.is_available():
352
+ pipe = pipe.to(device)
353
+ last_mode = "img2img"
354
+
355
+ prompt = current_model.prefix + prompt
356
+ ratio = min(height / img.height, width / img.width)
357
+ img = img.resize((int(img.width * ratio), int(img.height * ratio)), Image.LANCZOS)
358
+ result = pipe(
359
+ prompt,
360
+ negative_prompt=neg_prompt,
361
+ # num_images_per_prompt=n_images,
362
+ image=img,
363
+ num_inference_steps=int(steps),
364
+ strength=strength,
365
+ guidance_scale=guidance,
366
+ # width=width,
367
+ # height=height,
368
+ generator=generator,
369
+ )
370
+ if scale_factor > 1:
371
+ if scale == "ESRGAN4x":
372
+ fp32 = True if device == "cpu" else False
373
+ result.images[0] = realEsrgan(
374
+ input_dir=result.images[0],
375
+ suffix="",
376
+ output_dir="imgs",
377
+ fp32=fp32,
378
+ outscale=scale_factor,
379
+ )[0]
380
+ else:
381
+ result.images[0] = magnifier.magnify(
382
+ result.images[0], scale_factor=scale_factor
383
+ )
384
+ # save image
385
+ result.images[0].save(
386
+ "imgs/result-{}.png".format(datetime.datetime.now().strftime("%Y%m%d-%H%M%S"))
387
+ )
388
+ return replace_nsfw_images(result)
389
+
390
+
391
+ def replace_nsfw_images(results):
392
+ if is_colab:
393
+ return results.images[0]
394
+ if hasattr(results, "nsfw_content_detected") and results.nsfw_content_detected:
395
+ for i in range(len(results.images)):
396
+ if results.nsfw_content_detected[i]:
397
+ results.images[i] = Image.open("nsfw.png")
398
+ return results.images[0]
399
+
400
+
401
+ css = """.finetuned-diffusion-div div{display:inline-flex;align-items:center;gap:.8rem;font-size:1.75rem}.finetuned-diffusion-div div h1{font-weight:900;margin-bottom:7px}.finetuned-diffusion-div p{margin-bottom:10px;font-size:94%}a{text-decoration:underline}.tabs{margin-top:0;margin-bottom:0}#gallery{min-height:20rem}
402
+ """
403
+ with gr.Blocks(css=css) as demo:
404
+ if not os.path.exists("imgs"):
405
+ os.mkdir("imgs")
406
+
407
+ gr.Markdown("# Super Resolution Anime Diffusion")
408
+ gr.Markdown(
409
+ "## Author: [yangheng95](https://github.com/yangheng95) Github:[Github](https://github.com/yangheng95/stable-diffusion-webui)"
410
+ )
411
+ gr.Markdown(
412
+ "### This demo is running on a CPU, so it will take at least 20 minutes. "
413
+ "If you have a GPU, you can clone from [Github](https://github.com/yangheng95/SuperResolutionAnimeDiffusion) and run it locally."
414
+ )
415
+ gr.Markdown(
416
+ "### FYI: to generate a 512*512 image and magnify 4x, it only takes 5~8 seconds on a RTX 2080 GPU"
417
+ )
418
+ gr.Markdown(
419
+ "### You can duplicate this demo on HuggingFace Spaces, click [here](https://huggingface.co/spaces/yangheng/Super-Resolution-Anime-Diffusion?duplicate=true)"
420
+ )
421
+
422
+ with gr.Row():
423
+ with gr.Column(scale=55):
424
+ with gr.Group():
425
+ gr.Markdown("Text to image")
426
+
427
+ model_name = gr.Dropdown(
428
+ label="Model",
429
+ choices=[m.name for m in models],
430
+ value=current_model.name,
431
  )
432
+
433
+ with gr.Row(visible=False) as custom_model_group:
434
+ custom_model_path = gr.Textbox(
435
+ label="Custom model path",
436
+ placeholder="Path to model, e.g. nitrosocke/Arcane-Diffusion",
437
+ interactive=True,
 
 
 
 
 
 
 
 
438
  )
439
+ gr.HTML(
440
+ "<div><font size='2'>Custom models have to be downloaded first, so give it some time.</font></div>"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
441
  )
442
+
443
+ with gr.Row():
444
+ prompt = gr.Textbox(
445
+ label="Prompt",
446
+ show_label=False,
447
+ max_lines=2,
448
+ placeholder="Enter prompt. Style applied automatically",
449
  )
450
+ with gr.Row():
451
+ generate = gr.Button(value="Generate")
452
+
453
+ with gr.Row():
454
+ with gr.Group():
455
+ neg_prompt = gr.Textbox(
456
+ label="Negative prompt",
457
+ value="bad result, worst, random, invalid, inaccurate, imperfect, blurry, deformed,"
458
+ " disfigured, mutation, mutated, ugly, out of focus, bad anatomy, text, error,"
459
+ " extra digit, fewer digits, worst quality, low quality, normal quality, noise, "
460
+ "jpeg artifact, compression artifact, signature, watermark, username, logo, "
461
+ "low resolution, worst resolution, bad resolution, normal resolution, bad detail,"
462
+ " bad details, bad lighting, bad shadow, bad shading, bad background,"
463
+ " worst background.",
464
  )
465
+
466
+ image_out = gr.Image(height="auto", width="auto")
467
+ error_output = gr.Markdown()
468
+
469
+ with gr.Row():
470
+ gr.Markdown(
471
+ "# Random Image Generation Preview (512*768)x4 magnified"
472
+ )
473
+ for f_img in findfile.find_cwd_files(".png", recursive=2):
474
  with gr.Row():
475
+ image = gr.Image(height=512, value=PIL.Image.open(f_img))
476
+ # gallery = gr.Gallery(
477
+ # label="Generated images", show_label=False, elem_id="gallery"
478
+ # ).style(grid=[1], height="auto")
479
+
480
+ with gr.Column(scale=45):
481
+ with gr.Group():
482
+ gr.Markdown("Image to Image")
483
+
484
+ with gr.Row():
485
+ with gr.Group():
486
+ image = gr.Image(
487
+ label="Image", height=256, type="pil"
488
  )
489
+ strength = gr.Slider(
490
+ label="Transformation strength",
491
+ minimum=0,
492
+ maximum=1,
493
+ step=0.01,
494
+ value=0.5,
495
  )
496
+
497
+ with gr.Row():
498
+ with gr.Group():
499
+ # n_images = gr.Slider(label="Images", value=1, minimum=1, maximum=4, step=1)
500
+
501
+ with gr.Row():
502
+ guidance = gr.Slider(
503
+ label="Guidance scale", value=7.5, maximum=15
504
+ )
505
+ steps = gr.Slider(
506
+ label="Steps", value=15, minimum=2, maximum=75, step=1
507
+ )
508
+
509
+ with gr.Row():
510
+ width = gr.Slider(
511
+ label="Width",
512
+ value=512,
513
+ minimum=64,
514
+ maximum=1024,
515
+ step=8,
516
+ )
517
+ height = gr.Slider(
518
+ label="Height",
519
+ value=768,
520
+ minimum=64,
521
+ maximum=1024,
522
+ step=8,
523
+ )
524
+ with gr.Row():
525
+ scale = gr.Radio(
526
+ label="Scale",
527
+ choices=["Waifu2x", "ESRGAN4x"],
528
+ value="Waifu2x",
529
+ )
530
+ with gr.Row():
531
+ scale_factor = gr.Slider(
532
+ 1,
533
+ 8,
534
+ label="Scale factor (to magnify image) (1, 2, 4, 8)",
535
+ value=1,
536
+ step=1,
537
+ )
538
+
539
+ seed = gr.Slider(
540
+ 0, 2147483647, label="Seed (0 = random)", value=0, step=1
541
  )
542
+
543
+ if is_colab:
544
+ model_name.change(
545
+ on_model_change,
546
+ inputs=model_name,
547
+ outputs=[custom_model_group, prompt],
548
+ queue=False,
549
+ )
550
+ custom_model_path.change(
551
+ custom_model_changed, inputs=custom_model_path, outputs=None
552
+ )
553
+ # n_images.change(lambda n: gr.Gallery().style(grid=[2 if n > 1 else 1], height="auto"), inputs=n_images, outputs=gallery)
554
+
555
+ gr.Markdown(
556
+ "### based on [Anything V5]"
557
  )
558
+
559
+ inputs = [
560
+ model_name,
561
+ prompt,
562
+ guidance,
563
+ steps,
564
+ width,
565
+ height,
566
+ seed,
567
+ image,
568
+ strength,
569
+ neg_prompt,
570
+ scale,
571
+ scale_factor,
572
+ ]
573
+ outputs = [image_out, error_output]
574
+ prompt.submit(inference, inputs=inputs, outputs=outputs)
575
+ generate.click(inference, inputs=inputs, outputs=outputs, api_name="generate")
576
+
577
+ prompt_keys = [
578
+ "girl",
579
+ "lovely",
580
+ "cute",
581
+ "beautiful eyes",
582
+ "cumulonimbus clouds",
583
+ random.choice(["dress"]),
584
+ random.choice(["white hair"]),
585
+ random.choice(["blue eyes"]),
586
+ random.choice(["flower meadow"]),
587
+ random.choice(["Elif", "Angel"]),
588
+ ]
589
+ prompt.value = ",".join(prompt_keys)
590
+ ex = gr.Examples(
591
+ [
592
+ [models[0].name, prompt.value, 7.5, 15],
593
+ ],
594
+ inputs=[model_name, prompt, guidance, steps, seed],
595
+ outputs=outputs,
596
+ fn=inference,
597
+ cache_examples=False,
598
  )
599
+
600
+ print(f"Space built in {time.time() - start_time:.2f} seconds")
601
+
602
+ if not is_colab:
603
+ demo.queue()
604
+ demo.launch(debug=is_colab, share=is_colab)
605
+
606
+ __main()