Update handler.py
Browse files- handler.py +3 -10
handler.py
CHANGED
|
@@ -2,13 +2,12 @@ from typing import Dict, List, Any
|
|
| 2 |
from io import BytesIO
|
| 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=""):
|
| 11 |
-
self.model
|
| 12 |
|
| 13 |
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
|
| 14 |
"""
|
|
@@ -24,16 +23,10 @@ class EndpointHandler():
|
|
| 24 |
image = Image.open(BytesIO(base64.b64decode(inputs_request['image'])))
|
| 25 |
text = inputs_request['text']
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 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 |
}
|
|
|
|
| 2 |
from io import BytesIO
|
| 3 |
import base64
|
| 4 |
import logging
|
|
|
|
| 5 |
from PIL import Image
|
| 6 |
import numpy as np
|
| 7 |
|
| 8 |
class EndpointHandler():
|
| 9 |
def __init__(self, path=""):
|
| 10 |
+
self.model = AutoModel.from_pretrained('jinaai/jina-clip-v1', trust_remote_code=True)
|
| 11 |
|
| 12 |
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
|
| 13 |
"""
|
|
|
|
| 23 |
image = Image.open(BytesIO(base64.b64decode(inputs_request['image'])))
|
| 24 |
text = inputs_request['text']
|
| 25 |
|
| 26 |
+
text_embeddings = model.encode_text(text)
|
| 27 |
+
image_embeddings = model.encode_image(image)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
# Convert embeddings to lists of floats
|
| 29 |
serializable_results = {
|
|
|
|
| 30 |
'text_embedding': text_embedding.tolist() if isinstance(text_embedding, np.ndarray) else text_embedding,
|
| 31 |
'image_embedding': image_embedding.tolist() if isinstance(image_embedding, np.ndarray) else image_embedding
|
| 32 |
}
|