YassineB commited on
Commit
11fc8e6
·
1 Parent(s): 2640a98

Add cls model with optimum

Browse files
Files changed (2) hide show
  1. handler.py +35 -0
  2. requirements.txt +2 -0
handler.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Dict, List, Any
2
+ from optimum.onnxruntime import ORTModelForSequenceClassification
3
+ from transformers import pipeline, AutoTokenizer
4
+
5
+
6
+ class EndpointHandler():
7
+ def __init__(self):
8
+ # load the optimized model
9
+ path = "optimum/roberta-base-squad2"
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)
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", None)
28
+
29
+ # pass inputs with all kwargs in data
30
+ if parameters is not None:
31
+ prediction = self.pipeline(inputs, **parameters)
32
+ else:
33
+ prediction = self.pipeline(inputs)
34
+ # postprocess the prediction
35
+ return prediction
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ optimum
2
+ transformers