Create handler.py
Browse files- 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: 'Bridge', 1: 'CDP', 2: 'Cross-chain', 3: 'DEX', 4: 'Derivatives', 5: 'Insurance', 6: 'Lending', 7: 'NFT Lending', 8: 'Other', 9: 'Staking', 10: 'Synthetics'}
|
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)]
|