Update handler.py
Browse files- handler.py +43 -27
handler.py
CHANGED
|
@@ -4,27 +4,32 @@ import io
|
|
| 4 |
from PIL import Image
|
| 5 |
from ultralytics import YOLO
|
| 6 |
|
|
|
|
| 7 |
class EndpointHandler:
|
| 8 |
"""
|
| 9 |
Hugging Face Inference Endpoint handler for LocustGuard YOLO model.
|
| 10 |
|
| 11 |
-
Accepts
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
"conf": 0.25,
|
| 25 |
-
"iou": 0.45
|
| 26 |
-
}
|
| 27 |
-
}
|
| 28 |
|
| 29 |
Returns:
|
| 30 |
{
|
|
@@ -39,27 +44,38 @@ class EndpointHandler:
|
|
| 39 |
"""
|
| 40 |
|
| 41 |
def __init__(self, path: str = "."):
|
| 42 |
-
# ✅ Native ultralytics loader
|
| 43 |
self.model = YOLO(f"{path}/best.pt")
|
| 44 |
|
| 45 |
def __call__(self, data: Dict[str, Any]) -> Dict[str, Any]:
|
| 46 |
-
#
|
| 47 |
payload = data.get("inputs", data)
|
| 48 |
|
| 49 |
-
#
|
| 50 |
-
if
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
return {
|
| 52 |
-
"error": "
|
| 53 |
}
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
|
| 62 |
-
#
|
| 63 |
results = self.model(
|
| 64 |
image,
|
| 65 |
conf=conf,
|
|
|
|
| 4 |
from PIL import Image
|
| 5 |
from ultralytics import YOLO
|
| 6 |
|
| 7 |
+
|
| 8 |
class EndpointHandler:
|
| 9 |
"""
|
| 10 |
Hugging Face Inference Endpoint handler for LocustGuard YOLO model.
|
| 11 |
|
| 12 |
+
Accepts:
|
| 13 |
+
1) Direct HTTP / Spaces:
|
| 14 |
+
{
|
| 15 |
+
"image": "<base64>",
|
| 16 |
+
"conf": 0.25,
|
| 17 |
+
"iou": 0.45
|
| 18 |
+
}
|
| 19 |
|
| 20 |
+
2) Playground / Hosted API:
|
| 21 |
+
{
|
| 22 |
+
"inputs": {
|
| 23 |
+
"image": "<base64>",
|
| 24 |
+
"conf": 0.25,
|
| 25 |
+
"iou": 0.45
|
| 26 |
+
}
|
| 27 |
+
}
|
| 28 |
|
| 29 |
+
3) HF standard:
|
| 30 |
+
{
|
| 31 |
+
"inputs": "<base64>"
|
| 32 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
Returns:
|
| 35 |
{
|
|
|
|
| 44 |
"""
|
| 45 |
|
| 46 |
def __init__(self, path: str = "."):
|
| 47 |
+
# ✅ Native ultralytics loader
|
| 48 |
self.model = YOLO(f"{path}/best.pt")
|
| 49 |
|
| 50 |
def __call__(self, data: Dict[str, Any]) -> Dict[str, Any]:
|
| 51 |
+
# ---------------- NORMALIZE INPUT ----------------
|
| 52 |
payload = data.get("inputs", data)
|
| 53 |
|
| 54 |
+
# Case 1: HF standard sends raw base64 string
|
| 55 |
+
if isinstance(payload, str):
|
| 56 |
+
image_b64 = payload
|
| 57 |
+
conf = 0.25
|
| 58 |
+
iou = 0.45
|
| 59 |
+
|
| 60 |
+
# Case 2: Dict-based payload
|
| 61 |
+
elif isinstance(payload, dict) and "image" in payload:
|
| 62 |
+
image_b64 = payload["image"]
|
| 63 |
+
conf = float(payload.get("conf", 0.25))
|
| 64 |
+
iou = float(payload.get("iou", 0.45))
|
| 65 |
+
|
| 66 |
+
else:
|
| 67 |
return {
|
| 68 |
+
"error": "Invalid input. Expected base64 image under key 'image' or 'inputs'."
|
| 69 |
}
|
| 70 |
|
| 71 |
+
# ---------------- DECODE IMAGE ----------------
|
| 72 |
+
try:
|
| 73 |
+
image_bytes = base64.b64decode(image_b64)
|
| 74 |
+
image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
|
| 75 |
+
except Exception as e:
|
| 76 |
+
return {"error": f"Failed to decode image: {str(e)}"}
|
| 77 |
|
| 78 |
+
# ---------------- RUN INFERENCE ----------------
|
| 79 |
results = self.model(
|
| 80 |
image,
|
| 81 |
conf=conf,
|