Update handler.py
Browse files- handler.py +28 -5
handler.py
CHANGED
@@ -3,7 +3,8 @@ from io import BytesIO
|
|
3 |
import base64
|
4 |
import logging
|
5 |
import uform
|
6 |
-
|
|
|
7 |
|
8 |
class EndpointHandler():
|
9 |
def __init__(self, path=""):
|
@@ -11,9 +12,31 @@ class EndpointHandler():
|
|
11 |
|
12 |
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
|
13 |
"""
|
14 |
-
|
15 |
image (:obj:`string`)
|
16 |
candidates (:obj:`list`)
|
17 |
-
|
18 |
-
A :obj:`list
|
19 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
import base64
|
4 |
import logging
|
5 |
import uform
|
6 |
+
from PIL import Image
|
7 |
+
import numpy as np
|
8 |
|
9 |
class EndpointHandler():
|
10 |
def __init__(self, path=""):
|
|
|
12 |
|
13 |
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
|
14 |
"""
|
15 |
+
data args:
|
16 |
image (:obj:`string`)
|
17 |
candidates (:obj:`list`)
|
18 |
+
Return:
|
19 |
+
A :obj:`list`: une liste permettant de passer les embedding
|
20 |
+
"""
|
21 |
+
inputs_request = data.pop("inputs", data)
|
22 |
+
|
23 |
+
# decode base64 image to PIL
|
24 |
+
image = Image.open(BytesIO(base64.b64decode(inputs_request['image'])))
|
25 |
+
text = inputs_request['text']
|
26 |
+
|
27 |
+
image_data = self.processor.preprocess_image(image)
|
28 |
+
text_data = self.processor.preprocess_text(text)
|
29 |
+
|
30 |
+
image_features, image_embedding = self.model.encode_image(image_data)
|
31 |
+
text_features, text_embedding = self.model.encode_text(text_data)
|
32 |
+
joint_embedding = self.model.encode_multimodal(image=image_data, text=text_data)
|
33 |
+
|
34 |
+
# Convert embeddings to lists of floats
|
35 |
+
serializable_results = {
|
36 |
+
'joint_embedding': joint_embedding.tolist() if isinstance(joint_embedding, np.ndarray) else joint_embedding,
|
37 |
+
'text_embedding': text_embedding.tolist() if isinstance(text_embedding, np.ndarray) else text_embedding,
|
38 |
+
'image_embedding': image_embedding.tolist() if isinstance(image_embedding, np.ndarray) else image_embedding
|
39 |
+
}
|
40 |
+
|
41 |
+
return serializable_results
|
42 |
+
|