File size: 3,978 Bytes
c9bf2e6
 
d787e81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
92
93
94
95
96
97
98
99
100
101
import gradio as gr

from transformers import AutoTokenizer, AutoModelForSequenceClassification

tokenizer = AutoTokenizer.from_pretrained("Prasadrao/xlm-roberta-large-go-emotions-v3")
model = AutoModelForSequenceClassification.from_pretrained("Prasadrao/xlm-roberta-large-go-emotions-v3")

emotion_labels = ["admiration", "amusement", "anger", "annoyance", "approval",
                  "caring", "confusion", "curiosity", "desire", "disappointment",
                  "disapproval", "disgust", "embarrassment", "excitement",
                  "fear", "gratitude", "grief", "joy", "love", "nervousness",
                  "optimism", "pride", "realization", "relief", "remorse",
                  "sadness", "surprise", "neutral"]

def get_sentiment_emoji(sentiment):
    # Define the emojis corresponding to each sentiment
    emoji_mapping = {
        "disappointment": "๐Ÿ˜ž",
        "sadness": "๐Ÿ˜ข",
        "annoyance": "๐Ÿ˜ ",
        "neutral": "๐Ÿ˜",
        "disapproval": "๐Ÿ‘Ž",
        "realization": "๐Ÿ˜ฎ",
        "nervousness": "๐Ÿ˜ฌ",
        "approval": "๐Ÿ‘",
        "joy": "๐Ÿ˜„",
        "anger": "๐Ÿ˜ก",
        "embarrassment": "๐Ÿ˜ณ",
        "caring": "๐Ÿค—",
        "remorse": "๐Ÿ˜”",
        "disgust": "๐Ÿคข",
        "grief": "๐Ÿ˜ฅ",
        "confusion": "๐Ÿ˜•",
        "relief": "๐Ÿ˜Œ",
        "desire": "๐Ÿ˜",
        "admiration": "๐Ÿ˜Œ",
        "optimism": "๐Ÿ˜Š",
        "fear": "๐Ÿ˜จ",
        "love": "โค๏ธ",
        "excitement": "๐ŸŽ‰",
        "curiosity": "๐Ÿค”",
        "amusement": "๐Ÿ˜„",
        "surprise": "๐Ÿ˜ฒ",
        "gratitude": "๐Ÿ™",
        "pride": "๐Ÿฆ"
    }
    return emoji_mapping.get(sentiment, "")

import torch
import torch.nn.functional as F

def predict_emotion(text):
    inputs = tokenizer(text, return_tensors="pt")
    outputs = model(**inputs)
    logits = outputs.logits
    predicted_class = logits.argmax().item()
    predicted_emotion = emotion_labels[predicted_class]
    emoji = get_sentiment_emoji(predicted_emotion)
    predicted_emotion = f" {predicted_emotion} {emoji}"

    # Calculate softmax to get the probabilities
    probabilities = F.softmax(logits, dim=1)
    # Extract the score for the predicted class
    predicted_score = round(probabilities[0, predicted_class].item()*100)
    predicted_score = f"{predicted_score}%"

    return predicted_emotion, predicted_score

import torch
import os
import gradio as gr
import random

#server_port = random.randint(1000, 9000)


title = "Emotion Detective: Analyzing Textual Sentiments"
description = "Explore the power of sentiment analysis with our Emotion Detective! Simply input a sentence or text, and let our model predict the underlying emotion."
#article = "Sentiment Analysis, also known as opinion mining, is a branch of Natural Language Processing (NLP) that involves determining the emotional tone behind a piece of text"
examples = [["I feel ecstatic about winning the competition."],
            ["The news of her promotion made me feel proud and happy."],
            ["He felt devastated after hearing about the loss of his pet."],
            ["It's like I went through a rollercoaster of feelings!"],
            ["The memory of his actions haunted him, filling him with a profound sense of sorrow."],
            ["Despite the challenges ahead, he couldn't shake the feeling that good things were on the horizon"],
            ["The repugnant smell wafting from the trash bin made her nose crinkle in discomfort"],
            ["As he looked at his team's success, a feeling of triumph and contentment filled his heart."]]

demo = gr.Interface(
    fn=predict_emotion,
    inputs=gr.Textbox(lines=3,placeholder="Enter your text here.....",label="Text"),
    outputs=[gr.Textbox(label="Emotion"), gr.Textbox(label="Score")],
    title=title,
    description=description,
    flagging_options = ["True","False","Not Sure"],
    examples = examples,
    
)
# Launch the Gradio interface
demo.launch(share=True)