sguna commited on
Commit
ae68696
1 Parent(s): 0b0bb49

Upload handler.py

Browse files
Files changed (1) hide show
  1. 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