Mantas commited on
Commit
b0d1779
1 Parent(s): 9b56e73

Update handler.py

Browse files
Files changed (1) hide show
  1. handler.py +17 -11
handler.py CHANGED
@@ -1,20 +1,26 @@
1
  from typing import Dict, List, Any
2
- from transformers import pipeline
3
 
4
- class EndpointHandler():
5
  def __init__(self, path=""):
6
- self.pipeline = pipeline("text-classification", model=path)
 
 
 
7
 
8
  def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
9
  """
10
- data args:
11
- inputs (:obj: `str`)
12
-
13
- Return:
14
- A :obj:`list` | `dict`: will be serialized and returned
15
  """
16
  # get inputs
17
- inputs = data.pop("inputs",data)
 
 
 
18
  # run normal prediction
19
- prediction = self.pipeline(inputs)
20
- return prediction
 
 
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: 'Collectibles', 2: 'Domains', 3: 'Games', 4: 'Land', 5: 'Metaverse', 6: 'Music', 7: 'Other', 8: 'PFPs', 9: 'Sports'}}
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)]