Linoy Tsaban commited on
Commit
7e3c69d
1 Parent(s): 17372cb

Update app.py

Browse files

fixed clear functionality + temporarily disabled 'show reconstruction' option

Files changed (1) hide show
  1. app.py +90 -113
app.py CHANGED
@@ -5,6 +5,7 @@ import requests
5
  import random
6
  from io import BytesIO
7
  from utils import *
 
8
  from inversion_utils import *
9
  from modified_pipeline_semantic_stable_diffusion import SemanticStableDiffusionPipeline
10
  from torch import autocast, inference_mode
@@ -214,8 +215,8 @@ def get_example():
214
 
215
  ########
216
  # demo #
217
- ########
218
-
219
  intro = """
220
  <h1 style="font-weight: 1400; text-align: center; margin-bottom: 7px;">
221
  Edit Friendly DDPM X Semantic Guidance
@@ -270,40 +271,18 @@ with gr.Blocks(css='style.css') as demo:
270
  else:
271
  return row2.update(visible=True), row3.update(visible=True), add_concept_button.update(visible=False), 3
272
 
273
- def show_reconstruction_button():
274
- return reconstruct_button.update(visible=True)
275
-
276
- def hide_hide_reconstruction_button():
277
- return reconstruct_button.update(visible=True),hide_reconstruct_button.update(visible=False)
278
-
279
- def hide_reconstruction_buttons():
280
- return reconstruct_button.update(visible=False), hide_reconstruct_button.update(visible=False)
281
-
282
- def show_reconstruction():
283
- return ddpm_edited_image.update(visible=True)
284
-
285
- def hide_reconstruction():
286
- return ddpm_edited_image.update(visible=False)
287
-
288
- def show_hide_reconstruction_button():
289
- return reconstruct_button.update(visible=False), hide_reconstruct_button.update(visible=True)
290
 
291
  def reset_do_inversion():
292
  do_inversion = True
293
  return do_inversion
294
 
295
- def reset_do_reconstruction():
296
- do_reconstruction = True
297
- return do_reconstruction
298
 
299
- def show_inversion_progress():
300
- return inversion_progress.update(visible=True)
301
-
302
- def hide_inversion_progress():
303
  return inversion_progress.update(visible=False)
304
-
305
- def clear():
306
- return "",ddpm_edited_image.update(visible=False), None, reconstruct_button.update(visible=False), True
307
 
308
 
309
 
@@ -311,12 +290,10 @@ with gr.Blocks(css='style.css') as demo:
311
  wts = gr.State()
312
  zs = gr.State()
313
  do_inversion = gr.State(value=True)
314
- do_reconstruction = gr.State(value=True)
315
  reconstruction = gr.State()
316
  sega_concepts_counter = gr.State(1)
317
 
318
-
319
-
320
 
321
  with gr.Row():
322
  input_image = gr.Image(label="Input Image", interactive=True)
@@ -338,15 +315,20 @@ with gr.Blocks(css='style.css') as demo:
338
  max_lines=1,
339
  placeholder="Enter your 1st edit prompt",
340
  )
341
- caption_button = gr.Button("Caption Image").style(size='sm')
342
  with gr.TabItem('2. Add SEGA edit concepts', id=1):
343
  # 1st SEGA concept
344
  with gr.Row().style(mobile_collapse=False, equal_height=True):
345
  neg_guidance_1 = gr.Checkbox(
346
  label='Negative Guidance')
347
- warmup_1 = gr.Slider(label='Warmup', minimum=0, maximum=50, value=1, step=1, interactive=True)
348
- guidnace_scale_1 = gr.Slider(label='Concept Guidance Scale', minimum=1, maximum=15, value=5, step=0.25, interactive=True)
349
- threshold_1 = gr.Slider(label='Threshold', minimum=0.5, maximum=0.99, value=0.95, steps=0.01, interactive=True)
 
 
 
 
 
350
  edit_concept_1 = gr.Textbox(
351
  label="Edit Concept",
352
  show_label=False,
@@ -358,12 +340,18 @@ with gr.Blocks(css='style.css') as demo:
358
  with gr.Row(visible=False) as row2:
359
  neg_guidance_2 = gr.Checkbox(
360
  label='Negative Guidance',visible=True)
361
- warmup_2 = gr.Slider(label='Warmup', minimum=0, maximum=50, value=1, step=1, visible=True,interactive=True)
362
- guidnace_scale_2 = gr.Slider(label='Concept Guidance Scale', minimum=1, maximum=15, value=5, step=0.25,visible=True, interactive=True)
363
- threshold_2 = gr.Slider(label='Threshold', minimum=0.5, maximum=0.99, value=0.95, steps=0.01,visible=True, interactive=True)
 
 
 
 
 
 
364
  edit_concept_2 = gr.Textbox(
365
  label="Edit Concept",
366
- show_label=False,visible=True,
367
  max_lines=1,
368
  placeholder="Enter your 2st edit prompt",
369
  )
@@ -371,12 +359,18 @@ with gr.Blocks(css='style.css') as demo:
371
  with gr.Row(visible=False) as row3:
372
  neg_guidance_3 = gr.Checkbox(
373
  label='Negative Guidance',visible=True)
374
- warmup_3 = gr.Slider(label='Warmup', minimum=0, maximum=50, value=1, step=1, visible=True,interactive=True)
375
- guidnace_scale_3 = gr.Slider(label='Concept Guidance Scale', minimum=1, maximum=15, value=5, step=0.25,visible=True, interactive=True)
376
- threshold_3 = gr.Slider(label='Threshold', minimum=0.5, maximum=0.99, value=0.95, steps=0.01,visible=True, interactive=True)
 
 
 
 
 
 
377
  edit_concept_3 = gr.Textbox(
378
  label="Edit Concept",
379
- show_label=False,visible=True,
380
  max_lines=1,
381
  placeholder="Enter your 3rd edit prompt",
382
  )
@@ -388,8 +382,9 @@ with gr.Blocks(css='style.css') as demo:
388
  with gr.Row():
389
  run_button = gr.Button("Edit")
390
  reconstruct_button = gr.Button("Show Reconstruction", visible=False)
391
- hide_reconstruct_button = gr.Button("Hide Reconstruction", visible=False)
392
- clear_button = gr.Button("Clear")
 
393
 
394
  with gr.Accordion("Advanced Options", open=False):
395
  with gr.Row():
@@ -401,9 +396,11 @@ with gr.Blocks(css='style.css') as demo:
401
  randomize_seed = gr.Checkbox(label='Randomize seed', value=False)
402
  with gr.Column():
403
  skip = gr.Slider(minimum=0, maximum=60, value=36, label="Skip Steps", interactive=True)
404
- tar_cfg_scale = gr.Slider(minimum=7, maximum=18,value=15, label=f"Guidance Scale", interactive=True)
405
 
406
 
 
 
407
 
408
  # with gr.Accordion("Help", open=False):
409
  # gr.Markdown(help_text)
@@ -421,7 +418,7 @@ with gr.Blocks(css='style.css') as demo:
421
  fn = randomize_seed_fn,
422
  inputs = [seed, randomize_seed],
423
  outputs = [seed],
424
- queue = False).then(
425
  fn=load_and_invert,
426
  inputs=[input_image,
427
  do_inversion,
@@ -435,7 +432,7 @@ with gr.Blocks(css='style.css') as demo:
435
  tar_cfg_scale
436
  ],
437
  outputs=[wts, zs, do_inversion, inversion_progress],
438
- ).success(
439
  fn=edit,
440
  inputs=[input_image,
441
  wts, zs,
@@ -452,41 +449,15 @@ with gr.Blocks(css='style.css') as demo:
452
 
453
  ],
454
  # outputs=[sega_edited_image, reconstruct_button]
455
- outputs=[sega_edited_image]
456
- ).then(fn=show_reconstruction_button, outputs=[reconstruct_button]).then(
457
- fn =reset_do_reconstruction, outputs=[do_reconstruction]
458
- )
459
 
460
- reconstruct_button.click(
461
- fn = show_reconstruction,
462
- outputs = [ddpm_edited_image]
463
- ).then(
464
- fn = reconstruct,
465
- inputs = [tar_prompt,
466
- tar_cfg_scale,
467
- skip,
468
- wts, zs, do_reconstruction,
469
- reconstruction, reconstruct_button, hide_reconstruct_button ],
470
- outputs = [ddpm_edited_image,reconstruction,do_reconstruction, reconstruct_button, hide_reconstruct_button]
471
- )
472
-
473
- clear_button.click(
474
- fn = clear,
475
- outputs = [tar_prompt,ddpm_edited_image, sega_edited_image, reconstruct_button, do_reconstruction]
476
- )
477
 
478
 
479
  # Automatically start inverting upon input_image change
480
  input_image.change(
481
  fn = reset_do_inversion,
482
  outputs = [do_inversion],
483
- queue = False).then(
484
- fn = hide_reconstruction_buttons,
485
- outputs = [reconstruct_button, hide_reconstruct_button],
486
- queue=False).then(
487
- fn = hide_reconstruction,
488
- outputs=[ddpm_edited_image],
489
- queue=False).then(fn = show_inversion_progress, outputs=[inversion_progress],queue=False).then(
490
  fn=load_and_invert,
491
  inputs=[input_image,
492
  do_inversion,
@@ -501,52 +472,58 @@ with gr.Blocks(css='style.css') as demo:
501
  ],
502
  # outputs=[ddpm_edited_image, wts, zs, do_inversion],
503
  outputs=[wts, zs, do_inversion, inversion_progress],
504
- ).then(fn = hide_inversion_progress, outputs=[inversion_progress],queue=False).then(
505
- fn =reset_do_reconstruction, outputs=[do_reconstruction])
506
-
507
- hide_reconstruct_button.click(fn = hide_reconstruction,
508
- outputs=[ddpm_edited_image],
509
- queue=False).then(fn = hide_hide_reconstruction_button,
510
- outputs=[reconstruct_button,
511
- hide_reconstruct_button])
512
 
513
 
514
  # Repeat inversion when these params are changed:
515
  src_prompt.change(
516
  fn = reset_do_inversion,
517
- outputs = [do_inversion], queue = False
518
- ).then(
519
- fn =reset_do_reconstruction, outputs=[do_reconstruction])
520
 
521
  steps.change(fn = reset_do_inversion,
522
- outputs = [do_inversion], queue = False).then(
523
- fn =reset_do_reconstruction, outputs=[do_reconstruction])
524
 
525
 
526
  src_cfg_scale.change(fn = reset_do_inversion,
527
- outputs = [do_inversion], queue = False).then(
528
- fn =reset_do_reconstruction, outputs=[do_reconstruction])
529
-
530
-
531
-
532
- gr.Examples(
533
- label='Examples',
534
- examples=get_example(),
535
- inputs=[input_image, src_prompt, tar_prompt, steps,
536
- # src_cfg_scale,
537
- skip,
538
- tar_cfg_scale,
539
- edit_concept_1,
540
- edit_concept_2,
541
- guidnace_scale_1,
542
- warmup_1,
543
- # neg_guidance,
544
- sega_edited_image
545
- ],
546
- outputs=[sega_edited_image],
547
- # fn=edit,
548
- # cache_examples=True
549
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
550
 
551
 
552
 
 
5
  import random
6
  from io import BytesIO
7
  from utils import *
8
+ from constants import *
9
  from inversion_utils import *
10
  from modified_pipeline_semantic_stable_diffusion import SemanticStableDiffusionPipeline
11
  from torch import autocast, inference_mode
 
215
 
216
  ########
217
  # demo #
218
+ ########
219
+
220
  intro = """
221
  <h1 style="font-weight: 1400; text-align: center; margin-bottom: 7px;">
222
  Edit Friendly DDPM X Semantic Guidance
 
271
  else:
272
  return row2.update(visible=True), row3.update(visible=True), add_concept_button.update(visible=False), 3
273
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
274
 
275
  def reset_do_inversion():
276
  do_inversion = True
277
  return do_inversion
278
 
 
 
 
279
 
280
+ def update_inversion_progress_visibility(do_inversion):
281
+ if do_inversion:
282
+ return inversion_progress.update(visible=True)
283
+ else:
284
  return inversion_progress.update(visible=False)
285
+
 
 
286
 
287
 
288
 
 
290
  wts = gr.State()
291
  zs = gr.State()
292
  do_inversion = gr.State(value=True)
 
293
  reconstruction = gr.State()
294
  sega_concepts_counter = gr.State(1)
295
 
296
+
 
297
 
298
  with gr.Row():
299
  input_image = gr.Image(label="Input Image", interactive=True)
 
315
  max_lines=1,
316
  placeholder="Enter your 1st edit prompt",
317
  )
318
+ caption_button = gr.Button("Caption Image")
319
  with gr.TabItem('2. Add SEGA edit concepts', id=1):
320
  # 1st SEGA concept
321
  with gr.Row().style(mobile_collapse=False, equal_height=True):
322
  neg_guidance_1 = gr.Checkbox(
323
  label='Negative Guidance')
324
+ warmup_1 = gr.Slider(label='Warmup', minimum=0, maximum=50,
325
+ value=DEFAULT_WARMUP_STEPS,
326
+ step=1, interactive=True)
327
+ guidnace_scale_1 = gr.Slider(label='Concept Guidance Scale', minimum=1, maximum=30,
328
+ value=DEFAULT_SEGA_CONCEPT_GUIDANCE_SCALE,
329
+ step=0.5, interactive=True)
330
+ threshold_1 = gr.Slider(label='Threshold', minimum=0.5, maximum=0.99,
331
+ value=DEFAULT_THRESHOLD, steps=0.01, interactive=True)
332
  edit_concept_1 = gr.Textbox(
333
  label="Edit Concept",
334
  show_label=False,
 
340
  with gr.Row(visible=False) as row2:
341
  neg_guidance_2 = gr.Checkbox(
342
  label='Negative Guidance',visible=True)
343
+ warmup_2 = gr.Slider(label='Warmup', minimum=0, maximum=50,
344
+ value=DEFAULT_WARMUP_STEPS,
345
+ step=1, interactive=True)
346
+ guidnace_scale_2 = gr.Slider(label='Concept Guidance Scale', minimum=1, maximum=30,
347
+ value=DEFAULT_SEGA_CONCEPT_GUIDANCE_SCALE,
348
+ step=0.5, interactive=True)
349
+ threshold_2 = gr.Slider(label='Threshold', minimum=0.5, maximum=0.99,
350
+ value=DEFAULT_THRESHOLD,
351
+ steps=0.01, interactive=True)
352
  edit_concept_2 = gr.Textbox(
353
  label="Edit Concept",
354
+ show_label=False,
355
  max_lines=1,
356
  placeholder="Enter your 2st edit prompt",
357
  )
 
359
  with gr.Row(visible=False) as row3:
360
  neg_guidance_3 = gr.Checkbox(
361
  label='Negative Guidance',visible=True)
362
+ warmup_3 = gr.Slider(label='Warmup', minimum=0, maximum=50,
363
+ value=DEFAULT_WARMUP_STEPS, step=1,
364
+ interactive=True)
365
+ guidnace_scale_3 = gr.Slider(label='Concept Guidance Scale', minimum=1, maximum=30,
366
+ value=DEFAULT_SEGA_CONCEPT_GUIDANCE_SCALE,
367
+ step=0.5, interactive=True)
368
+ threshold_3 = gr.Slider(label='Threshold', minimum=0.5, maximum=0.99,
369
+ value=DEFAULT_THRESHOLD, steps=0.01,
370
+ interactive=True)
371
  edit_concept_3 = gr.Textbox(
372
  label="Edit Concept",
373
+ show_label=False,
374
  max_lines=1,
375
  placeholder="Enter your 3rd edit prompt",
376
  )
 
382
  with gr.Row():
383
  run_button = gr.Button("Edit")
384
  reconstruct_button = gr.Button("Show Reconstruction", visible=False)
385
+ undo_button = gr.Button("Undo", visible=False)
386
+
387
+ clear_button = gr.ClearButton()
388
 
389
  with gr.Accordion("Advanced Options", open=False):
390
  with gr.Row():
 
396
  randomize_seed = gr.Checkbox(label='Randomize seed', value=False)
397
  with gr.Column():
398
  skip = gr.Slider(minimum=0, maximum=60, value=36, label="Skip Steps", interactive=True)
399
+ tar_cfg_scale = gr.Slider(minimum=7, maximum=30,value=15, label=f"Guidance Scale", interactive=True)
400
 
401
 
402
+
403
+
404
 
405
  # with gr.Accordion("Help", open=False):
406
  # gr.Markdown(help_text)
 
418
  fn = randomize_seed_fn,
419
  inputs = [seed, randomize_seed],
420
  outputs = [seed],
421
+ queue = False).then(fn = update_inversion_progress_visibility, inputs =[do_inversion], outputs=[inversion_progress],queue=False).then(
422
  fn=load_and_invert,
423
  inputs=[input_image,
424
  do_inversion,
 
432
  tar_cfg_scale
433
  ],
434
  outputs=[wts, zs, do_inversion, inversion_progress],
435
+ ).then(fn = update_inversion_progress_visibility, inputs =[do_inversion], outputs=[inversion_progress],queue=False).success(
436
  fn=edit,
437
  inputs=[input_image,
438
  wts, zs,
 
449
 
450
  ],
451
  # outputs=[sega_edited_image, reconstruct_button]
452
+ outputs=[sega_edited_image] )
 
 
 
453
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
454
 
455
 
456
  # Automatically start inverting upon input_image change
457
  input_image.change(
458
  fn = reset_do_inversion,
459
  outputs = [do_inversion],
460
+ queue = False).then(fn = update_inversion_progress_visibility, inputs =[do_inversion], outputs=[inversion_progress],queue=False).then(
 
 
 
 
 
 
461
  fn=load_and_invert,
462
  inputs=[input_image,
463
  do_inversion,
 
472
  ],
473
  # outputs=[ddpm_edited_image, wts, zs, do_inversion],
474
  outputs=[wts, zs, do_inversion, inversion_progress],
475
+ ).then(fn = update_inversion_progress_visibility, inputs =[do_inversion], outputs=[inversion_progress],queue=False)
 
 
 
 
 
 
 
476
 
477
 
478
  # Repeat inversion when these params are changed:
479
  src_prompt.change(
480
  fn = reset_do_inversion,
481
+ outputs = [do_inversion], queue = False)
 
 
482
 
483
  steps.change(fn = reset_do_inversion,
484
+ outputs = [do_inversion], queue = False)
 
485
 
486
 
487
  src_cfg_scale.change(fn = reset_do_inversion,
488
+ outputs = [do_inversion], queue = False)
489
+
490
+ components_to_clear = [input_image,ddpm_edited_image,sega_edited_image, do_inversion,
491
+ src_prompt, steps, src_cfg_scale, seed,
492
+ tar_prompt, skip, tar_cfg_scale,
493
+ edit_concept_1, guidnace_scale_1,warmup_1, threshold_1, neg_guidance_1,
494
+ edit_concept_2, guidnace_scale_2,warmup_2, threshold_2, neg_guidance_2,
495
+ edit_concept_3, guidnace_scale_3,warmup_3, threshold_3, neg_guidance_3,
496
+
497
+ ]
498
+ clear_output_vals = [None, None, None, True,
499
+ "", DEFAULT_DIFFUSION_STEPS, DEFAULT_SOURCE_GUIDANCE_SCALE, DEFAULT_SEED,
500
+ "", DEFAULT_SKIP_STEPS, DEFAULT_TARGET_GUIDANCE_SCALE,
501
+ "", DEFAULT_SEGA_CONCEPT_GUIDANCE_SCALE, DEFAULT_WARMUP_STEPS, DEFAULT_THRESHOLD, DEFAULT_NEGATIVE_GUIDANCE,
502
+ "", DEFAULT_SEGA_CONCEPT_GUIDANCE_SCALE, DEFAULT_WARMUP_STEPS, DEFAULT_THRESHOLD, DEFAULT_NEGATIVE_GUIDANCE,
503
+ "", DEFAULT_SEGA_CONCEPT_GUIDANCE_SCALE, DEFAULT_WARMUP_STEPS, DEFAULT_THRESHOLD, DEFAULT_NEGATIVE_GUIDANCE,
504
+ ]
505
+ clear_button.click(lambda:clear_output_vals, outputs =components_to_clear)
506
+
507
+
508
+ # gr.Examples(
509
+ # label='Examples',
510
+ # examples=get_example(),
511
+ # inputs=[input_image, src_prompt, tar_prompt, steps,
512
+ # # src_cfg_scale,
513
+ # skip,
514
+ # tar_cfg_scale,
515
+ # edit_concept_1,
516
+ # edit_concept_2,
517
+ # guidnace_scale_1,
518
+ # warmup_1,
519
+ # # neg_guidance,
520
+ # sega_edited_image
521
+ # ],
522
+ # outputs=[sega_edited_image],
523
+ # # fn=edit,
524
+ # # cache_examples=True
525
+ # )
526
+
527
 
528
 
529