Update app.py
Browse files
app.py
CHANGED
@@ -1,12 +1,11 @@
|
|
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
|
8 |
from huggingface_hub import hf_hub_download
|
9 |
from importlib import import_module
|
|
|
|
|
10 |
|
11 |
# Load inference.py and model
|
12 |
repo_id = "logasanjeev/goemotions-bert"
|
@@ -77,7 +76,6 @@ def predict_emotions_with_details(text, confidence_threshold=0.0):
|
|
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}
|
@@ -107,135 +105,165 @@ def predict_emotions_with_details(text, confidence_threshold=0.0):
|
|
107 |
color="Category",
|
108 |
barmode="group",
|
109 |
title="Emotion Confidence Comparison",
|
110 |
-
height=
|
111 |
-
color_discrete_map={"Above Threshold": "#
|
112 |
)
|
113 |
fig.update_traces(texttemplate='%{y:.2f}', textposition='auto')
|
114 |
fig.update_layout(
|
115 |
-
margin=dict(t=
|
116 |
xaxis_title="",
|
117 |
yaxis_title="Confidence",
|
118 |
legend_title="",
|
119 |
-
legend=dict(orientation="h", yanchor="bottom", y=1.
|
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 |
-
#
|
127 |
custom_css = """
|
128 |
body {
|
129 |
-
font-family: '
|
130 |
-
background: linear-gradient(
|
131 |
-
color: #
|
132 |
margin: 0;
|
133 |
-
padding:
|
|
|
134 |
}
|
135 |
.gr-panel {
|
136 |
-
border-radius:
|
137 |
-
box-shadow: 0
|
138 |
-
background: rgba(255, 255, 255, 0.
|
139 |
-
backdrop-filter: blur(
|
140 |
-
padding:
|
141 |
margin: 20px auto;
|
142 |
-
max-width:
|
143 |
-
border: 1px solid rgba(255, 255, 255, 0.
|
|
|
|
|
|
|
|
|
144 |
}
|
145 |
.gr-button {
|
146 |
-
border-radius:
|
147 |
-
padding: 12px
|
148 |
-
font-weight:
|
149 |
-
background: #
|
150 |
color: white;
|
|
|
151 |
transition: all 0.3s ease;
|
152 |
-
|
|
|
153 |
}
|
154 |
.gr-button:hover {
|
155 |
-
background: #
|
156 |
transform: scale(1.05);
|
|
|
157 |
}
|
158 |
.gr-textbox, .gr-slider {
|
159 |
-
margin-bottom:
|
160 |
}
|
161 |
.gr-textbox label, .gr-slider label {
|
162 |
-
font-size: 1em;
|
163 |
-
font-weight:
|
164 |
-
color: #
|
165 |
-
margin-bottom:
|
|
|
166 |
}
|
167 |
.gr-textbox textarea, .gr-textbox input {
|
168 |
border: 1px solid rgba(255, 255, 255, 0.2);
|
169 |
-
border-radius:
|
170 |
-
padding:
|
171 |
-
font-size:
|
172 |
background: rgba(255, 255, 255, 0.1);
|
173 |
-
color: #
|
|
|
|
|
|
|
|
|
|
|
174 |
}
|
175 |
#title {
|
176 |
-
font-size:
|
177 |
-
font-weight:
|
178 |
-
color: #
|
179 |
text-align: center;
|
180 |
-
margin:
|
|
|
181 |
}
|
182 |
#description {
|
183 |
-
font-size: 1em;
|
184 |
-
color: #
|
185 |
text-align: center;
|
186 |
-
max-width:
|
187 |
-
margin: 0 auto
|
|
|
188 |
}
|
189 |
#examples-title {
|
190 |
-
font-size: 1.
|
191 |
-
font-weight:
|
192 |
-
color: #
|
193 |
-
margin:
|
194 |
text-align: center;
|
195 |
}
|
196 |
footer {
|
197 |
text-align: center;
|
198 |
-
margin:
|
199 |
-
padding:
|
200 |
-
font-size:
|
201 |
-
color: #
|
202 |
}
|
203 |
footer a {
|
204 |
-
color: #
|
205 |
text-decoration: none;
|
|
|
206 |
transition: color 0.3s ease;
|
207 |
}
|
208 |
footer a:hover {
|
209 |
-
color: #
|
210 |
}
|
211 |
.gr-plot {
|
212 |
-
margin-top:
|
213 |
-
background: rgba(255, 255, 255, 0.
|
214 |
-
border-radius:
|
215 |
-
padding:
|
|
|
216 |
}
|
217 |
.gr-examples .example {
|
218 |
-
background: rgba(255, 255, 255, 0.
|
219 |
-
border-radius:
|
220 |
-
padding:
|
221 |
-
margin:
|
222 |
transition: all 0.3s ease;
|
|
|
223 |
}
|
224 |
.gr-examples .example:hover {
|
225 |
-
background: rgba(255,
|
226 |
-
transform:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
227 |
}
|
228 |
"""
|
229 |
|
230 |
-
# Gradio Blocks UI (
|
231 |
with gr.Blocks(css=custom_css) as demo:
|
232 |
# Header
|
233 |
-
gr.Markdown(
|
|
|
|
|
|
|
234 |
gr.Markdown(
|
235 |
"""
|
236 |
<div id='description'>
|
237 |
-
|
238 |
-
|
239 |
</div>
|
240 |
""",
|
241 |
elem_id="description"
|
@@ -244,31 +272,57 @@ with gr.Blocks(css=custom_css) as demo:
|
|
244 |
# Input Section
|
245 |
with gr.Group():
|
246 |
text_input = gr.Textbox(
|
247 |
-
label="
|
248 |
-
placeholder="
|
249 |
-
lines=
|
250 |
-
show_label=True
|
|
|
251 |
)
|
252 |
confidence_slider = gr.Slider(
|
253 |
minimum=0.0,
|
254 |
maximum=0.9,
|
255 |
value=0.0,
|
256 |
step=0.05,
|
257 |
-
label="
|
258 |
-
info="Filter
|
|
|
259 |
)
|
260 |
-
submit_btn = gr.Button("
|
261 |
|
262 |
# Output Section
|
263 |
with gr.Group():
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
268 |
|
269 |
# Example carousel
|
270 |
with gr.Group():
|
271 |
-
gr.Markdown(
|
|
|
|
|
|
|
272 |
examples = gr.Examples(
|
273 |
examples=[
|
274 |
["I’m thrilled to win this award! 😄", "Joy Example"],
|
@@ -285,7 +339,7 @@ with gr.Blocks(css=custom_css) as demo:
|
|
285 |
gr.HTML(
|
286 |
"""
|
287 |
<footer>
|
288 |
-
|
289 |
<a href="https://huggingface.co/logasanjeev/goemotions-bert">Model Card</a> |
|
290 |
<a href="https://www.kaggle.com/code/ravindranlogasanjeev/evaluation-logasanjeev-goemotions-bert/notebook">Kaggle Notebook</a>
|
291 |
</footer>
|
|
|
1 |
import gradio as gr
|
2 |
import pandas as pd
|
3 |
import plotly.express as px
|
|
|
|
|
|
|
4 |
import torch
|
5 |
from huggingface_hub import hf_hub_download
|
6 |
from importlib import import_module
|
7 |
+
import shutil
|
8 |
+
import os
|
9 |
|
10 |
# Load inference.py and model
|
11 |
repo_id = "logasanjeev/goemotions-bert"
|
|
|
76 |
# Create grouped bar chart
|
77 |
fig = None
|
78 |
if filtered_predictions or top_5_emotions:
|
|
|
79 |
emotions = set([pred[0] for pred in filtered_predictions] + [emo[0] for emo in top_5_emotions])
|
80 |
thresholded_dict = {pred[0]: pred[1] for pred in filtered_predictions}
|
81 |
top_5_dict = {emo[0]: emo[1] for emo in top_5_emotions}
|
|
|
105 |
color="Category",
|
106 |
barmode="group",
|
107 |
title="Emotion Confidence Comparison",
|
108 |
+
height=400,
|
109 |
+
color_discrete_map={"Above Threshold": "#ff6b6b", "Top 5": "#4ecdc4"}
|
110 |
)
|
111 |
fig.update_traces(texttemplate='%{y:.2f}', textposition='auto')
|
112 |
fig.update_layout(
|
113 |
+
margin=dict(t=50, b=50),
|
114 |
xaxis_title="",
|
115 |
yaxis_title="Confidence",
|
116 |
legend_title="",
|
117 |
+
legend=dict(orientation="h", yanchor="bottom", y=1.05, xanchor="center", x=0.5),
|
118 |
plot_bgcolor="rgba(0,0,0,0)",
|
119 |
+
paper_bgcolor="rgba(0,0,0,0)",
|
120 |
+
font=dict(color="#e0e0e0")
|
121 |
)
|
122 |
|
123 |
return processed_text, thresholded_output, top_5_output, fig
|
124 |
|
125 |
+
# Enhanced CSS with vibrant colors, animations, and better UX
|
126 |
custom_css = """
|
127 |
body {
|
128 |
+
font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
|
129 |
+
background: linear-gradient(145deg, #1a1a3d 0%, #2e2e5c 100%);
|
130 |
+
color: #e6e6fa;
|
131 |
margin: 0;
|
132 |
+
padding: 20px;
|
133 |
+
min-height: 100vh;
|
134 |
}
|
135 |
.gr-panel {
|
136 |
+
border-radius: 12px;
|
137 |
+
box-shadow: 0 6px 20px rgba(0,0,0,0.25);
|
138 |
+
background: rgba(255, 255, 255, 0.08);
|
139 |
+
backdrop-filter: blur(8px);
|
140 |
+
padding: 25px;
|
141 |
margin: 20px auto;
|
142 |
+
max-width: 900px;
|
143 |
+
border: 1px solid rgba(255, 255, 255, 0.15);
|
144 |
+
transition: transform 0.3s ease;
|
145 |
+
}
|
146 |
+
.gr-panel:hover {
|
147 |
+
transform: translateY(-5px);
|
148 |
}
|
149 |
.gr-button {
|
150 |
+
border-radius: 8px;
|
151 |
+
padding: 12px 30px;
|
152 |
+
font-weight: 600;
|
153 |
+
background: linear-gradient(90deg, #ff6b6b 0%, #ff8e53 100%);
|
154 |
color: white;
|
155 |
+
border: none;
|
156 |
transition: all 0.3s ease;
|
157 |
+
cursor: pointer;
|
158 |
+
margin-top: 15px;
|
159 |
}
|
160 |
.gr-button:hover {
|
161 |
+
background: linear-gradient(90deg, #ff8e53 0%, #ff6b6b 100%);
|
162 |
transform: scale(1.05);
|
163 |
+
box-shadow: 0 4px 15px rgba(255, 107, 107, 0.4);
|
164 |
}
|
165 |
.gr-textbox, .gr-slider {
|
166 |
+
margin-bottom: 20px;
|
167 |
}
|
168 |
.gr-textbox label, .gr-slider label {
|
169 |
+
font-size: 1.1em;
|
170 |
+
font-weight: 600;
|
171 |
+
color: #e6e6fa;
|
172 |
+
margin-bottom: 8px;
|
173 |
+
display: block;
|
174 |
}
|
175 |
.gr-textbox textarea, .gr-textbox input {
|
176 |
border: 1px solid rgba(255, 255, 255, 0.2);
|
177 |
+
border-radius: 6px;
|
178 |
+
padding: 10px;
|
179 |
+
font-size: 1em;
|
180 |
background: rgba(255, 255, 255, 0.1);
|
181 |
+
color: #e6e6fa;
|
182 |
+
transition: border-color 0.3s ease;
|
183 |
+
}
|
184 |
+
.gr-textbox textarea:focus, .gr-textbox input:focus {
|
185 |
+
border-color: #ff6b6b;
|
186 |
+
outline: none;
|
187 |
}
|
188 |
#title {
|
189 |
+
font-size: 2.2em;
|
190 |
+
font-weight: 700;
|
191 |
+
color: #ffffff;
|
192 |
text-align: center;
|
193 |
+
margin: 40px 0 15px 0;
|
194 |
+
text-shadow: 0 2px 4px rgba(0,0,0,0.3);
|
195 |
}
|
196 |
#description {
|
197 |
+
font-size: 1.1em;
|
198 |
+
color: #d3d3fa;
|
199 |
text-align: center;
|
200 |
+
max-width: 700px;
|
201 |
+
margin: 0 auto 40px auto;
|
202 |
+
line-height: 1.5;
|
203 |
}
|
204 |
#examples-title {
|
205 |
+
font-size: 1.3em;
|
206 |
+
font-weight: 600;
|
207 |
+
color: #e6e6fa;
|
208 |
+
margin: 30px 0 15px 0;
|
209 |
text-align: center;
|
210 |
}
|
211 |
footer {
|
212 |
text-align: center;
|
213 |
+
margin: 40px 0;
|
214 |
+
padding: 20px;
|
215 |
+
font-size: 1em;
|
216 |
+
color: #d3d3fa;
|
217 |
}
|
218 |
footer a {
|
219 |
+
color: #ff6b6b;
|
220 |
text-decoration: none;
|
221 |
+
font-weight: 500;
|
222 |
transition: color 0.3s ease;
|
223 |
}
|
224 |
footer a:hover {
|
225 |
+
color: #ff8e53;
|
226 |
}
|
227 |
.gr-plot {
|
228 |
+
margin-top: 20px;
|
229 |
+
background: rgba(255, 255, 255, 0.1);
|
230 |
+
border-radius: 10px;
|
231 |
+
padding: 15px;
|
232 |
+
border: 1px solid rgba(255, 255, 255, 0.15);
|
233 |
}
|
234 |
.gr-examples .example {
|
235 |
+
background: rgba(255, 255, 255, 0.12);
|
236 |
+
border-radius: 8px;
|
237 |
+
padding: 12px;
|
238 |
+
margin: 8px 0;
|
239 |
transition: all 0.3s ease;
|
240 |
+
cursor: pointer;
|
241 |
}
|
242 |
.gr-examples .example:hover {
|
243 |
+
background: rgba(255, 107, 107, 0.2);
|
244 |
+
transform: translateY(-3px);
|
245 |
+
}
|
246 |
+
@keyframes fadeIn {
|
247 |
+
from { opacity: 0; transform: translateY(20px); }
|
248 |
+
to { opacity: 1; transform: translateY(0); }
|
249 |
+
}
|
250 |
+
.gr-panel, #title, #description, footer {
|
251 |
+
animation: fadeIn 0.5s ease-out;
|
252 |
}
|
253 |
"""
|
254 |
|
255 |
+
# Gradio Blocks UI (Enhanced for vibrancy and UX)
|
256 |
with gr.Blocks(css=custom_css) as demo:
|
257 |
# Header
|
258 |
+
gr.Markdown(
|
259 |
+
"<div id='title'>GoEmotions BERT Classifier</div>",
|
260 |
+
elem_id="title"
|
261 |
+
)
|
262 |
gr.Markdown(
|
263 |
"""
|
264 |
<div id='description'>
|
265 |
+
Discover the emotions in your text with our fine-tuned BERT model!
|
266 |
+
Type your thoughts below, adjust the confidence threshold, and explore the detected emotions with a vibrant visualization.
|
267 |
</div>
|
268 |
""",
|
269 |
elem_id="description"
|
|
|
272 |
# Input Section
|
273 |
with gr.Group():
|
274 |
text_input = gr.Textbox(
|
275 |
+
label="Share Your Thoughts",
|
276 |
+
placeholder="Try something like 'I’m super excited today!' or 'This is so annoying...'",
|
277 |
+
lines=3,
|
278 |
+
show_label=True,
|
279 |
+
elem_classes=["input-textbox"]
|
280 |
)
|
281 |
confidence_slider = gr.Slider(
|
282 |
minimum=0.0,
|
283 |
maximum=0.9,
|
284 |
value=0.0,
|
285 |
step=0.05,
|
286 |
+
label="Confidence Threshold",
|
287 |
+
info="Filter emotions below this confidence level (default thresholds apply)",
|
288 |
+
elem_classes=["input-slider"]
|
289 |
)
|
290 |
+
submit_btn = gr.Button("Analyze Emotions", variant="primary")
|
291 |
|
292 |
# Output Section
|
293 |
with gr.Group():
|
294 |
+
with gr.Row():
|
295 |
+
with gr.Column(scale=1):
|
296 |
+
processed_text_output = gr.Textbox(
|
297 |
+
label="Processed Text",
|
298 |
+
lines=1,
|
299 |
+
interactive=False,
|
300 |
+
elem_classes=["output-textbox"]
|
301 |
+
)
|
302 |
+
thresholded_output = gr.Textbox(
|
303 |
+
label="Detected Emotions (Above Threshold)",
|
304 |
+
lines=4,
|
305 |
+
interactive=False,
|
306 |
+
elem_classes=["output-textbox"]
|
307 |
+
)
|
308 |
+
top_5_output = gr.Textbox(
|
309 |
+
label="Top 5 Emotions",
|
310 |
+
lines=4,
|
311 |
+
interactive=False,
|
312 |
+
elem_classes=["output-textbox"]
|
313 |
+
)
|
314 |
+
with gr.Column(scale=1):
|
315 |
+
output_plot = gr.Plot(
|
316 |
+
label="Emotion Confidence Visualization",
|
317 |
+
elem_classes=["output-plot"]
|
318 |
+
)
|
319 |
|
320 |
# Example carousel
|
321 |
with gr.Group():
|
322 |
+
gr.Markdown(
|
323 |
+
"<div id='examples-title'>Explore Example Texts</div>",
|
324 |
+
elem_id="examples-title"
|
325 |
+
)
|
326 |
examples = gr.Examples(
|
327 |
examples=[
|
328 |
["I’m thrilled to win this award! 😄", "Joy Example"],
|
|
|
339 |
gr.HTML(
|
340 |
"""
|
341 |
<footer>
|
342 |
+
Created by logasanjeev |
|
343 |
<a href="https://huggingface.co/logasanjeev/goemotions-bert">Model Card</a> |
|
344 |
<a href="https://www.kaggle.com/code/ravindranlogasanjeev/evaluation-logasanjeev-goemotions-bert/notebook">Kaggle Notebook</a>
|
345 |
</footer>
|