Linoy Tsaban commited on
Commit
3718eb4
1 Parent(s): 8c532f2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -40
app.py CHANGED
@@ -265,9 +265,8 @@ help_text = """
265
  2. `Concept Guidance Scale` (SEGA)
266
  """
267
 
268
- with gr.Blocks(css='style.css') as demo:
269
 
270
-
271
  def add_concept(sega_concepts_counter):
272
  if sega_concepts_counter == 1:
273
  return row2.update(visible=True), row2_advanced.update(visible=True), row3.update(visible=False), row3_advanced.update(visible=False), add_concept_button.update(visible=True), 2
@@ -276,22 +275,30 @@ with gr.Blocks(css='style.css') as demo:
276
 
277
  def update_display_concept_1(add_1, edit_concept_1):
278
  if add_1 == 'Add':
279
- return edit_concept_1, concept_1.update(visible=True), edit_concept_1, guidnace_scale_1.update(visible=True), "Clear"
280
  else: # remove
281
- return "", concept_1.update(visible=False), "", guidnace_scale_1.update(visible=False), "Add"
282
 
283
  def update_display_concept_2(add_2, edit_concept_2):
284
  if add_2 == 'Add':
285
- return edit_concept_2, concept_2.update(visible=True),edit_concept_2, guidnace_scale_2.update(visible=True), "Clear"
286
  else: # remove
287
- return "", concept_2.update(visible=False), "", guidnace_scale_2.update(visible=False), "Add"
288
 
289
  def update_display_concept_3(add_3, edit_concept_3):
290
  if add_3 == 'Add':
291
- return edit_concept_3, concept_3.update(visible=True), edit_concept_3, guidnace_scale_3.update(visible=True), "Clear"
292
  else: # remove
293
- return "", concept_3.update(visible=False), "", guidnace_scale_3.update(visible=False), "Add"
294
-
 
 
 
 
 
 
 
 
295
 
296
 
297
 
@@ -309,6 +316,9 @@ with gr.Blocks(css='style.css') as demo:
309
  else:
310
  return inversion_progress.update(visible=False)
311
 
 
 
 
312
 
313
  gr.HTML(intro)
314
  wts = gr.State()
@@ -319,6 +329,20 @@ with gr.Blocks(css='style.css') as demo:
319
  sega_concepts_counter = gr.State(1)
320
 
321
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
322
  with gr.Row():
323
  input_image = gr.Image(label="Input Image", interactive=True)
324
  ddpm_edited_image = gr.Image(label=f"DDPM Reconstructed Image", interactive=False, visible=False)
@@ -328,28 +352,28 @@ with gr.Blocks(css='style.css') as demo:
328
  sega_edited_image.style(height=365, width=365)
329
 
330
  with gr.Row():
331
- with gr.Column() as col1:
332
- concept_1 = gr.Button(visible=False)
333
- guidnace_scale_1 = gr.Slider(label='Concept Guidance Scale', minimum=1, maximum=30,
 
 
 
 
 
334
  value=DEFAULT_SEGA_CONCEPT_GUIDANCE_SCALE,
335
  step=0.5, interactive=True,visible=False)
336
- with gr.Column() as col2:
337
- concept_2 = gr.Button(visible=False)
338
- guidnace_scale_2 = gr.Slider(label='Concept Guidance Scale', minimum=1, maximum=30,
339
- value=DEFAULT_SEGA_CONCEPT_GUIDANCE_SCALE,
340
- step=0.5, interactive=True,visible=False)
341
- with gr.Column() as col3:
342
- concept_3 = gr.Button(visible=False)
343
- guidnace_scale_3 = gr.Slider(label='Concept Guidance Scale', minimum=1, maximum=30,
344
  value=DEFAULT_SEGA_CONCEPT_GUIDANCE_SCALE,
345
  step=0.5, interactive=True,visible=False)
346
 
347
 
348
-
349
- with gr.Row():
350
- gallery = gr.Gallery(visible = False).style(
351
- columns=3,
352
- object_fit='contain')
353
 
354
 
355
 
@@ -416,20 +440,20 @@ with gr.Blocks(css='style.css') as demo:
416
  add_3 = gr.Button('Add')
417
 
418
 
419
-
420
 
421
  with gr.Row().style(mobile_collapse=False, equal_height=True):
422
  add_concept_button = gr.Button("+")
423
 
424
 
425
  with gr.Row():
426
- run_button = gr.Button("Edit")
427
  reconstruct_button = gr.Button("Show Reconstruction", visible=False)
428
- clear_button = gr.Button("Clear")
429
 
430
  with gr.Accordion("Advanced Options", open=False):
431
  with gr.Tabs() as tabs:
432
-
433
  with gr.TabItem('General options', id=2):
434
  with gr.Row():
435
  with gr.Column():
@@ -442,8 +466,8 @@ with gr.Blocks(css='style.css') as demo:
442
  with gr.Column():
443
  skip = gr.Slider(minimum=0, maximum=60, value=36, label="Skip Steps", interactive=True)
444
  tar_cfg_scale = gr.Slider(minimum=7, maximum=30,value=15, label=f"Guidance Scale", interactive=True)
445
-
446
- with gr.TabItem('SEGA options', id=3):
447
  # 1st SEGA concept
448
  with gr.Row().style(mobile_collapse=False, equal_height=True):
449
  warmup_1 = gr.Slider(label='Warmup', minimum=0, maximum=50,
@@ -475,9 +499,9 @@ with gr.Blocks(css='style.css') as demo:
475
  outputs = [tar_prompt]
476
  )
477
 
478
- add_1.click(fn = update_display_concept_1, inputs=[add_1, edit_concept_1], outputs=[concept_1, concept_1, edit_concept_1, guidnace_scale_1, add_1])
479
- add_2.click(fn = update_display_concept_2, inputs=[add_2, edit_concept_2], outputs=[concept_2, concept_2, edit_concept_2, guidnace_scale_2, add_2])
480
- add_3.click(fn = update_display_concept_3, inputs=[add_3, edit_concept_3], outputs=[concept_3, concept_3, edit_concept_3, guidnace_scale_3, add_3])
481
 
482
 
483
  add_concept_button.click(fn = add_concept, inputs=sega_concepts_counter,
@@ -513,6 +537,7 @@ with gr.Blocks(css='style.css') as demo:
513
 
514
  ],
515
  outputs=[sega_edited_image, reconstruct_button])
 
516
 
517
 
518
 
@@ -584,16 +609,16 @@ with gr.Blocks(css='style.css') as demo:
584
  clear_components = [input_image,ddpm_edited_image,ddpm_edited_image,sega_edited_image, do_inversion,
585
  src_prompt, steps, src_cfg_scale, seed,
586
  tar_prompt, skip, tar_cfg_scale, reconstruct_button,reconstruct_button,
587
- edit_concept_1, guidnace_scale_1,warmup_1, threshold_1, neg_guidance_1,
588
- edit_concept_2, guidnace_scale_2,warmup_2, threshold_2, neg_guidance_2,
589
- edit_concept_3, guidnace_scale_3,warmup_3, threshold_3, neg_guidance_3,]
590
 
591
  clear_components_output_vals = [None, None,ddpm_edited_image.update(visible=False), None, True,
592
  "", DEFAULT_DIFFUSION_STEPS, DEFAULT_SOURCE_GUIDANCE_SCALE, DEFAULT_SEED,
593
  "", DEFAULT_SKIP_STEPS, DEFAULT_TARGET_GUIDANCE_SCALE, reconstruct_button.update(value="Show Reconstruction"),reconstruct_button.update(visible=False),
594
- "", DEFAULT_SEGA_CONCEPT_GUIDANCE_SCALE, DEFAULT_WARMUP_STEPS, DEFAULT_THRESHOLD, DEFAULT_NEGATIVE_GUIDANCE,
595
- "", DEFAULT_SEGA_CONCEPT_GUIDANCE_SCALE, DEFAULT_WARMUP_STEPS, DEFAULT_THRESHOLD, DEFAULT_NEGATIVE_GUIDANCE,
596
- "", DEFAULT_SEGA_CONCEPT_GUIDANCE_SCALE, DEFAULT_WARMUP_STEPS, DEFAULT_THRESHOLD, DEFAULT_NEGATIVE_GUIDANCE,
597
  ]
598
 
599
 
 
265
  2. `Concept Guidance Scale` (SEGA)
266
  """
267
 
268
+ with gr.Blocks(css=css) as demo:
269
 
 
270
  def add_concept(sega_concepts_counter):
271
  if sega_concepts_counter == 1:
272
  return row2.update(visible=True), row2_advanced.update(visible=True), row3.update(visible=False), row3_advanced.update(visible=False), add_concept_button.update(visible=True), 2
 
275
 
276
  def update_display_concept_1(add_1, edit_concept_1):
277
  if add_1 == 'Add':
278
+ return box1.update(visible=True), edit_concept_1, concept_1.update(visible=True), edit_concept_1, guidnace_scale_1.update(visible=True), "Clear"
279
  else: # remove
280
+ return box1.update(visible=False),"", concept_1.update(visible=False), "", guidnace_scale_1.update(visible=False), "Add"
281
 
282
  def update_display_concept_2(add_2, edit_concept_2):
283
  if add_2 == 'Add':
284
+ return box2.update(visible=True), edit_concept_2, concept_2.update(visible=True),edit_concept_2, guidnace_scale_2.update(visible=True), "Clear"
285
  else: # remove
286
+ return box2.update(visible=False),"", concept_2.update(visible=False), "", guidnace_scale_2.update(visible=False), "Add"
287
 
288
  def update_display_concept_3(add_3, edit_concept_3):
289
  if add_3 == 'Add':
290
+ return box3.update(visible=True), edit_concept_3, concept_3.update(visible=True), edit_concept_3, guidnace_scale_3.update(visible=True), "Clear"
291
  else: # remove
292
+ return box3.update(visible=False), "", concept_3.update(visible=False), "", guidnace_scale_3.update(visible=False), "Add"
293
+
294
+ def display_editing_options(run_button, clear_button, sega_tab):
295
+ return run_button.update(visible=True), clear_button.update(visible=True), sega_tab.update(visible=True)
296
+
297
+ # def update_gallery_display(prev_output_image, sega_edited_image):
298
+ # if prev_output_image is None:
299
+ # return sega_edited_image, gallery.update(visible=True), sega_edited_image
300
+ # else:
301
+ # return prev_output_image, gallery.update(visible=True), sega_edited_image
302
 
303
 
304
 
 
316
  else:
317
  return inversion_progress.update(visible=False)
318
 
319
+ def undo():
320
+ return
321
+
322
 
323
  gr.HTML(intro)
324
  wts = gr.State()
 
329
  sega_concepts_counter = gr.State(1)
330
 
331
 
332
+ #Undo / gallery
333
+ # prev_wts = gr.State()
334
+ # prev_zs = gr.State()
335
+ # prev_src_prompt = gr.State()
336
+ # prev_tar_prompt = gr.State()
337
+ # prev_tar_guidance_scale = gr.State()
338
+ # prev_src_guidance_scale = gr.State()
339
+ # prev_num_steps = gr.State()
340
+ # prev_skip = gr.State()
341
+ # prev_output_image = gr.Image(visible=False)
342
+ # prev_input_image = gr.State()
343
+
344
+
345
+
346
  with gr.Row():
347
  input_image = gr.Image(label="Input Image", interactive=True)
348
  ddpm_edited_image = gr.Image(label=f"DDPM Reconstructed Image", interactive=False, visible=False)
 
352
  sega_edited_image.style(height=365, width=365)
353
 
354
  with gr.Row():
355
+ with gr.Box(visible=False) as box1:
356
+ concept_1 = gr.Button(visible=False)
357
+ guidnace_scale_1 = gr.Slider(label='Concept Guidance Scale', minimum=1, maximum=30,
358
+ value=DEFAULT_SEGA_CONCEPT_GUIDANCE_SCALE,
359
+ step=0.5, interactive=True,visible=False)
360
+ with gr.Box(visible=False) as box2:
361
+ concept_2 = gr.Button(visible=False)
362
+ guidnace_scale_2 = gr.Slider(label='Concept Guidance Scale', minimum=1, maximum=30,
363
  value=DEFAULT_SEGA_CONCEPT_GUIDANCE_SCALE,
364
  step=0.5, interactive=True,visible=False)
365
+ with gr.Box(visible=False) as box3:
366
+ concept_3 = gr.Button(visible=False)
367
+ guidnace_scale_3 = gr.Slider(label='Concept Guidance Scale', minimum=1, maximum=30,
 
 
 
 
 
368
  value=DEFAULT_SEGA_CONCEPT_GUIDANCE_SCALE,
369
  step=0.5, interactive=True,visible=False)
370
 
371
 
372
+
373
+ # with gr.Row():
374
+ # gallery = gr.Gallery(label = "History", visible = True).style(
375
+ # columns=1,rows=1,
376
+ # object_fit='contain')
377
 
378
 
379
 
 
440
  add_3 = gr.Button('Add')
441
 
442
 
443
+
444
 
445
  with gr.Row().style(mobile_collapse=False, equal_height=True):
446
  add_concept_button = gr.Button("+")
447
 
448
 
449
  with gr.Row():
450
+ run_button = gr.Button("Edit", visible=True)
451
  reconstruct_button = gr.Button("Show Reconstruction", visible=False)
452
+ clear_button = gr.Button("Clear", visible=True)
453
 
454
  with gr.Accordion("Advanced Options", open=False):
455
  with gr.Tabs() as tabs:
456
+
457
  with gr.TabItem('General options', id=2):
458
  with gr.Row():
459
  with gr.Column():
 
466
  with gr.Column():
467
  skip = gr.Slider(minimum=0, maximum=60, value=36, label="Skip Steps", interactive=True)
468
  tar_cfg_scale = gr.Slider(minimum=7, maximum=30,value=15, label=f"Guidance Scale", interactive=True)
469
+
470
+ with gr.TabItem('SEGA options', id=3) as sega_advanced_tab:
471
  # 1st SEGA concept
472
  with gr.Row().style(mobile_collapse=False, equal_height=True):
473
  warmup_1 = gr.Slider(label='Warmup', minimum=0, maximum=50,
 
499
  outputs = [tar_prompt]
500
  )
501
 
502
+ add_1.click(fn = update_display_concept_1, inputs=[add_1, edit_concept_1], outputs=[box1, concept_1, concept_1, edit_concept_1, guidnace_scale_1, add_1])
503
+ add_2.click(fn = update_display_concept_2, inputs=[add_2, edit_concept_2], outputs=[box2, concept_2, concept_2, edit_concept_2, guidnace_scale_2, add_2])
504
+ add_3.click(fn = update_display_concept_3, inputs=[add_3, edit_concept_3], outputs=[box3, concept_3, concept_3, edit_concept_3, guidnace_scale_3, add_3])
505
 
506
 
507
  add_concept_button.click(fn = add_concept, inputs=sega_concepts_counter,
 
537
 
538
  ],
539
  outputs=[sega_edited_image, reconstruct_button])
540
+ # .success(fn=update_gallery_display, inputs= [prev_output_image, sega_edited_image], outputs = [gallery, gallery, prev_output_image])
541
 
542
 
543
 
 
609
  clear_components = [input_image,ddpm_edited_image,ddpm_edited_image,sega_edited_image, do_inversion,
610
  src_prompt, steps, src_cfg_scale, seed,
611
  tar_prompt, skip, tar_cfg_scale, reconstruct_button,reconstruct_button,
612
+ edit_concept_1, guidnace_scale_1,guidnace_scale_1,warmup_1, threshold_1, neg_guidance_1, concept_1, concept_1,
613
+ edit_concept_2, guidnace_scale_2,guidnace_scale_2,warmup_2, threshold_2, neg_guidance_2, concept_2, concept_2, row2, row2_advanced,
614
+ edit_concept_3, guidnace_scale_3,guidnace_scale_3,warmup_3, threshold_3, neg_guidance_3, concept_3,concept_3, row3, row3_advanced ]
615
 
616
  clear_components_output_vals = [None, None,ddpm_edited_image.update(visible=False), None, True,
617
  "", DEFAULT_DIFFUSION_STEPS, DEFAULT_SOURCE_GUIDANCE_SCALE, DEFAULT_SEED,
618
  "", DEFAULT_SKIP_STEPS, DEFAULT_TARGET_GUIDANCE_SCALE, reconstruct_button.update(value="Show Reconstruction"),reconstruct_button.update(visible=False),
619
+ "", DEFAULT_SEGA_CONCEPT_GUIDANCE_SCALE,guidnace_scale_1.update(visible=False), DEFAULT_WARMUP_STEPS, DEFAULT_THRESHOLD, DEFAULT_NEGATIVE_GUIDANCE, "", concept_1.update(visible=False),
620
+ "", DEFAULT_SEGA_CONCEPT_GUIDANCE_SCALE,guidnace_scale_2.update(visible=False), DEFAULT_WARMUP_STEPS, DEFAULT_THRESHOLD, DEFAULT_NEGATIVE_GUIDANCE, "", concept_2.update(visible=False), row2.update(visible=False), row2_advanced.update(visible=False),
621
+ "", DEFAULT_SEGA_CONCEPT_GUIDANCE_SCALE,guidnace_scale_3.update(visible=False), DEFAULT_WARMUP_STEPS, DEFAULT_THRESHOLD, DEFAULT_NEGATIVE_GUIDANCE, "",concept_3.update(visible=False), row3.update(visible=False), row3_advanced.update(visible=False)
622
  ]
623
 
624