added model
Browse files- handler.py +27 -0
- requirements.txt +2 -0
handler.py
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import Any, Dict, List, AnyStr
|
2 |
+
import numpy as np
|
3 |
+
from transformers import CLIPProcessor, CLIPModel
|
4 |
+
from PIL import Image
|
5 |
+
from io import BytesIO
|
6 |
+
import base64
|
7 |
+
|
8 |
+
class EndpointHandler():
|
9 |
+
def __init__(self, path="") -> None:
|
10 |
+
"Preload all the elements we need at inference."
|
11 |
+
self.model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")
|
12 |
+
self.processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
|
13 |
+
self.path = path
|
14 |
+
|
15 |
+
def __call__(self, data: Dict[str, AnyStr]) -> List[Dict[str, AnyStr]]:
|
16 |
+
"Run the inference."
|
17 |
+
inputs = data.get('inputs')
|
18 |
+
text = inputs.get('text')
|
19 |
+
imageData = inputs.get('image')
|
20 |
+
image = Image.open(BytesIO(base64.b64decode(imageData)))
|
21 |
+
inputs = self.processor(text=text, images=image, return_tensors="pt", padding=True)
|
22 |
+
outputs = self.model(**inputs)
|
23 |
+
image_embeds = outputs.image_embeds.detach().numpy().flatten().tolist()
|
24 |
+
text_embeds = outputs.text_embeds.detach().numpy().flatten().tolist()
|
25 |
+
logits_per_image = outputs.logits_per_image.detach().numpy().flatten().tolist()
|
26 |
+
return {'image_embeddings': image_embeds, 'text_embeddings': text_embeds, 'logits_per_image': logits_per_image}
|
27 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
pillow
|
2 |
+
numpy
|