huzey commited on
Commit
6a4b8cd
·
1 Parent(s): fc1e5fe

add delete button

Browse files
Files changed (1) hide show
  1. app.py +42 -20
app.py CHANGED
@@ -2437,13 +2437,19 @@ with demo:
2437
  run_inspection_button = gr.Button("🔴 RUN Inspection", elem_id="run_inspection", variant='primary')
2438
  inspect_logging_text = gr.Textbox("Logging information", lines=3, label="Logging", elem_id="inspect_logging", type="text", placeholder="Logging information", autofocus=False, autoscroll=False)
2439
  # output_slot_radio = gr.Radio([1, 2, 3], label="Output Row", value=1, elem_id="output_slot", show_label=True)
2440
-
 
 
2441
  image_select.visible = False
2442
  tsne_select.visible = True
2443
  prompt_radio.change(fn=lambda x: gr.update(visible=x=="Tree"), inputs=prompt_radio, outputs=[tsne_select])
2444
  prompt_radio.change(fn=lambda x: gr.update(visible=x=="Image"), inputs=prompt_radio, outputs=[image_select])
2445
 
2446
- def make_one_output_row(i_row=1):
 
 
 
 
2447
  with gr.Row() as inspect_output_row:
2448
  with gr.Column(scale=5, min_width=200):
2449
  output_tree_image = gr.Image(label=f"spectral-tSNE tree [row#{i_row}]", elem_id="output_image", interactive=False)
@@ -2451,21 +2457,28 @@ with demo:
2451
  delete_button = gr.Button("❌ Delete", elem_id=f"delete_button_{i_row}", variant='secondary')
2452
  with gr.Column(scale=10, min_width=200):
2453
  heatmap_gallery = gr.Gallery(format='png', value=[], label=f"Cluster Heatmap [row#{i_row}]", show_label=True, elem_id="heatmap", columns=[6], rows=[1], object_fit="contain", height="500px", show_share_button=True, interactive=False)
2454
- delete_button.click(lambda: gr.update(visible=False), outputs=[inspect_output_row])
 
 
 
2455
  return inspect_output_row, output_tree_image, heatmap_gallery, text_block
2456
 
2457
  gr.Markdown('---')
2458
- MAX_ROWS = 10
2459
- current_output_row = gr.State(MAX_ROWS-1)
2460
  inspect_output_rows, output_tree_images, heatmap_galleries, text_blocks = [], [], [], []
2461
  for i_row in range(MAX_ROWS, 0, -1):
2462
- inspect_output_row, output_tree_image, heatmap_gallery, text_block = make_one_output_row(i_row)
2463
  inspect_output_row.visible = False
2464
  inspect_output_rows.append(inspect_output_row)
2465
  output_tree_images.append(output_tree_image)
2466
  heatmap_galleries.append(heatmap_gallery)
2467
  text_blocks.append(text_block)
2468
 
 
 
 
 
 
2469
 
2470
  def relative_xy_last_positive(prompts):
2471
  image = prompts['image']
@@ -2522,7 +2535,7 @@ with demo:
2522
  except:
2523
  raise gr.Error("""No blue point is selected. <br/>Please left-click on the image to select a blue point. <br/>After reloading the image (e.g., change granularity), please use the eraser to remove the previous point, then click on the image to select a blue point.""")
2524
 
2525
- def run_inspection(tsne_prompt, image_prompt, prompt_radio, output_slot, tsne2d_embed, edges, fps_eigvecs, fps_tsne_rgb, fps_indices, granularity, eigvecs, i_image, tsne3d_rgb, input_gallery, max_rows=MAX_ROWS):
2526
  if len(tsne2d_embed) == 0:
2527
  raise gr.Error("Please run FPS+Cluster first.")
2528
  closest_idx = find_closest_fps_point(prompt_radio, tsne_prompt, image_prompt, i_image, tsne2d_embed, eigvecs, fps_eigvecs)
@@ -2575,31 +2588,40 @@ with demo:
2575
  blend = Image.fromarray(blend.astype(np.uint8))
2576
  heatmap_images[i] = blend
2577
 
 
 
 
 
 
 
 
 
 
 
 
2578
  # tree_label = f"spectral-tSNE tree [row#{max_rows-output_slot}] k={granularity} idx={closest_idx} n={len(connected_idxs)}"
2579
- tree_label = f"spectral-tSNE tree [row#{max_rows-output_slot}]"
2580
- heatmap_label = f"Cluster Heatmap [row#{max_rows-output_slot}] k={granularity} idx={closest_idx} n={len(connected_idxs)}"
2581
  # update the output slots
2582
  output_rows = [gr.update() for _ in range(max_rows)]
2583
  output_tsne_plots = [gr.update() for _ in range(max_rows)]
2584
  output_heatmaps = [gr.update() for _ in range(max_rows)]
2585
  output_texts = [gr.update() for _ in range(max_rows)]
2586
- output_rows[output_slot] = gr.update(visible=True)
2587
- output_tsne_plots[output_slot] = gr.update(value=output_tsne_plot, label=tree_label)
2588
- output_heatmaps[output_slot] = gr.update(value=heatmap_images, label=heatmap_label)
2589
- output_texts[output_slot] = gr.update(value=logging_text)
2590
  # gr.Info(f"Output in [row#{max_rows-output_slot}]", 3)
2591
- logging_text += f"\nOutput: [row#{max_rows-output_slot}]"
2592
- output_slot -= 1
2593
- if output_slot < 0:
2594
- output_slot = max_rows - 1
2595
 
2596
- return *output_rows, *output_tsne_plots, *output_heatmaps, *output_texts, output_slot, logging_text
2597
 
2598
 
2599
  run_inspection_button.click(
2600
  run_inspection,
2601
- inputs=[tsne_prompt_image, image_plot, prompt_radio, current_output_row, tsne_2d_points, edges, fps_eigvecs, fps_tsne_rgb, fps_indices, granularity_slider, eigvecs, image_slider, tsne3d_rgb, input_gallery],
2602
- outputs=inspect_output_rows + output_tree_images + heatmap_galleries + text_blocks + [current_output_row, inspect_logging_text],
2603
  )
2604
 
2605
  with gr.Tab('PlayGround (eig)', visible=True) as test_playground_tab2:
 
2437
  run_inspection_button = gr.Button("🔴 RUN Inspection", elem_id="run_inspection", variant='primary')
2438
  inspect_logging_text = gr.Textbox("Logging information", lines=3, label="Logging", elem_id="inspect_logging", type="text", placeholder="Logging information", autofocus=False, autoscroll=False)
2439
  # output_slot_radio = gr.Radio([1, 2, 3], label="Output Row", value=1, elem_id="output_slot", show_label=True)
2440
+
2441
+ delete_all_output_button = gr.Button("❌ Delete All", elem_id="delete_all_output", variant='secondary')
2442
+
2443
  image_select.visible = False
2444
  tsne_select.visible = True
2445
  prompt_radio.change(fn=lambda x: gr.update(visible=x=="Tree"), inputs=prompt_radio, outputs=[tsne_select])
2446
  prompt_radio.change(fn=lambda x: gr.update(visible=x=="Image"), inputs=prompt_radio, outputs=[image_select])
2447
 
2448
+ MAX_ROWS = 20
2449
+ current_output_row = gr.State(0)
2450
+ output_row_occupy = gr.State([False] * MAX_ROWS)
2451
+
2452
+ def make_one_output_row(output_row_occupy, i_row=1):
2453
  with gr.Row() as inspect_output_row:
2454
  with gr.Column(scale=5, min_width=200):
2455
  output_tree_image = gr.Image(label=f"spectral-tSNE tree [row#{i_row}]", elem_id="output_image", interactive=False)
 
2457
  delete_button = gr.Button("❌ Delete", elem_id=f"delete_button_{i_row}", variant='secondary')
2458
  with gr.Column(scale=10, min_width=200):
2459
  heatmap_gallery = gr.Gallery(format='png', value=[], label=f"Cluster Heatmap [row#{i_row}]", show_label=True, elem_id="heatmap", columns=[6], rows=[1], object_fit="contain", height="500px", show_share_button=True, interactive=False)
2460
+ def delete_a_row(output_row_occupy, i_row=1):
2461
+ # output_row_occupy[i_row-1] = False
2462
+ return output_row_occupy, gr.update(visible=False)
2463
+ delete_button.click(partial(delete_a_row, i_row=i_row), output_row_occupy, outputs=[output_row_occupy, inspect_output_row])
2464
  return inspect_output_row, output_tree_image, heatmap_gallery, text_block
2465
 
2466
  gr.Markdown('---')
2467
+
 
2468
  inspect_output_rows, output_tree_images, heatmap_galleries, text_blocks = [], [], [], []
2469
  for i_row in range(MAX_ROWS, 0, -1):
2470
+ inspect_output_row, output_tree_image, heatmap_gallery, text_block = make_one_output_row(output_row_occupy, i_row)
2471
  inspect_output_row.visible = False
2472
  inspect_output_rows.append(inspect_output_row)
2473
  output_tree_images.append(output_tree_image)
2474
  heatmap_galleries.append(heatmap_gallery)
2475
  text_blocks.append(text_block)
2476
 
2477
+ def delete_all_output(output_row_occupy):
2478
+ n_rows = len(output_row_occupy)
2479
+ output_row_occupy = [False] * n_rows
2480
+ return output_row_occupy, *[gr.update(visible=False) for _ in range(n_rows)]
2481
+ delete_all_output_button.click(delete_all_output, inputs=[output_row_occupy], outputs=[output_row_occupy, *inspect_output_rows])
2482
 
2483
  def relative_xy_last_positive(prompts):
2484
  image = prompts['image']
 
2535
  except:
2536
  raise gr.Error("""No blue point is selected. <br/>Please left-click on the image to select a blue point. <br/>After reloading the image (e.g., change granularity), please use the eraser to remove the previous point, then click on the image to select a blue point.""")
2537
 
2538
+ def run_inspection(tsne_prompt, image_prompt, prompt_radio, current_output_row, tsne2d_embed, edges, fps_eigvecs, fps_tsne_rgb, fps_indices, granularity, eigvecs, i_image, tsne3d_rgb, input_gallery, output_row_occupy, max_rows=MAX_ROWS):
2539
  if len(tsne2d_embed) == 0:
2540
  raise gr.Error("Please run FPS+Cluster first.")
2541
  closest_idx = find_closest_fps_point(prompt_radio, tsne_prompt, image_prompt, i_image, tsne2d_embed, eigvecs, fps_eigvecs)
 
2588
  blend = Image.fromarray(blend.astype(np.uint8))
2589
  heatmap_images[i] = blend
2590
 
2591
+ # find the output slot
2592
+ # search from the last row
2593
+ found_flag = False
2594
+ for i_slot in range(max_rows-1, -1, -1):
2595
+ if not output_row_occupy[i_slot]:
2596
+ found_flag = True
2597
+ break
2598
+ if not found_flag:
2599
+ i_slot = 0
2600
+ gr.Warning("Output slots are full, Overwriting the first row. Please use '❌ Delete All' to clear all outputs.")
2601
+ output_row_occupy[i_slot] = True
2602
  # tree_label = f"spectral-tSNE tree [row#{max_rows-output_slot}] k={granularity} idx={closest_idx} n={len(connected_idxs)}"
2603
+ tree_label = f"spectral-tSNE tree [row#{current_output_row+1}]"
2604
+ heatmap_label = f"Cluster Heatmap [row#{current_output_row+1}] k={granularity} idx={closest_idx} n={len(connected_idxs)}"
2605
  # update the output slots
2606
  output_rows = [gr.update() for _ in range(max_rows)]
2607
  output_tsne_plots = [gr.update() for _ in range(max_rows)]
2608
  output_heatmaps = [gr.update() for _ in range(max_rows)]
2609
  output_texts = [gr.update() for _ in range(max_rows)]
2610
+ output_rows[i_slot] = gr.update(visible=True)
2611
+ output_tsne_plots[i_slot] = gr.update(value=output_tsne_plot, label=tree_label)
2612
+ output_heatmaps[i_slot] = gr.update(value=heatmap_images, label=heatmap_label)
2613
+ output_texts[i_slot] = gr.update(value=logging_text)
2614
  # gr.Info(f"Output in [row#{max_rows-output_slot}]", 3)
2615
+ logging_text += f"\nOutput: [row#{current_output_row+1}]"
2616
+ current_output_row += 1
 
 
2617
 
2618
+ return *output_rows, *output_tsne_plots, *output_heatmaps, *output_texts, current_output_row, output_row_occupy, logging_text
2619
 
2620
 
2621
  run_inspection_button.click(
2622
  run_inspection,
2623
+ inputs=[tsne_prompt_image, image_plot, prompt_radio, current_output_row, tsne_2d_points, edges, fps_eigvecs, fps_tsne_rgb, fps_indices, granularity_slider, eigvecs, image_slider, tsne3d_rgb, input_gallery, output_row_occupy],
2624
+ outputs=inspect_output_rows + output_tree_images + heatmap_galleries + text_blocks + [current_output_row, output_row_occupy, inspect_logging_text],
2625
  )
2626
 
2627
  with gr.Tab('PlayGround (eig)', visible=True) as test_playground_tab2: