|
from typing import Dict, List, Any |
|
from transformers import AutoTokenizer, AutoModel |
|
from optimum.onnxruntime.modeling_ort import ORTModelForCustomTasks |
|
|
|
import torch |
|
|
|
|
|
def mean_pooling(model_output, attention_mask): |
|
token_embeddings = model_output[0] |
|
input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float() |
|
return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9) |
|
|
|
class EndpointHandler(): |
|
def __init__(self, path=""): |
|
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu") |
|
self.model = ORTModelForCustomTasks.from_pretrained("optimum/sbert-all-MiniLM-L6-with-pooler") |
|
self.tokenizer = AutoTokenizer.from_pretrained("optimum/sbert-all-MiniLM-L6-with-pooler") |
|
|
|
|
|
|
|
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]: |
|
""" |
|
data args: |
|
inputs (:obj: `str` | `PIL.Image` | `np.array`) |
|
kwargs |
|
Return: |
|
A :obj:`list` | `dict`: will be serialized and returned |
|
""" |
|
sentences = data.pop("inputs",data) |
|
inputs = tokenizer("I love burritos!", return_tensors="pt") |
|
pred = self.model(**encoded_input) |
|
|
|
|
|
sentence_embeddings = mean_pooling(model_output, encoded_input['attention_mask']) |
|
return sentence_embeddings.tolist() |