Update handler.py
Browse files- handler.py +6 -4
handler.py
CHANGED
|
@@ -15,12 +15,15 @@ class EndpointHandler():
|
|
| 15 |
self.model.eval()
|
| 16 |
|
| 17 |
def __call__(self, data: Dict[str, Any]) -> Dict[str, Any]:
|
|
|
|
| 18 |
input_data = data.get("inputs", {})
|
|
|
|
| 19 |
encoded_images = input_data.get("images")
|
|
|
|
| 20 |
if not encoded_images:
|
| 21 |
return {"captions": [], "error": "No images provided"}
|
| 22 |
|
| 23 |
-
texts = input_data.get("texts", ["a photography of"] * len(encoded_images))
|
| 24 |
|
| 25 |
try:
|
| 26 |
byteImgIO = io.BytesIO()
|
|
@@ -39,12 +42,10 @@ class EndpointHandler():
|
|
| 39 |
if not raw_images:
|
| 40 |
print("No valid images found.")
|
| 41 |
processed_inputs = [
|
| 42 |
-
self.processor(image,
|
| 43 |
]
|
| 44 |
processed_inputs = {
|
| 45 |
"pixel_values": torch.cat([inp["pixel_values"] for inp in processed_inputs], dim=0).to(device),
|
| 46 |
-
"input_ids": torch.cat([inp["input_ids"] for inp in processed_inputs], dim=0).to(device),
|
| 47 |
-
"attention_mask": torch.cat([inp["attention_mask"] for inp in processed_inputs], dim=0).to(device),
|
| 48 |
"max_new_tokens":40
|
| 49 |
}
|
| 50 |
|
|
@@ -52,6 +53,7 @@ class EndpointHandler():
|
|
| 52 |
out = self.model.generate(**processed_inputs)
|
| 53 |
|
| 54 |
captions = self.processor.batch_decode(out, skip_special_tokens=True)
|
|
|
|
| 55 |
return {"captions": captions}
|
| 56 |
except Exception as e:
|
| 57 |
print(f"Error during processing: {str(e)}")
|
|
|
|
| 15 |
self.model.eval()
|
| 16 |
|
| 17 |
def __call__(self, data: Dict[str, Any]) -> Dict[str, Any]:
|
| 18 |
+
print("input data is here------------",data)
|
| 19 |
input_data = data.get("inputs", {})
|
| 20 |
+
print("input data is here-2-----------",input_data)
|
| 21 |
encoded_images = input_data.get("images")
|
| 22 |
+
print("input encoded_images is here------------",encoded_images)
|
| 23 |
if not encoded_images:
|
| 24 |
return {"captions": [], "error": "No images provided"}
|
| 25 |
|
| 26 |
+
#texts = input_data.get("texts", ["a photography of"] * len(encoded_images))
|
| 27 |
|
| 28 |
try:
|
| 29 |
byteImgIO = io.BytesIO()
|
|
|
|
| 42 |
if not raw_images:
|
| 43 |
print("No valid images found.")
|
| 44 |
processed_inputs = [
|
| 45 |
+
self.processor(image, return_tensors="pt") for image in zip(raw_images)
|
| 46 |
]
|
| 47 |
processed_inputs = {
|
| 48 |
"pixel_values": torch.cat([inp["pixel_values"] for inp in processed_inputs], dim=0).to(device),
|
|
|
|
|
|
|
| 49 |
"max_new_tokens":40
|
| 50 |
}
|
| 51 |
|
|
|
|
| 53 |
out = self.model.generate(**processed_inputs)
|
| 54 |
|
| 55 |
captions = self.processor.batch_decode(out, skip_special_tokens=True)
|
| 56 |
+
print("caption is here-------",captions)
|
| 57 |
return {"captions": captions}
|
| 58 |
except Exception as e:
|
| 59 |
print(f"Error during processing: {str(e)}")
|