Add merged model + processor
Browse files- handler.py +28 -6
handler.py
CHANGED
|
@@ -61,12 +61,34 @@ class EndpointHandler:
|
|
| 61 |
Expects {"inputs": "<base64‑encoded image>"}.
|
| 62 |
Returns the top prediction + per‑class probabilities.
|
| 63 |
"""
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
# Preprocess with your own transform
|
| 72 |
pixel_values = self.transform(image).unsqueeze(0) # [1, C, H, W]
|
|
|
|
| 61 |
Expects {"inputs": "<base64‑encoded image>"}.
|
| 62 |
Returns the top prediction + per‑class probabilities.
|
| 63 |
"""
|
| 64 |
+
# case 1 ─ raw bytes (default HF client / curl -T)
|
| 65 |
+
if isinstance(data, (bytes, bytearray)):
|
| 66 |
+
img_bytes = data
|
| 67 |
+
|
| 68 |
+
# case 2 ─ JSON with "inputs": <something>
|
| 69 |
+
elif isinstance(data, dict) and "inputs" in data:
|
| 70 |
+
inp = data["inputs"]
|
| 71 |
+
|
| 72 |
+
# Base‑64 string
|
| 73 |
+
if isinstance(inp, str):
|
| 74 |
+
img_bytes = base64.b64decode(inp.split(",")[-1]) # drop "data:..." if present
|
| 75 |
+
|
| 76 |
+
# Already‑bytes
|
| 77 |
+
elif isinstance(inp, (bytes, bytearray)):
|
| 78 |
+
img_bytes = inp
|
| 79 |
+
|
| 80 |
+
# Already a PIL Image object
|
| 81 |
+
elif hasattr(inp, "convert"):
|
| 82 |
+
image = inp # PIL.Image
|
| 83 |
+
else:
|
| 84 |
+
raise ValueError("Unsupported 'inputs' format")
|
| 85 |
+
|
| 86 |
+
else:
|
| 87 |
+
raise ValueError("Unsupported request body type")
|
| 88 |
+
|
| 89 |
+
# If we didn’t get a ready‑made PIL Image above, decode bytes → PIL
|
| 90 |
+
if "image" not in locals():
|
| 91 |
+
image = Image.open(io.BytesIO(img_bytes))
|
| 92 |
|
| 93 |
# Preprocess with your own transform
|
| 94 |
pixel_values = self.transform(image).unsqueeze(0) # [1, C, H, W]
|