Commit
·
e6a558b
1
Parent(s):
1cd9297
Upload handler.py
Browse files- handler.py +13 -4
handler.py
CHANGED
@@ -3,6 +3,8 @@ from PIL import Image
|
|
3 |
from transformers import Blip2Processor, Blip2ForConditionalGeneration
|
4 |
from typing import Dict, List, Any
|
5 |
import torch
|
|
|
|
|
6 |
|
7 |
class EndpointHandler():
|
8 |
def __init__(self, path=""):
|
@@ -13,12 +15,19 @@ class EndpointHandler():
|
|
13 |
self.model.to(self.device)
|
14 |
|
15 |
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
|
16 |
-
|
|
|
|
|
|
|
17 |
|
18 |
-
image = Image.open(image_path)
|
19 |
-
|
20 |
processed = self.processor(images=image, return_tensors="pt").to(self.device)
|
21 |
|
22 |
out = self.model.generate(**processed)
|
23 |
|
24 |
-
return self.processor.decode(out[0], skip_special_tokens=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
from transformers import Blip2Processor, Blip2ForConditionalGeneration
|
4 |
from typing import Dict, List, Any
|
5 |
import torch
|
6 |
+
import base64
|
7 |
+
from io import BytesIO
|
8 |
|
9 |
class EndpointHandler():
|
10 |
def __init__(self, path=""):
|
|
|
15 |
self.model.to(self.device)
|
16 |
|
17 |
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
|
18 |
+
image_encoded = data.pop("inputs", data)
|
19 |
+
|
20 |
+
# image = Image.open(image_path)
|
21 |
+
image = self.decode_base64_image(image_encoded)
|
22 |
|
|
|
|
|
23 |
processed = self.processor(images=image, return_tensors="pt").to(self.device)
|
24 |
|
25 |
out = self.model.generate(**processed)
|
26 |
|
27 |
+
return self.processor.decode(out[0], skip_special_tokens=True)
|
28 |
+
|
29 |
+
def decode_base64_image(self, image_string):
|
30 |
+
base64_image = base64.b64decode(image_string)
|
31 |
+
buffer = BytesIO(base64_image)
|
32 |
+
image = Image.open(buffer)
|
33 |
+
return image
|