Prasadrao commited on
Commit
d787e81
ยท
verified ยท
1 Parent(s): c9bf2e6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +98 -1
app.py CHANGED
@@ -1,3 +1,100 @@
1
  import gradio as gr
2
 
3
- gr.load("models/Prasadrao/xlm-roberta-large-go-emotions-v3").launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)