Update handler.py
Browse files- handler.py +19 -5
handler.py
CHANGED
|
@@ -8,13 +8,24 @@ class EndpointHandler:
|
|
| 8 |
"""
|
| 9 |
Hugging Face Inference Endpoint handler for LocustGuard YOLO model.
|
| 10 |
|
| 11 |
-
|
|
|
|
|
|
|
| 12 |
{
|
| 13 |
-
"image": "<base64
|
| 14 |
"conf": 0.25,
|
| 15 |
"iou": 0.45
|
| 16 |
}
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
Returns:
|
| 19 |
{
|
| 20 |
"detections": [
|
|
@@ -32,9 +43,12 @@ class EndpointHandler:
|
|
| 32 |
self.model = YOLO(f"{path}/best.pt")
|
| 33 |
|
| 34 |
def __call__(self, data: Dict[str, Any]) -> Dict[str, Any]:
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
image_bytes = base64.b64decode(image_b64)
|
| 40 |
image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
|
|
|
|
| 8 |
"""
|
| 9 |
Hugging Face Inference Endpoint handler for LocustGuard YOLO model.
|
| 10 |
|
| 11 |
+
Accepts BOTH formats:
|
| 12 |
+
|
| 13 |
+
Direct HTTP / Spaces:
|
| 14 |
{
|
| 15 |
+
"image": "<base64>",
|
| 16 |
"conf": 0.25,
|
| 17 |
"iou": 0.45
|
| 18 |
}
|
| 19 |
|
| 20 |
+
Playground / Hosted API:
|
| 21 |
+
{
|
| 22 |
+
"inputs": {
|
| 23 |
+
"image": "<base64>",
|
| 24 |
+
"conf": 0.25,
|
| 25 |
+
"iou": 0.45
|
| 26 |
+
}
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
Returns:
|
| 30 |
{
|
| 31 |
"detections": [
|
|
|
|
| 43 |
self.model = YOLO(f"{path}/best.pt")
|
| 44 |
|
| 45 |
def __call__(self, data: Dict[str, Any]) -> Dict[str, Any]:
|
| 46 |
+
# ✅ Compatibility layer (does NOT break anything)
|
| 47 |
+
payload = data.get("inputs", data)
|
| 48 |
+
|
| 49 |
+
image_b64 = payload["image"]
|
| 50 |
+
conf = float(payload.get("conf", 0.25))
|
| 51 |
+
iou = float(payload.get("iou", 0.45))
|
| 52 |
|
| 53 |
image_bytes = base64.b64decode(image_b64)
|
| 54 |
image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
|