Hitesh Satwani commited on
Commit ·
d2127bb
1
Parent(s): 76ddf4a
Fix handler to support 'inputs' key
Browse files- handler.py +13 -14
handler.py
CHANGED
|
@@ -13,22 +13,21 @@ class EndpointHandler:
|
|
| 13 |
self.model.to(self.device)
|
| 14 |
|
| 15 |
def __call__(self, data):
|
| 16 |
-
#
|
| 17 |
-
if isinstance(data, dict) and "
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
image = Image.open(io.BytesIO(response.content)).convert("RGB")
|
| 23 |
-
except Exception as e:
|
| 24 |
-
return {"error": f"Failed to load image from URL: {str(e)}"}
|
| 25 |
-
else:
|
| 26 |
return {"error": "Please provide an 'image_url' in the JSON payload."}
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
|
|
|
| 30 |
with torch.no_grad():
|
| 31 |
out = self.model.generate(**inputs)
|
| 32 |
-
|
| 33 |
-
caption = self.processor.decode(out[0], skip_special_tokens=True)
|
| 34 |
-
return {"caption": caption}
|
|
|
|
| 13 |
self.model.to(self.device)
|
| 14 |
|
| 15 |
def __call__(self, data):
|
| 16 |
+
# Unwrap Hugging Face "inputs" key if present
|
| 17 |
+
if isinstance(data, dict) and "inputs" in data:
|
| 18 |
+
data = data["inputs"]
|
| 19 |
+
|
| 20 |
+
image_url = data.get("image_url")
|
| 21 |
+
if not image_url:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
return {"error": "Please provide an 'image_url' in the JSON payload."}
|
| 23 |
|
| 24 |
+
try:
|
| 25 |
+
response = requests.get(image_url)
|
| 26 |
+
response.raise_for_status()
|
| 27 |
+
image = Image.open(io.BytesIO(response.content)).convert("RGB")
|
| 28 |
+
except Exception as e:
|
| 29 |
+
return {"error": f"Failed to load image: {str(e)}"}
|
| 30 |
|
| 31 |
+
inputs = self.processor(images=image, return_tensors="pt").to(self.device)
|
| 32 |
with torch.no_grad():
|
| 33 |
out = self.model.generate(**inputs)
|
|
|
|
|
|
|
|
|