Upload handler.py
Browse files- handler.py +9 -1
handler.py
CHANGED
|
@@ -4,7 +4,9 @@ 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=""):
|
| 10 |
# Preload all the elements you we need at inference.
|
|
@@ -13,10 +15,16 @@ class EndpointHandler():
|
|
| 13 |
|
| 14 |
|
| 15 |
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
inputs = data.get("inputs")
|
| 17 |
text = inputs.get("text")
|
| 18 |
imageData = inputs.get("image")
|
| 19 |
-
|
|
|
|
|
|
|
| 20 |
inputs = self.processor(text=text, images=image, return_tensors="pt", padding=True)
|
| 21 |
outputs = self.model(**inputs)
|
| 22 |
embeddings = outputs.image_embeds.detach().numpy().flatten().tolist()
|
|
|
|
| 4 |
from PIL import Image
|
| 5 |
from io import BytesIO
|
| 6 |
import base64
|
| 7 |
+
import requests
|
| 8 |
|
| 9 |
+
# handle clip embeddings by utilizing openAI CLIP pretrained model
|
| 10 |
class EndpointHandler():
|
| 11 |
def __init__(self, path=""):
|
| 12 |
# Preload all the elements you we need at inference.
|
|
|
|
| 15 |
|
| 16 |
|
| 17 |
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
|
| 18 |
+
# inputs = self.processor(text=["a photo of a cat", "a photo of a dog"], images=image, return_tensors="pt", padding=True)
|
| 19 |
+
# logits_per_image = outputs.logits_per_image # this is the image-text similarity score
|
| 20 |
+
# probs = logits_per_image.softmax(dim=1) # we can take the softmax to get the label probabilities
|
| 21 |
+
|
| 22 |
inputs = data.get("inputs")
|
| 23 |
text = inputs.get("text")
|
| 24 |
imageData = inputs.get("image")
|
| 25 |
+
url = inputs.get("image")
|
| 26 |
+
image = Image.open(requests.get(url, stream=True).raw)
|
| 27 |
+
# image = Image.open(BytesIO(base64.b64decode(imageData)))
|
| 28 |
inputs = self.processor(text=text, images=image, return_tensors="pt", padding=True)
|
| 29 |
outputs = self.model(**inputs)
|
| 30 |
embeddings = outputs.image_embeds.detach().numpy().flatten().tolist()
|