dchen0 commited on
Commit
69baf1e
·
verified ·
1 Parent(s): 2e97025

Add merged model + processor

Browse files
Files changed (1) hide show
  1. 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
- if "inputs" not in data:
65
- raise ValueError("Request JSON must contain an 'inputs' field.")
66
-
67
- # Decode base64 → PIL
68
- img_bytes = base64.b64decode(data["inputs"])
69
- image = Image.open(io.BytesIO(img_bytes))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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]