File size: 1,256 Bytes
fc8d34d
2925e7b
fc8d34d
 
2925e7b
 
 
16bf9bb
2925e7b
 
 
093fcf9
2925e7b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
156d9da
 
 
 
 
184cc66
2925e7b
 
 
 
 
 
 
 
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
import gradio as gr
from transformers import AutoModelForSequenceClassification, AutoTokenizer, pipeline


class Emotionclass:
    def __init__(self, model: str):
        self.model = AutoModelForSequenceClassification.from_pretrained(model)
        self.tokenizer = AutoTokenizer.from_pretrained(model)
        self.pipeline = pipeline(
            "text-classification",
            model=self.model,
            tokenizer=self.tokenizer,
            return_all_scores=True,
        )

    def predict(self, input: str):
        output = self.pipeline(input)[0]
        result = {
            "sad": output[0]["score"],
            "joy": output[1]["score"],
            "love": output[2]["score"],
            "anger": output[3]["score"],
            "fear": output[4]["score"],
            "surprise": output[5]["score"],
        }
        return result


def main():
    model = Emotionclass("bhadresh-savani/bert-base-uncased-emotion")
    iface = gr.Interface(
        fn=model.predict,
        inputs=gr.inputs.Textbox(
            lines=3,
            placeholder="type here",
            label="Input",
        ),
        outputs="label",
        title="Sentiment Classification",
    )

    iface.launch()


if __name__ == "__main__":
    main()