Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
|
4 |
+
# Load the sentiment analysis model
|
5 |
+
sentiment_analysis = pipeline(
|
6 |
+
"sentiment-analysis",
|
7 |
+
framework="pt",
|
8 |
+
model="SamLowe/roberta-base-go_emotions"
|
9 |
+
)
|
10 |
+
|
11 |
+
def analyze_sentiment(text):
|
12 |
+
results = sentiment_analysis(text)
|
13 |
+
# Sort the results by score in descending order
|
14 |
+
sorted_results = sorted(results, key=lambda x: x['score'], reverse=True)
|
15 |
+
# Select the top 3 labels and their scores
|
16 |
+
top_3_labels_scores = {result['label']: result['score'] for result in sorted_results[:3]}
|
17 |
+
return top_3_labels_scores
|
18 |
+
|
19 |
+
def get_sentiment_emoji(sentiment):
|
20 |
+
emoji_mapping = {
|
21 |
+
"disappointment": "๐",
|
22 |
+
"sadness": "๐ข",
|
23 |
+
"annoyance": "๐ ",
|
24 |
+
"neutral": "๐",
|
25 |
+
"disapproval": "๐",
|
26 |
+
"realization": "๐ฎ",
|
27 |
+
"nervousness": "๐ฌ",
|
28 |
+
"approval": "๐",
|
29 |
+
"joy": "๐",
|
30 |
+
"anger": "๐ก",
|
31 |
+
"embarrassment": "๐ณ",
|
32 |
+
"caring": "๐ค",
|
33 |
+
"remorse": "๐",
|
34 |
+
"disgust": "๐คข",
|
35 |
+
"grief": "๐ฅ",
|
36 |
+
"confusion": "๐",
|
37 |
+
"relief": "๐",
|
38 |
+
"desire": "๐",
|
39 |
+
"admiration": "๐",
|
40 |
+
"optimism": "๐",
|
41 |
+
"fear": "๐จ",
|
42 |
+
"love": "โค๏ธ",
|
43 |
+
"excitement": "๐",
|
44 |
+
"curiosity": "๐ค",
|
45 |
+
"amusement": "๐",
|
46 |
+
"surprise": "๐ฒ",
|
47 |
+
"gratitude": "๐",
|
48 |
+
"pride": "๐ฆ"
|
49 |
+
}
|
50 |
+
return emoji_mapping.get(sentiment, "")
|
51 |
+
|
52 |
+
def display_sentiment_results(sentiment_results, option):
|
53 |
+
sentiment_text = ""
|
54 |
+
for sentiment, score in sentiment_results.items():
|
55 |
+
emoji = get_sentiment_emoji(sentiment)
|
56 |
+
score_percentage = score * 100
|
57 |
+
if option == "Sentiment Only":
|
58 |
+
sentiment_text += f"{sentiment} {emoji}\n"
|
59 |
+
elif option == "Sentiment + Score":
|
60 |
+
sentiment_text += f"{sentiment} {emoji}: {score_percentage:.2f}%\n"
|
61 |
+
return sentiment_text
|
62 |
+
|
63 |
+
def inference(text_input, sentiment_option):
|
64 |
+
sentiment_results = analyze_sentiment(text_input)
|
65 |
+
sentiment_output = display_sentiment_results(sentiment_results, sentiment_option)
|
66 |
+
|
67 |
+
return sentiment_output
|
68 |
+
|
69 |
+
title = "๐ค Gradio UI"
|
70 |
+
description = "we have deployed our model on Gradio"
|
71 |
+
|
72 |
+
block = gr.Blocks()
|
73 |
+
|
74 |
+
with block:
|
75 |
+
gr.Markdown("# ๐ต๏ธ")
|
76 |
+
gr.Markdown("Between the Lines, Emotions Speak ๐คซ๐ - Decode the Silent Echoes with Mood Reader ๐ต๏ธโโ๏ธ๐ฌ Every Sentence with Mood Reader ๐ต๏ธโโ๏ธ๐ฌ")
|
77 |
+
with gr.Column():
|
78 |
+
text_input = gr.Textbox(label="Input Text", lines=4)
|
79 |
+
sentiment_option = gr.Radio(choices=["Sentiment Only", "Sentiment + Score"], label="Select an option")
|
80 |
+
analyze_btn = gr.Button("Analyze")
|
81 |
+
sentiment_output = gr.Textbox(label="Sentiment Analysis Results")
|
82 |
+
|
83 |
+
analyze_btn.click(
|
84 |
+
inference,
|
85 |
+
inputs=[text_input, sentiment_option],
|
86 |
+
outputs=[sentiment_output]
|
87 |
+
)
|
88 |
+
|
89 |
+
block.launch()
|