Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
import gradio as gr
|
2 |
import pandas as pd
|
3 |
import plotly.express as px
|
|
|
4 |
import shutil
|
5 |
import os
|
6 |
import torch
|
@@ -25,7 +26,7 @@ _, _ = predict_emotions("dummy text")
|
|
25 |
emotion_labels = inference_module.EMOTION_LABELS
|
26 |
default_thresholds = inference_module.THRESHOLDS
|
27 |
|
28 |
-
# Prediction function with
|
29 |
def predict_emotions_with_details(text, confidence_threshold=0.0):
|
30 |
if not text.strip():
|
31 |
return "Please enter some text.", "", "", None
|
@@ -73,51 +74,86 @@ def predict_emotions_with_details(text, confidence_threshold=0.0):
|
|
73 |
else:
|
74 |
thresholded_output = "\n".join([f"{emotion}: {confidence:.4f}" for emotion, confidence in filtered_predictions])
|
75 |
|
76 |
-
# Create bar chart
|
77 |
fig = None
|
78 |
-
if filtered_predictions:
|
79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
fig = px.bar(
|
81 |
df,
|
82 |
x="Emotion",
|
83 |
y="Confidence",
|
84 |
-
color="
|
85 |
-
|
86 |
-
title="Emotion Confidence
|
87 |
-
height=
|
88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
89 |
)
|
90 |
-
fig.update_traces(texttemplate='%{text:.2f}', textposition='auto')
|
91 |
-
fig.update_layout(showlegend=False, margin=dict(t=40, b=40), xaxis_title="", yaxis_title="Confidence")
|
92 |
|
93 |
return processed_text, thresholded_output, top_5_output, fig
|
94 |
|
95 |
-
#
|
96 |
custom_css = """
|
97 |
body {
|
98 |
-
font-family: '
|
99 |
-
background
|
100 |
-
color: #
|
|
|
|
|
101 |
}
|
102 |
.gr-panel {
|
103 |
-
border-radius:
|
104 |
-
box-shadow: 0
|
105 |
-
background:
|
|
|
106 |
padding: 20px;
|
107 |
margin: 20px auto;
|
108 |
-
max-width:
|
|
|
109 |
}
|
110 |
.gr-button {
|
111 |
border-radius: 6px;
|
112 |
-
padding:
|
113 |
font-weight: 500;
|
114 |
background: #4a90e2;
|
115 |
color: white;
|
116 |
-
transition:
|
117 |
margin-top: 10px;
|
118 |
}
|
119 |
.gr-button:hover {
|
120 |
-
background: #
|
|
|
121 |
}
|
122 |
.gr-textbox, .gr-slider {
|
123 |
margin-bottom: 15px;
|
@@ -125,25 +161,27 @@ body {
|
|
125 |
.gr-textbox label, .gr-slider label {
|
126 |
font-size: 1em;
|
127 |
font-weight: 500;
|
128 |
-
color: #
|
129 |
margin-bottom: 5px;
|
130 |
}
|
131 |
.gr-textbox textarea, .gr-textbox input {
|
132 |
-
border: 1px solid
|
133 |
border-radius: 4px;
|
134 |
padding: 8px;
|
135 |
font-size: 0.95em;
|
|
|
|
|
136 |
}
|
137 |
#title {
|
138 |
-
font-size:
|
139 |
font-weight: 600;
|
140 |
-
color: #
|
141 |
text-align: center;
|
142 |
-
margin:
|
143 |
}
|
144 |
#description {
|
145 |
font-size: 1em;
|
146 |
-
color: #
|
147 |
text-align: center;
|
148 |
max-width: 600px;
|
149 |
margin: 0 auto 30px auto;
|
@@ -151,7 +189,7 @@ body {
|
|
151 |
#examples-title {
|
152 |
font-size: 1.2em;
|
153 |
font-weight: 500;
|
154 |
-
color: #
|
155 |
margin: 20px 0 10px 0;
|
156 |
text-align: center;
|
157 |
}
|
@@ -160,21 +198,36 @@ footer {
|
|
160 |
margin: 30px 0;
|
161 |
padding: 15px;
|
162 |
font-size: 0.9em;
|
163 |
-
color: #
|
164 |
}
|
165 |
footer a {
|
166 |
color: #4a90e2;
|
167 |
text-decoration: none;
|
|
|
168 |
}
|
169 |
footer a:hover {
|
170 |
-
|
171 |
}
|
172 |
.gr-plot {
|
173 |
margin-top: 15px;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
174 |
}
|
175 |
"""
|
176 |
|
177 |
-
# Gradio Blocks UI (
|
178 |
with gr.Blocks(css=custom_css) as demo:
|
179 |
# Header
|
180 |
gr.Markdown("<div id='title'>GoEmotions BERT Classifier</div>", elem_id="title")
|
@@ -182,7 +235,7 @@ with gr.Blocks(css=custom_css) as demo:
|
|
182 |
"""
|
183 |
<div id='description'>
|
184 |
Predict emotions from text using a fine-tuned BERT model.
|
185 |
-
Enter your text below to see
|
186 |
</div>
|
187 |
""",
|
188 |
elem_id="description"
|
@@ -211,7 +264,7 @@ with gr.Blocks(css=custom_css) as demo:
|
|
211 |
processed_text_output = gr.Textbox(label="Preprocessed Text", lines=1, interactive=False)
|
212 |
thresholded_output = gr.Textbox(label="Predicted Emotions (Above Threshold)", lines=3, interactive=False)
|
213 |
top_5_output = gr.Textbox(label="Top 5 Emotions (Regardless of Threshold)", lines=3, interactive=False)
|
214 |
-
output_plot = gr.Plot(label="Emotion Confidence
|
215 |
|
216 |
# Example carousel
|
217 |
with gr.Group():
|
|
|
1 |
import gradio as gr
|
2 |
import pandas as pd
|
3 |
import plotly.express as px
|
4 |
+
import plotly.graph_objects as go
|
5 |
import shutil
|
6 |
import os
|
7 |
import torch
|
|
|
26 |
emotion_labels = inference_module.EMOTION_LABELS
|
27 |
default_thresholds = inference_module.THRESHOLDS
|
28 |
|
29 |
+
# Prediction function with grouped bar chart
|
30 |
def predict_emotions_with_details(text, confidence_threshold=0.0):
|
31 |
if not text.strip():
|
32 |
return "Please enter some text.", "", "", None
|
|
|
74 |
else:
|
75 |
thresholded_output = "\n".join([f"{emotion}: {confidence:.4f}" for emotion, confidence in filtered_predictions])
|
76 |
|
77 |
+
# Create grouped bar chart
|
78 |
fig = None
|
79 |
+
if filtered_predictions or top_5_emotions:
|
80 |
+
# Prepare data for grouped bar chart
|
81 |
+
emotions = set([pred[0] for pred in filtered_predictions] + [emo[0] for emo in top_5_emotions])
|
82 |
+
thresholded_dict = {pred[0]: pred[1] for pred in filtered_predictions}
|
83 |
+
top_5_dict = {emo[0]: emo[1] for emo in top_5_emotions}
|
84 |
+
|
85 |
+
data = {
|
86 |
+
"Emotion": [],
|
87 |
+
"Confidence": [],
|
88 |
+
"Category": []
|
89 |
+
}
|
90 |
+
|
91 |
+
for emotion in emotions:
|
92 |
+
if emotion in thresholded_dict:
|
93 |
+
data["Emotion"].append(emotion)
|
94 |
+
data["Confidence"].append(thresholded_dict[emotion])
|
95 |
+
data["Category"].append("Above Threshold")
|
96 |
+
if emotion in top_5_dict:
|
97 |
+
data["Emotion"].append(emotion)
|
98 |
+
data["Confidence"].append(top_5_dict[emotion])
|
99 |
+
data["Category"].append("Top 5")
|
100 |
+
|
101 |
+
df = pd.DataFrame(data)
|
102 |
+
|
103 |
fig = px.bar(
|
104 |
df,
|
105 |
x="Emotion",
|
106 |
y="Confidence",
|
107 |
+
color="Category",
|
108 |
+
barmode="group",
|
109 |
+
title="Emotion Confidence Comparison",
|
110 |
+
height=350,
|
111 |
+
color_discrete_map={"Above Threshold": "#4a90e2", "Top 5": "#66b3ff"}
|
112 |
+
)
|
113 |
+
fig.update_traces(texttemplate='%{y:.2f}', textposition='auto')
|
114 |
+
fig.update_layout(
|
115 |
+
margin=dict(t=40, b=40),
|
116 |
+
xaxis_title="",
|
117 |
+
yaxis_title="Confidence",
|
118 |
+
legend_title="",
|
119 |
+
legend=dict(orientation="h", yanchor="bottom", y=1.02, xanchor="center", x=0.5),
|
120 |
+
plot_bgcolor="rgba(0,0,0,0)",
|
121 |
+
paper_bgcolor="rgba(0,0,0,0)"
|
122 |
)
|
|
|
|
|
123 |
|
124 |
return processed_text, thresholded_output, top_5_output, fig
|
125 |
|
126 |
+
# Modern CSS with UI/UX principles
|
127 |
custom_css = """
|
128 |
body {
|
129 |
+
font-family: 'Roboto', sans-serif;
|
130 |
+
background: linear-gradient(135deg, #1e2a44 0%, #2a3a5a 100%);
|
131 |
+
color: #e0e0e0;
|
132 |
+
margin: 0;
|
133 |
+
padding: 0;
|
134 |
}
|
135 |
.gr-panel {
|
136 |
+
border-radius: 10px;
|
137 |
+
box-shadow: 0 4px 15px rgba(0,0,0,0.2);
|
138 |
+
background: rgba(255, 255, 255, 0.05);
|
139 |
+
backdrop-filter: blur(5px);
|
140 |
padding: 20px;
|
141 |
margin: 20px auto;
|
142 |
+
max-width: 800px;
|
143 |
+
border: 1px solid rgba(255, 255, 255, 0.1);
|
144 |
}
|
145 |
.gr-button {
|
146 |
border-radius: 6px;
|
147 |
+
padding: 12px 24px;
|
148 |
font-weight: 500;
|
149 |
background: #4a90e2;
|
150 |
color: white;
|
151 |
+
transition: all 0.3s ease;
|
152 |
margin-top: 10px;
|
153 |
}
|
154 |
.gr-button:hover {
|
155 |
+
background: #66b3ff;
|
156 |
+
transform: scale(1.05);
|
157 |
}
|
158 |
.gr-textbox, .gr-slider {
|
159 |
margin-bottom: 15px;
|
|
|
161 |
.gr-textbox label, .gr-slider label {
|
162 |
font-size: 1em;
|
163 |
font-weight: 500;
|
164 |
+
color: #e0e0e0;
|
165 |
margin-bottom: 5px;
|
166 |
}
|
167 |
.gr-textbox textarea, .gr-textbox input {
|
168 |
+
border: 1px solid rgba(255, 255, 255, 0.2);
|
169 |
border-radius: 4px;
|
170 |
padding: 8px;
|
171 |
font-size: 0.95em;
|
172 |
+
background: rgba(255, 255, 255, 0.1);
|
173 |
+
color: #e0e0e0;
|
174 |
}
|
175 |
#title {
|
176 |
+
font-size: 1.8em;
|
177 |
font-weight: 600;
|
178 |
+
color: #e0e0e0;
|
179 |
text-align: center;
|
180 |
+
margin: 30px 0 10px 0;
|
181 |
}
|
182 |
#description {
|
183 |
font-size: 1em;
|
184 |
+
color: #b0b0b0;
|
185 |
text-align: center;
|
186 |
max-width: 600px;
|
187 |
margin: 0 auto 30px auto;
|
|
|
189 |
#examples-title {
|
190 |
font-size: 1.2em;
|
191 |
font-weight: 500;
|
192 |
+
color: #e0e0e0;
|
193 |
margin: 20px 0 10px 0;
|
194 |
text-align: center;
|
195 |
}
|
|
|
198 |
margin: 30px 0;
|
199 |
padding: 15px;
|
200 |
font-size: 0.9em;
|
201 |
+
color: #b0b0b0;
|
202 |
}
|
203 |
footer a {
|
204 |
color: #4a90e2;
|
205 |
text-decoration: none;
|
206 |
+
transition: color 0.3s ease;
|
207 |
}
|
208 |
footer a:hover {
|
209 |
+
color: #66b3ff;
|
210 |
}
|
211 |
.gr-plot {
|
212 |
margin-top: 15px;
|
213 |
+
background: rgba(255, 255, 255, 0.05);
|
214 |
+
border-radius: 8px;
|
215 |
+
padding: 10px;
|
216 |
+
}
|
217 |
+
.gr-examples .example {
|
218 |
+
background: rgba(255, 255, 255, 0.1);
|
219 |
+
border-radius: 6px;
|
220 |
+
padding: 10px;
|
221 |
+
margin: 5px 0;
|
222 |
+
transition: all 0.3s ease;
|
223 |
+
}
|
224 |
+
.gr-examples .example:hover {
|
225 |
+
background: rgba(255, 255, 255, 0.2);
|
226 |
+
transform: scale(1.02);
|
227 |
}
|
228 |
"""
|
229 |
|
230 |
+
# Gradio Blocks UI (Modern and Visually Appealing)
|
231 |
with gr.Blocks(css=custom_css) as demo:
|
232 |
# Header
|
233 |
gr.Markdown("<div id='title'>GoEmotions BERT Classifier</div>", elem_id="title")
|
|
|
235 |
"""
|
236 |
<div id='description'>
|
237 |
Predict emotions from text using a fine-tuned BERT model.
|
238 |
+
Enter your text below to see detected emotions, their confidence scores, and a comparison with the top 5 emotions.
|
239 |
</div>
|
240 |
""",
|
241 |
elem_id="description"
|
|
|
264 |
processed_text_output = gr.Textbox(label="Preprocessed Text", lines=1, interactive=False)
|
265 |
thresholded_output = gr.Textbox(label="Predicted Emotions (Above Threshold)", lines=3, interactive=False)
|
266 |
top_5_output = gr.Textbox(label="Top 5 Emotions (Regardless of Threshold)", lines=3, interactive=False)
|
267 |
+
output_plot = gr.Plot(label="Emotion Confidence Comparison")
|
268 |
|
269 |
# Example carousel
|
270 |
with gr.Group():
|