File size: 1,266 Bytes
32e7a6a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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    

if __name__ == "__main__":
    model = Emotionclass("ncduy/bert-base-cased-finetuned-emotion")
    iface = gr.Interface(
        fn=model.predict,
        inputs=gr.inputs.Textbox(
            lines=3,
            placeholder="type here ...",
            label="Input",
        ),
        outputs="label",
        title="Emotion Classifier",
    )
    iface.launch()