Update handler.py
Browse files- handler.py +15 -9
handler.py
CHANGED
|
@@ -1,20 +1,26 @@
|
|
| 1 |
-
from
|
| 2 |
-
from optimum.onnxruntime import ORTModelForSequenceClassification
|
| 3 |
-
from transformers import AutoTokenizer, pipeline
|
| 4 |
-
|
| 5 |
|
| 6 |
class EndpointHandler():
|
| 7 |
def __init__(self, path=""):
|
| 8 |
# load the optimized model
|
| 9 |
-
model =
|
| 10 |
-
tokenizer =
|
| 11 |
|
| 12 |
self.pipeline = pipeline("text-classification", model=model, tokenizer=tokenizer)
|
| 13 |
|
| 14 |
|
| 15 |
-
def __call__(self, data
|
| 16 |
inputs = data.pop("inputs", data)
|
| 17 |
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import DistilBertTokenizerFast, DistilBertForSequenceClassification, pipeline
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
class EndpointHandler():
|
| 4 |
def __init__(self, path=""):
|
| 5 |
# load the optimized model
|
| 6 |
+
model = DistilBertForSequenceClassification.from_pretrained(path)
|
| 7 |
+
tokenizer = DistilBertTokenizerFast.from_pretrained(path, do_lower_case=True)
|
| 8 |
|
| 9 |
self.pipeline = pipeline("text-classification", model=model, tokenizer=tokenizer)
|
| 10 |
|
| 11 |
|
| 12 |
+
def __call__(self, data):
|
| 13 |
inputs = data.pop("inputs", data)
|
| 14 |
|
| 15 |
+
def iterator():
|
| 16 |
+
for i in inputs:
|
| 17 |
+
yield i
|
| 18 |
+
|
| 19 |
+
labels = []
|
| 20 |
+
for out in pipeline(iterator(), padding=True, truncation=True, max_length=253):
|
| 21 |
+
labels.append(int(out["label"][-1]))
|
| 22 |
|
| 23 |
+
return {
|
| 24 |
+
"pairs": inputs,
|
| 25 |
+
"evaluations": labels
|
| 26 |
+
}
|