Mantas's picture
Create handler.py
d06c586
raw
history blame contribute delete
919 Bytes
from typing import Dict, List, Any
from setfit import SetFitModel
class EndpointHandler:
def __init__(self, path=""):
# load model
self.model = SetFitModel.from_pretrained(path)
self.id2label = {0: 'Action', 1: 'Card Games', 2: 'GameFi', 3: 'Metaverse', 4: 'Move2Earn', 5: 'Other', 6: 'Play2Earn', 7: 'RPG', 8: 'Sports', 9: 'Strategy'}
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
"""
data args:
inputs (:obj: `str`)
Return:
A :obj:`list` | `dict`: will be serialized and returned
"""
# get inputs
inputs = data.pop("inputs", data)
if isinstance(inputs, str):
inputs = [inputs]
# run normal prediction
scores = self.model.predict_proba(inputs)[0]
return [{"label": self.id2label[i], "score": score.item()} for i, score in enumerate(scores)]