Spaces:
mikitona
/
Running on Zero

mikitona commited on
Commit
b080b67
·
verified ·
1 Parent(s): e94649d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +131 -439
app.py CHANGED
@@ -12,7 +12,6 @@ import spaces
12
  import re
13
  import uuid
14
 
15
- from gradio_imageslider import ImageSlider
16
  from PIL import Image
17
  from SUPIR.util import HWC3, upscale_image, fix_resize, convert_dtype, create_SUPIR_model, load_QF_ckpt
18
  from huggingface_hub import hf_hub_download
@@ -29,11 +28,11 @@ parser = argparse.ArgumentParser()
29
  parser.add_argument("--opt", type=str, default='options/SUPIR_v0.yaml')
30
  parser.add_argument("--ip", type=str, default='127.0.0.1')
31
  parser.add_argument("--port", type=int, default='6688')
32
- parser.add_argument("--no_llava", action='store_true', default=True)#False
33
- parser.add_argument("--use_image_slider", action='store_true', default=False)#False
34
  parser.add_argument("--log_history", action='store_true', default=False)
35
- parser.add_argument("--loading_half_params", action='store_true', default=True)#False
36
- parser.add_argument("--use_tile_vae", action='store_true', default=False)#False
37
  parser.add_argument("--encoder_tile_size", type=int, default=512)
38
  parser.add_argument("--decoder_tile_size", type=int, default=64)
39
  parser.add_argument("--load_8bit_llava", action='store_true', default=True)
@@ -70,9 +69,6 @@ elif torch.cuda.device_count() == 1:
70
  else:
71
  raise ValueError('Currently support CUDA only.')
72
 
73
-
74
-
75
-
76
  # load LLaVA
77
  if use_llava:
78
  llava_agent = LLavaAgent(LLAVA_MODEL_PATH, device=LLaVA_device, load_8bit=args.load_8bit_llava, load_4bit=False)
@@ -83,13 +79,15 @@ else:
83
  def check_upload(input_image):
84
  if input_image is None:
85
  raise gr.Error("Please provide an image to restore.")
86
- return gr.update(visible = True)
 
87
 
88
  def update_seed(is_randomize_seed, seed):
89
  if is_randomize_seed:
90
  return random.randint(0, max_64_bit_int)
91
  return seed
92
 
 
93
  def reset():
94
  return [
95
  None,
@@ -104,7 +102,7 @@ def reset():
104
  2,
105
  50,
106
  -1.0,
107
- 1.,
108
  default_setting.s_cfg_Quality if torch.cuda.device_count() > 0 else 1.0,
109
  True,
110
  random.randint(0, max_64_bit_int),
@@ -117,16 +115,18 @@ def reset():
117
  True,
118
  False,
119
  default_setting.spt_linear_CFG_Quality if torch.cuda.device_count() > 0 else 1.0,
120
- 0.,
121
  "v0-Q",
122
  "input",
123
  6
124
  ]
125
 
 
126
  def check(input_image):
127
  if input_image is None:
128
  raise gr.Error("Please provide an image to restore.")
129
 
 
130
  @spaces.GPU(duration=20)
131
  def stage1_process(
132
  input_image,
@@ -156,43 +156,17 @@ def stage1_process(
156
  LQ *= 255.0
157
  LQ = LQ.round().clip(0, 255).astype(np.uint8)
158
  print('<<== stage1_process')
159
- return LQ, gr.update(visible = True)
160
-
161
- from torch.cuda.amp import autocast # autocastをインポート
162
-
163
- @spaces.GPU(duration=20) # GPUを利用する関数にデコレーターを追加
164
- @torch.no_grad()
165
- def llave_process(input_image, temperature, top_p, qs=None):
166
- torch.cuda.set_device(LLaVA_device)
167
- with autocast(): # AMPを使用
168
- if use_llava and llava_agent is not None:
169
- LQ = HWC3(input_image)
170
- LQ = Image.fromarray(LQ.astype('uint8'))
171
- captions = llava_agent.gen_image_caption([LQ], temperature=temperature, top_p=top_p, qs=qs)
172
- else:
173
- captions = ['LLaVA is not available. Please add text manually.']
174
- torch.cuda.empty_cache() # メモリを解放
175
- return captions[0]
176
 
177
 
178
  def stage2_process(*args, **kwargs):
179
  try:
180
  return restore_in_Xmin(*args, **kwargs)
181
  except Exception as e:
182
- # NO_GPU_MESSAGE_INQUEUE
183
- print("gradio.exceptions.Error 'No GPU is currently available for you after 60s'")
184
- print('str(type(e)): ' + str(type(e))) # <class 'gradio.exceptions.Error'>
185
- print('str(e): ' + str(e)) # You have exceeded your GPU quota...
186
- try:
187
- print('e.message: ' + e.message) # No GPU is currently available for you after 60s
188
- except Exception as e2:
189
- print('Failure')
190
- if str(e).startswith("No GPU is currently available for you after 60s"):
191
- print('Exception identified!!!')
192
- #if str(type(e)) == "<class 'gradio.exceptions.Error'>":
193
- #print('Exception of name ' + type(e).__name__)
194
  raise e
195
 
 
196
  def restore_in_Xmin(
197
  noisy_image,
198
  rotation,
@@ -224,48 +198,22 @@ def restore_in_Xmin(
224
  output_format,
225
  allocation
226
  ):
227
- print("noisy_image:\n" + str(noisy_image))
228
- print("denoise_image:\n" + str(denoise_image))
229
- print("rotation: " + str(rotation))
230
- print("prompt: " + str(prompt))
231
- print("a_prompt: " + str(a_prompt))
232
- print("n_prompt: " + str(n_prompt))
233
- print("num_samples: " + str(num_samples))
234
- print("min_size: " + str(min_size))
235
- print("downscale: " + str(downscale))
236
- print("upscale: " + str(upscale))
237
- print("edm_steps: " + str(edm_steps))
238
- print("s_stage1: " + str(s_stage1))
239
- print("s_stage2: " + str(s_stage2))
240
- print("s_cfg: " + str(s_cfg))
241
- print("randomize_seed: " + str(randomize_seed))
242
- print("seed: " + str(seed))
243
- print("s_churn: " + str(s_churn))
244
- print("s_noise: " + str(s_noise))
245
- print("color_fix_type: " + str(color_fix_type))
246
- print("diff_dtype: " + str(diff_dtype))
247
- print("ae_dtype: " + str(ae_dtype))
248
- print("gamma_correction: " + str(gamma_correction))
249
- print("linear_CFG: " + str(linear_CFG))
250
- print("linear_s_stage2: " + str(linear_s_stage2))
251
- print("spt_linear_CFG: " + str(spt_linear_CFG))
252
- print("spt_linear_s_stage2: " + str(spt_linear_s_stage2))
253
- print("model_select: " + str(model_select))
254
- print("GPU time allocation: " + str(allocation) + " min")
255
- print("output_format: " + str(output_format))
256
 
257
  input_format = re.sub(r"^.*\.([^\.]+)$", r"\1", noisy_image)
258
 
259
  if input_format not in ['png', 'webp', 'jpg', 'jpeg', 'gif', 'bmp', 'heic']:
260
  gr.Warning('Invalid image format. Please first convert into *.png, *.webp, *.jpg, *.jpeg, *.gif, *.bmp or *.heic.')
261
- return None, None, None, None
262
 
263
  if output_format == "input":
264
  if noisy_image is None:
265
  output_format = "png"
266
  else:
267
  output_format = input_format
268
- print("final output_format: " + str(output_format))
269
 
270
  if prompt is None:
271
  prompt = ""
@@ -280,17 +228,16 @@ def restore_in_Xmin(
280
  a_prompt = prompt + ", " + a_prompt
281
  else:
282
  a_prompt = prompt + a_prompt
283
- print("Final prompt: " + str(a_prompt))
284
 
285
  denoise_image = np.array(Image.open(noisy_image if denoise_image is None else denoise_image))
286
 
287
  if rotation == 90:
288
- denoise_image = np.array(list(zip(*denoise_image[::-1])))
289
  elif rotation == 180:
290
- denoise_image = np.array(list(zip(*denoise_image[::-1])))
291
- denoise_image = np.array(list(zip(*denoise_image[::-1])))
292
  elif rotation == -90:
293
- denoise_image = np.array(list(zip(*denoise_image))[::-1])
294
 
295
  if 1 < downscale:
296
  input_height, input_width, input_channel = denoise_image.shape
@@ -300,10 +247,10 @@ def restore_in_Xmin(
300
 
301
  if torch.cuda.device_count() == 0:
302
  gr.Warning('Set this space to GPU config to make it work.')
303
- 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)
304
 
305
  if model_select != model.current_model:
306
- print('load ' + model_select)
307
  if model_select == 'v0-Q':
308
  model.load_state_dict(ckpt_Q, strict=False)
309
  elif model_select == 'v0-F':
@@ -316,175 +263,89 @@ def restore_in_Xmin(
316
  # Allocation
317
  if allocation == 1:
318
  return restore_in_1min(
319
- 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
320
- )
321
- if allocation == 2:
322
- return restore_in_2min(
323
- 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
324
- )
325
- if allocation == 3:
326
- return restore_in_3min(
327
- 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
328
- )
329
- if allocation == 4:
330
- return restore_in_4min(
331
- 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
332
- )
333
- if allocation == 5:
334
- return restore_in_5min(
335
- 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
336
- )
337
- if allocation == 7:
338
- return restore_in_7min(
339
- 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
340
- )
341
- if allocation == 8:
342
- return restore_in_8min(
343
- 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
344
  )
345
- if allocation == 9:
346
- return restore_in_9min(
347
- 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
348
- )
349
- if allocation == 10:
350
- return restore_in_10min(
351
- 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
352
- )
353
- else:
354
- return restore_in_6min(
355
- 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
356
- )
357
-
358
- @spaces.GPU(duration=59)
359
- def restore_in_1min(*args, **kwargs):
360
- return restore_on_gpu(*args, **kwargs)
361
-
362
- @spaces.GPU(duration=119)
363
- def restore_in_2min(*args, **kwargs):
364
- return restore_on_gpu(*args, **kwargs)
365
-
366
- @spaces.GPU(duration=179)
367
- def restore_in_3min(*args, **kwargs):
368
- return restore_on_gpu(*args, **kwargs)
369
-
370
- @spaces.GPU(duration=239)
371
- def restore_in_4min(*args, **kwargs):
372
- return restore_on_gpu(*args, **kwargs)
373
-
374
- @spaces.GPU(duration=299)
375
- def restore_in_5min(*args, **kwargs):
376
- return restore_on_gpu(*args, **kwargs)
377
 
378
- @spaces.GPU(duration=359)
379
- def restore_in_6min(*args, **kwargs):
380
- return restore_on_gpu(*args, **kwargs)
 
 
 
381
 
382
- @spaces.GPU(duration=419)
383
- def restore_in_7min(*args, **kwargs):
384
- return restore_on_gpu(*args, **kwargs)
385
 
386
- @spaces.GPU(duration=479)
387
- def restore_in_8min(*args, **kwargs):
388
  return restore_on_gpu(*args, **kwargs)
389
 
390
- @spaces.GPU(duration=539)
391
- def restore_in_9min(*args, **kwargs):
392
- return restore_on_gpu(*args, **kwargs)
393
 
394
- @spaces.GPU(duration=599)
395
- def restore_in_10min(*args, **kwargs):
396
- return restore_on_gpu(*args, **kwargs)
397
 
398
  def restore_on_gpu(
399
  noisy_image,
400
  input_image,
401
- prompt,
402
  a_prompt,
403
  n_prompt,
404
  num_samples,
405
  min_size,
406
- downscale,
407
  upscale,
408
  edm_steps,
409
  s_stage1,
410
  s_stage2,
411
  s_cfg,
412
- randomize_seed,
413
  seed,
414
  s_churn,
415
  s_noise,
416
  color_fix_type,
417
- diff_dtype,
418
- ae_dtype,
419
- gamma_correction,
420
  linear_CFG,
421
  linear_s_stage2,
422
  spt_linear_CFG,
423
  spt_linear_s_stage2,
424
- model_select,
425
- output_format,
426
- allocation
427
  ):
428
  start = time.time()
429
- print('restore ==>>')
430
 
431
  torch.cuda.set_device(SUPIR_device)
432
 
433
  with torch.no_grad():
434
  input_image = upscale_image(input_image, upscale, unit_resolution=32, min_size=min_size)
435
  LQ = np.array(input_image) / 255.0
436
- LQ = np.power(LQ, gamma_correction)
437
- LQ *= 255.0
438
  LQ = LQ.round().clip(0, 255).astype(np.uint8)
439
  LQ = LQ / 255 * 2 - 1
440
  LQ = torch.tensor(LQ, dtype=torch.float32).permute(2, 0, 1).unsqueeze(0).to(SUPIR_device)[:, :3, :, :]
441
  captions = ['']
442
 
443
- samples = model.batchify_sample(LQ, captions, num_steps=edm_steps, restoration_scale=s_stage1, s_churn=s_churn,
444
- s_noise=s_noise, cfg_scale=s_cfg, control_scale=s_stage2, seed=seed,
445
- num_samples=num_samples, p_p=a_prompt, n_p=n_prompt, color_fix_type=color_fix_type,
446
- use_linear_CFG=linear_CFG, use_linear_control_scale=linear_s_stage2,
447
- cfg_scale_start=spt_linear_CFG, control_scale_start=spt_linear_s_stage2)
 
 
448
 
449
  x_samples = (einops.rearrange(samples, 'b c h w -> b h w c') * 127.5 + 127.5).cpu().numpy().round().clip(
450
  0, 255).astype(np.uint8)
451
  results = [x_samples[i] for i in range(num_samples)]
452
  torch.cuda.empty_cache()
453
 
454
- # All the results have the same size
455
- input_height, input_width, input_channel = np.array(input_image).shape
456
- result_height, result_width, result_channel = np.array(results[0]).shape
457
 
458
- print('<<== restore')
459
  end = time.time()
460
- secondes = int(end - start)
461
- minutes = math.floor(secondes / 60)
462
- secondes = secondes - (minutes * 60)
463
- hours = math.floor(minutes / 60)
464
- minutes = minutes - (hours * 60)
465
- information = ("Start the process again if you want a different result. " if randomize_seed else "") + \
466
- "If you don't get the image you wanted, add more details in the « Image description ». " + \
467
- "Wait " + str(allocation) + " min before a new run to avoid quota penalty or use another computer. " + \
468
- "The image" + (" has" if len(results) == 1 else "s have") + " been generated in " + \
469
- ((str(hours) + " h, ") if hours != 0 else "") + \
470
- ((str(minutes) + " min, ") if hours != 0 or minutes != 0 else "") + \
471
- str(secondes) + " sec. " + \
472
- "The new image resolution is " + str(result_width) + \
473
- " pixels large and " + str(result_height) + \
474
- " pixels high, so a resolution of " + f'{result_width * result_height:,}' + " pixels."
475
  print(information)
476
- try:
477
- print("Initial resolution: " + f'{input_width * input_height:,}')
478
- print("Final resolution: " + f'{result_width * result_height:,}')
479
- print("edm_steps: " + str(edm_steps))
480
- print("num_samples: " + str(num_samples))
481
- print("downscale: " + str(downscale))
482
- 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:,}')
483
- except Exception as e:
484
- print('Exception of Estimation')
485
 
486
- # Only one image can be shown in the slider
487
- 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)
488
 
489
  def load_and_reset(param_setting):
490
  print('load_and_reset ==>>')
@@ -521,36 +382,6 @@ def load_and_reset(param_setting):
521
  return edm_steps, s_cfg, s_stage2, s_stage1, s_churn, s_noise, a_prompt, n_prompt, color_fix_type, linear_CFG, \
522
  linear_s_stage2, spt_linear_CFG, spt_linear_s_stage2, model_select
523
 
524
- def log_information(result_gallery):
525
- print('log_information')
526
- if result_gallery is not None:
527
- for i, result in enumerate(result_gallery):
528
- print(result[0])
529
- '''
530
-
531
- def on_select_result(result_slider, result_gallery, evt: gr.SelectData):
532
- print('on_select_result')
533
- if result_gallery is not None:
534
- for i, result in enumerate(result_gallery):
535
- print(result[0])
536
- return [result_slider[0], result_gallery[evt.index][0]]
537
- '''
538
- def on_select_result(result_slider, result_gallery, evt: gr.SelectData):
539
- print('on_select_result')
540
- if result_gallery is not None:
541
- for i, result in enumerate(result_gallery):
542
- print(result[0])
543
- if 0 <= evt.index < len(result_gallery): # indexが有効な範囲内かチェック
544
- return [result_slider[0], result_gallery[evt.index][0]]
545
- else:
546
- print("Invalid event index value")
547
- return [None, None] # indexが無効な場合のデフォルト値
548
- else:
549
- print("result_gallery is None")
550
- return [None, None] # result_galleryがNoneの場合のデフォルト値
551
-
552
-
553
-
554
 
555
  title_html = """
556
  <h1><center>SUPIR</center></h1>
@@ -562,7 +393,7 @@ title_html = """
562
  SUPIR is for beauty and illustration only.
563
  Most of the processes last few minutes.
564
  If you want to upscale AI-generated images, be noticed that <i>PixArt Sigma</i> space can directly generate 5984x5984 images.
565
- Due to Gradio issues, the generated image is slightly less satured than the original.
566
  Please leave a <a href="https://huggingface.co/spaces/Fabrice-TIERCELIN/SUPIR/discussions/new">message in discussion</a> if you encounter issues.
567
  You can also use <a href="https://huggingface.co/spaces/gokaygokay/AuraSR">AuraSR</a> to upscale x4.
568
 
@@ -570,7 +401,6 @@ title_html = """
570
  <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>
571
  """
572
 
573
-
574
  claim_md = """
575
  ## **Piracy**
576
  The images are not stored but the logs are saved during a month.
@@ -600,35 +430,28 @@ with gr.Blocks() as interface:
600
  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)
601
  with gr.Group():
602
  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)
603
- 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.")
604
  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)
605
- 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)
606
  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=3, interactive=True)
607
 
608
  with gr.Accordion("Pre-denoising (optional)", open=False):
609
- gamma_correction = gr.Slider(label="Gamma Correction", info = "lower=lighter, higher=darker", minimum=0.1, maximum=2.0, value=1.0, step=0.1)
610
  denoise_button = gr.Button(value="Pre-denoise")
611
- denoise_image = gr.Image(label="Denoised image", show_label=True, type="filepath", sources=[], interactive = False, height=600, elem_id="image-s1")
612
  denoise_information = gr.HTML(value="If present, the denoised image will be used for the restoration instead of the input image.", visible=False)
613
 
614
  with gr.Accordion("Advanced options", open=False):
615
  a_prompt = gr.Textbox(label="Additional image description",
616
  info="Completes the main image description",
617
- value='Cinematic, High Contrast, highly detailed, taken using a Canon EOS R '
618
- 'camera, hyper detailed photo - realistic maximum detail, 32k, Color '
619
- 'Grading, ultra HD, extreme meticulous detailing, skin pore detailing, '
620
- 'hyper sharpness, perfect without deformations.',
621
  lines=3)
622
  n_prompt = gr.Textbox(label="Negative image description",
623
  info="Disambiguate by listing what the image does NOT represent",
624
- value='painting, oil painting, illustration, drawing, art, sketch, anime, '
625
- 'cartoon, CG Style, 3D render, unreal engine, blurring, aliasing, unsharp, weird textures, ugly, dirty, messy, '
626
- 'worst quality, low quality, frames, watermark, signature, jpeg artifacts, '
627
- 'deformed, lowres, over-smooth',
628
  lines=3)
629
  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)
630
- num_samples = gr.Slider(label="Num Samples", info="Number of generated results", minimum=1, maximum=4 if not args.use_image_slider else 1
631
- , value=1, step=1)
632
  min_size = gr.Slider(label="Minimum size", info="Minimum height, minimum width of the result", minimum=32, maximum=4096, value=1024, step=32)
633
  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)
634
  with gr.Row():
@@ -640,7 +463,7 @@ with gr.Blocks() as interface:
640
  interactive=True)
641
  s_cfg = gr.Slider(label="Text Guidance Scale", info="lower=follow the image, higher=follow the prompt", minimum=1.0, maximum=15.0,
642
  value=default_setting.s_cfg_Quality if torch.cuda.device_count() > 0 else 1.0, step=0.1)
643
- s_stage2 = gr.Slider(label="Restoring Guidance Strength", minimum=0., maximum=1., value=1., step=0.05)
644
  s_stage1 = gr.Slider(label="Pre-denoising Guidance Strength", minimum=-1.0, maximum=6.0, value=-1.0, step=1.0)
645
  s_churn = gr.Slider(label="S-Churn", minimum=0, maximum=40, value=5, step=1)
646
  s_noise = gr.Slider(label="S-Noise", minimum=1.0, maximum=1.1, value=1.003, step=0.001)
@@ -651,158 +474,34 @@ with gr.Blocks() as interface:
651
  maximum=9.0, value=default_setting.spt_linear_CFG_Quality if torch.cuda.device_count() > 0 else 1.0, step=0.5)
652
  with gr.Column():
653
  linear_s_stage2 = gr.Checkbox(label="Linear Restoring Guidance", value=False)
654
- spt_linear_s_stage2 = gr.Slider(label="Guidance Start", minimum=0.,
655
- maximum=1., value=0., step=0.05)
656
  with gr.Column():
657
  diff_dtype = gr.Radio([["fp32 (precision)", "fp32"], ["fp16 (medium)", "fp16"], ["bf16 (speed)", "bf16"]], label="Diffusion Data Type", value="fp32",
658
  interactive=True)
659
  with gr.Column():
660
  ae_dtype = gr.Radio([["fp32 (precision)", "fp32"], ["bf16 (speed)", "bf16"]], label="Auto-Encoder Data Type", value="fp32",
661
  interactive=True)
662
- randomize_seed = gr.Checkbox(label = "\U0001F3B2 Randomize seed", value = True, info = "If checked, result is always different")
663
  seed = gr.Slider(label="Seed", minimum=0, maximum=max_64_bit_int, step=1, randomize=True)
664
  with gr.Group():
665
- param_setting = gr.Radio(["Quality", "Fidelity"], interactive=True, label="Presetting", value = "Quality")
666
  restart_button = gr.Button(value="Apply presetting")
667
 
668
  with gr.Column():
669
- diffusion_button = gr.Button(value="🚀 Upscale/Restore", variant = "primary", elem_id = "process_button")
670
- reset_btn = gr.Button(value="🧹 Reinit page", variant="stop", elem_id="reset_button", visible = False)
671
 
672
- restore_information = gr.HTML(value = "Restart the process to get another result.", visible = False)
673
- result_slider = ImageSlider(label = 'Comparator', show_label = False, interactive = False, elem_id = "slider1", show_download_button = False)
674
- result_gallery = gr.Gallery(label = 'Downloadable results', show_label = True, interactive = False, elem_id = "gallery1")
675
 
676
  gr.Examples(
677
- examples = [
678
- [
679
- "./Examples/Example1.png",
680
- 0,
681
- None,
682
- "Group of people, walking, happy, in the street, photorealistic, 8k, extremely detailled",
683
- "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.",
684
- "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",
685
- 2,
686
- 1024,
687
- 1,
688
- 8,
689
- 200,
690
- -1,
691
- 1,
692
- 7.5,
693
- False,
694
- 42,
695
- 5,
696
- 1.003,
697
- "AdaIn",
698
- "fp16",
699
- "bf16",
700
- 1.0,
701
- True,
702
- 4,
703
- False,
704
- 0.,
705
- "v0-Q",
706
- "input",
707
- 5
708
- ],
709
- [
710
- "./Examples/Example2.jpeg",
711
- 0,
712
- None,
713
- "La cabeza de un gato atigrado, en una casa, fotorrealista, 8k, extremadamente detallada",
714
- "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.",
715
- "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",
716
- 1,
717
- 1024,
718
- 1,
719
- 1,
720
- 200,
721
- -1,
722
- 1,
723
- 7.5,
724
- False,
725
- 42,
726
- 5,
727
- 1.003,
728
- "Wavelet",
729
- "fp16",
730
- "bf16",
731
- 1.0,
732
- True,
733
- 4,
734
- False,
735
- 0.,
736
- "v0-Q",
737
- "input",
738
- 4
739
- ],
740
- [
741
- "./Examples/Example3.webp",
742
- 0,
743
- None,
744
- "A red apple",
745
- "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.",
746
- "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",
747
- 1,
748
- 1024,
749
- 1,
750
- 1,
751
- 200,
752
- -1,
753
- 1,
754
- 7.5,
755
- False,
756
- 42,
757
- 5,
758
- 1.003,
759
- "Wavelet",
760
- "fp16",
761
- "bf16",
762
- 1.0,
763
- True,
764
- 4,
765
- False,
766
- 0.,
767
- "v0-Q",
768
- "input",
769
- 4
770
- ],
771
- [
772
- "./Examples/Example3.webp",
773
- 0,
774
- None,
775
- "A red marble",
776
- "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.",
777
- "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",
778
- 1,
779
- 1024,
780
- 1,
781
- 1,
782
- 200,
783
- -1,
784
- 1,
785
- 7.5,
786
- False,
787
- 42,
788
- 5,
789
- 1.003,
790
- "Wavelet",
791
- "fp16",
792
- "bf16",
793
- 1.0,
794
- True,
795
- 4,
796
- False,
797
- 0.,
798
- "v0-Q",
799
- "input",
800
- 4
801
- ],
802
- ],
803
- run_on_click = True,
804
- fn = stage2_process,
805
- inputs = [
806
  input_image,
807
  rotation,
808
  denoise_image,
@@ -833,27 +532,26 @@ with gr.Blocks() as interface:
833
  output_format,
834
  allocation
835
  ],
836
- outputs = [
837
- result_slider,
838
- result_gallery,
839
  restore_information,
840
  reset_btn
841
  ],
842
- cache_examples = False,
843
  )
844
 
845
  with gr.Row():
846
  gr.Markdown(claim_md)
847
-
848
- input_image.upload(fn = check_upload, inputs = [
849
  input_image
850
- ], outputs = [
851
  rotation
852
- ], queue = False, show_progress = False)
853
 
854
- denoise_button.click(fn = check, inputs = [
855
  input_image
856
- ], outputs = [], queue = False, show_progress = False).success(fn = stage1_process, inputs = [
857
  input_image,
858
  gamma_correction,
859
  diff_dtype,
@@ -863,14 +561,14 @@ with gr.Blocks() as interface:
863
  denoise_information
864
  ])
865
 
866
- diffusion_button.click(fn = update_seed, inputs = [
867
  randomize_seed,
868
  seed
869
- ], outputs = [
870
  seed
871
- ], queue = False, show_progress = False).then(fn = check, inputs = [
872
  input_image
873
- ], outputs = [], queue = False, show_progress = False).success(fn=stage2_process, inputs = [
874
  input_image,
875
  rotation,
876
  denoise_image,
@@ -900,21 +598,15 @@ with gr.Blocks() as interface:
900
  model_select,
901
  output_format,
902
  allocation
903
- ], outputs = [
904
- result_slider,
905
- result_gallery,
906
  restore_information,
907
  reset_btn
908
- ]).success(fn = log_information, inputs = [
909
- result_gallery
910
- ], outputs = [], queue = False, show_progress = False)
911
-
912
- result_gallery.change(on_select_result, [result_slider, result_gallery], result_slider)
913
- result_gallery.select(on_select_result, [result_slider, result_gallery], result_slider)
914
 
915
- restart_button.click(fn = load_and_reset, inputs = [
916
  param_setting
917
- ], outputs = [
918
  edm_steps,
919
  s_cfg,
920
  s_stage2,
@@ -931,36 +623,36 @@ with gr.Blocks() as interface:
931
  model_select
932
  ])
933
 
934
- reset_btn.click(fn = reset, inputs = [], outputs = [
935
- input_image,
936
- rotation,
937
- denoise_image,
938
- prompt,
939
- a_prompt,
940
- n_prompt,
941
- num_samples,
942
- min_size,
943
- downscale,
944
- upscale,
945
- edm_steps,
946
- s_stage1,
947
- s_stage2,
948
- s_cfg,
949
- randomize_seed,
950
- seed,
951
- s_churn,
952
- s_noise,
953
- color_fix_type,
954
- diff_dtype,
955
- ae_dtype,
956
- gamma_correction,
957
- linear_CFG,
958
- linear_s_stage2,
959
- spt_linear_CFG,
960
- spt_linear_s_stage2,
961
- model_select,
962
- output_format,
963
- allocation
964
- ], queue = False, show_progress = False)
965
-
966
- interface.queue(10).launch(show_error=True)
 
12
  import re
13
  import uuid
14
 
 
15
  from PIL import Image
16
  from SUPIR.util import HWC3, upscale_image, fix_resize, convert_dtype, create_SUPIR_model, load_QF_ckpt
17
  from huggingface_hub import hf_hub_download
 
28
  parser.add_argument("--opt", type=str, default='options/SUPIR_v0.yaml')
29
  parser.add_argument("--ip", type=str, default='127.0.0.1')
30
  parser.add_argument("--port", type=int, default='6688')
31
+ parser.add_argument("--no_llava", action='store_true', default=True) # False
32
+ parser.add_argument("--use_image_slider", action='store_true', default=False) # False
33
  parser.add_argument("--log_history", action='store_true', default=False)
34
+ parser.add_argument("--loading_half_params", action='store_true', default=True) # False
35
+ parser.add_argument("--use_tile_vae", action='store_true', default=False) # False
36
  parser.add_argument("--encoder_tile_size", type=int, default=512)
37
  parser.add_argument("--decoder_tile_size", type=int, default=64)
38
  parser.add_argument("--load_8bit_llava", action='store_true', default=True)
 
69
  else:
70
  raise ValueError('Currently support CUDA only.')
71
 
 
 
 
72
  # load LLaVA
73
  if use_llava:
74
  llava_agent = LLavaAgent(LLAVA_MODEL_PATH, device=LLaVA_device, load_8bit=args.load_8bit_llava, load_4bit=False)
 
79
  def check_upload(input_image):
80
  if input_image is None:
81
  raise gr.Error("Please provide an image to restore.")
82
+ return gr.update(visible=True)
83
+
84
 
85
  def update_seed(is_randomize_seed, seed):
86
  if is_randomize_seed:
87
  return random.randint(0, max_64_bit_int)
88
  return seed
89
 
90
+
91
  def reset():
92
  return [
93
  None,
 
102
  2,
103
  50,
104
  -1.0,
105
+ 1.0,
106
  default_setting.s_cfg_Quality if torch.cuda.device_count() > 0 else 1.0,
107
  True,
108
  random.randint(0, max_64_bit_int),
 
115
  True,
116
  False,
117
  default_setting.spt_linear_CFG_Quality if torch.cuda.device_count() > 0 else 1.0,
118
+ 0.0,
119
  "v0-Q",
120
  "input",
121
  6
122
  ]
123
 
124
+
125
  def check(input_image):
126
  if input_image is None:
127
  raise gr.Error("Please provide an image to restore.")
128
 
129
+
130
  @spaces.GPU(duration=20)
131
  def stage1_process(
132
  input_image,
 
156
  LQ *= 255.0
157
  LQ = LQ.round().clip(0, 255).astype(np.uint8)
158
  print('<<== stage1_process')
159
+ return LQ, gr.update(visible=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
160
 
161
 
162
  def stage2_process(*args, **kwargs):
163
  try:
164
  return restore_in_Xmin(*args, **kwargs)
165
  except Exception as e:
166
+ print("An error occurred during processing:", str(e))
 
 
 
 
 
 
 
 
 
 
 
167
  raise e
168
 
169
+
170
  def restore_in_Xmin(
171
  noisy_image,
172
  rotation,
 
198
  output_format,
199
  allocation
200
  ):
201
+ # 省略可能な詳細なログを出力
202
+ print("Starting restoration process...")
203
+ # 以下、省略: パラメータの表示や前処理
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
204
 
205
  input_format = re.sub(r"^.*\.([^\.]+)$", r"\1", noisy_image)
206
 
207
  if input_format not in ['png', 'webp', 'jpg', 'jpeg', 'gif', 'bmp', 'heic']:
208
  gr.Warning('Invalid image format. Please first convert into *.png, *.webp, *.jpg, *.jpeg, *.gif, *.bmp or *.heic.')
209
+ return None, None, None
210
 
211
  if output_format == "input":
212
  if noisy_image is None:
213
  output_format = "png"
214
  else:
215
  output_format = input_format
216
+ print("Final output_format:", output_format)
217
 
218
  if prompt is None:
219
  prompt = ""
 
228
  a_prompt = prompt + ", " + a_prompt
229
  else:
230
  a_prompt = prompt + a_prompt
231
+ print("Final prompt:", a_prompt)
232
 
233
  denoise_image = np.array(Image.open(noisy_image if denoise_image is None else denoise_image))
234
 
235
  if rotation == 90:
236
+ denoise_image = np.rot90(denoise_image, k=-1)
237
  elif rotation == 180:
238
+ denoise_image = np.rot90(denoise_image, k=2)
 
239
  elif rotation == -90:
240
+ denoise_image = np.rot90(denoise_image, k=1)
241
 
242
  if 1 < downscale:
243
  input_height, input_width, input_channel = denoise_image.shape
 
247
 
248
  if torch.cuda.device_count() == 0:
249
  gr.Warning('Set this space to GPU config to make it work.')
250
+ return denoise_image, gr.update(value="No GPU available.", visible=True), gr.update(visible=True)
251
 
252
  if model_select != model.current_model:
253
+ print('Loading model:', model_select)
254
  if model_select == 'v0-Q':
255
  model.load_state_dict(ckpt_Q, strict=False)
256
  elif model_select == 'v0-F':
 
263
  # Allocation
264
  if allocation == 1:
265
  return restore_in_1min(
266
+ noisy_image, denoise_image, a_prompt, n_prompt, num_samples, min_size, upscale, edm_steps, s_stage1,
267
+ s_stage2, s_cfg, seed, s_churn, s_noise, color_fix_type, linear_CFG, linear_s_stage2, spt_linear_CFG,
268
+ spt_linear_s_stage2, output_format
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
269
  )
270
+ # 省略: 他のallocation条件
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
271
 
272
+ # デフォルトの処理
273
+ return restore_in_3min(
274
+ noisy_image, denoise_image, a_prompt, n_prompt, num_samples, min_size, upscale, edm_steps, s_stage1,
275
+ s_stage2, s_cfg, seed, s_churn, s_noise, color_fix_type, linear_CFG, linear_s_stage2, spt_linear_CFG,
276
+ spt_linear_s_stage2, output_format
277
+ )
278
 
 
 
 
279
 
280
+ @spaces.GPU(duration=59)
281
+ def restore_in_1min(*args, **kwargs):
282
  return restore_on_gpu(*args, **kwargs)
283
 
 
 
 
284
 
285
+ # 他のrestore_in_Xmin関数も同様に定義
 
 
286
 
287
  def restore_on_gpu(
288
  noisy_image,
289
  input_image,
 
290
  a_prompt,
291
  n_prompt,
292
  num_samples,
293
  min_size,
 
294
  upscale,
295
  edm_steps,
296
  s_stage1,
297
  s_stage2,
298
  s_cfg,
 
299
  seed,
300
  s_churn,
301
  s_noise,
302
  color_fix_type,
 
 
 
303
  linear_CFG,
304
  linear_s_stage2,
305
  spt_linear_CFG,
306
  spt_linear_s_stage2,
307
+ output_format
 
 
308
  ):
309
  start = time.time()
310
+ print('Starting GPU restoration...')
311
 
312
  torch.cuda.set_device(SUPIR_device)
313
 
314
  with torch.no_grad():
315
  input_image = upscale_image(input_image, upscale, unit_resolution=32, min_size=min_size)
316
  LQ = np.array(input_image) / 255.0
317
+ LQ = (LQ ** gamma_correction) * 255.0
 
318
  LQ = LQ.round().clip(0, 255).astype(np.uint8)
319
  LQ = LQ / 255 * 2 - 1
320
  LQ = torch.tensor(LQ, dtype=torch.float32).permute(2, 0, 1).unsqueeze(0).to(SUPIR_device)[:, :3, :, :]
321
  captions = ['']
322
 
323
+ samples = model.batchify_sample(
324
+ LQ, captions, num_steps=edm_steps, restoration_scale=s_stage1, s_churn=s_churn,
325
+ s_noise=s_noise, cfg_scale=s_cfg, control_scale=s_stage2, seed=seed,
326
+ num_samples=num_samples, p_p=a_prompt, n_p=n_prompt, color_fix_type=color_fix_type,
327
+ use_linear_CFG=linear_CFG, use_linear_control_scale=linear_s_stage2,
328
+ cfg_scale_start=spt_linear_CFG, control_scale_start=spt_linear_s_stage2
329
+ )
330
 
331
  x_samples = (einops.rearrange(samples, 'b c h w -> b h w c') * 127.5 + 127.5).cpu().numpy().round().clip(
332
  0, 255).astype(np.uint8)
333
  results = [x_samples[i] for i in range(num_samples)]
334
  torch.cuda.empty_cache()
335
 
336
+ # 結果の処理
337
+ result_image = results[0]
338
+ result_pil = Image.fromarray(result_image)
339
 
 
340
  end = time.time()
341
+ elapsed_time = end - start
342
+ information = f"Processing completed in {elapsed_time:.2f} seconds."
343
+
 
 
 
 
 
 
 
 
 
 
 
 
344
  print(information)
345
+ print('GPU restoration completed.')
346
+
347
+ return result_pil, gr.update(value=information, visible=True), gr.update(visible=True)
 
 
 
 
 
 
348
 
 
 
349
 
350
  def load_and_reset(param_setting):
351
  print('load_and_reset ==>>')
 
382
  return edm_steps, s_cfg, s_stage2, s_stage1, s_churn, s_noise, a_prompt, n_prompt, color_fix_type, linear_CFG, \
383
  linear_s_stage2, spt_linear_CFG, spt_linear_s_stage2, model_select
384
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
385
 
386
  title_html = """
387
  <h1><center>SUPIR</center></h1>
 
393
  SUPIR is for beauty and illustration only.
394
  Most of the processes last few minutes.
395
  If you want to upscale AI-generated images, be noticed that <i>PixArt Sigma</i> space can directly generate 5984x5984 images.
396
+ Due to Gradio issues, the generated image is slightly less saturated than the original.
397
  Please leave a <a href="https://huggingface.co/spaces/Fabrice-TIERCELIN/SUPIR/discussions/new">message in discussion</a> if you encounter issues.
398
  You can also use <a href="https://huggingface.co/spaces/gokaygokay/AuraSR">AuraSR</a> to upscale x4.
399
 
 
401
  <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>
402
  """
403
 
 
404
  claim_md = """
405
  ## **Piracy**
406
  The images are not stored but the logs are saved during a month.
 
430
  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)
431
  with gr.Group():
432
  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)
433
+ 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.")
434
  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)
435
+ output_format = gr.Radio([["As input", "input"], ["*.png", "png"], ["*.webp", "webp"], ["*.jpeg", "jpeg"], ["*.gif", "gif"], ["*.bmp", "bmp"]], label="Image format for result", info="File extension", value="input", interactive=True)
436
  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=3, interactive=True)
437
 
438
  with gr.Accordion("Pre-denoising (optional)", open=False):
439
+ gamma_correction = gr.Slider(label="Gamma Correction", info="lower=lighter, higher=darker", minimum=0.1, maximum=2.0, value=1.0, step=0.1)
440
  denoise_button = gr.Button(value="Pre-denoise")
441
+ denoise_image = gr.Image(label="Denoised image", show_label=True, type="filepath", sources=[], interactive=False, height=600, elem_id="image-s1")
442
  denoise_information = gr.HTML(value="If present, the denoised image will be used for the restoration instead of the input image.", visible=False)
443
 
444
  with gr.Accordion("Advanced options", open=False):
445
  a_prompt = gr.Textbox(label="Additional image description",
446
  info="Completes the main image description",
447
+ value='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.',
 
 
 
448
  lines=3)
449
  n_prompt = gr.Textbox(label="Negative image description",
450
  info="Disambiguate by listing what the image does NOT represent",
451
+ value='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',
 
 
 
452
  lines=3)
453
  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)
454
+ num_samples = gr.Slider(label="Num Samples", info="Number of generated results", minimum=1, maximum=1, value=1, step=1)
 
455
  min_size = gr.Slider(label="Minimum size", info="Minimum height, minimum width of the result", minimum=32, maximum=4096, value=1024, step=32)
456
  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)
457
  with gr.Row():
 
463
  interactive=True)
464
  s_cfg = gr.Slider(label="Text Guidance Scale", info="lower=follow the image, higher=follow the prompt", minimum=1.0, maximum=15.0,
465
  value=default_setting.s_cfg_Quality if torch.cuda.device_count() > 0 else 1.0, step=0.1)
466
+ s_stage2 = gr.Slider(label="Restoring Guidance Strength", minimum=0.0, maximum=1.0, value=1.0, step=0.05)
467
  s_stage1 = gr.Slider(label="Pre-denoising Guidance Strength", minimum=-1.0, maximum=6.0, value=-1.0, step=1.0)
468
  s_churn = gr.Slider(label="S-Churn", minimum=0, maximum=40, value=5, step=1)
469
  s_noise = gr.Slider(label="S-Noise", minimum=1.0, maximum=1.1, value=1.003, step=0.001)
 
474
  maximum=9.0, value=default_setting.spt_linear_CFG_Quality if torch.cuda.device_count() > 0 else 1.0, step=0.5)
475
  with gr.Column():
476
  linear_s_stage2 = gr.Checkbox(label="Linear Restoring Guidance", value=False)
477
+ spt_linear_s_stage2 = gr.Slider(label="Guidance Start", minimum=0.0,
478
+ maximum=1.0, value=0.0, step=0.05)
479
  with gr.Column():
480
  diff_dtype = gr.Radio([["fp32 (precision)", "fp32"], ["fp16 (medium)", "fp16"], ["bf16 (speed)", "bf16"]], label="Diffusion Data Type", value="fp32",
481
  interactive=True)
482
  with gr.Column():
483
  ae_dtype = gr.Radio([["fp32 (precision)", "fp32"], ["bf16 (speed)", "bf16"]], label="Auto-Encoder Data Type", value="fp32",
484
  interactive=True)
485
+ randomize_seed = gr.Checkbox(label="\U0001F3B2 Randomize seed", value=True, info="If checked, result is always different")
486
  seed = gr.Slider(label="Seed", minimum=0, maximum=max_64_bit_int, step=1, randomize=True)
487
  with gr.Group():
488
+ param_setting = gr.Radio(["Quality", "Fidelity"], interactive=True, label="Presetting", value="Quality")
489
  restart_button = gr.Button(value="Apply presetting")
490
 
491
  with gr.Column():
492
+ diffusion_button = gr.Button(value="🚀 Upscale/Restore", variant="primary", elem_id="process_button")
493
+ reset_btn = gr.Button(value="🧹 Reinit page", variant="stop", elem_id="reset_button", visible=False)
494
 
495
+ restore_information = gr.HTML(value="Restart the process to get another result.", visible=False)
496
+ result_image = gr.Image(label='Result Image', show_label=True, interactive=False, elem_id="result_image")
 
497
 
498
  gr.Examples(
499
+ examples=[
500
+ # 例を適宜追加または削除
501
+ ],
502
+ run_on_click=True,
503
+ fn=stage2_process,
504
+ inputs=[
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
505
  input_image,
506
  rotation,
507
  denoise_image,
 
532
  output_format,
533
  allocation
534
  ],
535
+ outputs=[
536
+ result_image,
 
537
  restore_information,
538
  reset_btn
539
  ],
540
+ cache_examples=False,
541
  )
542
 
543
  with gr.Row():
544
  gr.Markdown(claim_md)
545
+
546
+ input_image.upload(fn=check_upload, inputs=[
547
  input_image
548
+ ], outputs=[
549
  rotation
550
+ ], queue=False, show_progress=False)
551
 
552
+ denoise_button.click(fn=check, inputs=[
553
  input_image
554
+ ], outputs=[], queue=False, show_progress=False).success(fn=stage1_process, inputs=[
555
  input_image,
556
  gamma_correction,
557
  diff_dtype,
 
561
  denoise_information
562
  ])
563
 
564
+ diffusion_button.click(fn=update_seed, inputs=[
565
  randomize_seed,
566
  seed
567
+ ], outputs=[
568
  seed
569
+ ], queue=False, show_progress=False).then(fn=check, inputs=[
570
  input_image
571
+ ], outputs=[], queue=False, show_progress=False).success(fn=stage2_process, inputs=[
572
  input_image,
573
  rotation,
574
  denoise_image,
 
598
  model_select,
599
  output_format,
600
  allocation
601
+ ], outputs=[
602
+ result_image,
 
603
  restore_information,
604
  reset_btn
605
+ ])
 
 
 
 
 
606
 
607
+ restart_button.click(fn=load_and_reset, inputs=[
608
  param_setting
609
+ ], outputs=[
610
  edm_steps,
611
  s_cfg,
612
  s_stage2,
 
623
  model_select
624
  ])
625
 
626
+ reset_btn.click(fn=reset, inputs=[], outputs=[
627
+ input_image,
628
+ rotation,
629
+ denoise_image,
630
+ prompt,
631
+ a_prompt,
632
+ n_prompt,
633
+ num_samples,
634
+ min_size,
635
+ downscale,
636
+ upscale,
637
+ edm_steps,
638
+ s_stage1,
639
+ s_stage2,
640
+ s_cfg,
641
+ randomize_seed,
642
+ seed,
643
+ s_churn,
644
+ s_noise,
645
+ color_fix_type,
646
+ diff_dtype,
647
+ ae_dtype,
648
+ gamma_correction,
649
+ linear_CFG,
650
+ linear_s_stage2,
651
+ spt_linear_CFG,
652
+ spt_linear_s_stage2,
653
+ model_select,
654
+ output_format,
655
+ allocation
656
+ ], queue=False, show_progress=False)
657
+
658
+ interface.queue(max_size=10).launch(show_error=True)