LiangLabUMB commited on
Commit
7c780ba
·
verified ·
1 Parent(s): 6396b53

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -11
app.py CHANGED
@@ -9,17 +9,26 @@ from PIL import Image
9
  import io
10
  from huggingface_hub import hf_hub_download
11
 
12
- # Download model at startup
13
- HF_REPO_ID = "myang4218/cellposemodel"
14
- HF_MODEL_FILENAME = "cellposemodel.npy"
15
- model_path = hf_hub_download(repo_id=HF_REPO_ID, filename=HF_MODEL_FILENAME)
16
- print(f"Model downloaded to: {model_path}")
 
 
 
17
 
18
  @spaces.GPU
19
- def segment_and_count(image):
20
- # Initialize model inside the GPU-decorated function
21
- model = models.CellposeModel(gpu=True, pretrained_model=model_path)
22
 
 
 
 
 
 
 
 
 
23
  # Convert PIL Image to numpy array
24
  image_np = np.array(image)
25
 
@@ -64,13 +73,16 @@ def segment_and_count(image):
64
  # Gradio interface
65
  demo = gr.Interface(
66
  fn=segment_and_count,
67
- inputs=gr.Image(type="pil"),
 
 
 
68
  outputs=[
69
- gr.Number(label="Number of Cells"),
70
  gr.Image(type="pil", label="Segmented Overlay")
71
  ],
72
  title="Cell Counter with Cellpose",
73
- description="Upload a microscopy image and count the number of cells using Cellpose segmentation.",
74
  )
75
 
76
  if __name__ == "__main__":
 
9
  import io
10
  from huggingface_hub import hf_hub_download
11
 
12
+ HF_REPO_ID = "myang4218/cellposemodel"
13
+
14
+ MODEL_OPTIONS = {
15
+ "Hemocytometer Model": "hemocytometermodel.npy",
16
+ "General Model": "generalmodel.npy"
17
+ }
18
+
19
+ loaded_models = {}
20
 
21
  @spaces.GPU
22
+ def segment_and_count(image, model_choice):
 
 
23
 
24
+ model_filename = MODEL_OPTIONS[model_choice]
25
+ model_path = hf_hub_download(repo_id=HF_REPO_ID, filename=model_filename)
26
+ if model_filename in loaded_models:
27
+ model = loaded_models[model_filename]
28
+ else:
29
+ model = models.CellposeModel(gpu=True, pretrained_model=model_path)
30
+ loaded_models[model_filename] = model
31
+
32
  # Convert PIL Image to numpy array
33
  image_np = np.array(image)
34
 
 
73
  # Gradio interface
74
  demo = gr.Interface(
75
  fn=segment_and_count,
76
+ inputs=[
77
+ gr.Image(type="pil", label="Microscopy Image"),
78
+ gr.Dropdown(choices=list(MODEL_OPTIONS.keys()), label="Select Model", value="Hemocytometer Model")
79
+ ],
80
  outputs=[
81
+ gr.Number(label="Number of Cells"),
82
  gr.Image(type="pil", label="Segmented Overlay")
83
  ],
84
  title="Cell Counter with Cellpose",
85
+ description="Upload a microscopy image and select a model to count the number of cells using Cellpose segmentation."
86
  )
87
 
88
  if __name__ == "__main__":