huzey commited on
Commit
d73ecb9
1 Parent(s): 8898a47
Files changed (1) hide show
  1. app.py +42 -27
app.py CHANGED
@@ -1,13 +1,13 @@
1
  # Author: Huzheng Yang
2
  # %%
3
- USE_SPACES = True
4
  BATCH_SIZE = 4
5
 
6
- if USE_SPACES: # huggingface ZeroGPU, dynamic GPU allocation
7
  try:
8
  import spaces
9
  except ImportError:
10
- USE_SPACES = False # run on local machine
11
  BATCH_SIZE = 1
12
 
13
  import os
@@ -309,7 +309,7 @@ def _ncut_run(*args, **kwargs):
309
  torch.cuda.empty_cache()
310
  return [], "Error: " + str(e)
311
 
312
- if USE_SPACES:
313
  @spaces.GPU(duration=20)
314
  def quick_run(*args, **kwargs):
315
  return _ncut_run(*args, **kwargs)
@@ -326,7 +326,7 @@ if USE_SPACES:
326
  def super_duper_long_run(*args, **kwargs):
327
  return _ncut_run(*args, **kwargs)
328
 
329
- if not USE_SPACES:
330
  def quick_run(*args, **kwargs):
331
  return _ncut_run(*args, **kwargs)
332
 
@@ -582,7 +582,7 @@ def make_parameters_section():
582
  from backbone import get_all_model_names
583
  model_names = get_all_model_names()
584
  model_dropdown = gr.Dropdown(model_names, label="Backbone", value="DiNO(dino_vitb8)", elem_id="model_name")
585
- layer_slider = gr.Slider(1, 12, step=1, label="Backbone: Layer index", value=12, elem_id="layer")
586
  node_type_dropdown = gr.Dropdown(["attn: attention output", "mlp: mlp output", "block: sum of residual"], label="Backbone: Layer type", value="block: sum of residual", elem_id="node_type", info="which feature to take from each layer?")
587
  num_eig_slider = gr.Slider(1, 1000, step=1, label="NCUT: Number of eigenvectors", value=100, elem_id="num_eig", info='increase for more clusters')
588
 
@@ -827,7 +827,7 @@ with gr.Blocks() as demo:
827
  with gr.Tab('Compare'):
828
  def add_one_model(i_model=1):
829
  with gr.Column(scale=5, min_width=200) as col:
830
- gr.Markdown(f'### Output Model {i_model}')
831
  output_gallery = gr.Gallery(value=[], label="NCUT Embedding", show_label=False, elem_id=f"ncut{i_model}", columns=[3], rows=[1], object_fit="contain", height="auto")
832
  submit_button = gr.Button("🔴RUN", elem_id=f"submit_button{i_model}")
833
  [
@@ -864,29 +864,44 @@ with gr.Blocks() as demo:
864
  load_images_button.click(lambda x: default_images, outputs=input_gallery)
865
 
866
 
867
- for i in range(1, 3):
868
- add_one_model(i)
869
 
870
- with gr.Row():
871
- for i in range(1, 4):
872
- with gr.Column(scale=5, min_width=200):
873
- slot = gr.Button("Add model", elem_id=f"add_model_button{i}")
874
- col = add_one_model(i+2)
875
- col.visible = False
876
-
877
- slot.click(
878
- fn=lambda x: gr.update(visible=True),
879
- outputs=col
880
- )
881
- slot.click(
882
- fn=lambda x: gr.update(visible=False),
883
- outputs=slot
884
- )
885
 
 
 
 
 
 
 
 
 
886
 
 
 
887
 
888
- download_all_models()
889
- download_all_datasets()
890
- demo.launch(share=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
891
 
892
  # %%
 
1
  # Author: Huzheng Yang
2
  # %%
3
+ HUGGINGFACE_SPACE = True
4
  BATCH_SIZE = 4
5
 
6
+ if HUGGINGFACE_SPACE: # huggingface ZeroGPU, dynamic GPU allocation
7
  try:
8
  import spaces
9
  except ImportError:
10
+ HUGGINGFACE_SPACE = False # run on local machine
11
  BATCH_SIZE = 1
12
 
13
  import os
 
309
  torch.cuda.empty_cache()
310
  return [], "Error: " + str(e)
311
 
312
+ if HUGGINGFACE_SPACE:
313
  @spaces.GPU(duration=20)
314
  def quick_run(*args, **kwargs):
315
  return _ncut_run(*args, **kwargs)
 
326
  def super_duper_long_run(*args, **kwargs):
327
  return _ncut_run(*args, **kwargs)
328
 
329
+ if not HUGGINGFACE_SPACE:
330
  def quick_run(*args, **kwargs):
331
  return _ncut_run(*args, **kwargs)
332
 
 
582
  from backbone import get_all_model_names
583
  model_names = get_all_model_names()
584
  model_dropdown = gr.Dropdown(model_names, label="Backbone", value="DiNO(dino_vitb8)", elem_id="model_name")
585
+ layer_slider = gr.Slider(1, 12, step=1, label="Backbone: Layer index", value=10, elem_id="layer")
586
  node_type_dropdown = gr.Dropdown(["attn: attention output", "mlp: mlp output", "block: sum of residual"], label="Backbone: Layer type", value="block: sum of residual", elem_id="node_type", info="which feature to take from each layer?")
587
  num_eig_slider = gr.Slider(1, 1000, step=1, label="NCUT: Number of eigenvectors", value=100, elem_id="num_eig", info='increase for more clusters')
588
 
 
827
  with gr.Tab('Compare'):
828
  def add_one_model(i_model=1):
829
  with gr.Column(scale=5, min_width=200) as col:
830
+ gr.Markdown(f'### Output Images')
831
  output_gallery = gr.Gallery(value=[], label="NCUT Embedding", show_label=False, elem_id=f"ncut{i_model}", columns=[3], rows=[1], object_fit="contain", height="auto")
832
  submit_button = gr.Button("🔴RUN", elem_id=f"submit_button{i_model}")
833
  [
 
864
  load_images_button.click(lambda x: default_images, outputs=input_gallery)
865
 
866
 
867
+ for i in range(2):
868
+ add_one_model()
869
 
870
+ # Create rows and buttons in a loop
871
+ rows = []
872
+ buttons = []
 
 
 
 
 
 
 
 
 
 
 
 
873
 
874
+ for i in range(4):
875
+ row = gr.Row(visible=False)
876
+ rows.append(row)
877
+
878
+ with row:
879
+ for j in range(3):
880
+ with gr.Column(scale=5, min_width=200):
881
+ add_one_model()
882
 
883
+ button = gr.Button("Add Compare", elem_id=f"add_button_{i}", visible=False if i > 0 else True)
884
+ buttons.append(button)
885
 
886
+ if i > 0:
887
+ # Reveal the current row and next button
888
+ buttons[i - 1].click(fn=lambda x: gr.update(visible=True), outputs=row)
889
+ buttons[i - 1].click(fn=lambda x: gr.update(visible=True), outputs=button)
890
+
891
+ # Hide the current button
892
+ buttons[i - 1].click(fn=lambda x: gr.update(visible=False), outputs=buttons[i - 1])
893
+
894
+ # Last button only reveals the last row and hides itself
895
+ buttons[-1].click(fn=lambda x: gr.update(visible=True), outputs=rows[-1])
896
+ buttons[-1].click(fn=lambda x: gr.update(visible=False), outputs=buttons[-1])
897
+
898
+
899
+
900
+ if HUGGINGFACE_SPACE:
901
+ download_all_models()
902
+ download_all_datasets()
903
+ demo.launch()
904
+ else:
905
+ demo.launch(share=True)
906
 
907
  # %%