File size: 1,144 Bytes
191d1d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import torch

from typing import Any, Dict, Union
from transformers import AutoModelForSequenceClassification, AutoTokenizer


class EndpointHandler:
    def __init__(self, path=""):
        # load model and tokenizer from path
        self.tokenizer = AutoTokenizer.from_pretrained(path)
        self.model = AutoModelForSequenceClassification.from_pretrained(
            path, device_map="auto", trust_remote_code=True
        )
        self.device = "cuda" if torch.cuda.is_available() else "cpu"

    def __call__(self, data: Dict[str, Any]) -> Dict[str, Union[str, float]]:
        # process input
        inputs = data.pop("inputs", data)

        # preprocess
        inputs = self.tokenizer(inputs, return_tensors="pt").to(self.device)

        # pass inputs with all kwargs in data
        logits = self.model(**inputs)[0]

        # postprocess the prediction
        predicted_class_id = int(torch.argmax(logits, dim=-1))
        predicted_score = float(logits[0, predicted_class_id])
        predicted_label = str(self.model.config.id2label[predicted_class_id])

        return {'label': predicted_label, 'score': predicted_score}