Upgrade to Gradio 4.x

#4
by multimodalart HF staff - opened
Files changed (2) hide show
  1. README.md +1 -1
  2. app.py +38 -43
README.md CHANGED
@@ -4,7 +4,7 @@ emoji: 🦀
4
  colorFrom: yellow
5
  colorTo: red
6
  sdk: gradio
7
- sdk_version: 3.43.0
8
  app_file: app.py
9
  pinned: true
10
  ---
 
4
  colorFrom: yellow
5
  colorTo: red
6
  sdk: gradio
7
+ sdk_version: 4.7.1
8
  app_file: app.py
9
  pinned: true
10
  ---
app.py CHANGED
@@ -65,7 +65,7 @@ def reconstruct(
65
  return (
66
  reconstruction,
67
  reconstruction,
68
- ddpm_edited_image.update(visible=False),
69
  do_reconstruction,
70
  "Show Reconstruction",
71
  )
@@ -84,7 +84,7 @@ def reconstruct(
84
  return (
85
  reconstruction,
86
  reconstruction,
87
- ddpm_edited_image.update(visible=True),
88
  do_reconstruction,
89
  "Hide Reconstruction",
90
  )
@@ -123,7 +123,7 @@ def load_and_invert(
123
  zs = gr.State(value=zs_tensor)
124
  do_inversion = False
125
 
126
- return wts, zs, do_inversion, inversion_progress.update(visible=False)
127
 
128
  ## SEGA ##
129
 
@@ -203,7 +203,7 @@ def edit(input_image,
203
  # wts=wts.value,
204
  zs=zs.value, **editing_args)
205
 
206
- return sega_out.images[0], reconstruct_button.update(visible=True), do_reconstruction, reconstruction, wts, zs, do_inversion, show_share_button
207
 
208
 
209
  else: # if sega concepts were not added, performs regular ddpm sampling
@@ -212,9 +212,9 @@ def edit(input_image,
212
  pure_ddpm_img = sample(zs.value, wts.value, prompt_tar=tar_prompt, skip=skip, cfg_scale_tar=tar_cfg_scale)
213
  reconstruction = gr.State(value=pure_ddpm_img)
214
  do_reconstruction = False
215
- return pure_ddpm_img, reconstruct_button.update(visible=False), do_reconstruction, reconstruction, wts, zs, do_inversion, show_share_button
216
 
217
- return reconstruction.value, reconstruct_button.update(visible=False), do_reconstruction, reconstruction, wts, zs, do_inversion, show_share_button
218
 
219
 
220
  def randomize_seed_fn(seed, is_random):
@@ -330,12 +330,10 @@ def swap_visibilities(input_image,
330
  concept1_update = update_display_concept("Remove" if neg_guidance_1 else "Add", edit_concept_1, neg_guidance_1, sega_concepts_counter)
331
  if(edit_concept_2 != ""):
332
  concept2_update = update_display_concept("Remove" if neg_guidance_2 else "Add", edit_concept_2, neg_guidance_2, sega_concepts_counter+1)
333
- if(edit_concept_3 != ""):
334
- concept3_update = update_display_concept("Remove" if neg_guidance_3 else "Add", edit_concept_3, neg_guidance_3, sega_concepts_counter+2)
335
  else:
336
  concept2_update = gr.update(visible=False), gr.update(visible=False),gr.update(visible=False), gr.update(value=neg_guidance_2),gr.update(visible=True),gr.update(visible=False),sega_concepts_counter+1
337
- concept3_update = gr.update(visible=False), gr.update(visible=False),gr.update(visible=False), gr.update(value=neg_guidance_3),gr.update(visible=False),gr.update(visible=False),sega_concepts_counter+1
338
- return (gr.update(visible=True), *concept1_update[:-1], *concept2_update[:-1], *concept3_update)
339
 
340
 
341
 
@@ -414,7 +412,7 @@ with gr.Blocks(css="style.css") as demo:
414
 
415
 
416
  def display_editing_options(run_button, clear_button, sega_tab):
417
- return run_button.update(visible=True), clear_button.update(visible=True), sega_tab.update(visible=True)
418
 
419
  def update_interactive_mode(add_button_label):
420
  if add_button_label == "Clear":
@@ -445,15 +443,15 @@ with gr.Blocks(css="style.css") as demo:
445
 
446
  def update_inversion_progress_visibility(input_image, do_inversion):
447
  if do_inversion and not input_image is None:
448
- return inversion_progress.update(visible=True)
449
  else:
450
- return inversion_progress.update(visible=False)
451
 
452
  def update_edit_progress_visibility(input_image, do_inversion):
453
  # if do_inversion and not input_image is None:
454
  # return inversion_progress.update(visible=True)
455
  # else:
456
- return inversion_progress.update(visible=True)
457
 
458
 
459
  gr.HTML(intro)
@@ -469,10 +467,7 @@ with gr.Blocks(css="style.css") as demo:
469
  input_image = gr.Image(label="Input Image", interactive=True, elem_id="input_image")
470
  ddpm_edited_image = gr.Image(label=f"Pure DDPM Inversion Image", interactive=False, visible=False)
471
  sega_edited_image = gr.Image(label=f"LEDITS Edited Image", interactive=False, elem_id="output_image")
472
- input_image.style(height=365, width=365)
473
- ddpm_edited_image.style(height=365, width=365)
474
- sega_edited_image.style(height=365, width=365)
475
-
476
  with gr.Group(visible=False) as share_btn_container:
477
  with gr.Group(elem_id="share-btn-container"):
478
  community_icon = gr.HTML(community_icon_html, visible=True)
@@ -480,7 +475,7 @@ with gr.Blocks(css="style.css") as demo:
480
  share_button = gr.Button("Share to community", elem_id="share-btn", visible=True)
481
 
482
  with gr.Row():
483
- with gr.Box(visible=False, elem_id="box1") as box1:
484
  with gr.Row():
485
  concept_1 = gr.Button(scale=3, value="")
486
  remove_concept1 = gr.Button("x", scale=1, min_width=10)
@@ -489,7 +484,7 @@ with gr.Blocks(css="style.css") as demo:
489
  info="How strongly the concept should modify the image",
490
  value=DEFAULT_SEGA_CONCEPT_GUIDANCE_SCALE,
491
  step=0.5, interactive=True)
492
- with gr.Box(visible=False, elem_id="box2") as box2:
493
  with gr.Row():
494
  concept_2 = gr.Button(scale=3, value="")
495
  remove_concept2 = gr.Button("x", scale=1, min_width=10)
@@ -498,7 +493,7 @@ with gr.Blocks(css="style.css") as demo:
498
  info="How strongly the concept should modify the image",
499
  value=DEFAULT_SEGA_CONCEPT_GUIDANCE_SCALE,
500
  step=0.5, interactive=True)
501
- with gr.Box(visible=False, elem_id="box3") as box3:
502
  with gr.Row():
503
  concept_3 = gr.Button(scale=3, value="")
504
  remove_concept3 = gr.Button("x", scale=1, min_width=10)
@@ -512,12 +507,12 @@ with gr.Blocks(css="style.css") as demo:
512
  with gr.Row():
513
  inversion_progress = gr.Textbox(visible=False, label="Inversion progress")
514
 
515
- with gr.Box():
516
  intro_segs = gr.Markdown("Add/Remove Concepts from your Image <span style=\"font-size: 12px; color: rgb(156, 163, 175)\">with Semantic Guidance</span>")
517
  # 1st SEGA concept
518
- with gr.Row().style(mobile_collapse=False) as row1:
519
  with gr.Column(scale=3, min_width=100):
520
- with gr.Row().style(mobile_collapse=True):
521
  # with gr.Column(scale=3, min_width=100):
522
  edit_concept_1 = gr.Textbox(
523
  label="Concept",
@@ -534,16 +529,16 @@ with gr.Blocks(css="style.css") as demo:
534
  label='Remove Concept?')
535
 
536
  with gr.Column(scale=1, min_width=100):
537
- with gr.Row().style(mobile_collapse=False): # better mobile ui
538
  with gr.Column():
539
  add_1 = gr.Button('Add')
540
  remove_1 = gr.Button('Remove')
541
 
542
 
543
  # 2nd SEGA concept
544
- with gr.Row(visible=False).style(equal_height=True) as row2:
545
  with gr.Column(scale=3, min_width=100):
546
- with gr.Row().style(mobile_collapse=True): #better mobile UI
547
  # with gr.Column(scale=3, min_width=100):
548
  edit_concept_2 = gr.Textbox(
549
  label="Concept",
@@ -559,15 +554,15 @@ with gr.Blocks(css="style.css") as demo:
559
  label='Remove Concept?')
560
 
561
  with gr.Column(scale=1, min_width=100):
562
- with gr.Row().style(mobile_collapse=False): # better mobile ui
563
  with gr.Column():
564
  add_2 = gr.Button('Add')
565
  remove_2 = gr.Button('Remove')
566
 
567
  # 3rd SEGA concept
568
- with gr.Row(visible=False).style(equal_height=True) as row3:
569
  with gr.Column(scale=3, min_width=100):
570
- with gr.Row().style(mobile_collapse=True): #better mobile UI
571
  # with gr.Column(scale=3, min_width=100):
572
  edit_concept_3 = gr.Textbox(
573
  label="Concept",
@@ -583,12 +578,12 @@ with gr.Blocks(css="style.css") as demo:
583
  label='Remove Concept?',visible=True)
584
 
585
  with gr.Column(scale=1, min_width=100):
586
- with gr.Row().style(mobile_collapse=False): # better mobile ui
587
  with gr.Column():
588
  add_3 = gr.Button('Add')
589
  remove_3 = gr.Button('Remove')
590
 
591
- with gr.Row(visible=False).style(equal_height=True) as row4:
592
  gr.Markdown("### Max of 3 concepts reached. Remove a concept to add more")
593
 
594
  #with gr.Row(visible=False).style(mobile_collapse=False, equal_height=True):
@@ -605,7 +600,7 @@ with gr.Blocks(css="style.css") as demo:
605
 
606
 
607
  with gr.Accordion("Advanced Options", open=False):
608
- with gr.Row().style(mobile_collapse=False, equal_height=True):
609
  tar_prompt = gr.Textbox(
610
  label="Describe your edited image (optional)",
611
  elem_id="target_prompt",
@@ -634,7 +629,7 @@ with gr.Blocks(css="style.css") as demo:
634
  with gr.TabItem('SEGA options', id=3) as sega_advanced_tab:
635
  # 1st SEGA concept
636
  gr.Markdown("1st concept")
637
- with gr.Row().style(mobile_collapse=False, equal_height=True):
638
  warmup_1 = gr.Slider(label='Warmup', minimum=0, maximum=50,
639
  value=DEFAULT_WARMUP_STEPS,
640
  step=1, interactive=True, info="At which step to start applying semantic guidance. Bigger values reduce edit concept's effect")
@@ -814,16 +809,16 @@ with gr.Blocks(css="style.css") as demo:
814
  edit_concept_3, guidnace_scale_3,guidnace_scale_3,warmup_3, threshold_3, neg_guidance_3,dropdown3, concept_3,concept_3, row3,
815
  row4,sega_concepts_counter, box1, box2, box3 ]
816
 
817
- clear_components_output_vals = [None, None,ddpm_edited_image.update(visible=False), None, True,
818
  "", DEFAULT_DIFFUSION_STEPS, DEFAULT_SOURCE_GUIDANCE_SCALE, DEFAULT_SEED,
819
- "", DEFAULT_SKIP_STEPS, DEFAULT_TARGET_GUIDANCE_SCALE, reconstruct_button.update(value="Show Reconstruction"),reconstruct_button.update(visible=False),
820
- "", DEFAULT_SEGA_CONCEPT_GUIDANCE_SCALE,guidnace_scale_1.update(visible=False), DEFAULT_WARMUP_STEPS, DEFAULT_THRESHOLD, DEFAULT_NEGATIVE_GUIDANCE, "custom","", concept_1.update(visible=False), row1.update(visible=True),
821
- "", DEFAULT_SEGA_CONCEPT_GUIDANCE_SCALE,guidnace_scale_2.update(visible=False), DEFAULT_WARMUP_STEPS, DEFAULT_THRESHOLD, DEFAULT_NEGATIVE_GUIDANCE, "custom","", concept_2.update(visible=False), row2.update(visible=False),
822
- "", DEFAULT_SEGA_CONCEPT_GUIDANCE_SCALE,guidnace_scale_3.update(visible=False), DEFAULT_WARMUP_STEPS, DEFAULT_THRESHOLD, DEFAULT_NEGATIVE_GUIDANCE, "custom","",concept_3.update(visible=False), row3.update(visible=False), row4.update(visible=False), gr.update(value=0),
823
- box1.update(visible=False), box2.update(visible=False), box3.update(visible=False)]
824
 
825
 
826
- clear_button.click(lambda: clear_components_output_vals, outputs =clear_components)
827
 
828
  reconstruct_button.click(lambda: ddpm_edited_image.update(visible=True), outputs=[ddpm_edited_image]).then(fn = reconstruct,
829
  inputs = [tar_prompt,
@@ -842,7 +837,7 @@ with gr.Blocks(css="style.css") as demo:
842
  outputs = [seed],
843
  queue = False)
844
 
845
- share_button.click(None, [], [], _js=share_js)
846
 
847
  gr.Examples(
848
  label='Examples',
@@ -872,7 +867,7 @@ with gr.Blocks(css="style.css") as demo:
872
  seed,
873
  sega_concepts_counter
874
  ],
875
- outputs=[share_btn_container, box1, concept_1, guidnace_scale_1,neg_guidance_1, row1, row2,box2, concept_2, guidnace_scale_2,neg_guidance_2,row2, row3,box3, concept_3, guidnace_scale_3,neg_guidance_3,row3, row3,sega_concepts_counter],
876
  cache_examples=True
877
  )
878
 
 
65
  return (
66
  reconstruction,
67
  reconstruction,
68
+ gr.update(visible=False),
69
  do_reconstruction,
70
  "Show Reconstruction",
71
  )
 
84
  return (
85
  reconstruction,
86
  reconstruction,
87
+ gr.update(visible=True),
88
  do_reconstruction,
89
  "Hide Reconstruction",
90
  )
 
123
  zs = gr.State(value=zs_tensor)
124
  do_inversion = False
125
 
126
+ return wts, zs, do_inversion, gr.update(visible=False)
127
 
128
  ## SEGA ##
129
 
 
203
  # wts=wts.value,
204
  zs=zs.value, **editing_args)
205
 
206
+ return sega_out.images[0], gr.update(visible=True), do_reconstruction, reconstruction, wts, zs, do_inversion, show_share_button
207
 
208
 
209
  else: # if sega concepts were not added, performs regular ddpm sampling
 
212
  pure_ddpm_img = sample(zs.value, wts.value, prompt_tar=tar_prompt, skip=skip, cfg_scale_tar=tar_cfg_scale)
213
  reconstruction = gr.State(value=pure_ddpm_img)
214
  do_reconstruction = False
215
+ return pure_ddpm_img, gr.update(visible=False), do_reconstruction, reconstruction, wts, zs, do_inversion, show_share_button
216
 
217
+ return reconstruction.value, gr.update(visible=False), do_reconstruction, reconstruction, wts, zs, do_inversion, show_share_button
218
 
219
 
220
  def randomize_seed_fn(seed, is_random):
 
330
  concept1_update = update_display_concept("Remove" if neg_guidance_1 else "Add", edit_concept_1, neg_guidance_1, sega_concepts_counter)
331
  if(edit_concept_2 != ""):
332
  concept2_update = update_display_concept("Remove" if neg_guidance_2 else "Add", edit_concept_2, neg_guidance_2, sega_concepts_counter+1)
 
 
333
  else:
334
  concept2_update = gr.update(visible=False), gr.update(visible=False),gr.update(visible=False), gr.update(value=neg_guidance_2),gr.update(visible=True),gr.update(visible=False),sega_concepts_counter+1
335
+
336
+ return (gr.update(visible=True), *concept1_update[:-1], *concept2_update)
337
 
338
 
339
 
 
412
 
413
 
414
  def display_editing_options(run_button, clear_button, sega_tab):
415
+ return gr.update(visible=True), gr.update(visible=True), gr.update(visible=True)
416
 
417
  def update_interactive_mode(add_button_label):
418
  if add_button_label == "Clear":
 
443
 
444
  def update_inversion_progress_visibility(input_image, do_inversion):
445
  if do_inversion and not input_image is None:
446
+ return gr.update(visible=True)
447
  else:
448
+ return gr.update(visible=False)
449
 
450
  def update_edit_progress_visibility(input_image, do_inversion):
451
  # if do_inversion and not input_image is None:
452
  # return inversion_progress.update(visible=True)
453
  # else:
454
+ return gr.update(visible=True)
455
 
456
 
457
  gr.HTML(intro)
 
467
  input_image = gr.Image(label="Input Image", interactive=True, elem_id="input_image")
468
  ddpm_edited_image = gr.Image(label=f"Pure DDPM Inversion Image", interactive=False, visible=False)
469
  sega_edited_image = gr.Image(label=f"LEDITS Edited Image", interactive=False, elem_id="output_image")
470
+
 
 
 
471
  with gr.Group(visible=False) as share_btn_container:
472
  with gr.Group(elem_id="share-btn-container"):
473
  community_icon = gr.HTML(community_icon_html, visible=True)
 
475
  share_button = gr.Button("Share to community", elem_id="share-btn", visible=True)
476
 
477
  with gr.Row():
478
+ with gr.Group(visible=False, elem_id="box1") as box1:
479
  with gr.Row():
480
  concept_1 = gr.Button(scale=3, value="")
481
  remove_concept1 = gr.Button("x", scale=1, min_width=10)
 
484
  info="How strongly the concept should modify the image",
485
  value=DEFAULT_SEGA_CONCEPT_GUIDANCE_SCALE,
486
  step=0.5, interactive=True)
487
+ with gr.Group(visible=False, elem_id="box2") as box2:
488
  with gr.Row():
489
  concept_2 = gr.Button(scale=3, value="")
490
  remove_concept2 = gr.Button("x", scale=1, min_width=10)
 
493
  info="How strongly the concept should modify the image",
494
  value=DEFAULT_SEGA_CONCEPT_GUIDANCE_SCALE,
495
  step=0.5, interactive=True)
496
+ with gr.Group(visible=False, elem_id="box3") as box3:
497
  with gr.Row():
498
  concept_3 = gr.Button(scale=3, value="")
499
  remove_concept3 = gr.Button("x", scale=1, min_width=10)
 
507
  with gr.Row():
508
  inversion_progress = gr.Textbox(visible=False, label="Inversion progress")
509
 
510
+ with gr.Group():
511
  intro_segs = gr.Markdown("Add/Remove Concepts from your Image <span style=\"font-size: 12px; color: rgb(156, 163, 175)\">with Semantic Guidance</span>")
512
  # 1st SEGA concept
513
+ with gr.Row() as row1:
514
  with gr.Column(scale=3, min_width=100):
515
+ with gr.Row():
516
  # with gr.Column(scale=3, min_width=100):
517
  edit_concept_1 = gr.Textbox(
518
  label="Concept",
 
529
  label='Remove Concept?')
530
 
531
  with gr.Column(scale=1, min_width=100):
532
+ with gr.Row(): # better mobile ui
533
  with gr.Column():
534
  add_1 = gr.Button('Add')
535
  remove_1 = gr.Button('Remove')
536
 
537
 
538
  # 2nd SEGA concept
539
+ with gr.Row(visible=False) as row2:
540
  with gr.Column(scale=3, min_width=100):
541
+ with gr.Row(): #better mobile UI
542
  # with gr.Column(scale=3, min_width=100):
543
  edit_concept_2 = gr.Textbox(
544
  label="Concept",
 
554
  label='Remove Concept?')
555
 
556
  with gr.Column(scale=1, min_width=100):
557
+ with gr.Row(): # better mobile ui
558
  with gr.Column():
559
  add_2 = gr.Button('Add')
560
  remove_2 = gr.Button('Remove')
561
 
562
  # 3rd SEGA concept
563
+ with gr.Row(visible=False) as row3:
564
  with gr.Column(scale=3, min_width=100):
565
+ with gr.Row(): #better mobile UI
566
  # with gr.Column(scale=3, min_width=100):
567
  edit_concept_3 = gr.Textbox(
568
  label="Concept",
 
578
  label='Remove Concept?',visible=True)
579
 
580
  with gr.Column(scale=1, min_width=100):
581
+ with gr.Row(): # better mobile ui
582
  with gr.Column():
583
  add_3 = gr.Button('Add')
584
  remove_3 = gr.Button('Remove')
585
 
586
+ with gr.Row(visible=False) as row4:
587
  gr.Markdown("### Max of 3 concepts reached. Remove a concept to add more")
588
 
589
  #with gr.Row(visible=False).style(mobile_collapse=False, equal_height=True):
 
600
 
601
 
602
  with gr.Accordion("Advanced Options", open=False):
603
+ with gr.Row():
604
  tar_prompt = gr.Textbox(
605
  label="Describe your edited image (optional)",
606
  elem_id="target_prompt",
 
629
  with gr.TabItem('SEGA options', id=3) as sega_advanced_tab:
630
  # 1st SEGA concept
631
  gr.Markdown("1st concept")
632
+ with gr.Row():
633
  warmup_1 = gr.Slider(label='Warmup', minimum=0, maximum=50,
634
  value=DEFAULT_WARMUP_STEPS,
635
  step=1, interactive=True, info="At which step to start applying semantic guidance. Bigger values reduce edit concept's effect")
 
809
  edit_concept_3, guidnace_scale_3,guidnace_scale_3,warmup_3, threshold_3, neg_guidance_3,dropdown3, concept_3,concept_3, row3,
810
  row4,sega_concepts_counter, box1, box2, box3 ]
811
 
812
+ clear_components_output_vals = [None, None,gr.update(visible=False), None, True,
813
  "", DEFAULT_DIFFUSION_STEPS, DEFAULT_SOURCE_GUIDANCE_SCALE, DEFAULT_SEED,
814
+ "", DEFAULT_SKIP_STEPS, DEFAULT_TARGET_GUIDANCE_SCALE, gr.update(value="Show Reconstruction"),gr.update(visible=False),
815
+ "", DEFAULT_SEGA_CONCEPT_GUIDANCE_SCALE,gr.update(visible=False), DEFAULT_WARMUP_STEPS, DEFAULT_THRESHOLD, DEFAULT_NEGATIVE_GUIDANCE, "custom","", gr.update(visible=False), gr.update(visible=True),
816
+ "", DEFAULT_SEGA_CONCEPT_GUIDANCE_SCALE,gr.update(visible=False), DEFAULT_WARMUP_STEPS, DEFAULT_THRESHOLD, DEFAULT_NEGATIVE_GUIDANCE, "custom","", gr.update(visible=False), gr.update(visible=False),
817
+ "", DEFAULT_SEGA_CONCEPT_GUIDANCE_SCALE,gr.update(visible=False), DEFAULT_WARMUP_STEPS, DEFAULT_THRESHOLD, DEFAULT_NEGATIVE_GUIDANCE, "custom","",gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(value=0),
818
+ gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)]
819
 
820
 
821
+ clear_button.click(lambda: clear_components_output_vals, outputs = clear_components)
822
 
823
  reconstruct_button.click(lambda: ddpm_edited_image.update(visible=True), outputs=[ddpm_edited_image]).then(fn = reconstruct,
824
  inputs = [tar_prompt,
 
837
  outputs = [seed],
838
  queue = False)
839
 
840
+ share_button.click(None, [], [], js=share_js)
841
 
842
  gr.Examples(
843
  label='Examples',
 
867
  seed,
868
  sega_concepts_counter
869
  ],
870
+ outputs=[share_btn_container, box1, concept_1, guidnace_scale_1,neg_guidance_1, row1, row2,box2, concept_2, guidnace_scale_2,neg_guidance_2,row2, row3,sega_concepts_counter],
871
  cache_examples=True
872
  )
873