Commit
•
5d9ef01
1
Parent(s):
e674c14
Update pipeline.py
Browse files- pipeline.py +11 -15
pipeline.py
CHANGED
@@ -2,14 +2,13 @@ from typing import Dict, List, Any
|
|
2 |
from optimum.onnxruntime import ORTModelForFeatureExtraction
|
3 |
from transformers import pipeline, AutoTokenizer
|
4 |
|
5 |
-
|
|
|
6 |
class PreTrainedPipeline():
|
7 |
def __init__(self, path=""):
|
8 |
# load the optimized model
|
9 |
-
model = ORTModelForFeatureExtraction.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) -> Dict[str, Any]:
|
@@ -22,13 +21,10 @@ class PreTrainedPipeline():
|
|
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 |
-
#
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
embeddings = cls_pooling(self.pipeline(inputs))
|
33 |
-
return {"vectors": [122.23]}
|
34 |
-
|
|
|
2 |
from optimum.onnxruntime import ORTModelForFeatureExtraction
|
3 |
from transformers import pipeline, AutoTokenizer
|
4 |
|
5 |
+
def cls_pooling(model_output):
|
6 |
+
return model_output.last_hidden_state[:,0]
|
7 |
class PreTrainedPipeline():
|
8 |
def __init__(self, path=""):
|
9 |
# load the optimized model
|
10 |
+
self.model = ORTModelForFeatureExtraction.from_pretrained(path)
|
11 |
+
self.tokenizer = AutoTokenizer.from_pretrained(path, model_max_length=128)
|
|
|
|
|
12 |
|
13 |
|
14 |
def __call__(self, inputs: Any) -> Dict[str, Any]:
|
|
|
21 |
- "label": A string representing what the label/class is. There can be multiple labels.
|
22 |
- "score": A score between 0 and 1 describing how confident the model is for this label/class.
|
23 |
"""
|
24 |
+
# tokenize the input
|
25 |
+
encoded_input = self.tokenizer(inputs, padding="longest", truncation=True, return_tensors='pt')
|
26 |
+
# run the model
|
27 |
+
model_output = self.model(**encoded_input, return_dict=True)
|
28 |
+
embeddings = cls_pooling(model_output)
|
29 |
+
|
30 |
+
return {"vectors": embeddings[0].tolist()}
|
|
|
|
|
|