joshuaKnauber
commited on
Commit
•
ba55ec2
1
Parent(s):
26e6016
clip
Browse files- .gitignore +1 -0
- handler.py +25 -0
- requirements.txt +2 -0
.gitignore
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
/venv
|
handler.py
ADDED
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import Dict, List, Any
|
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 |
+
|
9 |
+
class EndpointHandler():
|
10 |
+
def __init__(self, path=""):
|
11 |
+
# Preload all the elements you we need at inference.
|
12 |
+
self.model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")
|
13 |
+
self.processor = CLIPProcessor.from_pretrained(
|
14 |
+
"openai/clip-vit-base-patch32")
|
15 |
+
|
16 |
+
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
|
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,
|
22 |
+
return_tensors="pt", padding=True)
|
23 |
+
outputs = self.model(**inputs)
|
24 |
+
embeddings = outputs.image_embeds.detach().numpy().flatten().tolist()
|
25 |
+
return {"embeddings": embeddings}
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
pillow
|
2 |
+
numpy
|