custom pipeline
Browse files- pipeline.py +40 -0
- requirements.txt +2 -0
pipeline.py
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from torch import Tensor
|
3 |
+
from transformers import AutoTokenizer, AutoModel
|
4 |
+
from typing import List
|
5 |
+
import os
|
6 |
+
|
7 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
8 |
+
|
9 |
+
|
10 |
+
class PreTrainedPipeline():
|
11 |
+
def __init__(self, path=""):
|
12 |
+
# load the optimized model
|
13 |
+
self.model_path = os.path.join("", '.')
|
14 |
+
self.tokenizer = AutoTokenizer.from_pretrained(self.model_path)
|
15 |
+
self.model = AutoModel.from_pretrained(self.model_path)
|
16 |
+
self.model.eval()
|
17 |
+
self.model = self.model.to(device)
|
18 |
+
|
19 |
+
def __call__(self, inputs: str) -> List[float]:
|
20 |
+
"""
|
21 |
+
Args:
|
22 |
+
data (:obj:):
|
23 |
+
includes the input data and the parameters for the inference.
|
24 |
+
Return:
|
25 |
+
A :obj:`dict`:. The object returned should be a dict like {"feature_vector": [0.6331314444541931,0.8802216053009033,...,-0.7866355180740356,]} containing :
|
26 |
+
- "feature_vector": A list of floats corresponding to the image embedding.
|
27 |
+
"""
|
28 |
+
|
29 |
+
batch_dict = self.tokenizer(inputs, max_length=512,
|
30 |
+
padding=True, truncation=True, return_tensors='pt')
|
31 |
+
with torch.no_grad():
|
32 |
+
outputs = self.model(**batch_dict)
|
33 |
+
embeddings = self.average_pool(outputs.last_hidden_state,
|
34 |
+
batch_dict['attention_mask'])
|
35 |
+
return embeddings.cpu().numpy().tolist()
|
36 |
+
|
37 |
+
def average_pool(self, last_hidden_states: Tensor, attention_mask: Tensor) -> Tensor:
|
38 |
+
last_hidden = last_hidden_states.masked_fill(
|
39 |
+
~attention_mask[..., None].bool(), 0.0)
|
40 |
+
return last_hidden.sum(dim=1) / attention_mask.sum(dim=1)[..., None]
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
transformers
|
2 |
+
torch
|