jon-pascal commited on
Commit
0fccdcf
1 Parent(s): 45daab4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +506 -393
app.py CHANGED
@@ -32,11 +32,11 @@ parser = argparse.ArgumentParser()
32
  parser.add_argument("--opt", type=str, default='options/SUPIR_v0.yaml')
33
  parser.add_argument("--ip", type=str, default='127.0.0.1')
34
  parser.add_argument("--port", type=int, default='6688')
35
- parser.add_argument("--no_llava", action='store_true', default=True) # False
36
- parser.add_argument("--use_image_slider", action='store_true', default=False) # False
37
  parser.add_argument("--log_history", action='store_true', default=False)
38
- parser.add_argument("--loading_half_params", action='store_true', default=False) # False
39
- parser.add_argument("--use_tile_vae", action='store_true', default=True) # False
40
  parser.add_argument("--encoder_tile_size", type=int, default=512)
41
  parser.add_argument("--decoder_tile_size", type=int, default=64)
42
  parser.add_argument("--load_8bit_llava", action='store_true', default=False)
@@ -59,22 +59,7 @@ if torch.cuda.device_count() > 0:
59
  def check_upload(input_image):
60
  if input_image is None:
61
  raise gr.Error("Please provide an image to restore.")
62
- return gr.update(visible=True)
63
-
64
- def process_uploaded_image(image_path):
65
- image = Image.open(image_path)
66
- width, height = image.size
67
- max_dim = max(width, height)
68
- if max_dim > 1024:
69
- if width > height:
70
- new_width = 1024
71
- new_height = int((1024 / width) * height)
72
- else:
73
- new_height = 1024
74
- new_width = int((1024 / height) * width)
75
- image = image.resize((new_width, new_height), Image.ANTIALIAS)
76
- image.save(image_path)
77
- return image_path
78
 
79
  def update_seed(is_randomize_seed, seed):
80
  if is_randomize_seed:
@@ -83,6 +68,9 @@ def update_seed(is_randomize_seed, seed):
83
 
84
  def reset():
85
  return [
 
 
 
86
  None,
87
  "Cinematic, High Contrast, highly detailed, taken using a Canon EOS R camera, hyper detailed photo - realistic maximum detail, 32k, Color Grading, ultra HD, extreme meticulous detailing, skin pore detailing, hyper sharpness, perfect without deformations.",
88
  "painting, oil painting, illustration, drawing, art, sketch, anime, cartoon, CG Style, 3D render, unreal engine, blurring, aliasing, unsharp, weird textures, ugly, dirty, messy, worst quality, low quality, frames, watermark, signature, jpeg artifacts, deformed, lowres, over-smooth",
@@ -101,81 +89,73 @@ def reset():
101
  "Wavelet",
102
  "fp32",
103
  "fp32",
 
104
  True,
105
  False,
106
  default_setting.spt_linear_CFG_Quality if torch.cuda.device_count() > 0 else 1.0,
107
  0.,
108
  "v0-Q",
109
- 4
 
110
  ]
111
 
112
  def check(input_image):
113
  if input_image is None:
114
  raise gr.Error("Please provide an image to restore.")
115
 
116
- def stage2_process(
 
117
  input_image,
118
- prompt,
119
- a_prompt,
120
- n_prompt,
121
- num_samples,
122
- min_size,
123
- downscale,
124
- upscale,
125
- edm_steps,
126
- s_stage1,
127
- s_stage2,
128
- s_cfg,
129
- randomize_seed,
130
- seed,
131
- s_churn,
132
- s_noise,
133
- color_fix_type,
134
- diff_dtype,
135
- ae_dtype,
136
  gamma_correction,
137
- linear_CFG,
138
- linear_s_stage2,
139
- spt_linear_CFG,
140
- spt_linear_s_stage2,
141
- model_select,
142
- allocation
143
  ):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
144
  try:
145
- return restore_in_Xmin(
146
- input_image,
147
- prompt,
148
- a_prompt,
149
- n_prompt,
150
- num_samples,
151
- min_size,
152
- downscale,
153
- upscale,
154
- edm_steps,
155
- s_stage1,
156
- s_stage2,
157
- s_cfg,
158
- randomize_seed,
159
- seed,
160
- s_churn,
161
- s_noise,
162
- color_fix_type,
163
- diff_dtype,
164
- ae_dtype,
165
- gamma_correction,
166
- linear_CFG,
167
- linear_s_stage2,
168
- spt_linear_CFG,
169
- spt_linear_s_stage2,
170
- model_select,
171
- allocation
172
- )
173
  except Exception as e:
174
- print(f"Exception occurred: {str(e)}")
 
 
 
 
 
 
 
 
 
 
 
175
  raise e
176
 
177
  def restore_in_Xmin(
178
- input_image,
 
 
179
  prompt,
180
  a_prompt,
181
  n_prompt,
@@ -200,16 +180,51 @@ def restore_in_Xmin(
200
  spt_linear_CFG,
201
  spt_linear_s_stage2,
202
  model_select,
 
203
  allocation
204
  ):
205
- print("Starting image restoration process...")
206
- input_format = re.sub(r"^.*\.([^\.]+)$", r"\1", input_image)
207
-
208
- if input_format.lower() not in ['png', 'webp', 'jpg', 'jpeg', 'gif', 'bmp', 'heic']:
209
- gr.Warning('Invalid image format. Please use a supported image format.')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
210
  return None, None, None, None
211
 
212
- output_format = "png"
 
 
 
 
 
213
 
214
  if prompt is None:
215
  prompt = ""
@@ -226,7 +241,15 @@ def restore_in_Xmin(
226
  a_prompt = prompt + a_prompt
227
  print("Final prompt: " + str(a_prompt))
228
 
229
- denoise_image = np.array(Image.open(input_image))
 
 
 
 
 
 
 
 
230
 
231
  if 1 < downscale:
232
  input_height, input_width, input_channel = denoise_image.shape
@@ -236,10 +259,10 @@ def restore_in_Xmin(
236
 
237
  if torch.cuda.device_count() == 0:
238
  gr.Warning('Set this space to GPU config to make it work.')
239
- return [input_image, denoise_image], gr.update(label="Downloadable results", format=output_format, value=[denoise_image]), None, gr.update(visible=True)
240
 
241
  if model_select != model.current_model:
242
- print('Loading model: ' + model_select)
243
  if model_select == 'v0-Q':
244
  model.load_state_dict(ckpt_Q, strict=False)
245
  elif model_select == 'v0-F':
@@ -250,26 +273,46 @@ def restore_in_Xmin(
250
  model.model.dtype = convert_dtype(diff_dtype)
251
 
252
  # Allocation
253
- allocation_functions = {
254
- 1: restore_in_1min,
255
- 2: restore_in_2min,
256
- 3: restore_in_3min,
257
- 4: restore_in_4min,
258
- 5: restore_in_5min,
259
- 6: restore_in_6min,
260
- 7: restore_in_7min,
261
- 8: restore_in_8min,
262
- 9: restore_in_9min,
263
- 10: restore_in_10min,
264
- }
265
-
266
- restore_function = allocation_functions.get(allocation, restore_in_4min)
267
- return restore_function(
268
- input_image, denoise_image, prompt, a_prompt, n_prompt, num_samples, min_size, downscale, upscale,
269
- edm_steps, s_stage1, s_stage2, s_cfg, randomize_seed, seed, s_churn, s_noise, color_fix_type,
270
- diff_dtype, ae_dtype, gamma_correction, linear_CFG, linear_s_stage2, spt_linear_CFG,
271
- spt_linear_s_stage2, model_select, output_format, allocation
272
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
273
 
274
  @spaces.GPU(duration=59)
275
  def restore_in_1min(*args, **kwargs):
@@ -312,8 +355,8 @@ def restore_in_10min(*args, **kwargs):
312
  return restore_on_gpu(*args, **kwargs)
313
 
314
  def restore_on_gpu(
315
- input_image_path,
316
- denoise_image,
317
  prompt,
318
  a_prompt,
319
  n_prompt,
@@ -342,12 +385,11 @@ def restore_on_gpu(
342
  allocation
343
  ):
344
  start = time.time()
345
- print('Starting GPU restoration...')
346
 
347
  torch.cuda.set_device(SUPIR_device)
348
 
349
  with torch.no_grad():
350
- input_image = Image.open(input_image_path)
351
  input_image = upscale_image(input_image, upscale, unit_resolution=32, min_size=min_size)
352
  LQ = np.array(input_image) / 255.0
353
  LQ = np.power(LQ, gamma_correction)
@@ -357,23 +399,22 @@ def restore_on_gpu(
357
  LQ = torch.tensor(LQ, dtype=torch.float32).permute(2, 0, 1).unsqueeze(0).to(SUPIR_device)[:, :3, :, :]
358
  captions = ['']
359
 
360
- samples = model.batchify_sample(
361
- LQ, captions, num_steps=edm_steps, restoration_scale=s_stage1, s_churn=s_churn,
362
- s_noise=s_noise, cfg_scale=s_cfg, control_scale=s_stage2, seed=seed,
363
- num_samples=num_samples, p_p=a_prompt, n_p=n_prompt, color_fix_type=color_fix_type,
364
- use_linear_CFG=linear_CFG, use_linear_control_scale=linear_s_stage2,
365
- cfg_scale_start=spt_linear_CFG, control_scale_start=spt_linear_s_stage2
366
- )
367
 
368
  x_samples = (einops.rearrange(samples, 'b c h w -> b h w c') * 127.5 + 127.5).cpu().numpy().round().clip(
369
  0, 255).astype(np.uint8)
370
  results = [x_samples[i] for i in range(num_samples)]
371
  torch.cuda.empty_cache()
372
 
 
373
  input_height, input_width, input_channel = np.array(input_image).shape
374
  result_height, result_width, result_channel = np.array(results[0]).shape
375
 
376
- print('Restoration completed.')
377
  end = time.time()
378
  secondes = int(end - start)
379
  minutes = math.floor(secondes / 60)
@@ -381,16 +422,34 @@ def restore_on_gpu(
381
  hours = math.floor(minutes / 60)
382
  minutes = minutes - (hours * 60)
383
  information = ("Start the process again if you want a different result. " if randomize_seed else "") + \
384
- "The image has been enhanced successfully."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
385
 
386
  # Only one image can be shown in the slider
387
- return [input_image_path] + [results[0]], gr.update(label="Downloadable results", format=output_format, value=results), gr.update(value=information, visible=True), gr.update(visible=True)
388
 
389
  def load_and_reset(param_setting):
390
- print('Resetting parameters...')
391
  if torch.cuda.device_count() == 0:
392
  gr.Warning('Set this space to GPU config to make it work.')
393
- return None, None, None, None, None, None, None, None, None, None, None, None, None
394
  edm_steps = default_setting.edm_steps
395
  s_stage2 = 1.0
396
  s_stage1 = -1.0
@@ -417,281 +476,277 @@ def load_and_reset(param_setting):
417
  else:
418
  raise NotImplementedError
419
  gr.Info('The parameters are reset.')
420
- print('Parameters reset completed.')
421
  return edm_steps, s_cfg, s_stage2, s_stage1, s_churn, s_noise, a_prompt, n_prompt, color_fix_type, linear_CFG, \
422
  linear_s_stage2, spt_linear_CFG, spt_linear_s_stage2, model_select
423
 
424
  def log_information(result_gallery):
425
- print('Logging information...')
426
  if result_gallery is not None:
427
  for i, result in enumerate(result_gallery):
428
  print(result[0])
429
 
430
  def on_select_result(result_slider, result_gallery, evt: gr.SelectData):
431
- print('Result selected.')
432
  if result_gallery is not None:
433
  for i, result in enumerate(result_gallery):
434
  print(result[0])
435
  return [result_slider[0], result_gallery[evt.index][0]]
436
 
437
  title_html = """
438
- <h1><center>Maree's Magical Photo Tool</center></h1>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
439
  """
440
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
441
  # Gradio interface
442
  with gr.Blocks() as interface:
443
  if torch.cuda.device_count() == 0:
444
  with gr.Row():
445
  gr.HTML("""
446
- <p style="background-color: red;"><big><big><big><b>⚠️To use this tool, set a GPU with sufficient VRAM.</b></big></big></big></p>
 
 
 
447
  """)
448
  gr.HTML(title_html)
449
 
450
- input_image = gr.Image(label="Upload your photo", show_label=True, type="filepath", height=400, elem_id="image-input")
 
451
  with gr.Group():
452
- prompt = gr.Textbox(
453
- label="Describe your photo",
454
- info="Tell me about your photo so I can make it better.",
455
- value="",
456
- placeholder="Type a description...",
457
- lines=3
458
- )
459
- upscale = gr.Radio(
460
- [["x1", 1], ["x2", 2], ["x3", 3], ["x4", 4]],
461
- label="Upscale factor",
462
- info="Choose how much to enlarge the photo",
463
- value=2,
464
- interactive=True
465
- )
466
- allocation = gr.Radio(
467
- [["1 min", 1], ["2 min", 2], ["3 min", 3], ["4 min", 4], ["5 min", 5]],
468
- label="GPU allocation time (for Jon)",
469
- info="You can ignore this setting",
470
- value=4,
471
- interactive=True
472
- )
473
-
474
- gamma_correction = gr.Number(value=1.0, visible=False) # Hidden component with default value 1.0
475
 
476
  with gr.Accordion("Advanced options", open=False):
477
- a_prompt = gr.Textbox(
478
- label="Additional image description",
479
- info="Completes the main image description",
480
- value='Cinematic, High Contrast, highly detailed, taken using a Canon EOS R '
481
- 'camera, hyper detailed photo - realistic maximum detail, 32k, Color '
482
- 'Grading, ultra HD, extreme meticulous detailing, skin pore detailing, '
483
- 'hyper sharpness, perfect without deformations.',
484
- lines=3
485
- )
486
- n_prompt = gr.Textbox(
487
- label="Negative image description",
488
- info="Disambiguate by listing what the image does NOT represent",
489
- value='painting, oil painting, illustration, drawing, art, sketch, anime, '
490
- 'cartoon, CG Style, 3D render, unreal engine, blurring, aliasing, unsharp, weird textures, ugly, dirty, messy, '
491
- 'worst quality, low quality, frames, watermark, signature, jpeg artifacts, '
492
- 'deformed, lowres, over-smooth',
493
- lines=3
494
- )
495
- edm_steps = gr.Slider(
496
- label="Steps",
497
- info="Lower=faster, higher=more details",
498
- minimum=1,
499
- maximum=200,
500
- value=default_setting.edm_steps if torch.cuda.device_count() > 0 else 1,
501
- step=1
502
- )
503
- num_samples = gr.Slider(
504
- label="Num Samples",
505
- info="Number of generated results",
506
- minimum=1,
507
- maximum=4 if not args.use_image_slider else 1,
508
- value=1,
509
- step=1
510
- )
511
- min_size = gr.Slider(
512
- label="Minimum size",
513
- info="Minimum height, minimum width of the result",
514
- minimum=32,
515
- maximum=4096,
516
- value=1024,
517
- step=32
518
- )
519
- downscale = gr.Radio(
520
- [["/1", 1], ["/2", 2], ["/3", 3], ["/4", 4]],
521
- label="Pre-downscale factor",
522
- info="Reducing blurred image reduces the process time",
523
- value=1,
524
- interactive=True
525
- )
526
  with gr.Row():
527
  with gr.Column():
528
- model_select = gr.Radio(
529
- [["💃 Quality (v0-Q)", "v0-Q"], ["🎯 Fidelity (v0-F)", "v0-F"]],
530
- label="Model Selection",
531
- info="Pretrained model",
532
- value="v0-Q",
533
- interactive=True
534
- )
535
  with gr.Column():
536
- color_fix_type = gr.Radio(
537
- [["None", "None"], ["AdaIn (improve as a photo)", "AdaIn"], ["Wavelet (for JPEG artifacts)", "Wavelet"]],
538
- label="Color-Fix Type",
539
- info="AdaIn=Improve following a style, Wavelet=For JPEG artifacts",
540
- value="AdaIn",
541
- interactive=True
542
- )
543
- s_cfg = gr.Slider(
544
- label="Text Guidance Scale",
545
- info="Lower=follow the image, higher=follow the prompt",
546
- minimum=1.0,
547
- maximum=15.0,
548
- value=default_setting.s_cfg_Quality if torch.cuda.device_count() > 0 else 1.0,
549
- step=0.1
550
- )
551
- s_stage2 = gr.Slider(
552
- label="Restoring Guidance Strength",
553
- minimum=0.,
554
- maximum=1.,
555
- value=1.,
556
- step=0.05
557
- )
558
- s_stage1 = gr.Slider(
559
- label="Pre-denoising Guidance Strength",
560
- minimum=-1.0,
561
- maximum=6.0,
562
- value=-1.0,
563
- step=1.0
564
- )
565
- s_churn = gr.Slider(
566
- label="S-Churn",
567
- minimum=0,
568
- maximum=40,
569
- value=5,
570
- step=1
571
- )
572
- s_noise = gr.Slider(
573
- label="S-Noise",
574
- minimum=1.0,
575
- maximum=1.1,
576
- value=1.003,
577
- step=0.001
578
- )
579
  with gr.Row():
580
  with gr.Column():
581
  linear_CFG = gr.Checkbox(label="Linear CFG", value=True)
582
- spt_linear_CFG = gr.Slider(
583
- label="CFG Start",
584
- minimum=1.0,
585
- maximum=9.0,
586
- value=default_setting.spt_linear_CFG_Quality if torch.cuda.device_count() > 0 else 1.0,
587
- step=0.5
588
- )
589
  with gr.Column():
590
  linear_s_stage2 = gr.Checkbox(label="Linear Restoring Guidance", value=False)
591
- spt_linear_s_stage2 = gr.Slider(
592
- label="Guidance Start",
593
- minimum=0.,
594
- maximum=1.,
595
- value=0.,
596
- step=0.05
597
- )
598
  with gr.Column():
599
- diff_dtype = gr.Radio(
600
- [["fp32 (precision)", "fp32"], ["fp16 (medium)", "fp16"], ["bf16 (speed)", "bf16"]],
601
- label="Diffusion Data Type",
602
- value="fp32",
603
- interactive=True
604
- )
605
  with gr.Column():
606
- ae_dtype = gr.Radio(
607
- [["fp32 (precision)", "fp32"], ["bf16 (speed)", "bf16"]],
608
- label="Auto-Encoder Data Type",
609
- value="fp32",
610
- interactive=True
611
- )
612
- randomize_seed = gr.Checkbox(
613
- label="\U0001F3B2 Randomize seed",
614
- value=True,
615
- info="If checked, result is always different"
616
- )
617
- seed = gr.Slider(
618
- label="Seed",
619
- minimum=0,
620
- maximum=max_64_bit_int,
621
- step=1,
622
- randomize=True
623
- )
624
  with gr.Group():
625
- param_setting = gr.Radio(
626
- ["Quality", "Fidelity"],
627
- interactive=True,
628
- label="Presetting",
629
- value="Quality"
630
- )
631
  restart_button = gr.Button(value="Apply presetting")
632
 
633
  with gr.Column():
634
- diffusion_button = gr.Button(
635
- value="🚀 Enhance Photo",
636
- variant="primary",
637
- elem_id="process_button"
638
- )
639
- reset_btn = gr.Button(
640
- value="🧹 Reset",
641
- variant="stop",
642
- elem_id="reset_button",
643
- visible=False
644
- )
645
-
646
- restore_information = gr.HTML(
647
- value="Start the process again if you want a different result.",
648
- visible=False
649
- )
650
- result_slider = ImageSlider(
651
- label='Comparator',
652
- show_label=False,
653
- interactive=False,
654
- elem_id="slider1",
655
- show_download_button=False
656
- )
657
- result_gallery = gr.Gallery(
658
- label='Downloadable results',
659
- show_label=True,
660
- interactive=False,
661
- elem_id="gallery1"
662
- )
663
-
664
- input_image.upload(
665
- fn=process_uploaded_image,
666
- inputs=input_image,
667
- outputs=input_image,
668
- queue=False
669
- )
670
-
671
- input_image.upload(
672
- fn=check_upload,
673
- inputs=input_image,
674
- outputs=[],
675
- queue=False,
676
- show_progress=False
677
- )
678
-
679
- diffusion_button.click(
680
- fn=update_seed,
681
- inputs=[randomize_seed, seed],
682
- outputs=[seed],
683
- queue=False,
684
- show_progress=False
685
- ).then(
686
- fn=check,
687
- inputs=[input_image],
688
- outputs=[],
689
- queue=False,
690
- show_progress=False
691
- ).success(
692
- fn=stage2_process,
693
- inputs=[
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
694
  input_image,
 
 
695
  prompt,
696
  a_prompt,
697
  n_prompt,
@@ -710,57 +765,117 @@ with gr.Blocks() as interface:
710
  color_fix_type,
711
  diff_dtype,
712
  ae_dtype,
713
- gamma_correction, # Use the hidden gamma_correction component
714
  linear_CFG,
715
  linear_s_stage2,
716
  spt_linear_CFG,
717
  spt_linear_s_stage2,
718
  model_select,
 
719
  allocation
720
  ],
721
- outputs=[
722
  result_slider,
723
  result_gallery,
724
  restore_information,
725
  reset_btn
726
- ]
727
- ).success(
728
- fn=log_information,
729
- inputs=[result_gallery],
730
- outputs=[],
731
- queue=False,
732
- show_progress=False
733
  )
734
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
735
  result_gallery.change(on_select_result, [result_slider, result_gallery], result_slider)
736
  result_gallery.select(on_select_result, [result_slider, result_gallery], result_slider)
737
 
738
- restart_button.click(
739
- fn=load_and_reset,
740
- inputs=[param_setting],
741
- outputs=[
742
- edm_steps,
743
- s_cfg,
744
- s_stage2,
745
- s_stage1,
746
- s_churn,
747
- s_noise,
748
- a_prompt,
749
- n_prompt,
750
- color_fix_type,
751
- linear_CFG,
752
- linear_s_stage2,
753
- spt_linear_CFG,
754
- spt_linear_s_stage2,
755
- model_select
756
- ]
757
- )
758
-
759
- reset_btn.click(
760
- fn=reset,
761
- inputs=[],
762
- outputs=[
763
  input_image,
 
 
764
  prompt,
765
  a_prompt,
766
  n_prompt,
@@ -779,16 +894,14 @@ with gr.Blocks() as interface:
779
  color_fix_type,
780
  diff_dtype,
781
  ae_dtype,
782
- gamma_correction, # Use the hidden gamma_correction component
783
  linear_CFG,
784
  linear_s_stage2,
785
  spt_linear_CFG,
786
  spt_linear_s_stage2,
787
  model_select,
 
788
  allocation
789
- ],
790
- queue=False,
791
- show_progress=False
792
- )
793
-
794
  interface.queue(10).launch()
 
32
  parser.add_argument("--opt", type=str, default='options/SUPIR_v0.yaml')
33
  parser.add_argument("--ip", type=str, default='127.0.0.1')
34
  parser.add_argument("--port", type=int, default='6688')
35
+ parser.add_argument("--no_llava", action='store_true', default=True)#False
36
+ parser.add_argument("--use_image_slider", action='store_true', default=False)#False
37
  parser.add_argument("--log_history", action='store_true', default=False)
38
+ parser.add_argument("--loading_half_params", action='store_true', default=False)#False
39
+ parser.add_argument("--use_tile_vae", action='store_true', default=True)#False
40
  parser.add_argument("--encoder_tile_size", type=int, default=512)
41
  parser.add_argument("--decoder_tile_size", type=int, default=64)
42
  parser.add_argument("--load_8bit_llava", action='store_true', default=False)
 
59
  def check_upload(input_image):
60
  if input_image is None:
61
  raise gr.Error("Please provide an image to restore.")
62
+ return gr.update(visible = True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
 
64
  def update_seed(is_randomize_seed, seed):
65
  if is_randomize_seed:
 
68
 
69
  def reset():
70
  return [
71
+ None,
72
+ 0,
73
+ None,
74
  None,
75
  "Cinematic, High Contrast, highly detailed, taken using a Canon EOS R camera, hyper detailed photo - realistic maximum detail, 32k, Color Grading, ultra HD, extreme meticulous detailing, skin pore detailing, hyper sharpness, perfect without deformations.",
76
  "painting, oil painting, illustration, drawing, art, sketch, anime, cartoon, CG Style, 3D render, unreal engine, blurring, aliasing, unsharp, weird textures, ugly, dirty, messy, worst quality, low quality, frames, watermark, signature, jpeg artifacts, deformed, lowres, over-smooth",
 
89
  "Wavelet",
90
  "fp32",
91
  "fp32",
92
+ 1.0,
93
  True,
94
  False,
95
  default_setting.spt_linear_CFG_Quality if torch.cuda.device_count() > 0 else 1.0,
96
  0.,
97
  "v0-Q",
98
+ "input",
99
+ 6
100
  ]
101
 
102
  def check(input_image):
103
  if input_image is None:
104
  raise gr.Error("Please provide an image to restore.")
105
 
106
+ @spaces.GPU(duration=420)
107
+ def stage1_process(
108
  input_image,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
109
  gamma_correction,
110
+ diff_dtype,
111
+ ae_dtype
 
 
 
 
112
  ):
113
+ print('stage1_process ==>>')
114
+ if torch.cuda.device_count() == 0:
115
+ gr.Warning('Set this space to GPU config to make it work.')
116
+ return None, None
117
+ torch.cuda.set_device(SUPIR_device)
118
+ LQ = HWC3(np.array(Image.open(input_image)))
119
+ LQ = fix_resize(LQ, 512)
120
+ # stage1
121
+ LQ = np.array(LQ) / 255 * 2 - 1
122
+ LQ = torch.tensor(LQ, dtype=torch.float32).permute(2, 0, 1).unsqueeze(0).to(SUPIR_device)[:, :3, :, :]
123
+
124
+ model.ae_dtype = convert_dtype(ae_dtype)
125
+ model.model.dtype = convert_dtype(diff_dtype)
126
+
127
+ LQ = model.batchify_denoise(LQ, is_stage1=True)
128
+ LQ = (LQ[0].permute(1, 2, 0) * 127.5 + 127.5).cpu().numpy().round().clip(0, 255).astype(np.uint8)
129
+ # gamma correction
130
+ LQ = LQ / 255.0
131
+ LQ = np.power(LQ, gamma_correction)
132
+ LQ *= 255.0
133
+ LQ = LQ.round().clip(0, 255).astype(np.uint8)
134
+ print('<<== stage1_process')
135
+ return LQ, gr.update(visible = True)
136
+
137
+ def stage2_process(*args, **kwargs):
138
  try:
139
+ return restore_in_Xmin(*args, **kwargs)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
140
  except Exception as e:
141
+ # NO_GPU_MESSAGE_INQUEUE
142
+ print("gradio.exceptions.Error 'No GPU is currently available for you after 60s'")
143
+ print('str(type(e)): ' + str(type(e))) # <class 'gradio.exceptions.Error'>
144
+ print('str(e): ' + str(e)) # You have exceeded your GPU quota...
145
+ try:
146
+ print('e.message: ' + e.message) # No GPU is currently available for you after 60s
147
+ except Exception as e2:
148
+ print('Failure')
149
+ if str(e).startswith("No GPU is currently available for you after 60s"):
150
+ print('Exception identified!!!')
151
+ #if str(type(e)) == "<class 'gradio.exceptions.Error'>":
152
+ #print('Exception of name ' + type(e).__name__)
153
  raise e
154
 
155
  def restore_in_Xmin(
156
+ noisy_image,
157
+ rotation,
158
+ denoise_image,
159
  prompt,
160
  a_prompt,
161
  n_prompt,
 
180
  spt_linear_CFG,
181
  spt_linear_s_stage2,
182
  model_select,
183
+ output_format,
184
  allocation
185
  ):
186
+ print("noisy_image:\n" + str(noisy_image))
187
+ print("denoise_image:\n" + str(denoise_image))
188
+ print("rotation: " + str(rotation))
189
+ print("prompt: " + str(prompt))
190
+ print("a_prompt: " + str(a_prompt))
191
+ print("n_prompt: " + str(n_prompt))
192
+ print("num_samples: " + str(num_samples))
193
+ print("min_size: " + str(min_size))
194
+ print("downscale: " + str(downscale))
195
+ print("upscale: " + str(upscale))
196
+ print("edm_steps: " + str(edm_steps))
197
+ print("s_stage1: " + str(s_stage1))
198
+ print("s_stage2: " + str(s_stage2))
199
+ print("s_cfg: " + str(s_cfg))
200
+ print("randomize_seed: " + str(randomize_seed))
201
+ print("seed: " + str(seed))
202
+ print("s_churn: " + str(s_churn))
203
+ print("s_noise: " + str(s_noise))
204
+ print("color_fix_type: " + str(color_fix_type))
205
+ print("diff_dtype: " + str(diff_dtype))
206
+ print("ae_dtype: " + str(ae_dtype))
207
+ print("gamma_correction: " + str(gamma_correction))
208
+ print("linear_CFG: " + str(linear_CFG))
209
+ print("linear_s_stage2: " + str(linear_s_stage2))
210
+ print("spt_linear_CFG: " + str(spt_linear_CFG))
211
+ print("spt_linear_s_stage2: " + str(spt_linear_s_stage2))
212
+ print("model_select: " + str(model_select))
213
+ print("GPU time allocation: " + str(allocation) + " min")
214
+ print("output_format: " + str(output_format))
215
+
216
+ input_format = re.sub(r"^.*\.([^\.]+)$", r"\1", noisy_image)
217
+
218
+ if input_format not in ['png', 'webp', 'jpg', 'jpeg', 'gif', 'bmp', 'heic']:
219
+ gr.Warning('Invalid image format. Please first convert into *.png, *.webp, *.jpg, *.jpeg, *.gif, *.bmp or *.heic.')
220
  return None, None, None, None
221
 
222
+ if output_format == "input":
223
+ if noisy_image is None:
224
+ output_format = "png"
225
+ else:
226
+ output_format = input_format
227
+ print("final output_format: " + str(output_format))
228
 
229
  if prompt is None:
230
  prompt = ""
 
241
  a_prompt = prompt + a_prompt
242
  print("Final prompt: " + str(a_prompt))
243
 
244
+ denoise_image = np.array(Image.open(noisy_image if denoise_image is None else denoise_image))
245
+
246
+ if rotation == 90:
247
+ denoise_image = np.array(list(zip(*denoise_image[::-1])))
248
+ elif rotation == 180:
249
+ denoise_image = np.array(list(zip(*denoise_image[::-1])))
250
+ denoise_image = np.array(list(zip(*denoise_image[::-1])))
251
+ elif rotation == -90:
252
+ denoise_image = np.array(list(zip(*denoise_image))[::-1])
253
 
254
  if 1 < downscale:
255
  input_height, input_width, input_channel = denoise_image.shape
 
259
 
260
  if torch.cuda.device_count() == 0:
261
  gr.Warning('Set this space to GPU config to make it work.')
262
+ return [noisy_image, denoise_image], gr.update(label="Downloadable results in *." + output_format + " format", format = output_format, value = [denoise_image]), None, gr.update(visible=True)
263
 
264
  if model_select != model.current_model:
265
+ print('load ' + model_select)
266
  if model_select == 'v0-Q':
267
  model.load_state_dict(ckpt_Q, strict=False)
268
  elif model_select == 'v0-F':
 
273
  model.model.dtype = convert_dtype(diff_dtype)
274
 
275
  # Allocation
276
+ if allocation == 1:
277
+ return restore_in_1min(
278
+ noisy_image, denoise_image, prompt, a_prompt, n_prompt, num_samples, min_size, downscale, upscale, edm_steps, s_stage1, s_stage2, s_cfg, randomize_seed, seed, s_churn, s_noise, color_fix_type, diff_dtype, ae_dtype, gamma_correction, linear_CFG, linear_s_stage2, spt_linear_CFG, spt_linear_s_stage2, model_select, output_format, allocation
279
+ )
280
+ if allocation == 2:
281
+ return restore_in_2min(
282
+ noisy_image, denoise_image, prompt, a_prompt, n_prompt, num_samples, min_size, downscale, upscale, edm_steps, s_stage1, s_stage2, s_cfg, randomize_seed, seed, s_churn, s_noise, color_fix_type, diff_dtype, ae_dtype, gamma_correction, linear_CFG, linear_s_stage2, spt_linear_CFG, spt_linear_s_stage2, model_select, output_format, allocation
283
+ )
284
+ if allocation == 3:
285
+ return restore_in_3min(
286
+ noisy_image, denoise_image, prompt, a_prompt, n_prompt, num_samples, min_size, downscale, upscale, edm_steps, s_stage1, s_stage2, s_cfg, randomize_seed, seed, s_churn, s_noise, color_fix_type, diff_dtype, ae_dtype, gamma_correction, linear_CFG, linear_s_stage2, spt_linear_CFG, spt_linear_s_stage2, model_select, output_format, allocation
287
+ )
288
+ if allocation == 4:
289
+ return restore_in_4min(
290
+ noisy_image, denoise_image, prompt, a_prompt, n_prompt, num_samples, min_size, downscale, upscale, edm_steps, s_stage1, s_stage2, s_cfg, randomize_seed, seed, s_churn, s_noise, color_fix_type, diff_dtype, ae_dtype, gamma_correction, linear_CFG, linear_s_stage2, spt_linear_CFG, spt_linear_s_stage2, model_select, output_format, allocation
291
+ )
292
+ if allocation == 5:
293
+ return restore_in_5min(
294
+ noisy_image, denoise_image, prompt, a_prompt, n_prompt, num_samples, min_size, downscale, upscale, edm_steps, s_stage1, s_stage2, s_cfg, randomize_seed, seed, s_churn, s_noise, color_fix_type, diff_dtype, ae_dtype, gamma_correction, linear_CFG, linear_s_stage2, spt_linear_CFG, spt_linear_s_stage2, model_select, output_format, allocation
295
+ )
296
+ if allocation == 7:
297
+ return restore_in_7min(
298
+ noisy_image, denoise_image, prompt, a_prompt, n_prompt, num_samples, min_size, downscale, upscale, edm_steps, s_stage1, s_stage2, s_cfg, randomize_seed, seed, s_churn, s_noise, color_fix_type, diff_dtype, ae_dtype, gamma_correction, linear_CFG, linear_s_stage2, spt_linear_CFG, spt_linear_s_stage2, model_select, output_format, allocation
299
+ )
300
+ if allocation == 8:
301
+ return restore_in_8min(
302
+ noisy_image, denoise_image, prompt, a_prompt, n_prompt, num_samples, min_size, downscale, upscale, edm_steps, s_stage1, s_stage2, s_cfg, randomize_seed, seed, s_churn, s_noise, color_fix_type, diff_dtype, ae_dtype, gamma_correction, linear_CFG, linear_s_stage2, spt_linear_CFG, spt_linear_s_stage2, model_select, output_format, allocation
303
+ )
304
+ if allocation == 9:
305
+ return restore_in_9min(
306
+ noisy_image, denoise_image, prompt, a_prompt, n_prompt, num_samples, min_size, downscale, upscale, edm_steps, s_stage1, s_stage2, s_cfg, randomize_seed, seed, s_churn, s_noise, color_fix_type, diff_dtype, ae_dtype, gamma_correction, linear_CFG, linear_s_stage2, spt_linear_CFG, spt_linear_s_stage2, model_select, output_format, allocation
307
+ )
308
+ if allocation == 10:
309
+ return restore_in_10min(
310
+ noisy_image, denoise_image, prompt, a_prompt, n_prompt, num_samples, min_size, downscale, upscale, edm_steps, s_stage1, s_stage2, s_cfg, randomize_seed, seed, s_churn, s_noise, color_fix_type, diff_dtype, ae_dtype, gamma_correction, linear_CFG, linear_s_stage2, spt_linear_CFG, spt_linear_s_stage2, model_select, output_format, allocation
311
+ )
312
+ else:
313
+ return restore_in_6min(
314
+ noisy_image, denoise_image, prompt, a_prompt, n_prompt, num_samples, min_size, downscale, upscale, edm_steps, s_stage1, s_stage2, s_cfg, randomize_seed, seed, s_churn, s_noise, color_fix_type, diff_dtype, ae_dtype, gamma_correction, linear_CFG, linear_s_stage2, spt_linear_CFG, spt_linear_s_stage2, model_select, output_format, allocation
315
+ )
316
 
317
  @spaces.GPU(duration=59)
318
  def restore_in_1min(*args, **kwargs):
 
355
  return restore_on_gpu(*args, **kwargs)
356
 
357
  def restore_on_gpu(
358
+ noisy_image,
359
+ input_image,
360
  prompt,
361
  a_prompt,
362
  n_prompt,
 
385
  allocation
386
  ):
387
  start = time.time()
388
+ print('restore ==>>')
389
 
390
  torch.cuda.set_device(SUPIR_device)
391
 
392
  with torch.no_grad():
 
393
  input_image = upscale_image(input_image, upscale, unit_resolution=32, min_size=min_size)
394
  LQ = np.array(input_image) / 255.0
395
  LQ = np.power(LQ, gamma_correction)
 
399
  LQ = torch.tensor(LQ, dtype=torch.float32).permute(2, 0, 1).unsqueeze(0).to(SUPIR_device)[:, :3, :, :]
400
  captions = ['']
401
 
402
+ samples = model.batchify_sample(LQ, captions, num_steps=edm_steps, restoration_scale=s_stage1, s_churn=s_churn,
403
+ s_noise=s_noise, cfg_scale=s_cfg, control_scale=s_stage2, seed=seed,
404
+ num_samples=num_samples, p_p=a_prompt, n_p=n_prompt, color_fix_type=color_fix_type,
405
+ use_linear_CFG=linear_CFG, use_linear_control_scale=linear_s_stage2,
406
+ cfg_scale_start=spt_linear_CFG, control_scale_start=spt_linear_s_stage2)
 
 
407
 
408
  x_samples = (einops.rearrange(samples, 'b c h w -> b h w c') * 127.5 + 127.5).cpu().numpy().round().clip(
409
  0, 255).astype(np.uint8)
410
  results = [x_samples[i] for i in range(num_samples)]
411
  torch.cuda.empty_cache()
412
 
413
+ # All the results have the same size
414
  input_height, input_width, input_channel = np.array(input_image).shape
415
  result_height, result_width, result_channel = np.array(results[0]).shape
416
 
417
+ print('<<== restore')
418
  end = time.time()
419
  secondes = int(end - start)
420
  minutes = math.floor(secondes / 60)
 
422
  hours = math.floor(minutes / 60)
423
  minutes = minutes - (hours * 60)
424
  information = ("Start the process again if you want a different result. " if randomize_seed else "") + \
425
+ "If you don't get the image you wanted, add more details in the « Image description ». " + \
426
+ "Wait " + str(allocation) + " min before a new run to avoid quota penalty or use another computer. " + \
427
+ "The image" + (" has" if len(results) == 1 else "s have") + " been generated in " + \
428
+ ((str(hours) + " h, ") if hours != 0 else "") + \
429
+ ((str(minutes) + " min, ") if hours != 0 or minutes != 0 else "") + \
430
+ str(secondes) + " sec. " + \
431
+ "The new image resolution is " + str(result_width) + \
432
+ " pixels large and " + str(result_height) + \
433
+ " pixels high, so a resolution of " + f'{result_width * result_height:,}' + " pixels."
434
+ print(information)
435
+ try:
436
+ print("Initial resolution: " + f'{input_width * input_height:,}')
437
+ print("Final resolution: " + f'{result_width * result_height:,}')
438
+ print("edm_steps: " + str(edm_steps))
439
+ print("num_samples: " + str(num_samples))
440
+ print("downscale: " + str(downscale))
441
+ print("Estimated minutes: " + f'{(((result_width * result_height**(1/1.75)) * input_width * input_height * (edm_steps**(1/2)) * (num_samples**(1/2.5)))**(1/2.5)) / 25000:,}')
442
+ except Exception as e:
443
+ print('Exception of Estimation')
444
 
445
  # Only one image can be shown in the slider
446
+ return [noisy_image] + [results[0]], gr.update(label="Downloadable results in *." + output_format + " format", format = output_format, value = results), gr.update(value = information, visible = True), gr.update(visible=True)
447
 
448
  def load_and_reset(param_setting):
449
+ print('load_and_reset ==>>')
450
  if torch.cuda.device_count() == 0:
451
  gr.Warning('Set this space to GPU config to make it work.')
452
+ return None, None, None, None, None, None, None, None, None, None, None, None, None, None
453
  edm_steps = default_setting.edm_steps
454
  s_stage2 = 1.0
455
  s_stage1 = -1.0
 
476
  else:
477
  raise NotImplementedError
478
  gr.Info('The parameters are reset.')
479
+ print('<<== load_and_reset')
480
  return edm_steps, s_cfg, s_stage2, s_stage1, s_churn, s_noise, a_prompt, n_prompt, color_fix_type, linear_CFG, \
481
  linear_s_stage2, spt_linear_CFG, spt_linear_s_stage2, model_select
482
 
483
  def log_information(result_gallery):
484
+ print('log_information')
485
  if result_gallery is not None:
486
  for i, result in enumerate(result_gallery):
487
  print(result[0])
488
 
489
  def on_select_result(result_slider, result_gallery, evt: gr.SelectData):
490
+ print('on_select_result')
491
  if result_gallery is not None:
492
  for i, result in enumerate(result_gallery):
493
  print(result[0])
494
  return [result_slider[0], result_gallery[evt.index][0]]
495
 
496
  title_html = """
497
+ <h1><center>SUPIR</center></h1>
498
+ <big><center>Upscale your images up to x10 freely, without account, without watermark and download it</center></big>
499
+ <center><big><big>🤸<big><big><big><big><big><big>🤸</big></big></big></big></big></big></big></big></center>
500
+
501
+ <p>This is an online demo of SUPIR, a practicing model scaling for photo-realistic image restoration.
502
+ The content added by SUPIR is <b><u>imagination, not real-world information</u></b>.
503
+ SUPIR is for beauty and illustration only.
504
+ Most of the processes last few minutes.
505
+ If you want to upscale AI-generated images, be noticed that <i>PixArt Sigma</i> space can directly generate 5984x5984 images.
506
+ Due to Gradio issues, the generated image is slightly less satured than the original.
507
+ Please leave a <a href="https://huggingface.co/spaces/Fabrice-TIERCELIN/SUPIR/discussions/new">message in discussion</a> if you encounter issues.
508
+ You can also use <a href="https://huggingface.co/spaces/gokaygokay/AuraSR">AuraSR</a> to upscale x4.
509
+
510
+ <p><center><a href="https://arxiv.org/abs/2401.13627">Paper</a> &emsp; <a href="http://supir.xpixel.group/">Project Page</a> &emsp; <a href="https://huggingface.co/blog/MonsterMMORPG/supir-sota-image-upscale-better-than-magnific-ai">Local Install Guide</a></center></p>
511
+ <p><center><a style="display:inline-block" href='https://github.com/Fanghua-Yu/SUPIR'><img alt="GitHub Repo stars" src="https://img.shields.io/github/stars/Fanghua-Yu/SUPIR?style=social"></a></center></p>
512
  """
513
 
514
+
515
+ claim_md = """
516
+ ## **Piracy**
517
+ The images are not stored but the logs are saved during a month.
518
+ ## **How to get SUPIR**
519
+ You can get SUPIR on HuggingFace by [duplicating this space](https://huggingface.co/spaces/Fabrice-TIERCELIN/SUPIR?duplicate=true) and set GPU.
520
+ You can also install SUPIR on your computer following [this tutorial](https://huggingface.co/blog/MonsterMMORPG/supir-sota-image-upscale-better-than-magnific-ai).
521
+ You can install _Pinokio_ on your computer and then install _SUPIR_ into it. It should be quite easy if you have an Nvidia GPU.
522
+ ## **Terms of use**
523
+ By using this service, users are required to agree to the following terms: The service is a research preview intended for non-commercial use only. It only provides limited safety measures and may generate offensive content. It must not be used for any illegal, harmful, violent, racist, or sexual purposes. The service may collect user dialogue data for future research. Please submit a feedback to us if you get any inappropriate answer! We will collect those to keep improving our models. For an optimal experience, please use desktop computers for this demo, as mobile devices may compromise its quality.
524
+ ## **License**
525
+ The service is a research preview intended for non-commercial use only, subject to the model [License](https://github.com/Fanghua-Yu/SUPIR) of SUPIR.
526
+ """
527
+
528
  # Gradio interface
529
  with gr.Blocks() as interface:
530
  if torch.cuda.device_count() == 0:
531
  with gr.Row():
532
  gr.HTML("""
533
+ <p style="background-color: red;"><big><big><big><b>⚠️To use SUPIR, <a href="https://huggingface.co/spaces/Fabrice-TIERCELIN/SUPIR?duplicate=true">duplicate this space</a> and set a GPU with 30 GB VRAM.</b>
534
+
535
+ You can't use SUPIR directly here because this space runs on a CPU, which is not enough for SUPIR. Please provide <a href="https://huggingface.co/spaces/Fabrice-TIERCELIN/SUPIR/discussions/new">feedback</a> if you have issues.
536
+ </big></big></big></p>
537
  """)
538
  gr.HTML(title_html)
539
 
540
+ input_image = gr.Image(label="Input (*.png, *.webp, *.jpeg, *.jpg, *.gif, *.bmp, *.heic)", show_label=True, type="filepath", height=600, elem_id="image-input")
541
+ rotation = gr.Radio([["No rotation", 0], ["⤵ Rotate +90°", 90], ["↩ Return 180°", 180], ["⤴ Rotate -90°", -90]], label="Orientation correction", info="Will apply the following rotation before restoring the image; the AI needs a good orientation to understand the content", value=0, interactive=True, visible=False)
542
  with gr.Group():
543
+ prompt = gr.Textbox(label="Image description", info="Help the AI understand what the image represents; describe as much as possible, especially the details we can't see on the original image; you can write in any language", value="", placeholder="A 33 years old man, walking, in the street, Santiago, morning, Summer, photorealistic", lines=3)
544
+ prompt_hint = gr.HTML("You can use a <a href='"'https://huggingface.co/spaces/MaziyarPanahi/llava-llama-3-8b'"'>LlaVa space</a> to auto-generate the description of your image.")
545
+ upscale = gr.Radio([["x1", 1], ["x2", 2], ["x3", 3], ["x4", 4], ["x5", 5], ["x6", 6], ["x7", 7], ["x8", 8], ["x9", 9], ["x10", 10]], label="Upscale factor", info="Resolution x1 to x10", value=2, interactive=True)
546
+ output_format = gr.Radio([["As input", "input"], ["*.png", "png"], ["*.webp", "webp"], ["*.jpeg", "jpeg"], ["*.gif", "gif"], ["*.bmp", "bmp"]], label="Image format for result", info="File extention", value="input", interactive=True)
547
+ allocation = gr.Radio([["1 min", 1], ["2 min", 2], ["3 min", 3], ["4 min", 4], ["5 min", 5]], label="GPU allocation time", info="lower=May abort run, higher=Quota penalty for next runs", value=5, interactive=True)
548
+
549
+ with gr.Accordion("Pre-denoising (optional)", open=False):
550
+ gamma_correction = gr.Slider(label="Gamma Correction", info = "lower=lighter, higher=darker", minimum=0.1, maximum=2.0, value=1.0, step=0.1)
551
+ denoise_button = gr.Button(value="Pre-denoise")
552
+ denoise_image = gr.Image(label="Denoised image", show_label=True, type="filepath", sources=[], interactive = False, height=600, elem_id="image-s1")
553
+ denoise_information = gr.HTML(value="If present, the denoised image will be used for the restoration instead of the input image.", visible=False)
 
 
 
 
 
 
 
 
 
 
 
 
554
 
555
  with gr.Accordion("Advanced options", open=False):
556
+ a_prompt = gr.Textbox(label="Additional image description",
557
+ info="Completes the main image description",
558
+ value='Cinematic, High Contrast, highly detailed, taken using a Canon EOS R '
559
+ 'camera, hyper detailed photo - realistic maximum detail, 32k, Color '
560
+ 'Grading, ultra HD, extreme meticulous detailing, skin pore detailing, '
561
+ 'hyper sharpness, perfect without deformations.',
562
+ lines=3)
563
+ n_prompt = gr.Textbox(label="Negative image description",
564
+ info="Disambiguate by listing what the image does NOT represent",
565
+ value='painting, oil painting, illustration, drawing, art, sketch, anime, '
566
+ 'cartoon, CG Style, 3D render, unreal engine, blurring, aliasing, unsharp, weird textures, ugly, dirty, messy, '
567
+ 'worst quality, low quality, frames, watermark, signature, jpeg artifacts, '
568
+ 'deformed, lowres, over-smooth',
569
+ lines=3)
570
+ edm_steps = gr.Slider(label="Steps", info="lower=faster, higher=more details", minimum=1, maximum=200, value=default_setting.edm_steps if torch.cuda.device_count() > 0 else 1, step=1)
571
+ num_samples = gr.Slider(label="Num Samples", info="Number of generated results", minimum=1, maximum=4 if not args.use_image_slider else 1
572
+ , value=1, step=1)
573
+ min_size = gr.Slider(label="Minimum size", info="Minimum height, minimum width of the result", minimum=32, maximum=4096, value=1024, step=32)
574
+ downscale = gr.Radio([["/1", 1], ["/2", 2], ["/3", 3], ["/4", 4], ["/5", 5], ["/6", 6], ["/7", 7], ["/8", 8], ["/9", 9], ["/10", 10]], label="Pre-downscale factor", info="Reducing blurred image reduce the process time", value=1, interactive=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
575
  with gr.Row():
576
  with gr.Column():
577
+ model_select = gr.Radio([["💃 Quality (v0-Q)", "v0-Q"], ["🎯 Fidelity (v0-F)", "v0-F"]], label="Model Selection", info="Pretrained model", value="v0-Q",
578
+ interactive=True)
 
 
 
 
 
579
  with gr.Column():
580
+ color_fix_type = gr.Radio([["None", "None"], ["AdaIn (improve as a photo)", "AdaIn"], ["Wavelet (for JPEG artifacts)", "Wavelet"]], label="Color-Fix Type", info="AdaIn=Improve following a style, Wavelet=For JPEG artifacts", value="AdaIn",
581
+ interactive=True)
582
+ s_cfg = gr.Slider(label="Text Guidance Scale", info="lower=follow the image, higher=follow the prompt", minimum=1.0, maximum=15.0,
583
+ value=default_setting.s_cfg_Quality if torch.cuda.device_count() > 0 else 1.0, step=0.1)
584
+ s_stage2 = gr.Slider(label="Restoring Guidance Strength", minimum=0., maximum=1., value=1., step=0.05)
585
+ s_stage1 = gr.Slider(label="Pre-denoising Guidance Strength", minimum=-1.0, maximum=6.0, value=-1.0, step=1.0)
586
+ s_churn = gr.Slider(label="S-Churn", minimum=0, maximum=40, value=5, step=1)
587
+ s_noise = gr.Slider(label="S-Noise", minimum=1.0, maximum=1.1, value=1.003, step=0.001)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
588
  with gr.Row():
589
  with gr.Column():
590
  linear_CFG = gr.Checkbox(label="Linear CFG", value=True)
591
+ spt_linear_CFG = gr.Slider(label="CFG Start", minimum=1.0,
592
+ maximum=9.0, value=default_setting.spt_linear_CFG_Quality if torch.cuda.device_count() > 0 else 1.0, step=0.5)
 
 
 
 
 
593
  with gr.Column():
594
  linear_s_stage2 = gr.Checkbox(label="Linear Restoring Guidance", value=False)
595
+ spt_linear_s_stage2 = gr.Slider(label="Guidance Start", minimum=0.,
596
+ maximum=1., value=0., step=0.05)
 
 
 
 
 
597
  with gr.Column():
598
+ diff_dtype = gr.Radio([["fp32 (precision)", "fp32"], ["fp16 (medium)", "fp16"], ["bf16 (speed)", "bf16"]], label="Diffusion Data Type", value="fp32",
599
+ interactive=True)
 
 
 
 
600
  with gr.Column():
601
+ ae_dtype = gr.Radio([["fp32 (precision)", "fp32"], ["bf16 (speed)", "bf16"]], label="Auto-Encoder Data Type", value="fp32",
602
+ interactive=True)
603
+ randomize_seed = gr.Checkbox(label = "\U0001F3B2 Randomize seed", value = True, info = "If checked, result is always different")
604
+ seed = gr.Slider(label="Seed", minimum=0, maximum=max_64_bit_int, step=1, randomize=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
605
  with gr.Group():
606
+ param_setting = gr.Radio(["Quality", "Fidelity"], interactive=True, label="Presetting", value = "Quality")
 
 
 
 
 
607
  restart_button = gr.Button(value="Apply presetting")
608
 
609
  with gr.Column():
610
+ diffusion_button = gr.Button(value="🚀 Upscale/Restore", variant = "primary", elem_id = "process_button")
611
+ reset_btn = gr.Button(value="🧹 Reinit page", variant="stop", elem_id="reset_button", visible = False)
612
+
613
+ restore_information = gr.HTML(value = "Restart the process to get another result.", visible = False)
614
+ result_slider = ImageSlider(label = 'Comparator', show_label = False, interactive = False, elem_id = "slider1", show_download_button = False)
615
+ result_gallery = gr.Gallery(label = 'Downloadable results', show_label = True, interactive = False, elem_id = "gallery1")
616
+
617
+ gr.Examples(
618
+ examples = [
619
+ [
620
+ "./Examples/Example1.png",
621
+ 0,
622
+ None,
623
+ "Group of people, walking, happy, in the street, photorealistic, 8k, extremely detailled",
624
+ "Cinematic, High Contrast, highly detailed, taken using a Canon EOS R camera, hyper detailed photo - realistic maximum detail, 32k, Color Grading, ultra HD, extreme meticulous detailing, skin pore detailing, hyper sharpness, perfect without deformations.",
625
+ "painting, oil painting, illustration, drawing, art, sketch, anime, cartoon, CG Style, 3D render, unreal engine, blurring, aliasing, unsharp, weird textures, ugly, dirty, messy, worst quality, low quality, frames, watermark, signature, jpeg artifacts, deformed, lowres, over-smooth",
626
+ 2,
627
+ 1024,
628
+ 1,
629
+ 8,
630
+ 200,
631
+ -1,
632
+ 1,
633
+ 7.5,
634
+ False,
635
+ 42,
636
+ 5,
637
+ 1.003,
638
+ "AdaIn",
639
+ "fp16",
640
+ "bf16",
641
+ 1.0,
642
+ True,
643
+ 4,
644
+ False,
645
+ 0.,
646
+ "v0-Q",
647
+ "input",
648
+ 5
649
+ ],
650
+ [
651
+ "./Examples/Example2.jpeg",
652
+ 0,
653
+ None,
654
+ "La cabeza de un gato atigrado, en una casa, fotorrealista, 8k, extremadamente detallada",
655
+ "Cinematic, High Contrast, highly detailed, taken using a Canon EOS R camera, hyper detailed photo - realistic maximum detail, 32k, Color Grading, ultra HD, extreme meticulous detailing, skin pore detailing, hyper sharpness, perfect without deformations.",
656
+ "painting, oil painting, illustration, drawing, art, sketch, anime, cartoon, CG Style, 3D render, unreal engine, blurring, aliasing, unsharp, weird textures, ugly, dirty, messy, worst quality, low quality, frames, watermark, signature, jpeg artifacts, deformed, lowres, over-smooth",
657
+ 1,
658
+ 1024,
659
+ 1,
660
+ 1,
661
+ 200,
662
+ -1,
663
+ 1,
664
+ 7.5,
665
+ False,
666
+ 42,
667
+ 5,
668
+ 1.003,
669
+ "Wavelet",
670
+ "fp16",
671
+ "bf16",
672
+ 1.0,
673
+ True,
674
+ 4,
675
+ False,
676
+ 0.,
677
+ "v0-Q",
678
+ "input",
679
+ 4
680
+ ],
681
+ [
682
+ "./Examples/Example3.webp",
683
+ 0,
684
+ None,
685
+ "A red apple",
686
+ "Cinematic, High Contrast, highly detailed, taken using a Canon EOS R camera, hyper detailed photo - realistic maximum detail, 32k, Color Grading, ultra HD, extreme meticulous detailing, skin pore detailing, hyper sharpness, perfect without deformations.",
687
+ "painting, oil painting, illustration, drawing, art, sketch, anime, cartoon, CG Style, 3D render, unreal engine, blurring, aliasing, unsharp, weird textures, ugly, dirty, messy, worst quality, low quality, frames, watermark, signature, jpeg artifacts, deformed, lowres, over-smooth",
688
+ 1,
689
+ 1024,
690
+ 1,
691
+ 1,
692
+ 200,
693
+ -1,
694
+ 1,
695
+ 7.5,
696
+ False,
697
+ 42,
698
+ 5,
699
+ 1.003,
700
+ "Wavelet",
701
+ "fp16",
702
+ "bf16",
703
+ 1.0,
704
+ True,
705
+ 4,
706
+ False,
707
+ 0.,
708
+ "v0-Q",
709
+ "input",
710
+ 4
711
+ ],
712
+ [
713
+ "./Examples/Example3.webp",
714
+ 0,
715
+ None,
716
+ "A red marble",
717
+ "Cinematic, High Contrast, highly detailed, taken using a Canon EOS R camera, hyper detailed photo - realistic maximum detail, 32k, Color Grading, ultra HD, extreme meticulous detailing, skin pore detailing, hyper sharpness, perfect without deformations.",
718
+ "painting, oil painting, illustration, drawing, art, sketch, anime, cartoon, CG Style, 3D render, unreal engine, blurring, aliasing, unsharp, weird textures, ugly, dirty, messy, worst quality, low quality, frames, watermark, signature, jpeg artifacts, deformed, lowres, over-smooth",
719
+ 1,
720
+ 1024,
721
+ 1,
722
+ 1,
723
+ 200,
724
+ -1,
725
+ 1,
726
+ 7.5,
727
+ False,
728
+ 42,
729
+ 5,
730
+ 1.003,
731
+ "Wavelet",
732
+ "fp16",
733
+ "bf16",
734
+ 1.0,
735
+ True,
736
+ 4,
737
+ False,
738
+ 0.,
739
+ "v0-Q",
740
+ "input",
741
+ 4
742
+ ],
743
+ ],
744
+ run_on_click = True,
745
+ fn = stage2_process,
746
+ inputs = [
747
  input_image,
748
+ rotation,
749
+ denoise_image,
750
  prompt,
751
  a_prompt,
752
  n_prompt,
 
765
  color_fix_type,
766
  diff_dtype,
767
  ae_dtype,
768
+ gamma_correction,
769
  linear_CFG,
770
  linear_s_stage2,
771
  spt_linear_CFG,
772
  spt_linear_s_stage2,
773
  model_select,
774
+ output_format,
775
  allocation
776
  ],
777
+ outputs = [
778
  result_slider,
779
  result_gallery,
780
  restore_information,
781
  reset_btn
782
+ ],
783
+ cache_examples = False,
 
 
 
 
 
784
  )
785
 
786
+ with gr.Row():
787
+ gr.Markdown(claim_md)
788
+
789
+ input_image.upload(fn = check_upload, inputs = [
790
+ input_image
791
+ ], outputs = [
792
+ rotation
793
+ ], queue = False, show_progress = False)
794
+
795
+ denoise_button.click(fn = check, inputs = [
796
+ input_image
797
+ ], outputs = [], queue = False, show_progress = False).success(fn = stage1_process, inputs = [
798
+ input_image,
799
+ gamma_correction,
800
+ diff_dtype,
801
+ ae_dtype
802
+ ], outputs=[
803
+ denoise_image,
804
+ denoise_information
805
+ ])
806
+
807
+ diffusion_button.click(fn = update_seed, inputs = [
808
+ randomize_seed,
809
+ seed
810
+ ], outputs = [
811
+ seed
812
+ ], queue = False, show_progress = False).then(fn = check, inputs = [
813
+ input_image
814
+ ], outputs = [], queue = False, show_progress = False).success(fn=stage2_process, inputs = [
815
+ input_image,
816
+ rotation,
817
+ denoise_image,
818
+ prompt,
819
+ a_prompt,
820
+ n_prompt,
821
+ num_samples,
822
+ min_size,
823
+ downscale,
824
+ upscale,
825
+ edm_steps,
826
+ s_stage1,
827
+ s_stage2,
828
+ s_cfg,
829
+ randomize_seed,
830
+ seed,
831
+ s_churn,
832
+ s_noise,
833
+ color_fix_type,
834
+ diff_dtype,
835
+ ae_dtype,
836
+ gamma_correction,
837
+ linear_CFG,
838
+ linear_s_stage2,
839
+ spt_linear_CFG,
840
+ spt_linear_s_stage2,
841
+ model_select,
842
+ output_format,
843
+ allocation
844
+ ], outputs = [
845
+ result_slider,
846
+ result_gallery,
847
+ restore_information,
848
+ reset_btn
849
+ ]).success(fn = log_information, inputs = [
850
+ result_gallery
851
+ ], outputs = [], queue = False, show_progress = False)
852
+
853
  result_gallery.change(on_select_result, [result_slider, result_gallery], result_slider)
854
  result_gallery.select(on_select_result, [result_slider, result_gallery], result_slider)
855
 
856
+ restart_button.click(fn = load_and_reset, inputs = [
857
+ param_setting
858
+ ], outputs = [
859
+ edm_steps,
860
+ s_cfg,
861
+ s_stage2,
862
+ s_stage1,
863
+ s_churn,
864
+ s_noise,
865
+ a_prompt,
866
+ n_prompt,
867
+ color_fix_type,
868
+ linear_CFG,
869
+ linear_s_stage2,
870
+ spt_linear_CFG,
871
+ spt_linear_s_stage2,
872
+ model_select
873
+ ])
874
+
875
+ reset_btn.click(fn = reset, inputs = [], outputs = [
 
 
 
 
 
876
  input_image,
877
+ rotation,
878
+ denoise_image,
879
  prompt,
880
  a_prompt,
881
  n_prompt,
 
894
  color_fix_type,
895
  diff_dtype,
896
  ae_dtype,
897
+ gamma_correction,
898
  linear_CFG,
899
  linear_s_stage2,
900
  spt_linear_CFG,
901
  spt_linear_s_stage2,
902
  model_select,
903
+ output_format,
904
  allocation
905
+ ], queue = False, show_progress = False)
906
+
 
 
 
907
  interface.queue(10).launch()