philschmid HF staff commited on
Commit
e84b5e4
1 Parent(s): 8e58269

Upload pipeline.py

Browse files
Files changed (1) hide show
  1. pipeline.py +33 -0
pipeline.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Dict, List, Any
2
+ from optimum.onnxruntime import ORTModelForSequenceClassification
3
+ from transformers import pipeline, AutoTokenizer
4
+
5
+
6
+ class PreTrainedPipeline():
7
+ def __init__(self, path=""):
8
+ # load the optimized model
9
+ model = ORTModelForSequenceClassification.from_pretrained(path)
10
+ tokenizer = AutoTokenizer.from_pretrained(path, model_max_length=128)
11
+ # create inference pipeline
12
+ self.pipeline = pipeline("feature-extraction", model=model, tokenizer=tokenizer)
13
+
14
+
15
+ def __call__(self, inputs: Any) -> List[List[Dict[str, float]]]:
16
+ """
17
+ Args:
18
+ data (:obj:`str`):
19
+ a string containing some text
20
+ Return:
21
+ A :obj:`list`:. The object returned should be a list of one list like [[{"label": 0.9939950108528137}]] containing :
22
+ - "label": A string representing what the label/class is. There can be multiple labels.
23
+ - "score": A score between 0 and 1 describing how confident the model is for this label/class.
24
+ """
25
+ # pop inputs for pipeline
26
+ def cls_pooling(pipeline_output):
27
+ """
28
+ Return the [CLS] token embedding
29
+ """
30
+ return [_h[0] for _h in pipeline_output]
31
+
32
+ embeddings = cls_pooling(self.pipeline(inputs))
33
+ return embeddings