multimodalart HF staff commited on
Commit
5197fe6
1 Parent(s): 917af41
Files changed (2) hide show
  1. app.py +27 -21
  2. share_btn.py +109 -0
app.py CHANGED
@@ -12,6 +12,7 @@ from torch import autocast, inference_mode
12
  from diffusers import StableDiffusionPipeline
13
  from diffusers import DDIMScheduler
14
  from transformers import AutoProcessor, BlipForConditionalGeneration
 
15
 
16
  # load pipelines
17
  sd_model_id = "stabilityai/stable-diffusion-2-1-base"
@@ -144,7 +145,7 @@ def edit(input_image,
144
  randomize_seed,
145
  src_prompt,
146
  src_cfg_scale):
147
-
148
  if do_inversion or randomize_seed:
149
  x0 = load_512(input_image, device=device).to(torch.float16)
150
  # invert and retrieve noise maps and latent
@@ -173,7 +174,7 @@ def edit(input_image,
173
  num_inference_steps=steps,
174
  use_ddpm=True, wts=wts.value, zs=zs.value[skip:], **editing_args)
175
 
176
- return sega_out.images[0], reconstruct_button.update(visible=True), do_reconstruction, reconstruction, wts, zs, do_inversion
177
 
178
  else: # if sega concepts were not added, performs regular ddpm sampling
179
 
@@ -181,9 +182,9 @@ def edit(input_image,
181
  pure_ddpm_img = sample(zs.value, wts.value, prompt_tar=tar_prompt, skip=skip, cfg_scale_tar=tar_cfg_scale)
182
  reconstruction = gr.State(value=pure_ddpm_img)
183
  do_reconstruction = False
184
- return pure_ddpm_img, reconstruct_button.update(visible=False), do_reconstruction, reconstruction, wts, zs, do_inversion
185
 
186
- return reconstruction.value, reconstruct_button.update(visible=False), do_reconstruction, reconstruction, wts, zs, do_inversion
187
 
188
 
189
  def randomize_seed_fn(seed, randomize_seed):
@@ -309,7 +310,8 @@ def swap_visibilities(input_image,
309
  concept2_update = update_display_concept("Remove" if neg_guidance_2 else "Add", edit_concept_2, neg_guidance_2, sega_concepts_counter+1)
310
  else:
311
  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
312
- return (*concept1_update[:-1], *concept2_update)
 
313
 
314
 
315
 
@@ -470,50 +472,53 @@ with gr.Blocks(css="style.css") as demo:
470
  sega_concepts_counter = gr.State(0)
471
  image_caption = gr.State(value="")
472
 
473
-
474
-
475
  with gr.Row():
476
- input_image = gr.Image(label="Input Image", interactive=True)
477
  ddpm_edited_image = gr.Image(label=f"Pure DDPM Inversion Image", interactive=False, visible=False)
478
- sega_edited_image = gr.Image(label=f"LEDITS Edited Image", interactive=False)
479
  input_image.style(height=365, width=365)
480
  ddpm_edited_image.style(height=365, width=365)
481
  sega_edited_image.style(height=365, width=365)
482
-
 
 
 
 
 
 
483
  with gr.Row():
484
- with gr.Box(visible=False) as box1:
485
  with gr.Row():
486
- concept_1 = gr.Button(scale=3)
487
  remove_concept1 = gr.Button("x", scale=1, min_width=10)
488
  with gr.Row():
489
  guidnace_scale_1 = gr.Slider(label='Concept Guidance Scale', minimum=1, maximum=30,
490
  info="How strongly the concept should modify the image",
491
  value=DEFAULT_SEGA_CONCEPT_GUIDANCE_SCALE,
492
  step=0.5, interactive=True)
493
- with gr.Box(visible=False) as box2:
494
  with gr.Row():
495
- concept_2 = gr.Button(scale=3)
496
  remove_concept2 = gr.Button("x", scale=1, min_width=10)
497
  with gr.Row():
498
  guidnace_scale_2 = gr.Slider(label='Concept Guidance Scale', minimum=1, maximum=30,
499
  info="How strongly the concept should modify the image",
500
  value=DEFAULT_SEGA_CONCEPT_GUIDANCE_SCALE,
501
  step=0.5, interactive=True)
502
- with gr.Box(visible=False) as box3:
503
  with gr.Row():
504
- concept_3 = gr.Button(visible=False, scale=3)
505
  remove_concept3 = gr.Button("x", scale=1, min_width=10)
506
  with gr.Row():
507
  guidnace_scale_3 = gr.Slider(label='Concept Guidance Scale', minimum=1, maximum=30,
508
  info="How strongly the concept should modify the image",
509
  value=DEFAULT_SEGA_CONCEPT_GUIDANCE_SCALE,
510
- step=0.5, interactive=True,visible=False)
511
 
512
 
513
  with gr.Row():
514
  inversion_progress = gr.Textbox(visible=False, label="Inversion progress")
515
 
516
-
517
  with gr.Box():
518
  intro_segs = gr.Markdown("Add/Remove Concepts from your Image <span style=\"font-size: 12px; color: rgb(156, 163, 175)\">with Semantic Guidance</span>")
519
  # 1st SEGA concept
@@ -601,6 +606,7 @@ with gr.Blocks(css="style.css") as demo:
601
  with gr.Row().style(mobile_collapse=False, equal_height=True):
602
  tar_prompt = gr.Textbox(
603
  label="Describe your edited image (optional)",
 
604
  # show_label=False,
605
  max_lines=1, value="", scale=3,
606
  placeholder="Target prompt, DDPM Inversion", info = "DDPM Inversion Prompt. Can help with global changes, modify to what you would like to see"
@@ -716,7 +722,7 @@ with gr.Blocks(css="style.css") as demo:
716
 
717
 
718
  ],
719
- outputs=[sega_edited_image, reconstruct_button, do_reconstruction, reconstruction, wts, zs, do_inversion])
720
  # .success(fn=update_gallery_display, inputs= [prev_output_image, sega_edited_image], outputs = [gallery, gallery, prev_output_image])
721
 
722
 
@@ -829,7 +835,7 @@ with gr.Blocks(css="style.css") as demo:
829
  outputs = [seed],
830
  queue = False)
831
 
832
-
833
 
834
  gr.Examples(
835
  label='Examples',
@@ -852,7 +858,7 @@ with gr.Blocks(css="style.css") as demo:
852
  tar_cfg_scale,
853
  sega_concepts_counter
854
  ],
855
- outputs=[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],
856
  cache_examples=True
857
  )
858
 
 
12
  from diffusers import StableDiffusionPipeline
13
  from diffusers import DDIMScheduler
14
  from transformers import AutoProcessor, BlipForConditionalGeneration
15
+ from share_btn import community_icon_html, loading_icon_html, share_js
16
 
17
  # load pipelines
18
  sd_model_id = "stabilityai/stable-diffusion-2-1-base"
 
145
  randomize_seed,
146
  src_prompt,
147
  src_cfg_scale):
148
+ show_share_button = gr.update(visible=True)
149
  if do_inversion or randomize_seed:
150
  x0 = load_512(input_image, device=device).to(torch.float16)
151
  # invert and retrieve noise maps and latent
 
174
  num_inference_steps=steps,
175
  use_ddpm=True, wts=wts.value, zs=zs.value[skip:], **editing_args)
176
 
177
+ return sega_out.images[0], reconstruct_button.update(visible=True), do_reconstruction, reconstruction, wts, zs, do_inversion, show_share_button
178
 
179
  else: # if sega concepts were not added, performs regular ddpm sampling
180
 
 
182
  pure_ddpm_img = sample(zs.value, wts.value, prompt_tar=tar_prompt, skip=skip, cfg_scale_tar=tar_cfg_scale)
183
  reconstruction = gr.State(value=pure_ddpm_img)
184
  do_reconstruction = False
185
+ return pure_ddpm_img, reconstruct_button.update(visible=False), do_reconstruction, reconstruction, wts, zs, do_inversion, show_share_button
186
 
187
+ return reconstruction.value, reconstruct_button.update(visible=False), do_reconstruction, reconstruction, wts, zs, do_inversion, show_share_button
188
 
189
 
190
  def randomize_seed_fn(seed, randomize_seed):
 
310
  concept2_update = update_display_concept("Remove" if neg_guidance_2 else "Add", edit_concept_2, neg_guidance_2, sega_concepts_counter+1)
311
  else:
312
  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
313
+
314
+ return (gr.update(visible=True), *concept1_update[:-1], *concept2_update)
315
 
316
 
317
 
 
472
  sega_concepts_counter = gr.State(0)
473
  image_caption = gr.State(value="")
474
 
 
 
475
  with gr.Row():
476
+ input_image = gr.Image(label="Input Image", interactive=True, elem_id="input_image")
477
  ddpm_edited_image = gr.Image(label=f"Pure DDPM Inversion Image", interactive=False, visible=False)
478
+ sega_edited_image = gr.Image(label=f"LEDITS Edited Image", interactive=False, elem_id="output_image")
479
  input_image.style(height=365, width=365)
480
  ddpm_edited_image.style(height=365, width=365)
481
  sega_edited_image.style(height=365, width=365)
482
+
483
+ with gr.Group(visible=False) as share_btn_container:
484
+ with gr.Group(elem_id="share-btn-container"):
485
+ community_icon = gr.HTML(community_icon_html, visible=True)
486
+ loading_icon = gr.HTML(loading_icon_html, visible=False)
487
+ share_button = gr.Button("Share to community", elem_id="share-btn", visible=True)
488
+
489
  with gr.Row():
490
+ with gr.Box(visible=False, elem_id="box1") as box1:
491
  with gr.Row():
492
+ concept_1 = gr.Button(scale=3, value="")
493
  remove_concept1 = gr.Button("x", scale=1, min_width=10)
494
  with gr.Row():
495
  guidnace_scale_1 = gr.Slider(label='Concept Guidance Scale', minimum=1, maximum=30,
496
  info="How strongly the concept should modify the image",
497
  value=DEFAULT_SEGA_CONCEPT_GUIDANCE_SCALE,
498
  step=0.5, interactive=True)
499
+ with gr.Box(visible=False, elem_id="box2") as box2:
500
  with gr.Row():
501
+ concept_2 = gr.Button(scale=3, value="")
502
  remove_concept2 = gr.Button("x", scale=1, min_width=10)
503
  with gr.Row():
504
  guidnace_scale_2 = gr.Slider(label='Concept Guidance Scale', minimum=1, maximum=30,
505
  info="How strongly the concept should modify the image",
506
  value=DEFAULT_SEGA_CONCEPT_GUIDANCE_SCALE,
507
  step=0.5, interactive=True)
508
+ with gr.Box(visible=False, elem_id="box3") as box3:
509
  with gr.Row():
510
+ concept_3 = gr.Button(scale=3, value="")
511
  remove_concept3 = gr.Button("x", scale=1, min_width=10)
512
  with gr.Row():
513
  guidnace_scale_3 = gr.Slider(label='Concept Guidance Scale', minimum=1, maximum=30,
514
  info="How strongly the concept should modify the image",
515
  value=DEFAULT_SEGA_CONCEPT_GUIDANCE_SCALE,
516
+ step=0.5, interactive=True)
517
 
518
 
519
  with gr.Row():
520
  inversion_progress = gr.Textbox(visible=False, label="Inversion progress")
521
 
 
522
  with gr.Box():
523
  intro_segs = gr.Markdown("Add/Remove Concepts from your Image <span style=\"font-size: 12px; color: rgb(156, 163, 175)\">with Semantic Guidance</span>")
524
  # 1st SEGA concept
 
606
  with gr.Row().style(mobile_collapse=False, equal_height=True):
607
  tar_prompt = gr.Textbox(
608
  label="Describe your edited image (optional)",
609
+ elem_id="target_prompt",
610
  # show_label=False,
611
  max_lines=1, value="", scale=3,
612
  placeholder="Target prompt, DDPM Inversion", info = "DDPM Inversion Prompt. Can help with global changes, modify to what you would like to see"
 
722
 
723
 
724
  ],
725
+ outputs=[sega_edited_image, reconstruct_button, do_reconstruction, reconstruction, wts, zs, do_inversion, share_btn_container])
726
  # .success(fn=update_gallery_display, inputs= [prev_output_image, sega_edited_image], outputs = [gallery, gallery, prev_output_image])
727
 
728
 
 
835
  outputs = [seed],
836
  queue = False)
837
 
838
+ share_button.click(None, [], [], _js=share_js)
839
 
840
  gr.Examples(
841
  label='Examples',
 
858
  tar_cfg_scale,
859
  sega_concepts_counter
860
  ],
861
+ 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],
862
  cache_examples=True
863
  )
864
 
share_btn.py ADDED
@@ -0,0 +1,109 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ community_icon_html = """<svg id="share-btn-share-icon" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32">
2
+ <path d="M20.6081 3C21.7684 3 22.8053 3.49196 23.5284 4.38415C23.9756 4.93678 24.4428 5.82749 24.4808 7.16133C24.9674 7.01707 25.4353 6.93643 25.8725 6.93643C26.9833 6.93643 27.9865 7.37587 28.696 8.17411C29.6075 9.19872 30.0124 10.4579 29.8361 11.7177C29.7523 12.3177 29.5581 12.8555 29.2678 13.3534C29.8798 13.8646 30.3306 14.5763 30.5485 15.4322C30.719 16.1032 30.8939 17.5006 29.9808 18.9403C30.0389 19.0342 30.0934 19.1319 30.1442 19.2318C30.6932 20.3074 30.7283 21.5229 30.2439 22.6548C29.5093 24.3704 27.6841 25.7219 24.1397 27.1727C21.9347 28.0753 19.9174 28.6523 19.8994 28.6575C16.9842 29.4379 14.3477 29.8345 12.0653 29.8345C7.87017 29.8345 4.8668 28.508 3.13831 25.8921C0.356375 21.6797 0.754104 17.8269 4.35369 14.1131C6.34591 12.058 7.67023 9.02782 7.94613 8.36275C8.50224 6.39343 9.97271 4.20438 12.4172 4.20438H12.4179C12.6236 4.20438 12.8314 4.2214 13.0364 4.25468C14.107 4.42854 15.0428 5.06476 15.7115 6.02205C16.4331 5.09583 17.134 4.359 17.7682 3.94323C18.7242 3.31737 19.6794 3 20.6081 3ZM20.6081 5.95917C20.2427 5.95917 19.7963 6.1197 19.3039 6.44225C17.7754 7.44319 14.8258 12.6772 13.7458 14.7131C13.3839 15.3952 12.7655 15.6837 12.2086 15.6837C11.1036 15.6837 10.2408 14.5497 12.1076 13.1085C14.9146 10.9402 13.9299 7.39584 12.5898 7.1776C12.5311 7.16799 12.4731 7.16355 12.4172 7.16355C11.1989 7.16355 10.6615 9.33114 10.6615 9.33114C10.6615 9.33114 9.0863 13.4148 6.38031 16.206C3.67434 18.998 3.5346 21.2388 5.50675 24.2246C6.85185 26.2606 9.42666 26.8753 12.0653 26.8753C14.8021 26.8753 17.6077 26.2139 19.1799 25.793C19.2574 25.7723 28.8193 22.984 27.6081 20.6107C27.4046 20.212 27.0693 20.0522 26.6471 20.0522C24.9416 20.0522 21.8393 22.6726 20.5057 22.6726C20.2076 22.6726 19.9976 22.5416 19.9116 22.222C19.3433 20.1173 28.552 19.2325 27.7758 16.1839C27.639 15.6445 27.2677 15.4256 26.746 15.4263C24.4923 15.4263 19.4358 19.5181 18.3759 19.5181C18.2949 19.5181 18.2368 19.4937 18.2053 19.4419C17.6743 18.557 17.9653 17.9394 21.7082 15.6009C25.4511 13.2617 28.0783 11.8545 26.5841 10.1752C26.4121 9.98141 26.1684 9.8956 25.8725 9.8956C23.6001 9.89634 18.2311 14.9403 18.2311 14.9403C18.2311 14.9403 16.7821 16.496 15.9057 16.496C15.7043 16.496 15.533 16.4139 15.4169 16.2112C14.7956 15.1296 21.1879 10.1286 21.5484 8.06535C21.7928 6.66715 21.3771 5.95917 20.6081 5.95917Z" fill="#FF9D00"></path>
3
+ <path d="M5.50686 24.2246C3.53472 21.2387 3.67446 18.9979 6.38043 16.206C9.08641 13.4147 10.6615 9.33111 10.6615 9.33111C10.6615 9.33111 11.2499 6.95933 12.59 7.17757C13.93 7.39581 14.9139 10.9401 12.1069 13.1084C9.29997 15.276 12.6659 16.7489 13.7459 14.713C14.8258 12.6772 17.7747 7.44316 19.304 6.44221C20.8326 5.44128 21.9089 6.00204 21.5484 8.06532C21.188 10.1286 14.795 15.1295 15.4171 16.2118C16.0391 17.2934 18.2312 14.9402 18.2312 14.9402C18.2312 14.9402 25.0907 8.49588 26.5842 10.1752C28.0776 11.8545 25.4512 13.2616 21.7082 15.6008C17.9646 17.9393 17.6744 18.557 18.2054 19.4418C18.7372 20.3266 26.9998 13.1351 27.7759 16.1838C28.5513 19.2324 19.3434 20.1173 19.9117 22.2219C20.48 24.3274 26.3979 18.2382 27.6082 20.6107C28.8193 22.9839 19.2574 25.7722 19.18 25.7929C16.0914 26.62 8.24723 28.3726 5.50686 24.2246Z" fill="#FFD21E"></path>
4
+ </svg>"""
5
+
6
+ loading_icon_html = """<svg id="share-btn-loading-icon" style="display:none;" class="animate-spin"
7
+ style="color: #ffffff;
8
+ "
9
+ xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" fill="none" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><circle style="opacity: 0.25;" cx="12" cy="12" r="10" stroke="white" stroke-width="4"></circle><path style="opacity: 0.75;" fill="white" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path></svg>"""
10
+
11
+ share_js = r"""async () => {
12
+ async function uploadFile(file){
13
+ const UPLOAD_URL = 'https://huggingface.co/uploads';
14
+ const response = await fetch(UPLOAD_URL, {
15
+ method: 'POST',
16
+ headers: {
17
+ 'Content-Type': file.type,
18
+ 'X-Requested-With': 'XMLHttpRequest',
19
+ },
20
+ body: file,
21
+ });
22
+ const url = await response.text();
23
+ return url;
24
+ }
25
+
26
+ function getButtonText(componentId) {
27
+ const buttonEl = gradioEl.querySelector(`${componentId} button`);
28
+ return buttonEl ? buttonEl.textContent : '';
29
+ }
30
+
31
+ const gradioEl = document.querySelector('body > gradio-app');
32
+ const imgEls = [gradioEl.querySelector('#input_image img'), gradioEl.querySelector('#output_image img')];
33
+ const concepts = [
34
+ { value: getButtonText('#box1'), parent: gradioEl.querySelector('#box1 span[data-testid="block-info"]') },
35
+ { value: getButtonText('#box2'), parent: gradioEl.querySelector('#box2 span[data-testid="block-info"]') },
36
+ { value: getButtonText('#box3'), parent: gradioEl.querySelector('#box3 span[data-testid="block-info"]') }
37
+ ];
38
+
39
+ const promptTxt = gradioEl.querySelector('#target_prompt input').value;
40
+ const shareBtnEl = gradioEl.querySelector('#share-btn');
41
+ const shareIconEl = gradioEl.querySelector('#share-btn-share-icon');
42
+ const loadingIconEl = gradioEl.querySelector('#share-btn-loading-icon');
43
+ if(!imgEls[1]){
44
+ return;
45
+ };
46
+ shareBtnEl.style.pointerEvents = 'none';
47
+ shareIconEl.style.display = 'none';
48
+ loadingIconEl.style.removeProperty('display');
49
+
50
+ async function processImage(imgEl, imgId) {
51
+ const res = await fetch(imgEl.src);
52
+ const blob = await res.blob();
53
+ const fileType = blob.type.includes('png') ? 'png' : 'jpg';
54
+ const fileName = `diffuse-the-rest-${imgId}.${fileType}`;
55
+ return new File([blob], fileName, { type: blob.type });
56
+ }
57
+
58
+ const files = await Promise.all(imgEls.map((imgEl, index) => processImage(imgEl, Date.now() + index % 200)));
59
+ const urls = await Promise.all(files.map((file) => uploadFile(file)));
60
+
61
+ const labels = ['Source image', 'Target image'];
62
+ const htmlImgs = urls.map((url, index) => `<div>${labels[index]}: <img src='${url}' width='400' height='400' style="margin: 0"></div>`);
63
+
64
+ let descriptionMd = `<div style='display: flex; flex-wrap: wrap; column-gap: 0.75rem;'>${htmlImgs.join(`\n`)}</div>`;
65
+
66
+ if (promptTxt) {
67
+ descriptionMd += `<span style="font-size:1.2em">Target image prompt: <b>${promptTxt}</b></span><br>`;
68
+ } else {
69
+ descriptionMd += `<span style="font-size:1.2em">Target image prompt: <b>""</b></span><br>`;
70
+ }
71
+
72
+ const conceptHeaders = [];
73
+ const conceptDescriptions = [];
74
+ const conceptTableRows = [];
75
+ concepts.forEach((concept, index) => {
76
+ if (concept.value) {
77
+ const label = concept.parent.textContent.includes('negative') ? `remove concept` : `add concept`;
78
+ conceptHeaders.push(`<th>${label}</th>`);
79
+ conceptDescriptions.push(`${label}: ${concept.value}`);
80
+ conceptTableRows.push(`<td>${concept.value}</td>`);
81
+ }
82
+ });
83
+
84
+ let title = 'Editing';
85
+ if (promptTxt) {
86
+ title += ` "${promptTxt}"`;
87
+ }
88
+ if (conceptDescriptions.length > 0) {
89
+ title += ` to ${conceptDescriptions.join(', ')}`;
90
+ descriptionMd += `<table style="font-size: 1.1em">
91
+ <tr>
92
+ ${conceptHeaders.join('\n')}
93
+ </tr>
94
+ <tr>
95
+ ${conceptTableRows.join('\n')}
96
+ </tr>
97
+ </table>`;
98
+ }
99
+
100
+ const params = new URLSearchParams({
101
+ title: title,
102
+ description: descriptionMd,
103
+ });
104
+ const paramsStr = params.toString();
105
+ window.open(`https://huggingface.co/spaces/editing-images/ledits/discussions/new?${paramsStr}`, '_blank');
106
+ shareBtnEl.style.removeProperty('pointer-events');
107
+ shareIconEl.style.removeProperty('display');
108
+ loadingIconEl.style.display = 'none';
109
+ }"""