wjm55 commited on
Commit
3b05c4f
·
1 Parent(s): af48921
Files changed (3) hide show
  1. app.py +73 -0
  2. dockerfile +18 -0
  3. requirements.txt +4 -0
app.py ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, UploadFile
2
+ from ultralytics import YOLOE
3
+ import io
4
+ from PIL import Image
5
+ import numpy as np
6
+ import os
7
+ from huggingface_hub import hf_hub_download
8
+ from ultralytics import YOLO
9
+ import requests
10
+
11
+
12
+ ###
13
+
14
+ #pip install -q "git+https://github.com/THU-MIG/yoloe.git#subdirectory=third_party/CLIP"
15
+ #pip install -q "git+https://github.com/THU-MIG/yoloe.git#subdirectory=third_party/ml-mobileclip"
16
+ #pip install -q "git+https://github.com/THU-MIG/yoloe.git#subdirectory=third_party/lvis-api"
17
+ #pip install -q "git+https://github.com/THU-MIG/yoloe.git"
18
+
19
+ #wget -q https://docs-assets.developer.apple.com/ml-research/datasets/mobileclip/mobileclip_blt.pt
20
+
21
+
22
+ def init_model():
23
+ is_pf=True
24
+ model_id = "yoloe-11s"
25
+ # Create a models directory if it doesn't exist
26
+ os.makedirs("models", exist_ok=True)
27
+ filename = f"{model_id}-seg.pt" if not is_pf else f"{model_id}-seg-pf.pt"
28
+ path = hf_hub_download(repo_id="jameslahm/yoloe", filename=filename)
29
+ local_path = os.path.join("models", path)
30
+ # Download and load model
31
+ model = YOLOE(local_path)
32
+ model.eval()
33
+ return model
34
+
35
+ app = FastAPI()
36
+
37
+ # Initialize model at startup
38
+ model = init_model()
39
+
40
+ @app.post("/predict")
41
+ async def predict(image_url: str, texts: str = "hat"):
42
+ # Set classes to filter
43
+ class_list = [text.strip() for text in texts.split(',')]
44
+
45
+ # Download and open image from URL
46
+ response = requests.get(image_url)
47
+ image = Image.open(io.BytesIO(response.content))
48
+
49
+ # Get text embeddings and set classes properly
50
+ text_embeddings = model.get_text_pe(class_list)
51
+ model.set_classes(class_list, text_embeddings)
52
+
53
+ # Run inference with the PIL Image
54
+ results = model.predict(source=image, conf=0.25, iou=0.7)
55
+
56
+ # Extract detection results
57
+ result = results[0]
58
+ # print(result)
59
+ detections = []
60
+
61
+ for box in result.boxes:
62
+ detection = {
63
+ "class": result.names[int(box.cls[0])],
64
+ "confidence": float(box.conf[0]),
65
+ "bbox": box.xyxy[0].tolist() # Convert bbox tensor to list
66
+ }
67
+ detections.append(detection)
68
+ print(detections)
69
+ return {"detections": detections}
70
+
71
+ if __name__ == "__main__":
72
+ import uvicorn
73
+ uvicorn.run(app, host="0.0.0.0", port=7860)
dockerfile ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker
2
+ # you will also find guides on how best to write your Dockerfile
3
+
4
+ FROM python:3.10
5
+
6
+ RUN useradd -m -u 1000 user
7
+ USER user
8
+ ENV PATH="/home/user/.local/bin:$PATH"
9
+
10
+ WORKDIR /app
11
+
12
+ COPY --chown=user ./requirements.txt requirements.txt
13
+ RUN pip install --no-cache-dir --upgrade -r requirements.txt
14
+
15
+ RUN wget -q https://docs-assets.developer.apple.com/ml-research/datasets/mobileclip/mobileclip_blt.pt
16
+
17
+ COPY --chown=user . /app
18
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ "git+https://github.com/THU-MIG/yoloe.git#subdirectory=third_party/CLIP"
2
+ "git+https://github.com/THU-MIG/yoloe.git#subdirectory=third_party/ml-mobileclip"
3
+ "git+https://github.com/THU-MIG/yoloe.git#subdirectory=third_party/lvis-api"
4
+ "git+https://github.com/THU-MIG/yoloe.git"