handler
Browse files- handler.py +31 -0
handler.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import List
|
| 2 |
+
from transformers import CLIPTokenizer, CLIPModel
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
class EndpointHandler():
|
| 6 |
+
def __init__(self, path=""):
|
| 7 |
+
# self.model= load_model(path)
|
| 8 |
+
hf_model_path = "openai/clip-vit-large-patch14"
|
| 9 |
+
self.model = CLIPModel.from_pretrained(hf_model_path)
|
| 10 |
+
self.tokenizer = CLIPTokenizer.from_pretrained(hf_model_path)
|
| 11 |
+
|
| 12 |
+
def __call__(self, inputs: str) -> List[float]:
|
| 13 |
+
"""
|
| 14 |
+
data args:
|
| 15 |
+
inputs (:obj: `str` | `PIL.Image` | `np.array`)
|
| 16 |
+
kwargs
|
| 17 |
+
Return:
|
| 18 |
+
A :obj:`list` | `dict`: will be serialized and returned
|
| 19 |
+
"""
|
| 20 |
+
|
| 21 |
+
# pseudo
|
| 22 |
+
# self.model(input)
|
| 23 |
+
token_inputs = self.tokenizer([inputs], padding=True, return_tensors="pt")
|
| 24 |
+
query_embed = self.model.get_text_features(**token_inputs)
|
| 25 |
+
np_query_embed = query_embed.detach().cpu().numpy()[0].tolist()
|
| 26 |
+
return np_query_embed
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
# if __name__ == "__main__":
|
| 30 |
+
# handler = EndpointHandler()
|
| 31 |
+
# print(handler("a dog"))
|