Update app.py
Browse files8 unmodified lines91011121314151617181920212223242538 unmodified lines646566676869707172737475768 unmodified linesimport iofrom huggingface_hub import hf_hub_download# Download model at startupHF_REPO_ID = "myang4218/cellposemodel"HF_MODEL_FILENAME = "cellposemodel.npy"model_path = hf_hub_download(repo_id=HF_REPO_ID, filename=HF_MODEL_FILENAME)print(f"Model downloaded to: {model_path}")@spaces.GPUdef segment_and_count(image):# Initialize model inside the GPU-decorated functionmodel = models.CellposeModel(gpu=True, pretrained_model=model_path)# Convert PIL Image to numpy arrayimage_np = np.array(image)38 unmodified lines# Gradio interfacedemo = gr.Interface(fn=segment_and_count,inputs=gr.Image(type="pil"),outputs=[gr.Number(label="Number of Cells"),gr.Image(type="pil", label="Segmented Overlay")],title="Cell Counter with Cellpose",description="Upload a microscopy image and count the number of cells using Cellpose segmentation.",)if __name__ == "__main__":8 unmodified lines91011121314151617181920212223242526272829303132333438 unmodified lines737475767778798081828384858687888 unmodified linesimport iofrom huggingface_hub import hf_hub_downloadHF_REPO_ID = "myang4218/cellposemodel"MODEL_OPTIONS = {"Hemocytometer Model": "hemocytometermodel.npy","General Model": "generalmodel.npy"}loaded_models = {}@spaces.GPUdef segment_and_count(image, model_choice):model_filename = MODEL_OPTIONS[model_choice]model_path = hf_hub_download(repo_id=HF_REPO_ID, filename=model_filename)if model_filename in loaded_models:model = loaded_models[model_filename]else:model = models.CellposeModel(gpu=True, pretrained_model=model_path)loaded_models[model_filename] = model# Convert PIL Image to numpy arrayimage_np = np.array(image)38 unmodified lines# Gradio interfacedemo = gr.Interface(fn=segment_and_count,inputs=[gr.Image(type="pil", label="Microscopy Image"),gr.Dropdown(choices=list(MODEL_OPTIONS.keys()), label="Select Model", value="Hemocytometer Model")],outputs=[gr.Number(label="Number of Cells"),gr.Image(type="pil", label="Segmented Overlay")],title="Cell Counter with Cellpose",description="Upload a microscopy image and select a model to count the number of cells using Cellpose segmentation.")if __name__ == "__main__":
app.py+23-11