Update handler.py
Browse files- handler.py +58 -3
handler.py
CHANGED
@@ -19,10 +19,16 @@ class EndpointHandler():
|
|
19 |
|
20 |
def __call__(self, data: Dict[str, Any]) -> Dict[str, Any]:
|
21 |
try:
|
22 |
-
|
23 |
|
24 |
-
# Convert base64 encoded image string to
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
# Ensure the image is in RGB mode (if necessary)
|
28 |
if raw_image.mode != "RGB":
|
@@ -42,3 +48,52 @@ class EndpointHandler():
|
|
42 |
# Log the error for better tracking
|
43 |
print(f"Error during processing: {str(e)}")
|
44 |
return {"caption": "", "error": str(e)}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
def __call__(self, data: Dict[str, Any]) -> Dict[str, Any]:
|
21 |
try:
|
22 |
+
image_data = data.get("inputs", None)
|
23 |
|
24 |
+
# Convert base64 encoded image string to bytes
|
25 |
+
image_bytes = base64.b64decode(image_data)
|
26 |
+
|
27 |
+
# Create a BytesIO object from the bytes data
|
28 |
+
image_buffer = BytesIO(image_bytes)
|
29 |
+
|
30 |
+
# Open the image from the buffer
|
31 |
+
raw_image = Image.open(image_buffer)
|
32 |
|
33 |
# Ensure the image is in RGB mode (if necessary)
|
34 |
if raw_image.mode != "RGB":
|
|
|
48 |
# Log the error for better tracking
|
49 |
print(f"Error during processing: {str(e)}")
|
50 |
return {"caption": "", "error": str(e)}
|
51 |
+
|
52 |
+
|
53 |
+
|
54 |
+
|
55 |
+
|
56 |
+
# from PIL import Image
|
57 |
+
# from typing import Dict, Any
|
58 |
+
# import torch
|
59 |
+
# import base64
|
60 |
+
# from io import BytesIO
|
61 |
+
# from transformers import BlipForConditionalGeneration, BlipProcessor
|
62 |
+
|
63 |
+
# device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
64 |
+
|
65 |
+
# class EndpointHandler():
|
66 |
+
# def __init__(self, path=""):
|
67 |
+
# self.processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-large")
|
68 |
+
# self.model = BlipForConditionalGeneration.from_pretrained(
|
69 |
+
# "Salesforce/blip-image-captioning-large"
|
70 |
+
# ).to(device)
|
71 |
+
# self.model.eval()
|
72 |
+
# self.max_length = 16
|
73 |
+
# self.num_beams = 4
|
74 |
+
|
75 |
+
# def __call__(self, data: Dict[str, Any]) -> Dict[str, Any]:
|
76 |
+
# try:
|
77 |
+
# image_bytes = data.get("inputs", None)
|
78 |
+
|
79 |
+
# # Convert base64 encoded image string to a PIL Image
|
80 |
+
# raw_image = Image.open(BytesIO(image_bytes))
|
81 |
+
|
82 |
+
# # Ensure the image is in RGB mode (if necessary)
|
83 |
+
# if raw_image.mode != "RGB":
|
84 |
+
# raw_image = raw_image.convert(mode="RGB")
|
85 |
+
|
86 |
+
# # Extract pixel values and move them to the device
|
87 |
+
# pixel_values = self.processor(raw_image, return_tensors="pt").pixel_values.to(device)
|
88 |
+
|
89 |
+
# # Generate the caption
|
90 |
+
# gen_kwargs = {"max_length": self.max_length, "num_beams": self.num_beams}
|
91 |
+
# output_ids = self.model.generate(pixel_values, **gen_kwargs)
|
92 |
+
|
93 |
+
# caption = self.processor.batch_decode(output_ids[0], skip_special_tokens=True).strip()
|
94 |
+
|
95 |
+
# return {"caption": caption}
|
96 |
+
# except Exception as e:
|
97 |
+
# # Log the error for better tracking
|
98 |
+
# print(f"Error during processing: {str(e)}")
|
99 |
+
# return {"caption": "", "error": str(e)}
|