Spaces:
Sleeping
Sleeping
File size: 21,267 Bytes
01ecf84 99c957e 815e70f 99c957e d71c05d 99c957e d71c05d 815e70f 99c957e 01ecf84 815e70f 01ecf84 815e70f 01ecf84 fee56c6 d71c05d 99c957e 815e70f 99c957e d71c05d 99c957e 815e70f 99c957e 815e70f 99c957e 815e70f 99c957e d71c05d 815e70f d71c05d bc62dd0 d71c05d 99c957e d71c05d 99c957e d71c05d 99c957e d71c05d 99c957e bc62dd0 99c957e d71c05d 99c957e d71c05d 99c957e d71c05d 815e70f 99c957e d71c05d 99c957e 815e70f d71c05d 99c957e d71c05d 99c957e d71c05d 99c957e 815e70f d71c05d 99c957e d71c05d 99c957e d71c05d 99c957e d71c05d 815e70f 99c957e 815e70f d71c05d 815e70f d71c05d 99c957e d71c05d 815e70f d71c05d 815e70f 99c957e 815e70f 99c957e 815e70f 99c957e 815e70f d71c05d 815e70f d71c05d 815e70f 99c957e 81745ec d71c05d 99c957e 815e70f bc62dd0 | 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 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 | import os
from huggingface_hub import login
login(token=os.getenv("HF_TOKEN"))
import gradio as gr
import torch
import torch.nn.functional as F
from transformers import DistilBertTokenizer, DistilBertForSequenceClassification
import whisper
import pickle
from huggingface_hub import hf_hub_download
# ββ Model Loading ββββββββββββββββββββββββββββββββββββββββββ
model_path = "InfoBayAI/Audio-to-Sentiment_Intelligence_Model"
tokenizer = DistilBertTokenizer.from_pretrained(
model_path,
token=os.getenv("HF_TOKEN")
)
model = DistilBertForSequenceClassification.from_pretrained(
model_path,
token=os.getenv("HF_TOKEN")
)
label_path = hf_hub_download(
repo_id=model_path,
filename="label_encoder.pkl",
token=os.getenv("HF_TOKEN")
)
with open(label_path, "rb") as f:
encoder = pickle.load(f)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)
model.eval()
# π€ Whisper model
whisper_model = whisper.load_model("base")
# ββ Sentiment Prediction βββββββββββββββββββββββββββββββββββ
def predict_sentiment(text):
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=128)
inputs = {k: v.to(device) for k, v in inputs.items()}
with torch.no_grad():
outputs = model(**inputs)
probs = F.softmax(outputs.logits, dim=1)
confidence, predicted_class = torch.max(probs, dim=1)
label = encoder.inverse_transform([predicted_class.item()])[0]
confidence = confidence.item() * 100
return label, confidence
# ββ Main Pipeline ββββββββββββββββββββββββββββββββββββββββββ
def process_audio(audio_file):
if audio_file is None:
return "β οΈ No audio provided.\n\nPlease upload a file or record audio using your microphone, then click Analyze."
# Gradio 6 Audio with type="filepath" may pass a tuple (path, [start, end])
# when the user interacts with the trim tool. Always extract just the filepath
# so Whisper processes the full audio rather than a trimmed segment.
if isinstance(audio_file, (tuple, list)):
audio_file = audio_file[0]
if not audio_file:
return "β οΈ No audio file found."
try:
# Transcribe full audio β do NOT pass trim timestamps
result = whisper_model.transcribe(audio_file, task="translate")
text = result["text"].strip()
if not text:
return "β οΈ Could not transcribe any speech from the audio.\n\nPlease ensure the audio contains clear speech."
label, confidence = predict_sentiment(text)
# Confidence bar (ASCII)
filled = int(confidence / 5)
bar = "β" * filled + "β" * (20 - filled)
sentiment_emoji = {
"positive": "π", "negative": "π", "neutral": "π",
"happy": "π", "sad": "π’", "angry": "π ", "fear": "π¨",
"surprise": "π²", "disgust": "π€’"
}.get(label.lower(), "π―")
return (
f"ββββββββββββββββββββββββββββββ\n"
f" π― TRANSCRIPTION\n"
f"ββββββββββββββββββββββββββββββ\n\n"
f"{text}\n\n"
f"ββββββββββββββββββββββββββββββ\n"
f" {sentiment_emoji} SENTIMENT ANALYSIS\n"
f"ββββββββββββββββββββββββββββββ\n\n"
f" Sentiment : {label.upper()}\n"
f" Confidence : {confidence:.1f}%\n\n"
f" [{bar}]\n"
)
except Exception as e:
return f"β Error during processing:\n\n{str(e)}\n\nPlease try again with a different audio file."
# ββ CSS ββββββββββββββββββββββββββββββββββββββββββββββββββββ
APP_CSS = """
/* ββββ GOOGLE FONTS ββββ */
@import url('https://fonts.googleapis.com/css2?family=Space+Mono:wght@400;700&family=Syne:wght@400;600;700;800&display=swap');
/* ββββ RESET & ROOT ββββ */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
:root {
--bg-base: #080810;
--bg-surface: #0f0f1c;
--bg-card: #13132a;
--bg-element: #1a1a35;
--border: #252540;
--border-glow: #4f3fbf;
--accent: #7c5cfc;
--accent-light: #a78bfa;
--accent-dim: #3d2d8a;
--text-primary: #e8e4ff;
--text-muted: #7870a8;
--text-dim: #3d3a6e;
--success: #34d399;
--danger: #f87171;
--font-display: 'Syne', sans-serif;
--font-mono: 'Space Mono', monospace;
--radius-sm: 8px;
--radius-md: 14px;
--radius-lg: 20px;
--shadow-glow: 0 0 30px rgba(124, 92, 252, 0.15);
}
/* ββββ BODY & CONTAINER ββββ */
body,
.gradio-container,
.gradio-container > * {
background-color: var(--bg-base) !important;
font-family: var(--font-display) !important;
color: var(--text-primary) !important;
}
.gradio-container {
max-width: 1400px !important;
width: 100% !important;
min-height: 100vh !important;
margin: 0 auto !important;
padding: 32px 24px 60px !important;
}
/* ββββ HEADER ββββ */
.app-header {
text-align: center;
padding: 40px 0 32px;
}
.app-header h1 {
font-family: var(--font-display) !important;
font-size: clamp(28px, 4vw, 48px) !important;
font-weight: 800 !important;
color: var(--text-primary) !important;
letter-spacing: -1px !important;
line-height: 1.1 !important;
margin-bottom: 12px !important;
}
.app-header h1 span { color: var(--accent-light); }
.app-subtitle {
color: var(--text-muted);
font-family: var(--font-mono);
font-size: 13px;
letter-spacing: 0.5px;
padding: 6px 16px;
border: 1px solid var(--border);
border-radius: 100px;
display: inline-block;
background: var(--bg-surface);
}
/* ββββ LAYOUT ROW ββββ */
.main-row {
gap: 20px !important;
align-items: stretch !important;
}
/* ββββ PANELS ββββ */
.panel-input,
.panel-output {
background: var(--bg-card) !important;
border: 1px solid var(--border) !important;
border-radius: var(--radius-lg) !important;
padding: 28px !important;
box-shadow: var(--shadow-glow) !important;
display: flex !important;
flex-direction: column !important;
gap: 0 !important;
}
.panel-input { flex: 1 !important; min-width: 360px !important; }
.panel-output { flex: 1.6 !important; min-width: 420px !important; }
/* ββββ SECTION HEADINGS ββββ */
.panel-input h2,
.panel-output h2 {
font-family: var(--font-display) !important;
font-size: 15px !important;
font-weight: 700 !important;
color: var(--text-muted) !important;
text-transform: uppercase !important;
letter-spacing: 2px !important;
margin-bottom: 20px !important;
padding-bottom: 12px !important;
border-bottom: 1px solid var(--border) !important;
}
/* ββββ HINT LABELS ββββ */
.hint-label {
font-family: var(--font-mono);
font-size: 11px;
color: var(--text-muted);
letter-spacing: 0.5px;
margin-bottom: 8px;
margin-top: 4px;
}
/* ββββ AUDIO COMPONENTS β THE CRITICAL FIX ββββ */
/* Outer wrapper Gradio creates */
.audio-component-wrap {
width: 100% !important;
margin-bottom: 16px !important;
}
/* Kill ALL overflow:hidden on audio internals */
.audio-component-wrap *,
[data-testid="audio"],
[data-testid="audio"] *,
.waveform-container,
.waveform-container *,
.waveform-controls,
.waveform-controls * {
overflow: visible !important;
}
/* The main audio block */
[data-testid="audio"],
.gr-audio,
.audio-wrap > div {
background: var(--bg-element) !important;
border: 1px solid var(--border) !important;
border-radius: var(--radius-md) !important;
padding: 0 !important;
width: 100% !important;
overflow: visible !important;
}
/* Waveform canvas area β give it proper height */
.waveform-container canvas,
[class*="waveform"] canvas {
width: 100% !important;
min-height: 80px !important;
display: block !important;
border-radius: var(--radius-sm) var(--radius-sm) 0 0 !important;
}
/* Controls toolbar row β MUST NOT clip */
.waveform-controls,
[class*="controls"],
.audio-controls,
[data-testid="waveform-controls"] {
display: flex !important;
flex-direction: row !important;
flex-wrap: nowrap !important;
align-items: center !important;
gap: 6px !important;
padding: 10px 12px !important;
background: var(--bg-card) !important;
border-top: 1px solid var(--border) !important;
border-radius: 0 0 var(--radius-md) var(--radius-md) !important;
width: 100% !important;
min-height: 52px !important;
overflow: visible !important;
}
/* Time labels */
.waveform-controls span,
[class*="time"],
[class*="timestamp"] {
font-family: var(--font-mono) !important;
font-size: 11px !important;
color: var(--text-muted) !important;
white-space: nowrap !important;
flex-shrink: 0 !important;
}
/* ALL icon/control buttons in audio toolbar */
.waveform-controls button,
.audio-controls button,
[data-testid="audio"] button,
[class*="icon-button"],
[class*="control-button"],
[class*="audio-button"] {
background: var(--bg-surface) !important;
border: 1px solid var(--border) !important;
color: var(--accent-light) !important;
border-radius: var(--radius-sm) !important;
width: 32px !important;
height: 32px !important;
min-width: 32px !important;
min-height: 32px !important;
max-width: 32px !important;
max-height: 32px !important;
padding: 0 !important;
display: flex !important;
align-items: center !important;
justify-content: center !important;
cursor: pointer !important;
flex-shrink: 0 !important;
transition: background 0.15s, border-color 0.15s !important;
}
.waveform-controls button:hover,
.audio-controls button:hover,
[data-testid="audio"] button:hover {
background: var(--accent-dim) !important;
border-color: var(--accent) !important;
}
/* SVG icons inside buttons */
.waveform-controls button svg,
.audio-controls button svg,
[data-testid="audio"] button svg {
width: 14px !important;
height: 14px !important;
fill: currentColor !important;
stroke: currentColor !important;
flex-shrink: 0 !important;
}
/* Play button β slightly larger accent */
[data-testid="play-pause-button"],
[aria-label="Play"],
[aria-label="Pause"] {
background: var(--accent) !important;
border-color: var(--accent) !important;
color: #fff !important;
width: 36px !important;
height: 36px !important;
min-width: 36px !important;
max-width: 36px !important;
border-radius: 50% !important;
}
/* Speed badge */
[class*="speed"],
[aria-label*="speed"],
[aria-label*="Speed"] {
font-family: var(--font-mono) !important;
font-size: 10px !important;
min-width: 36px !important;
max-width: 36px !important;
padding: 0 4px !important;
letter-spacing: 0 !important;
}
/* Upload drag area */
[data-testid="audio"] .upload-container,
[data-testid="audio"] .empty-state,
.audio-component-wrap .upload-container {
background: var(--bg-element) !important;
border: 2px dashed var(--border-glow) !important;
border-radius: var(--radius-md) !important;
min-height: 100px !important;
display: flex !important;
flex-direction: column !important;
align-items: center !important;
justify-content: center !important;
color: var(--text-muted) !important;
font-family: var(--font-mono) !important;
font-size: 12px !important;
cursor: pointer !important;
transition: border-color 0.2s, background 0.2s !important;
}
.audio-component-wrap .upload-container:hover {
border-color: var(--accent) !important;
background: rgba(124, 92, 252, 0.06) !important;
}
/* ββββ DIVIDER ββββ */
.section-divider {
border: none !important;
border-top: 1px solid var(--border) !important;
margin: 22px 0 !important;
}
/* ββββ ANALYZE BUTTONS ββββ */
.btn-analyze {
width: 100% !important;
padding: 13px 20px !important;
font-family: var(--font-display) !important;
font-size: 14px !important;
font-weight: 700 !important;
letter-spacing: 0.5px !important;
color: #fff !important;
background: linear-gradient(135deg, var(--accent-dim), var(--accent)) !important;
border: none !important;
border-radius: var(--radius-md) !important;
cursor: pointer !important;
transition: opacity 0.2s, transform 0.1s, box-shadow 0.2s !important;
box-shadow: 0 4px 20px rgba(124, 92, 252, 0.3) !important;
margin-bottom: 4px !important;
}
/* Catch all Gradio primary button variants */
button.lg,
button[class*="primary"],
.svelte-cmf5ev,
[data-testid*="analyze"] {
background: linear-gradient(135deg, var(--accent-dim), var(--accent)) !important;
color: #fff !important;
border: none !important;
border-radius: var(--radius-md) !important;
font-family: var(--font-display) !important;
font-size: 14px !important;
font-weight: 700 !important;
padding: 13px 20px !important;
width: 100% !important;
cursor: pointer !important;
box-shadow: 0 4px 20px rgba(124, 92, 252, 0.3) !important;
transition: opacity 0.2s, transform 0.1s !important;
margin-bottom: 4px !important;
}
button.lg:hover,
button[class*="primary"]:hover {
opacity: 0.88 !important;
transform: translateY(-1px) !important;
box-shadow: 0 6px 28px rgba(124, 92, 252, 0.45) !important;
}
button.lg:active { transform: translateY(0) !important; }
/* Secondary / clear buttons */
.btn-secondary button,
button.secondary,
button[class*="secondary"] {
background: var(--bg-element) !important;
border: 1px solid var(--border) !important;
color: var(--text-muted) !important;
font-family: var(--font-display) !important;
font-size: 13px !important;
font-weight: 600 !important;
border-radius: var(--radius-md) !important;
padding: 10px 20px !important;
width: 100% !important;
cursor: pointer !important;
transition: background 0.2s, color 0.2s !important;
}
.btn-secondary button:hover { background: var(--bg-surface) !important; color: var(--text-primary) !important; }
/* ββββ OUTPUT TEXTBOX ββββ */
.panel-output textarea,
.panel-output .gr-textbox textarea,
textarea {
background: var(--bg-surface) !important;
color: var(--text-primary) !important;
border: 1px solid var(--border) !important;
border-radius: var(--radius-md) !important;
font-family: var(--font-mono) !important;
font-size: 13px !important;
line-height: 1.8 !important;
min-height: 420px !important;
max-height: 70vh !important;
padding: 20px !important;
resize: vertical !important;
width: 100% !important;
transition: border-color 0.2s !important;
}
textarea:focus {
outline: none !important;
border-color: var(--accent) !important;
box-shadow: 0 0 0 3px rgba(124, 92, 252, 0.12) !important;
}
textarea::placeholder { color: var(--text-dim) !important; }
/* ββββ LABELS ββββ */
label span,
.label-wrap span,
.gr-textbox label span,
[class*="label"] {
font-family: var(--font-mono) !important;
font-size: 11px !important;
color: var(--text-muted) !important;
text-transform: uppercase !important;
letter-spacing: 1px !important;
}
/* ββββ SCROLLBARS ββββ */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: var(--bg-base); }
::-webkit-scrollbar-thumb { background: var(--border-glow); border-radius: 4px; }
/* ββββ HIDE GRADIO FOOTER ββββ */
footer,
.built-with,
[class*="built-with"],
.footer,
.svelte-1ax1toq,
.gradio-container ~ footer {
display: none !important;
visibility: hidden !important;
height: 0 !important;
overflow: hidden !important;
}
/* ββββ RECORDING PULSE ββββ */
@keyframes pulse-rec {
0%, 100% { box-shadow: 0 0 0 0 rgba(239, 68, 68, 0.5); }
50% { box-shadow: 0 0 0 10px rgba(239, 68, 68, 0); }
}
[data-testid="record-button"][aria-pressed="true"],
.recording button {
animation: pulse-rec 1.2s infinite !important;
background: #dc2626 !important;
border-color: #ef4444 !important;
}
/* ββββ RESPONSIVE ββββ */
@media (max-width: 800px) {
.gradio-container { padding: 16px 12px 40px !important; }
.panel-input, .panel-output { min-width: 100% !important; padding: 18px !important; }
}
"""
# ββ Interface ββββββββββββββββββββββββββββββββββββββββββββββ
def create_interface():
# Gradio 6.0: css and theme go in launch(), NOT Blocks()
with gr.Blocks(theme=gr.themes.Base()) as interface:
# Header
gr.HTML("""
<div class="app-header">
<h1>π§ Audio Sentiment <span>Analyzer</span></h1>
<div class="app-subtitle">DistilBERT Β· Whisper Β· Multi-language Β· Real-time analysis</div>
</div>
""")
with gr.Row(elem_classes="main-row", equal_height=False):
# ββ INPUT PANEL βββββββββββββββββββββββββββββββ
with gr.Column(scale=1, elem_classes="panel-input"):
gr.Markdown("## π€ Input Panel")
# Upload
gr.HTML("<div class='hint-label'>π Upload audio file β MP3, WAV, M4A, OGG, FLAC</div>")
upload_audio = gr.Audio(
sources=["upload"],
type="filepath",
label="Upload Audio File",
elem_classes="audio-component-wrap",
)
upload_btn = gr.Button(
"π Analyze Uploaded Audio",
variant="primary",
elem_classes="btn-analyze"
)
gr.HTML("<hr class='section-divider'>")
# Record
gr.HTML("<div class='hint-label'>ποΈ Record live from microphone</div>")
record_audio = gr.Audio(
sources=["microphone"],
type="filepath",
label="Microphone Recording",
elem_classes="audio-component-wrap",
)
record_btn = gr.Button(
"ποΈ Analyze Recorded Audio",
variant="primary",
elem_classes="btn-analyze"
)
gr.HTML("<hr class='section-divider'>")
with gr.Row(elem_classes="btn-secondary"):
clear_btn = gr.Button("π§Ή Clear All", variant="secondary")
# ββ OUTPUT PANEL ββββββββββββββββββββββββββββββ
with gr.Column(scale=2, elem_classes="panel-output"):
gr.Markdown("## π Result Panel")
output_text = gr.Textbox(
lines=22,
label="Analysis Result",
placeholder=(
"Results will appear here after analysis...\n\n"
" π― Transcription\n"
" π Sentiment label\n"
" π Confidence score\n"
" βββ Confidence bar"
),
interactive=False,
)
with gr.Row(elem_classes="btn-secondary"):
clear_result_btn = gr.Button("π§Ή Clear Result", variant="secondary")
# ββ Events ββββββββββββββββββββββββββββββββββββββ
upload_btn.click(fn=process_audio, inputs=upload_audio, outputs=output_text)
record_btn.click(fn=process_audio, inputs=record_audio, outputs=output_text)
clear_btn.click(
fn=lambda: (None, None, ""),
inputs=[],
outputs=[upload_audio, record_audio, output_text]
)
clear_result_btn.click(
fn=lambda: "",
inputs=[],
outputs=output_text
)
return interface
# ββ Launch βββββββββββββββββββββββββββββββββββββββββββββββββ
if __name__ == "__main__":
interface = create_interface()
# Gradio 6.0: css and theme are passed to launch()
interface.launch(share=True, css=APP_CSS) |