huzey commited on
Commit
6dceac4
1 Parent(s): 9c40fc4

update delpoy

Browse files
Files changed (1) hide show
  1. app.py +14 -15
app.py CHANGED
@@ -1,14 +1,13 @@
1
  # Author: Huzheng Yang
2
  # %%
3
- USE_HUGGINGFACE_SPACE = True
4
- BATCH_SIZE = 4
5
-
6
- if USE_HUGGINGFACE_SPACE: # huggingface ZeroGPU, dynamic GPU allocation
7
- try:
8
- import spaces
9
- except ImportError:
10
- USE_HUGGINGFACE_SPACE = False # run on local machine
11
- BATCH_SIZE = 1
12
 
13
  import gradio as gr
14
 
@@ -18,7 +17,6 @@ from PIL import Image
18
  import numpy as np
19
  import time
20
  import threading
21
- import os
22
 
23
  from ncut_pytorch.backbone import extract_features, load_model
24
  from ncut_pytorch.backbone import MODEL_DICT, LAYER_DICT, RES_DICT
@@ -257,6 +255,7 @@ def ncut_run(
257
  logging_str += "Running NCut for each image independently\n"
258
  rgb = []
259
  for i_image in range(features.shape[0]):
 
260
  feature = features[i_image]
261
  _rgb, _logging_str, _ = compute_ncut(
262
  feature[None],
@@ -315,7 +314,7 @@ def _ncut_run(*args, **kwargs):
315
  torch.cuda.empty_cache()
316
  return [], "Error: " + str(e)
317
 
318
- if USE_HUGGINGFACE_SPACE:
319
  @spaces.GPU(duration=20)
320
  def quick_run(*args, **kwargs):
321
  return _ncut_run(*args, **kwargs)
@@ -332,7 +331,7 @@ if USE_HUGGINGFACE_SPACE:
332
  def super_duper_long_run(*args, **kwargs):
333
  return _ncut_run(*args, **kwargs)
334
 
335
- if not USE_HUGGINGFACE_SPACE:
336
  def quick_run(*args, **kwargs):
337
  return _ncut_run(*args, **kwargs)
338
 
@@ -739,7 +738,7 @@ with demo:
739
  with gr.Accordion("➡️ Recursion config", open=True):
740
  l1_num_eig_slider = gr.Slider(1, 1000, step=1, label="Recursion #1: N eigenvectors", value=100, elem_id="l1_num_eig")
741
  l2_num_eig_slider = gr.Slider(1, 1000, step=1, label="Recursion #2: N eigenvectors", value=50, elem_id="l2_num_eig")
742
- l3_num_eig_slider = gr.Slider(1, 1000, step=1, label="Recursion #3: N eigenvectors", value=25, elem_id="l3_num_eig")
743
  metric_dropdown = gr.Dropdown(["euclidean", "cosine"], label="Recursion distance metric", value="cosine", elem_id="recursion_metric")
744
 
745
  [
@@ -826,7 +825,7 @@ with demo:
826
  )
827
 
828
  with gr.Tab('Text'):
829
- if USE_HUGGINGFACE_SPACE:
830
  from app_text import make_demo
831
  else:
832
  from draft_gradio_app_text import make_demo
@@ -901,7 +900,7 @@ with demo:
901
 
902
 
903
 
904
- if USE_HUGGINGFACE_SPACE:
905
  from ncut_pytorch.backbone import download_all_models
906
  threading.Thread(target=download_all_models).start()
907
  from ncut_pytorch.backbone_text import download_all_models
 
1
  # Author: Huzheng Yang
2
  # %%
3
+ import os
4
+ USE_HUGGINGFACE_ZEROGPU = os.getenv("USE_HUGGINGFACE_ZEROGPU", "False").lower() in ["true", "1", "yes"]
5
+ #%%
6
+ if USE_HUGGINGFACE_ZEROGPU: # huggingface ZeroGPU, dynamic GPU allocation
7
+ import spaces
8
+ BATCH_SIZE = 4
9
+ else: # run on local machine
10
+ BATCH_SIZE = 1
 
11
 
12
  import gradio as gr
13
 
 
17
  import numpy as np
18
  import time
19
  import threading
 
20
 
21
  from ncut_pytorch.backbone import extract_features, load_model
22
  from ncut_pytorch.backbone import MODEL_DICT, LAYER_DICT, RES_DICT
 
255
  logging_str += "Running NCut for each image independently\n"
256
  rgb = []
257
  for i_image in range(features.shape[0]):
258
+ logging_str += f"Image #{i_image+1}\n"
259
  feature = features[i_image]
260
  _rgb, _logging_str, _ = compute_ncut(
261
  feature[None],
 
314
  torch.cuda.empty_cache()
315
  return [], "Error: " + str(e)
316
 
317
+ if USE_HUGGINGFACE_ZEROGPU:
318
  @spaces.GPU(duration=20)
319
  def quick_run(*args, **kwargs):
320
  return _ncut_run(*args, **kwargs)
 
331
  def super_duper_long_run(*args, **kwargs):
332
  return _ncut_run(*args, **kwargs)
333
 
334
+ if not USE_HUGGINGFACE_ZEROGPU:
335
  def quick_run(*args, **kwargs):
336
  return _ncut_run(*args, **kwargs)
337
 
 
738
  with gr.Accordion("➡️ Recursion config", open=True):
739
  l1_num_eig_slider = gr.Slider(1, 1000, step=1, label="Recursion #1: N eigenvectors", value=100, elem_id="l1_num_eig")
740
  l2_num_eig_slider = gr.Slider(1, 1000, step=1, label="Recursion #2: N eigenvectors", value=50, elem_id="l2_num_eig")
741
+ l3_num_eig_slider = gr.Slider(1, 1000, step=1, label="Recursion #3: N eigenvectors", value=50, elem_id="l3_num_eig")
742
  metric_dropdown = gr.Dropdown(["euclidean", "cosine"], label="Recursion distance metric", value="cosine", elem_id="recursion_metric")
743
 
744
  [
 
825
  )
826
 
827
  with gr.Tab('Text'):
828
+ if USE_HUGGINGFACE_ZEROGPU:
829
  from app_text import make_demo
830
  else:
831
  from draft_gradio_app_text import make_demo
 
900
 
901
 
902
 
903
+ if USE_HUGGINGFACE_ZEROGPU:
904
  from ncut_pytorch.backbone import download_all_models
905
  threading.Thread(target=download_all_models).start()
906
  from ncut_pytorch.backbone_text import download_all_models