Upload 2 files
Browse files- handler.py +31 -0
- requirements.txt +2 -0
handler.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Dict, List, Any
|
| 2 |
+
from optimum.onnxruntime import ORTModelForSequenceClassification
|
| 3 |
+
from transformers import AutoTokenizer
|
| 4 |
+
from optimum.pipelines import pipeline
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
class EndpointHandler():
|
| 8 |
+
def __init__(self, path=""):
|
| 9 |
+
# load the optimized model
|
| 10 |
+
model = ORTModelForSequenceClassification.from_pretrained(path)
|
| 11 |
+
tokenizer = AutoTokenizer.from_pretrained(path)
|
| 12 |
+
# create inference pipeline
|
| 13 |
+
self.pipeline = pipeline("text-classification", model=model, tokenizer=tokenizer, device=0)
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def __call__(self, data: Any) -> List[List[Dict[str, float]]]:
|
| 17 |
+
"""
|
| 18 |
+
Args:
|
| 19 |
+
data (:obj:):
|
| 20 |
+
includes the input data and the parameters for the inference.
|
| 21 |
+
Return:
|
| 22 |
+
A :obj:`list`:. The object returned should be a list of one list like [[{"label": 0.9939950108528137}]] containing :
|
| 23 |
+
- "label": A string representing what the label/class is. There can be multiple labels.
|
| 24 |
+
- "score": A score between 0 and 1 describing how confident the model is for this label/class.
|
| 25 |
+
"""
|
| 26 |
+
inputs = data.pop("inputs", data)
|
| 27 |
+
parameters = data.pop("parameters", dict())
|
| 28 |
+
|
| 29 |
+
prediction = self.pipeline(inputs, **parameters)
|
| 30 |
+
|
| 31 |
+
return prediction
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers==4.44.2
|
| 2 |
+
optimum[onnxruntime-gpu]==1.22.0
|