Fabrice-TIERCELIN commited on
Commit
7813cdf
1 Parent(s): c3029a6
Files changed (1) hide show
  1. gradio_demo.py +30 -15
gradio_demo.py CHANGED
@@ -65,10 +65,18 @@ if torch.cuda.device_count() > 0:
65
  else:
66
  llava_agent = None
67
 
 
 
 
 
 
68
  def check(input_image):
69
  if input_image is None:
70
  raise gr.Error("Please provide an image to restore.")
71
 
 
 
 
72
  @spaces.GPU(duration=180)
73
  def stage1_process(input_image, gamma_correction):
74
  print('Start stage1_process')
@@ -114,7 +122,7 @@ def stage2_process(input_image, prompt, a_prompt, n_prompt, num_samples, upscale
114
  print('Start stage2_process')
115
  if torch.cuda.device_count() == 0:
116
  gr.Warning('Set this space to GPU config to make it work.')
117
- return None, None, None, None
118
  torch.cuda.set_device(SUPIR_device)
119
  event_id = str(time.time_ns())
120
  event_dict = {'event_id': event_id, 'localtime': time.ctime(), 'prompt': prompt, 'a_prompt': a_prompt,
@@ -169,7 +177,7 @@ def stage2_process(input_image, prompt, a_prompt, n_prompt, num_samples, upscale
169
  for i, result in enumerate(results):
170
  Image.fromarray(result).save(f'./history/{event_id[:5]}/{event_id[5:]}/HQ_{i}.png')
171
  print('End stage2_process')
172
- return [input_image] + results, event_id, 3, ''
173
 
174
  def load_and_reset(param_setting):
175
  print('Start load_and_reset')
@@ -231,10 +239,10 @@ else:
231
  title_md = """
232
  <h1><center>SUPIR Image Upscaler</center></h1>
233
 
234
- SUPIR is a practicing model scaling for photo-realistic image restoration. It is still a research project under tested and is not yet a stable commercial product.
235
 
236
- <a href="https://arxiv.org/abs/2401.13627">Paper</a> &emsp; <a href="http://supir.xpixel.group/">Project Page</a> &emsp; <a href="https://github.com/Fanghua-Yu/SUPIR/blob/master/assets/DemoGuide.png">How to play</a> &emsp; <a href="https://huggingface.co/blog/MonsterMMORPG/supir-sota-image-upscale-better-than-magnific-ai">Local Install Guide</a>
237
- <p style="background-color: blue;">For now, only the restoring is working (the most important one). The pre-denoising and LLaVa description are failing. LLaVa is disabled.</p>
238
  """
239
 
240
 
@@ -249,7 +257,7 @@ The service is a research preview intended for non-commercial use only, subject
249
  """
250
 
251
  # Gradio interface
252
- with gr.Blocks(title='SUPIR') as interface:
253
  with gr.Row():
254
  gr.HTML(title_md)
255
 
@@ -275,13 +283,14 @@ with gr.Blocks(title='SUPIR') as interface:
275
  with gr.Accordion("Restoring options", open=False):
276
  num_samples = gr.Slider(label="Num Samples", info="Number of generated results; I discourage to increase because the process is limited to 3 min", minimum=1, maximum=4 if not args.use_image_slider else 1
277
  , value=1, step=1)
278
- upscale = gr.Slider(label="Upscale", info="The resolution increase factor", minimum=1, maximum=8, value=1, step=1)
279
  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)
280
  s_cfg = gr.Slider(label="Text Guidance Scale", info="lower=follow the image, higher=follow the prompt", minimum=1.0, maximum=15.0,
281
  value=default_setting.s_cfg_Quality if torch.cuda.device_count() > 0 else 1.0, step=0.1)
282
  s_stage2 = gr.Slider(label="Restoring Guidance Strength", minimum=0., maximum=1., value=1., step=0.05)
283
  s_stage1 = gr.Slider(label="Pre-denoising Guidance Strength", minimum=-1.0, maximum=6.0, value=-1.0, step=1.0)
284
- seed = gr.Slider(label="Seed", info="-1=Different each time, other=Reproducible", minimum=-1, maximum=2147483647, step=1, randomize=True)
 
285
  s_churn = gr.Slider(label="S-Churn", minimum=0, maximum=40, value=5, step=1)
286
  s_noise = gr.Slider(label="S-Noise", minimum=1.0, maximum=1.1, value=1.003, step=0.001)
287
  a_prompt = gr.Textbox(label="Default Positive Prompt",
@@ -329,9 +338,9 @@ with gr.Blocks(title='SUPIR') as interface:
329
  result_gallery = ImageSlider(label='Output', show_label=False, elem_id="gallery1")
330
  with gr.Row():
331
  with gr.Column():
332
- denoise_button = gr.Button(value="Pre-denoise (KO)")
333
  with gr.Column():
334
- llave_button = gr.Button(value="Generate description by LlaVa (KO)")
335
  with gr.Column():
336
  diffusion_button = gr.Button(value="🚀 Restore", variant = "primary")
337
  with gr.Row():
@@ -369,9 +378,17 @@ with gr.Blocks(title='SUPIR') as interface:
369
  prompt
370
  ])
371
 
372
- diffusion_button.click(fn = check, inputs = [
 
 
 
 
 
373
  input_image
374
- ], outputs = [], queue = False, show_progress = False).success(fn=stage2_process, inputs = [
 
 
 
375
  input_image,
376
  prompt,
377
  a_prompt,
@@ -396,9 +413,7 @@ with gr.Blocks(title='SUPIR') as interface:
396
  model_select
397
  ], outputs = [
398
  result_gallery,
399
- event_id,
400
- fb_score,
401
- fb_text
402
  ])
403
 
404
  restart_button.click(fn = load_and_reset, inputs = [
 
65
  else:
66
  llava_agent = None
67
 
68
+ def update_seed(is_randomize_seed, seed):
69
+ if is_randomize_seed:
70
+ return random.randint(0, max_64_bit_int)
71
+ return seed
72
+
73
  def check(input_image):
74
  if input_image is None:
75
  raise gr.Error("Please provide an image to restore.")
76
 
77
+ def reset_feedback():
78
+ return 3, ''
79
+
80
  @spaces.GPU(duration=180)
81
  def stage1_process(input_image, gamma_correction):
82
  print('Start stage1_process')
 
122
  print('Start stage2_process')
123
  if torch.cuda.device_count() == 0:
124
  gr.Warning('Set this space to GPU config to make it work.')
125
+ return None, None
126
  torch.cuda.set_device(SUPIR_device)
127
  event_id = str(time.time_ns())
128
  event_dict = {'event_id': event_id, 'localtime': time.ctime(), 'prompt': prompt, 'a_prompt': a_prompt,
 
177
  for i, result in enumerate(results):
178
  Image.fromarray(result).save(f'./history/{event_id[:5]}/{event_id[5:]}/HQ_{i}.png')
179
  print('End stage2_process')
180
+ return [input_image] + results, event_id
181
 
182
  def load_and_reset(param_setting):
183
  print('Start load_and_reset')
 
239
  title_md = """
240
  <h1><center>SUPIR Image Upscaler</center></h1>
241
 
242
+ <p>SUPIR is a practicing model scaling for photo-realistic image restoration. It is still a research project under tested and is not yet a stable commercial product.
243
 
244
+ <a href="https://arxiv.org/abs/2401.13627">Paper</a> &emsp; <a href="http://supir.xpixel.group/">Project Page</a> &emsp; <a href="https://github.com/Fanghua-Yu/SUPIR/blob/master/assets/DemoGuide.png">How to play</a> &emsp; <a href="https://huggingface.co/blog/MonsterMMORPG/supir-sota-image-upscale-better-than-magnific-ai">Local Install Guide</a></p>
245
+ <p style="background-color: blue;">LLaVa is disabled.</p>
246
  """
247
 
248
 
 
257
  """
258
 
259
  # Gradio interface
260
+ with gr.Blocks(title="SUPIR") as interface:
261
  with gr.Row():
262
  gr.HTML(title_md)
263
 
 
283
  with gr.Accordion("Restoring options", open=False):
284
  num_samples = gr.Slider(label="Num Samples", info="Number of generated results; I discourage to increase because the process is limited to 3 min", minimum=1, maximum=4 if not args.use_image_slider else 1
285
  , value=1, step=1)
286
+ upscale = gr.Slider(label="Upscale factor", info="Resolution x1, x2, x3, x4, x5, x6, x7 or x8", minimum=1, maximum=8, value=1, step=1)
287
  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)
288
  s_cfg = gr.Slider(label="Text Guidance Scale", info="lower=follow the image, higher=follow the prompt", minimum=1.0, maximum=15.0,
289
  value=default_setting.s_cfg_Quality if torch.cuda.device_count() > 0 else 1.0, step=0.1)
290
  s_stage2 = gr.Slider(label="Restoring Guidance Strength", minimum=0., maximum=1., value=1., step=0.05)
291
  s_stage1 = gr.Slider(label="Pre-denoising Guidance Strength", minimum=-1.0, maximum=6.0, value=-1.0, step=1.0)
292
+ randomize_seed = gr.Checkbox(label = "\U0001F3B2 Randomize seed", value = True, info = "If checked, result is always different")
293
+ seed = gr.Slider(label="Seed", minimum=0, maximum=2147483647, step=1, randomize=True)
294
  s_churn = gr.Slider(label="S-Churn", minimum=0, maximum=40, value=5, step=1)
295
  s_noise = gr.Slider(label="S-Noise", minimum=1.0, maximum=1.1, value=1.003, step=0.001)
296
  a_prompt = gr.Textbox(label="Default Positive Prompt",
 
338
  result_gallery = ImageSlider(label='Output', show_label=False, elem_id="gallery1")
339
  with gr.Row():
340
  with gr.Column():
341
+ denoise_button = gr.Button(value="Pre-denoise")
342
  with gr.Column():
343
+ llave_button = gr.Button(value="Generate description by LlaVa (disabled)")
344
  with gr.Column():
345
  diffusion_button = gr.Button(value="🚀 Restore", variant = "primary")
346
  with gr.Row():
 
378
  prompt
379
  ])
380
 
381
+ diffusion_button.click(fn = update_seed, inputs = [
382
+ randomize_seed,
383
+ seed
384
+ ], outputs = [
385
+ seed
386
+ ], queue = False, show_progress = False).then(fn = check, inputs = [
387
  input_image
388
+ ], outputs = [], queue = False, show_progress = False).then(fn = reset_feedback, inputs = [], outputs = [
389
+ fb_score,
390
+ fb_text
391
+ ], queue = False, show_progress = False).success(fn=stage2_process, inputs = [
392
  input_image,
393
  prompt,
394
  a_prompt,
 
413
  model_select
414
  ], outputs = [
415
  result_gallery,
416
+ event_id
 
 
417
  ])
418
 
419
  restart_button.click(fn = load_and_reset, inputs = [