Shalani08 commited on
Commit
6a88e99
·
verified ·
1 Parent(s): c98ffe0

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -0
app.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, UploadFile, File
2
+ import tifffile
3
+ import numpy as np
4
+ from cellpose import models
5
+ from huggingface_hub import hf_hub_download
6
+
7
+ app = FastAPI()
8
+
9
+ # --------------------------------------------------
10
+ # Download model from HF model repo
11
+ # --------------------------------------------------
12
+ MODEL_REPO = "Shalani08/cellpose_3.1.1_finetuned"
13
+ MODEL_FILE = "ddq_model"
14
+
15
+ model_path = hf_hub_download(
16
+ repo_id=MODEL_REPO,
17
+ filename=MODEL_FILE
18
+ )
19
+
20
+ # --------------------------------------------------
21
+ # Load Cellpose model (CPU)
22
+ # --------------------------------------------------
23
+ model = models.CellposeModel(
24
+ pretrained_model=model_path,
25
+ gpu=False
26
+ )
27
+
28
+ # --------------------------------------------------
29
+ # API endpoint
30
+ # --------------------------------------------------
31
+ @app.post("/segment")
32
+ async def segment(
33
+ image: UploadFile = File(...),
34
+ diameter: float = 0
35
+ ):
36
+ img = tifffile.imread(image.file)
37
+
38
+ masks, flows, styles = model.eval(
39
+ img,
40
+ diameter=diameter,
41
+ channels=[0, 0]
42
+ )
43
+
44
+ return {
45
+ "shape": masks.shape,
46
+ "masks": masks.tolist()
47
+ }