Mantas commited on
Commit
d06c586
1 Parent(s): 40cc0f5

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: 'Action', 1: 'Card Games', 2: 'GameFi', 3: 'Metaverse', 4: 'Move2Earn', 5: 'Other', 6: 'Play2Earn', 7: 'RPG', 8: 'Sports', 9: 'Strategy'}
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)]