File size: 11,788 Bytes
c885400 f4dcd5d d581a83 0ce1d0f d581a83 e4fe643 d581a83 c885400 0ce1d0f c885400 d581a83 c885400 d581a83 c885400 d581a83 0ce1d0f 4df5a3d 0ce1d0f d581a83 0ce1d0f d581a83 0ce1d0f d581a83 c885400 d581a83 c885400 d581a83 c885400 0ce1d0f d581a83 c885400 0ce1d0f d581a83 c885400 d581a83 f4dcd5d 0ce1d0f d581a83 0ce1d0f f4dcd5d 4df5a3d c885400 0ce1d0f f4dcd5d 0ce1d0f f4dcd5d 0ce1d0f f4dcd5d 0ce1d0f f4dcd5d 0ce1d0f f4dcd5d 0ce1d0f f4dcd5d 0ce1d0f f4dcd5d 0ce1d0f f4dcd5d 0ce1d0f f4dcd5d 0ce1d0f f4dcd5d 0ce1d0f f4dcd5d 0ce1d0f f4dcd5d 0ce1d0f f4dcd5d 0ce1d0f 4df5a3d 0ce1d0f f4dcd5d 4df5a3d f4dcd5d 0ce1d0f 4df5a3d 0ce1d0f 4df5a3d f4dcd5d 0ce1d0f f4dcd5d 0ce1d0f f4dcd5d 0ce1d0f f4dcd5d 0ce1d0f f4dcd5d 0ce1d0f 4df5a3d 0ce1d0f f4dcd5d 0ce1d0f f4dcd5d 0ce1d0f f4dcd5d 4df5a3d 0ce1d0f 4df5a3d f4dcd5d 4df5a3d d581a83 0ce1d0f 4df5a3d 0ce1d0f 4df5a3d 0ce1d0f 4df5a3d f4dcd5d c885400 f4dcd5d c885400 f4dcd5d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 |
import gradio as gr
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
import shutil
import os
import torch
from huggingface_hub import hf_hub_download
from importlib import import_module
# Load inference.py and model
repo_id = "logasanjeev/goemotions-bert"
local_file = hf_hub_download(repo_id=repo_id, filename="inference.py")
print("Downloaded inference.py successfully!")
current_dir = os.getcwd()
destination = os.path.join(current_dir, "inference.py")
shutil.copy(local_file, destination)
print("Copied inference.py to current directory!")
inference_module = import_module("inference")
predict_emotions = inference_module.predict_emotions
print("Imported predict_emotions successfully!")
_, _ = predict_emotions("dummy text")
emotion_labels = inference_module.EMOTION_LABELS
default_thresholds = inference_module.THRESHOLDS
# Prediction function with export capability
def predict_emotions_with_details(text, confidence_threshold=0.0, chart_type="bar"):
if not text.strip():
return "Please enter some text.", "", "", None, None, False
predictions_str, processed_text = predict_emotions(text)
# Parse predictions
predictions = []
if predictions_str != "No emotions predicted.":
for line in predictions_str.split("\n"):
emotion, confidence = line.split(": ")
predictions.append((emotion, float(confidence)))
# Get raw logits for all emotions
encodings = inference_module.TOKENIZER(
processed_text,
padding='max_length',
truncation=True,
max_length=128,
return_tensors='pt'
)
input_ids = encodings['input_ids'].to(inference_module.DEVICE)
attention_mask = encodings['attention_mask'].to(inference_module.DEVICE)
with torch.no_grad():
outputs = inference_module.MODEL(input_ids, attention_mask=attention_mask)
logits = torch.sigmoid(outputs.logits).cpu().numpy()[0]
# All emotions for top 5
all_emotions = [(emotion_labels[i], round(logit, 4)) for i, logit in enumerate(logits)]
all_emotions.sort(key=lambda x: x[1], reverse=True)
top_5_emotions = all_emotions[:5]
top_5_output = "\n".join([f"{emotion}: {confidence:.4f}" for emotion, confidence in top_5_emotions])
# Filter predictions based on threshold
filtered_predictions = []
for emotion, confidence in predictions:
thresh = default_thresholds[emotion_labels.index(emotion)]
adjusted_thresh = max(thresh, confidence_threshold)
if confidence >= adjusted_thresh:
filtered_predictions.append((emotion, confidence))
if not filtered_predictions:
thresholded_output = "No emotions predicted above thresholds."
else:
thresholded_output = "\n".join([f"{emotion}: {confidence:.4f}" for emotion, confidence in filtered_predictions])
# Create visualization
fig = None
df_export = None
if filtered_predictions:
df = pd.DataFrame(filtered_predictions, columns=["Emotion", "Confidence"])
df_export = df.copy()
if chart_type == "bar":
fig = px.bar(
df,
x="Emotion",
y="Confidence",
color="Emotion",
text="Confidence",
title="Emotion Confidence Levels (Above Threshold)",
height=400,
color_discrete_sequence=px.colors.qualitative.Plotly
)
fig.update_traces(texttemplate='%{text:.2f}', textposition='auto')
fig.update_layout(showlegend=False, margin=dict(t=40, b=40), xaxis_title="", yaxis_title="Confidence")
else: # pie chart
fig = px.pie(
df,
names="Emotion",
values="Confidence",
title="Emotion Confidence Distribution (Above Threshold)",
height=400,
color_discrete_sequence=px.colors.qualitative.Plotly
)
fig.update_traces(textinfo='percent+label', pull=[0.1] + [0] * (len(df) - 1))
fig.update_layout(margin=dict(t=40, b=40))
return processed_text, thresholded_output, top_5_output, fig, df_export, True
# Custom CSS for enhanced styling
custom_css = """
body {
font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
background-color: #f5f7fa;
}
.gr-panel {
border-radius: 16px;
box-shadow: 0 6px 20px rgba(0,0,0,0.08);
background: white;
padding: 20px;
margin-bottom: 20px;
}
.gr-button {
border-radius: 8px;
padding: 12px 24px;
font-weight: 600;
transition: all 0.3s ease;
}
.gr-button-primary {
background: #4a90e2;
color: white;
}
.gr-button-primary:hover {
background: #357abd;
}
.gr-button-secondary {
background: #e4e7eb;
color: #333;
}
.gr-button-secondary:hover {
background: #d1d5db;
}
#title {
font-size: 2.8em;
font-weight: 700;
color: #1a3c6e;
text-align: center;
margin-bottom: 10px;
}
#description {
font-size: 1.2em;
color: #555;
text-align: center;
max-width: 800px;
margin: 0 auto 30px auto;
}
#theme-toggle {
position: fixed;
top: 20px;
right: 20px;
background: none;
border: none;
font-size: 1.5em;
cursor: pointer;
transition: transform 0.3s;
}
#theme-toggle:hover {
transform: scale(1.2);
}
.dark-mode {
background: #1e2a44;
color: #e0e0e0;
}
.dark-mode .gr-panel {
background: #2a3a5a;
box-shadow: 0 6px 20px rgba(0,0,0,0.2);
}
.dark-mode #title {
color: #66b3ff;
}
.dark-mode #description {
color: #b0b0b0;
}
.dark-mode .gr-button-secondary {
background: #3a4a6a;
color: #e0e0e0;
}
.dark-mode .gr-button-secondary:hover {
background: #4a5a7a;
}
#loading {
font-style: italic;
color: #888;
text-align: center;
display: none;
}
#loading.visible {
display: block;
}
#examples-title {
font-size: 1.5em;
font-weight: 600;
color: #1a3c6e;
margin-bottom: 10px;
}
.dark-mode #examples-title {
color: #66b3ff;
}
footer {
text-align: center;
margin-top: 40px;
padding: 20px;
font-size: 0.9em;
color: #666;
}
footer a {
color: #4a90e2;
text-decoration: none;
}
footer a:hover {
text-decoration: underline;
}
.dark-mode footer {
color: #b0b0b0;
}
"""
# JavaScript for theme toggle
theme_js = """
function toggleTheme() {
document.body.classList.toggle('dark-mode');
const toggleBtn = document.getElementById('theme-toggle');
toggleBtn.innerHTML = document.body.classList.contains('dark-mode') ? '☀️' : '🌙';
}
function showLoading() {
document.getElementById('loading').classList.add('visible');
}
function hideLoading() {
document.getElementById('loading').classList.remove('visible');
}
"""
# Gradio Blocks UI
with gr.Blocks(css=custom_css) as demo:
# Theme toggle button
gr.HTML(
"""
<button id='theme-toggle' onclick='toggleTheme()'>🌙</button>
<script>{}</script>
""".format(theme_js)
)
# Header
gr.Markdown("<div id='title'>GoEmotions BERT Classifier</div>", elem_id="title")
gr.Markdown(
"""
<div id='description'>
Predict emotions from text using a fine-tuned BERT-base model on the GoEmotions dataset.
Detect 28 emotions with optimized thresholds (Micro F1: 0.6006).
View preprocessed text, top 5 emotions, and thresholded predictions with interactive visualizations!
</div>
""",
elem_id="description"
)
# Main content
with gr.Row():
with gr.Column(scale=1):
# Input Section
with gr.Group():
gr.Markdown("### Input Text")
text_input = gr.Textbox(
label="Enter Your Text",
placeholder="Type something like 'I’m just chilling today'...",
lines=3,
show_label=False
)
confidence_slider = gr.Slider(
minimum=0.0,
maximum=0.9,
value=0.0,
step=0.05,
label="Minimum Confidence Threshold",
info="Filter predictions below this confidence level (default thresholds still apply)"
)
chart_type = gr.Radio(
choices=["bar", "pie"],
value="bar",
label="Chart Type",
info="Choose how to visualize the emotion confidences"
)
with gr.Row():
submit_btn = gr.Button("Predict Emotions", variant="primary")
reset_btn = gr.Button("Reset", variant="secondary")
# Loading indicator
loading_indicator = gr.HTML("<div id='loading'>Predicting emotions, please wait...</div>")
# Output Section
with gr.Row():
with gr.Column(scale=1):
with gr.Group():
gr.Markdown("### Results")
processed_text_output = gr.Textbox(label="Preprocessed Text", lines=2, interactive=False)
thresholded_output = gr.Textbox(label="Predicted Emotions (Above Threshold)", lines=5, interactive=False)
top_5_output = gr.Textbox(label="Top 5 Emotions (Regardless of Threshold)", lines=5, interactive=False)
output_plot = gr.Plot(label="Emotion Confidence Visualization (Above Threshold)")
# Export predictions
export_btn = gr.File(label="Download Predictions as CSV", visible=False)
# Example carousel
with gr.Group():
gr.Markdown("<div id='examples-title'>Example Texts</div>", elem_id="examples-title")
examples = gr.Examples(
examples=[
["I’m just chilling today.", "Neutral Example"],
["Thank you for saving my life!", "Gratitude Example"],
["I’m nervous about my exam tomorrow.", "Nervousness Example"],
["I love my new puppy so much!", "Love Example"],
["I’m so relieved the storm passed.", "Relief Example"]
],
inputs=[text_input],
label="",
examples_per_page=3
)
# Footer
gr.HTML(
"""
<footer>
Built with ❤️ by logasanjeev |
<a href="https://huggingface.co/logasanjeev/goemotions-bert">Model Card</a> |
<a href="https://www.kaggle.com/code/ravindranlogasanjeev/evaluation-logasanjeev-goemotions-bert/notebook">Kaggle Notebook</a> |
<a href="https://github.com/logasanjeev">GitHub</a>
</footer>
"""
)
# State to manage loading visibility
loading_state = gr.State(value=False)
# Bind predictions with loading spinner
def start_loading():
return True
def stop_loading(processed_text, thresholded_output, top_5_output, fig, df_export, loading_state):
return processed_text, thresholded_output, top_5_output, fig, df_export, False
submit_btn.click(
fn=start_loading,
inputs=[],
outputs=[loading_state]
).then(
fn=predict_emotions_with_details,
inputs=[text_input, confidence_slider, chart_type],
outputs=[processed_text_output, thresholded_output, top_5_output, output_plot, export_btn, loading_state]
)
# Reset functionality
reset_btn.click(
fn=lambda: ("", "", "", None, None, False),
inputs=[],
outputs=[text_input, processed_text_output, thresholded_output, top_5_output, output_plot, export_btn, loading_state]
)
# Launch
if __name__ == "__main__":
demo.launch() |