handler custom
Browse files- handler.py +23 -0
- requirements.txt +1 -0
handler.py
ADDED
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import Dict, List, Any
|
2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
3 |
+
|
4 |
+
|
5 |
+
class EndpointHandler:
|
6 |
+
def __init__(self, path=""):
|
7 |
+
# load the model
|
8 |
+
tokenizer = AutoTokenizer.from_pretrained(path)
|
9 |
+
model = AutoModelForCausalLM.from_pretrained(path, device_map="auto")
|
10 |
+
# create inference pipeline
|
11 |
+
self.pipeline = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
12 |
+
|
13 |
+
def __call__(self, data: Any) -> List[List[Dict[str, float]]]:
|
14 |
+
inputs = data.pop("inputs", data)
|
15 |
+
parameters = data.pop("parameters", None)
|
16 |
+
|
17 |
+
# pass inputs with all kwargs in data
|
18 |
+
if parameters is not None:
|
19 |
+
prediction = self.pipeline(inputs, **parameters)
|
20 |
+
else:
|
21 |
+
prediction = self.pipeline(inputs)
|
22 |
+
# postprocess the prediction
|
23 |
+
return prediction
|
requirements.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
transformers
|