|
from typing import Dict, List, Any |
|
from setfit import SetFitModel |
|
|
|
|
|
class EndpointHandler: |
|
def __init__(self, path=""): |
|
|
|
self.model = SetFitModel.from_pretrained(path) |
|
|
|
self.id2label = {0: "Absent", 1: "Present"} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]: |
|
""" |
|
data args: |
|
inputs (:obj: `List[str]`) - List of strings |
|
Return: |
|
A :obj:`list` of dicts: each dict contains 'label' and 'score' for each input string |
|
""" |
|
|
|
inputs = data.pop("inputs", data) |
|
if not isinstance(inputs, list): |
|
raise ValueError("Input must be a list of strings") |
|
|
|
|
|
all_scores = self.model.predict_proba(inputs) |
|
|
|
|
|
results = [] |
|
for scores in all_scores: |
|
results.append([ |
|
{"label": self.id2label[i], "score": score.item()} for i, score in enumerate(scores) |
|
]) |
|
|
|
return results |