Upload handler.py
Browse files- handler.py +11 -1
handler.py
CHANGED
@@ -30,6 +30,17 @@ class EndpointHandler():
|
|
30 |
text = data.get("text", "")
|
31 |
parameters = data.pop("parameters", {})
|
32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
processed_image = self.processor(images=images, text=text, return_tensors="pt")
|
34 |
processed_image["pixel_values"] = processed_image["pixel_values"].to(device)
|
35 |
processed_image = {**processed_image, **parameters}
|
@@ -43,4 +54,3 @@ class EndpointHandler():
|
|
43 |
return {"description": description}
|
44 |
|
45 |
|
46 |
-
handler = EndpointHandler()
|
|
|
30 |
text = data.get("text", "")
|
31 |
parameters = data.pop("parameters", {})
|
32 |
|
33 |
+
try:
|
34 |
+
# Ensure inputs is a list of image bytes, even if only a single image is provided
|
35 |
+
if isinstance(images, bytes): # Single image as bytes
|
36 |
+
raw_images = [Image.open(BytesIO(images))]
|
37 |
+
elif isinstance(images, list): # Multiple images as list of bytes
|
38 |
+
raw_images = [Image.open(BytesIO(_img)) for _img in images if isinstance(_img, bytes)]
|
39 |
+
else:
|
40 |
+
raise ValueError("Invalid image input format. Expected bytes or list of bytes.")
|
41 |
+
except Exception as e:
|
42 |
+
return {"error": f"Error fetching or processing image: {str(e)}"}
|
43 |
+
|
44 |
processed_image = self.processor(images=images, text=text, return_tensors="pt")
|
45 |
processed_image["pixel_values"] = processed_image["pixel_values"].to(device)
|
46 |
processed_image = {**processed_image, **parameters}
|
|
|
54 |
return {"description": description}
|
55 |
|
56 |
|
|