File size: 1,929 Bytes
7fc03a0
 
 
 
 
1218f5b
 
 
7fc03a0
1218f5b
7fc03a0
 
1218f5b
7fc03a0
1218f5b
 
 
7fc03a0
 
 
fa44a09
7fc03a0
 
 
 
 
 
1218f5b
7fc03a0
 
1218f5b
7fc03a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from transformers import AutoModelForSequenceClassification, AutoTokenizer, pipeline


class EmotionClassifier:
    def __init__(self): # since we have defined the models below, this class will call itself.
        self.model = AutoModelForSequenceClassification.from_pretrained("barbieheimer/MND_TweetEvalBert_model") # specify the model from repo.
        self.tokenizer = AutoTokenizer.from_pretrained("barbieheimer/MND_TweetEvalBert_model") #need to spicify the tokeniser from repo too
        self.pipeline = pipeline(
            "text-classification", # specify pipeline task here.
            model=self.model,
            tokenizer=self.tokenizer,
            return_all_scores=True, # so that all emotional scores are displayed.
        )
    # creating a prediction definition.
    def predict(self, input_text: str): # defining what the output will look like.
        pred = self.pipeline(input_text)[0] # processing text input.
        result = {
            "Anger 😠": pred[0]["score"],
           "Joy 😂": pred[1]["score"],
           "Optimism 😲": pred[2]["score"],
           "Sadness 😭": pred[3]["score"],
        }
        return result


def main():
    # call the emotionclassifier class to use our model, and now we can use the gradio UI.
    model = EmotionClassifier()
    iface = gr.Interface(
        fn=model.predict, # using the model to predict
        inputs=gr.inputs.Textbox(
            lines=3,
            placeholder="Type a phrase that has some emotion",
            label="Input Text",
        ),
        outputs="label",
        title="Emotion Classification",
        examples=[
    ["The movie was a bummer."],
    ["I cannot wait to watch all these movies!"],
    ["The ending of the movie really irks me, gives me the ick fr."],
    ["The protagonist seems to have a lot of hope...."]
],
    )

    iface.launch()


if __name__ == "__main__":
    main()