jpohhhh commited on
Commit
28f4382
·
1 Parent(s): 8a3e3f9

Create handler.py

Browse files
Files changed (1) hide show
  1. handler.py +31 -0
handler.py ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Dict, List, Any
2
+ from transformers import AutoTokenizer, AutoModel
3
+ import torch
4
+
5
+ #Mean Pooling - Take attention mask into account for correct averaging
6
+ def mean_pooling(model_output, attention_mask):
7
+ token_embeddings = model_output[0] #First element of model_output contains all token embeddings
8
+ input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
9
+ return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)
10
+
11
+ class EndpointHandler():
12
+ def __init__(self, path=""):
13
+ self.tokenizer = AutoTokenizer.from_pretrained('sentence-transformers/msmarco-MiniLM-L-6-v3')
14
+ self.model = AutoModel.from_pretrained('sentence-transformers/msmarco-MiniLM-L-6-v3')
15
+
16
+ def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
17
+ """
18
+ data args:
19
+ inputs (:obj: `str` | `PIL.Image` | `np.array`)
20
+ kwargs
21
+ Return:
22
+ A :obj:`list` | `dict`: will be serialized and returned
23
+ """
24
+ encoded_input = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt')
25
+ # Compute token embeddings
26
+ with torch.no_grad():
27
+ model_output = self.model(**encoded_input)
28
+
29
+ # Perform pooling. In this case, max pooling.
30
+ sentence_embeddings = mean_pooling(model_output, encoded_input['attention_mask'])
31
+ return model_output