add test handler
Browse files- handler.py +22 -0
handler.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Dict, List, Any
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
+
|
| 4 |
+
class EndpointHandler():
|
| 5 |
+
def __init__(self, path=""):
|
| 6 |
+
# Preload all the elements you are going to need at inference.
|
| 7 |
+
# pseudo:
|
| 8 |
+
self.tokenizer= AutoTokenizer.from_pretrained(path)
|
| 9 |
+
self.model= AutoModelForCausalLM.from_pretrained(path)
|
| 10 |
+
|
| 11 |
+
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
|
| 12 |
+
"""
|
| 13 |
+
data args:
|
| 14 |
+
inputs (:obj: `str` | `PIL.Image` | `np.array`)
|
| 15 |
+
kwargs
|
| 16 |
+
Return:
|
| 17 |
+
A :obj:`list` | `dict`: will be serialized and returned
|
| 18 |
+
"""
|
| 19 |
+
text = data.pop("text")
|
| 20 |
+
inputs = self.tokenizer(text, return_tensors="pt")
|
| 21 |
+
logits = self.model(inputs).logits
|
| 22 |
+
return [{"predictions":logits.argmax(dim=-1)}]
|