Update handler.py
Browse files- handler.py +4 -6
handler.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
from PIL import Image
|
| 2 |
-
from typing import Dict, Any
|
| 3 |
import torch
|
| 4 |
import base64
|
| 5 |
from io import BytesIO
|
|
@@ -17,15 +17,14 @@ class EndpointHandler():
|
|
| 17 |
self.max_length = 16
|
| 18 |
self.num_beams = 4
|
| 19 |
|
| 20 |
-
def __call__(self, data: Dict[str, Any]) ->
|
| 21 |
-
# def __call__(self, image_data: str) -> dict:
|
| 22 |
try:
|
| 23 |
image_bytes = data.get("inputs", None)
|
| 24 |
|
| 25 |
# Convert base64 encoded image string to a PIL Image
|
| 26 |
raw_image = Image.open(BytesIO(image_bytes))
|
| 27 |
|
| 28 |
-
# Ensure the image is in RGB mode
|
| 29 |
if raw_image.mode != "RGB":
|
| 30 |
raw_image = raw_image.convert(mode="RGB")
|
| 31 |
|
|
@@ -40,7 +39,6 @@ class EndpointHandler():
|
|
| 40 |
|
| 41 |
return {"caption": caption}
|
| 42 |
except Exception as e:
|
|
|
|
| 43 |
print(f"Error during processing: {str(e)}")
|
| 44 |
return {"caption": "", "error": str(e)}
|
| 45 |
-
|
| 46 |
-
|
|
|
|
| 1 |
from PIL import Image
|
| 2 |
+
from typing import Dict, Any
|
| 3 |
import torch
|
| 4 |
import base64
|
| 5 |
from io import BytesIO
|
|
|
|
| 17 |
self.max_length = 16
|
| 18 |
self.num_beams = 4
|
| 19 |
|
| 20 |
+
def __call__(self, data: Dict[str, Any]) -> Dict[str, Any]:
|
|
|
|
| 21 |
try:
|
| 22 |
image_bytes = data.get("inputs", None)
|
| 23 |
|
| 24 |
# Convert base64 encoded image string to a PIL Image
|
| 25 |
raw_image = Image.open(BytesIO(image_bytes))
|
| 26 |
|
| 27 |
+
# Ensure the image is in RGB mode (if necessary)
|
| 28 |
if raw_image.mode != "RGB":
|
| 29 |
raw_image = raw_image.convert(mode="RGB")
|
| 30 |
|
|
|
|
| 39 |
|
| 40 |
return {"caption": caption}
|
| 41 |
except Exception as e:
|
| 42 |
+
# Log the error for better tracking
|
| 43 |
print(f"Error during processing: {str(e)}")
|
| 44 |
return {"caption": "", "error": str(e)}
|
|
|
|
|
|