Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,100 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 4 |
+
|
| 5 |
+
tokenizer = AutoTokenizer.from_pretrained("Prasadrao/xlm-roberta-large-go-emotions-v3")
|
| 6 |
+
model = AutoModelForSequenceClassification.from_pretrained("Prasadrao/xlm-roberta-large-go-emotions-v3")
|
| 7 |
+
|
| 8 |
+
emotion_labels = ["admiration", "amusement", "anger", "annoyance", "approval",
|
| 9 |
+
"caring", "confusion", "curiosity", "desire", "disappointment",
|
| 10 |
+
"disapproval", "disgust", "embarrassment", "excitement",
|
| 11 |
+
"fear", "gratitude", "grief", "joy", "love", "nervousness",
|
| 12 |
+
"optimism", "pride", "realization", "relief", "remorse",
|
| 13 |
+
"sadness", "surprise", "neutral"]
|
| 14 |
+
|
| 15 |
+
def get_sentiment_emoji(sentiment):
|
| 16 |
+
# Define the emojis corresponding to each sentiment
|
| 17 |
+
emoji_mapping = {
|
| 18 |
+
"disappointment": "๐",
|
| 19 |
+
"sadness": "๐ข",
|
| 20 |
+
"annoyance": "๐ ",
|
| 21 |
+
"neutral": "๐",
|
| 22 |
+
"disapproval": "๐",
|
| 23 |
+
"realization": "๐ฎ",
|
| 24 |
+
"nervousness": "๐ฌ",
|
| 25 |
+
"approval": "๐",
|
| 26 |
+
"joy": "๐",
|
| 27 |
+
"anger": "๐ก",
|
| 28 |
+
"embarrassment": "๐ณ",
|
| 29 |
+
"caring": "๐ค",
|
| 30 |
+
"remorse": "๐",
|
| 31 |
+
"disgust": "๐คข",
|
| 32 |
+
"grief": "๐ฅ",
|
| 33 |
+
"confusion": "๐",
|
| 34 |
+
"relief": "๐",
|
| 35 |
+
"desire": "๐",
|
| 36 |
+
"admiration": "๐",
|
| 37 |
+
"optimism": "๐",
|
| 38 |
+
"fear": "๐จ",
|
| 39 |
+
"love": "โค๏ธ",
|
| 40 |
+
"excitement": "๐",
|
| 41 |
+
"curiosity": "๐ค",
|
| 42 |
+
"amusement": "๐",
|
| 43 |
+
"surprise": "๐ฒ",
|
| 44 |
+
"gratitude": "๐",
|
| 45 |
+
"pride": "๐ฆ"
|
| 46 |
+
}
|
| 47 |
+
return emoji_mapping.get(sentiment, "")
|
| 48 |
+
|
| 49 |
+
import torch
|
| 50 |
+
import torch.nn.functional as F
|
| 51 |
+
|
| 52 |
+
def predict_emotion(text):
|
| 53 |
+
inputs = tokenizer(text, return_tensors="pt")
|
| 54 |
+
outputs = model(**inputs)
|
| 55 |
+
logits = outputs.logits
|
| 56 |
+
predicted_class = logits.argmax().item()
|
| 57 |
+
predicted_emotion = emotion_labels[predicted_class]
|
| 58 |
+
emoji = get_sentiment_emoji(predicted_emotion)
|
| 59 |
+
predicted_emotion = f" {predicted_emotion} {emoji}"
|
| 60 |
+
|
| 61 |
+
# Calculate softmax to get the probabilities
|
| 62 |
+
probabilities = F.softmax(logits, dim=1)
|
| 63 |
+
# Extract the score for the predicted class
|
| 64 |
+
predicted_score = round(probabilities[0, predicted_class].item()*100)
|
| 65 |
+
predicted_score = f"{predicted_score}%"
|
| 66 |
+
|
| 67 |
+
return predicted_emotion, predicted_score
|
| 68 |
+
|
| 69 |
+
import torch
|
| 70 |
+
import os
|
| 71 |
+
import gradio as gr
|
| 72 |
+
import random
|
| 73 |
+
|
| 74 |
+
#server_port = random.randint(1000, 9000)
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
title = "Emotion Detective: Analyzing Textual Sentiments"
|
| 78 |
+
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."
|
| 79 |
+
#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"
|
| 80 |
+
examples = [["I feel ecstatic about winning the competition."],
|
| 81 |
+
["The news of her promotion made me feel proud and happy."],
|
| 82 |
+
["He felt devastated after hearing about the loss of his pet."],
|
| 83 |
+
["It's like I went through a rollercoaster of feelings!"],
|
| 84 |
+
["The memory of his actions haunted him, filling him with a profound sense of sorrow."],
|
| 85 |
+
["Despite the challenges ahead, he couldn't shake the feeling that good things were on the horizon"],
|
| 86 |
+
["The repugnant smell wafting from the trash bin made her nose crinkle in discomfort"],
|
| 87 |
+
["As he looked at his team's success, a feeling of triumph and contentment filled his heart."]]
|
| 88 |
+
|
| 89 |
+
demo = gr.Interface(
|
| 90 |
+
fn=predict_emotion,
|
| 91 |
+
inputs=gr.Textbox(lines=3,placeholder="Enter your text here.....",label="Text"),
|
| 92 |
+
outputs=[gr.Textbox(label="Emotion"), gr.Textbox(label="Score")],
|
| 93 |
+
title=title,
|
| 94 |
+
description=description,
|
| 95 |
+
flagging_options = ["True","False","Not Sure"],
|
| 96 |
+
examples = examples,
|
| 97 |
+
|
| 98 |
+
)
|
| 99 |
+
# Launch the Gradio interface
|
| 100 |
+
demo.launch(share=True)
|