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)