dariadaria commited on
Commit
c14b84e
·
1 Parent(s): 821fb54

Create handler.py

Browse files
Files changed (1) hide show
  1. handler.py +36 -0
handler.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
2
+ import torch
3
+
4
+
5
+ class EndpointHandler:
6
+ def __init__(self, path=""):
7
+ self.tokenizer = AutoTokenizer.from_pretrained(path)
8
+ self.model = AutoModelForSequenceClassification.from_pretrained(path, num_labels=3)
9
+ def tokenize(text, topic):
10
+ return self.tokenizer(
11
+ topic,
12
+ text,
13
+ max_length=384, #512
14
+ truncation="only_second",
15
+ return_offsets_mapping=False,
16
+ padding="max_length",
17
+ return_tensors='pt'
18
+ )
19
+ self.tokenize = tokenize
20
+
21
+ def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
22
+ """
23
+ data args:
24
+ topic (:obj: `str`)
25
+ text (:obj: `str`)
26
+ Return:
27
+ A :obj:`list` | `dict`: will be serialized and returned
28
+ """
29
+ topic = data.pop("topic", data)
30
+ text = data.pop("text", data)
31
+ tokenized_inputs = self.tokenize(text, topic)
32
+
33
+ output = self.model(**tokenized_inputs)
34
+ prediction = torch.argmax(output.logits, dim=-1).item()
35
+ label = self.model.config.id2label[prediction]
36
+ return label