Prasadrao's picture
Update app.py
d787e81 verified
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)