File size: 2,553 Bytes
619d92b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
from typing import Dict, List, Any
from setfit import SetFitModel


class EndpointHandler:
    def __init__(self, path=""):
        # load model
        self.model = SetFitModel.from_pretrained(path)
        # ag_news id to label mapping
        self.id2label = {
            0: "Art",
            1: "Artificial Intelligence",
            2: "Beauty",
            3: "Blockchain",
            4: "Business",
            5: "Cities",
            6: "Cultural Studies",
            7: "Data Science",
            8: "Design",
            9: "Dev Ops",
            10: "Drugs",
            11: "Economics",
            12: "Education",
            13: "Equality",
            14: "Family",
            15: "Fashion",
            16: "Finance",
            17: "Food",
            18: "Gadgets",
            19: "Gaming",
            20: "Health",
            21: "Home",
            22: "Humor",
            23: "Language",
            24: "Law",
            25: "Leadership",
            26: "Makers",
            27: "Marketing",
            28: "Mathematics",
            29: "Mental Health",
            30: "Mindfulness",
            31: "Movies",
            32: "Music",
            33: "Nature",
            34: "News",
            35: "Operating Systems",
            36: "Pets",
            37: "Philosophy",
            38: "Photography",
            39: "Podcasts",
            40: "Politics",
            41: "Product Management",
            42: "Productivity",
            43: "Programming",
            44: "Programming Languages",
            45: "Race",
            46: "Relationships",
            47: "Religion",
            48: "Remote Work",
            49: "Science",
            50: "Security",
            51: "Sexuality",
            52: "Spirituality",
            53: "Sports",
            54: "Tech Companies",
            55: "Television",
            56: "Transportation",
            57: "Travel",
            58: "Writing",
        }

    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)
        ]