Spaces:
Runtime error
Runtime error
Commit
·
85d16b6
1
Parent(s):
92d06e8
Update app.py
Browse files
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 |
-
|
|
|
|
| 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(
|
| 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
|
| 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 |
|