|
|
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): |
|
|
|
|
|
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}" |
|
|
|
|
|
|
|
|
probabilities = F.softmax(logits, dim=1) |
|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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." |
|
|
|
|
|
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, |
|
|
|
|
|
) |
|
|
|
|
|
demo.launch(share=True) |
|
|
|