amobionovo commited on
Commit
f78d3e5
·
verified ·
1 Parent(s): 09cf2bd

Update handler.py

Browse files
Files changed (1) hide show
  1. 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 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
  {
@@ -39,27 +44,38 @@ class EndpointHandler:
39
  """
40
 
41
  def __init__(self, path: str = "."):
42
- # ✅ Native ultralytics loader (NO torch.hub)
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
- # Robust guard: prevents KeyError and hard crashes
50
- if not isinstance(payload, dict) or "image" not in payload:
 
 
 
 
 
 
 
 
 
 
 
51
  return {
52
- "error": "Missing required key: 'image'. Expected base64-encoded image."
53
  }
54
 
55
- image_b64 = payload["image"]
56
- conf = float(payload.get("conf", 0.25))
57
- iou = float(payload.get("iou", 0.45))
58
-
59
- image_bytes = base64.b64decode(image_b64)
60
- image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
61
 
62
- # Run inference
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,