Mantas commited on
Commit
bec438c
1 Parent(s): 50d544d

Create handler.py

Browse files
Files changed (1) hide show
  1. handler.py +26 -0
handler.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Dict, List, Any
2
+ from setfit import SetFitModel
3
+
4
+ class EndpointHandler:
5
+ def __init__(self, path=""):
6
+ # load model
7
+ self.model = SetFitModel.from_pretrained(path)
8
+
9
+ self.id2label = {{0: 'Art', 1: 'Dedicated', 2: 'Domains', 3: 'Gaming', 4: 'General', 5: 'Music', 6: 'Other'}}
10
+
11
+ def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
12
+ """
13
+ data args:
14
+ inputs (:obj: `str`)
15
+ Return:
16
+ A :obj:`list` | `dict`: will be serialized and returned
17
+ """
18
+ # get inputs
19
+ inputs = data.pop("inputs", data)
20
+ if isinstance(inputs, str):
21
+ inputs = [inputs]
22
+
23
+ # run normal prediction
24
+ scores = self.model.predict_proba(inputs)[0]
25
+
26
+ return [{"label": self.id2label[i], "score": score.item()} for i, score in enumerate(scores)]