Spaces:
Runtime error
Runtime error
File size: 27,338 Bytes
8c3bee7 | 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 | <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>CleanVoice β Denoiser</title>
<link href="https://fonts.googleapis.com/css2?family=Syne:wght@400;600;800&family=JetBrains+Mono:wght@300;400;500&display=swap" rel="stylesheet"/>
<style>
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
:root {
--bg: #0a0a0f;
--surface: #11111a;
--surface2: #1a1a28;
--border: #2a2a3f;
--accent: #7c6aff;
--accent2: #ff6a9b;
--text: #e8e8f0;
--muted: #6b6b88;
--success: #4dffb4;
--danger: #ff4d6a;
--warning: #ffb347;
--radius: 16px;
}
body {
background: var(--bg); color: var(--text);
font-family: 'Syne', sans-serif;
min-height: 100vh; display: flex; flex-direction: column;
align-items: center; justify-content: flex-start;
padding: 48px 20px 80px; overflow-x: hidden;
}
body::before {
content: ''; position: fixed; inset: 0;
background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 256 256' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='4' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)' opacity='0.05'/%3E%3C/svg%3E");
pointer-events: none; z-index: 0; opacity: .4;
}
.orb { position: fixed; border-radius: 50%; filter: blur(120px); pointer-events: none; z-index: 0; }
.orb-1 { width: 500px; height: 500px; background: rgba(124,106,255,.12); top: -150px; left: -100px; }
.orb-2 { width: 400px; height: 400px; background: rgba(255,106,155,.1); bottom: -100px; right: -80px; }
.wrapper { position: relative; z-index: 1; width: 100%; max-width: 640px; }
header { text-align: center; margin-bottom: 48px; }
.logo-badge {
display: inline-flex; align-items: center; gap: 8px;
background: var(--surface2); border: 1px solid var(--border);
padding: 6px 14px; border-radius: 999px;
font-family: 'JetBrains Mono', monospace; font-size: 11px;
color: var(--accent); letter-spacing: .12em; text-transform: uppercase; margin-bottom: 20px;
}
.logo-badge span { width: 6px; height: 6px; background: var(--accent); border-radius: 50%; animation: pulse 2s infinite; }
h1 { font-size: clamp(2rem,6vw,3.2rem); font-weight: 800; line-height: 1.1;
background: linear-gradient(135deg,#fff 30%,var(--accent));
-webkit-background-clip: text; -webkit-text-fill-color: transparent; }
.subtitle { color: var(--muted); font-size: .95rem; margin-top: 10px; letter-spacing: .02em; }
.card {
background: var(--surface); border: 1px solid var(--border);
border-radius: var(--radius); padding: 32px;
box-shadow: 0 0 0 1px rgba(255,255,255,.03), 0 24px 64px rgba(0,0,0,.5);
margin-bottom: 24px; animation: fadeUp .5s ease both;
}
.card-label {
font-family: 'JetBrains Mono', monospace; font-size: 10px;
letter-spacing: .15em; text-transform: uppercase; color: var(--muted);
margin-bottom: 20px; display: flex; align-items: center; gap: 8px;
}
.card-label::after { content: ''; flex: 1; height: 1px; background: var(--border); }
.tabs {
display: grid; grid-template-columns: 1fr 1fr;
background: var(--bg); border: 1px solid var(--border);
border-radius: 10px; padding: 4px; margin-bottom: 28px; gap: 4px;
}
.tab-btn {
padding: 10px; border: none; border-radius: 7px;
background: transparent; color: var(--muted);
font-family: 'Syne', sans-serif; font-size: .875rem; font-weight: 600;
cursor: pointer; transition: all .2s; display: flex; align-items: center; justify-content: center; gap: 8px;
}
.tab-btn.active { background: var(--surface2); color: var(--text); box-shadow: 0 2px 8px rgba(0,0,0,.4); }
.tab-btn:hover:not(.active) { color: var(--text); }
.tab-panel { display: none; }
.tab-panel.active { display: block; animation: fadeIn .2s ease; }
.upload-zone {
border: 2px dashed var(--border); border-radius: 12px;
padding: 40px 24px; text-align: center; cursor: pointer; transition: all .25s;
}
.upload-zone:hover, .upload-zone.drag-over { border-color: var(--accent); background: rgba(124,106,255,.05); }
.upload-zone input[type=file] { display: none; }
.upload-icon { font-size: 2.5rem; margin-bottom: 12px; display: block; }
.upload-zone p { color: var(--muted); font-size: .875rem; }
.upload-zone strong { color: var(--text); }
.upload-zone .formats { font-family: 'JetBrains Mono', monospace; font-size: .75rem; color: var(--muted); margin-top: 8px; opacity: .7; }
.file-preview { display: none; align-items: center; gap: 12px; background: var(--surface2); border-radius: 10px; padding: 14px 16px; margin-top: 16px; }
.file-preview.visible { display: flex; }
.file-icon { font-size: 1.4rem; }
.file-info { flex: 1; min-width: 0; }
.file-name { font-size: .875rem; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.file-size { font-family: 'JetBrains Mono', monospace; font-size: .75rem; color: var(--muted); }
.file-remove { background: none; border: none; color: var(--muted); cursor: pointer; font-size: 1rem; padding: 4px; transition: color .2s; }
.file-remove:hover { color: var(--danger); }
.recorder-area { display: flex; flex-direction: column; align-items: center; gap: 24px; }
.record-btn {
width: 96px; height: 96px; border-radius: 50%; border: 2px solid var(--border);
background: var(--surface2); cursor: pointer; display: flex; align-items: center;
justify-content: center; font-size: 2rem; transition: all .2s; position: relative;
}
.record-btn:hover { border-color: var(--accent); transform: scale(1.04); }
.record-btn.recording { border-color: var(--danger); background: rgba(255,77,106,.1); animation: recPulse 1.4s ease-in-out infinite; }
.record-btn .rec-dot { width: 28px; height: 28px; border-radius: 50%; background: var(--danger); transition: all .3s; }
.record-btn.recording .rec-dot { border-radius: 6px; transform: scale(.8); }
.rec-timer { font-family: 'JetBrains Mono', monospace; font-size: 2rem; font-weight: 300; color: var(--text); letter-spacing: .08em; min-width: 100px; text-align: center; }
.rec-status { font-size: .8rem; color: var(--muted); letter-spacing: .08em; text-transform: uppercase; font-family: 'JetBrains Mono', monospace; }
.rec-status.live { color: var(--danger); }
.waveform { display: flex; align-items: center; gap: 3px; height: 36px; opacity: 0; transition: opacity .3s; }
.waveform.active { opacity: 1; }
.wave-bar { width: 3px; border-radius: 99px; background: var(--accent); animation: wave .8s ease-in-out infinite; transform-origin: bottom; }
.wave-bar:nth-child(1) { animation-delay:0s; height:12px; }
.wave-bar:nth-child(2) { animation-delay:.1s; height:22px; }
.wave-bar:nth-child(3) { animation-delay:.2s; height:32px; }
.wave-bar:nth-child(4) { animation-delay:.1s; height:20px; }
.wave-bar:nth-child(5) { animation-delay:.3s; height:28px; }
.wave-bar:nth-child(6) { animation-delay:.15s; height:16px; }
.wave-bar:nth-child(7) { animation-delay:.25s; height:24px; }
.wave-bar:nth-child(8) { animation-delay:.05s; height:18px; }
.wave-bar:nth-child(9) { animation-delay:.35s; height:30px; }
.wave-bar:nth-child(10) { animation-delay:.2s; height:14px; }
.recorded-preview { width: 100%; display: none; flex-direction: column; gap: 12px; }
.recorded-preview.visible { display: flex; }
.recorded-preview audio { width: 100%; border-radius: 8px; filter: invert(1) hue-rotate(200deg); }
.recorded-actions { display: flex; gap: 8px; }
.btn-ghost { flex: 1; padding: 10px; border-radius: 8px; border: 1px solid var(--border); background: transparent; color: var(--muted); font-family: 'Syne', sans-serif; font-size: .85rem; cursor: pointer; transition: all .2s; }
.btn-ghost:hover { border-color: var(--text); color: var(--text); }
.submit-btn {
width: 100%; padding: 16px;
background: linear-gradient(135deg, var(--accent), #a855f7);
border: none; border-radius: 12px; color: #fff;
font-family: 'Syne', sans-serif; font-size: 1rem; font-weight: 700;
letter-spacing: .04em; cursor: pointer; transition: all .25s;
display: flex; align-items: center; justify-content: center; gap: 10px; margin-top: 8px;
}
.submit-btn:hover { transform: translateY(-2px); box-shadow: 0 8px 32px rgba(124,106,255,.35); }
.submit-btn:active { transform: translateY(0); }
.submit-btn:disabled { opacity: .45; cursor: not-allowed; transform: none; box-shadow: none; }
/* ββ result card ββ */
.result-card { display: none; animation: fadeUp .4s ease both; }
.result-card.visible { display: block; }
.result-inner { display: flex; flex-direction: column; gap: 16px; }
.result-audio-label { font-family: 'JetBrains Mono', monospace; font-size: .75rem; color: var(--success); letter-spacing: .1em; text-transform: uppercase; }
.result-audio audio { width: 100%; border-radius: 8px; filter: invert(1) hue-rotate(130deg); }
.transcript-box {
background: var(--bg); border: 1px solid var(--border); border-radius: 10px; padding: 16px;
font-family: 'JetBrains Mono', monospace; font-size: .82rem; color: var(--text);
line-height: 1.7; white-space: pre-wrap; max-height: 200px; overflow-y: auto;
}
.copy-btn { align-self: flex-end; padding: 8px 16px; background: var(--surface2); border: 1px solid var(--border); border-radius: 8px; color: var(--muted); font-family: 'Syne', sans-serif; font-size: .8rem; cursor: pointer; transition: all .2s; }
.copy-btn:hover { color: var(--text); border-color: var(--text); }
/* ββ pretty error box ββ */
.error-card { display: none; animation: fadeUp .3s ease both; }
.error-card.visible { display: block; }
.error-header {
display: flex; align-items: center; gap: 10px; margin-bottom: 14px;
}
.error-badge {
font-family: 'JetBrains Mono', monospace; font-size: 11px; font-weight: 500;
background: rgba(255,77,106,.15); color: var(--danger);
border: 1px solid rgba(255,77,106,.3); padding: 3px 10px; border-radius: 999px;
letter-spacing: .08em; text-transform: uppercase;
}
.error-title { font-size: .9rem; font-weight: 600; color: var(--danger); }
.error-json {
background: var(--bg); border: 1px solid rgba(255,77,106,.25);
border-radius: 10px; padding: 16px; overflow-x: auto;
font-family: 'JetBrains Mono', monospace; font-size: .8rem; line-height: 1.8;
}
/* JSON syntax highlighting */
.json-key { color: #7c6aff; }
.json-str { color: #4dffb4; }
.json-num { color: #ffb347; }
.json-bool { color: #ff6a9b; }
.json-null { color: #6b6b88; }
.json-brace { color: var(--muted); }
/* ββ progress bar ββ */
.progress-track { height: 3px; background: var(--border); border-radius: 99px; overflow: hidden; margin-top: 12px; display: none; }
.progress-track.active { display: block; }
.progress-fill { height: 100%; background: linear-gradient(90deg,var(--accent),#a855f7); border-radius: 99px; width: 0%; transition: width .4s ease; }
/* ββ status steps ββ */
.status-steps { display: none; flex-direction: column; gap: 6px; margin-top: 12px; }
.status-steps.active { display: flex; }
.status-step {
display: flex; align-items: center; gap: 10px;
font-family: 'JetBrains Mono', monospace; font-size: .75rem; color: var(--muted);
transition: color .3s;
}
.status-step.done { color: var(--success); }
.status-step.active-step { color: var(--accent); }
.step-dot { width: 6px; height: 6px; border-radius: 50%; background: currentColor; flex-shrink: 0; }
.toast { position: fixed; bottom: 28px; left: 50%; transform: translateX(-50%) translateY(20px); background: var(--surface2); border: 1px solid var(--border); padding: 12px 20px; border-radius: 10px; font-size: .85rem; opacity: 0; transition: all .3s; pointer-events: none; white-space: nowrap; z-index: 100; }
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
@keyframes fadeUp { from { opacity:0; transform:translateY(18px); } to { opacity:1; transform:translateY(0); } }
@keyframes fadeIn { from { opacity:0; } to { opacity:1; } }
@keyframes pulse { 0%,100% { opacity:1; } 50% { opacity:.4; } }
@keyframes recPulse { 0%,100% { box-shadow:0 0 0 0 rgba(255,77,106,.4); } 50% { box-shadow:0 0 0 14px rgba(255,77,106,0); } }
@keyframes wave { 0%,100% { transform:scaleY(1); } 50% { transform:scaleY(.35); } }
</style>
</head>
<body>
<div class="orb orb-1"></div>
<div class="orb orb-2"></div>
<div class="wrapper">
<header>
<div class="logo-badge"><span></span> CleanVoice AI</div>
<h1>Audio Denoiser</h1>
<p class="subtitle">Remove noise, fillers & stutters from your recordings</p>
</header>
<!-- input card -->
<div class="card">
<div class="card-label">Input Audio</div>
<div class="tabs">
<button class="tab-btn active" data-tab="upload">
<svg width="15" height="15" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24"><path d="M21 15v4a2 2 0 01-2 2H5a2 2 0 01-2-2v-4"/><polyline points="17 8 12 3 7 8"/><line x1="12" y1="3" x2="12" y2="15"/></svg>
Upload File
</button>
<button class="tab-btn" data-tab="record">
<svg width="15" height="15" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24"><path d="M12 2a3 3 0 013 3v7a3 3 0 01-6 0V5a3 3 0 013-3z"/><path d="M19 10v2a7 7 0 01-14 0v-2"/><line x1="12" y1="19" x2="12" y2="22"/></svg>
Record
</button>
</div>
<div class="tab-panel active" id="panel-upload">
<div class="upload-zone" id="dropZone">
<input type="file" id="fileInput" accept="audio/*,.wav,.mp3,.m4a,.ogg,.flac,.webm"/>
<span class="upload-icon">π΅</span>
<p><strong>Drop your audio file here</strong><br>or click to browse</p>
<p class="formats">WAV Β· MP3 Β· M4A Β· OGG Β· FLAC Β· WEBM</p>
</div>
<div class="file-preview" id="filePreview">
<span class="file-icon">π§</span>
<div class="file-info">
<div class="file-name" id="fileName">β</div>
<div class="file-size" id="fileSize">β</div>
</div>
<button class="file-remove" id="fileRemove" title="Remove">β</button>
</div>
</div>
<div class="tab-panel" id="panel-record">
<div class="recorder-area">
<div class="waveform" id="waveform">
<div class="wave-bar"></div><div class="wave-bar"></div><div class="wave-bar"></div>
<div class="wave-bar"></div><div class="wave-bar"></div><div class="wave-bar"></div>
<div class="wave-bar"></div><div class="wave-bar"></div><div class="wave-bar"></div>
<div class="wave-bar"></div>
</div>
<button class="record-btn" id="recordBtn"><div class="rec-dot"></div></button>
<div class="rec-timer" id="recTimer">00:00</div>
<div class="rec-status" id="recStatus">tap to record</div>
<div class="recorded-preview" id="recordedPreview">
<audio id="recordedAudio" controls></audio>
<div class="recorded-actions">
<button class="btn-ghost" id="reRecordBtn">βΊ Re-record</button>
</div>
</div>
</div>
</div>
</div>
<button class="submit-btn" id="submitBtn" disabled>
<svg width="18" height="18" fill="none" stroke="currentColor" stroke-width="2.2" viewBox="0 0 24 24"><polygon points="5 3 19 12 5 21 5 3"/></svg>
Process Audio
</button>
<!-- progress -->
<div class="progress-track" id="progressTrack"><div class="progress-fill" id="progressFill"></div></div>
<div class="status-steps" id="statusSteps">
<div class="status-step" id="step1"><div class="step-dot"></div> Uploading audioβ¦</div>
<div class="status-step" id="step2"><div class="step-dot"></div> Converting to WAVβ¦</div>
<div class="status-step" id="step3"><div class="step-dot"></div> Running denoiserβ¦</div>
<div class="status-step" id="step4"><div class="step-dot"></div> Cleaning transcriptβ¦</div>
</div>
<!-- result -->
<div class="card result-card" id="resultCard" style="margin-top:24px;">
<div class="card-label">Output</div>
<div class="result-inner">
<div>
<div class="result-audio-label">β¦ Cleaned Audio</div>
<div class="result-audio" style="margin-top:10px;"><audio id="resultAudio" controls></audio></div>
</div>
<div>
<div class="result-audio-label" style="color:var(--accent)">β¦ Transcript</div>
<div class="transcript-box" id="transcriptBox">β</div>
</div>
<button class="copy-btn" id="copyBtn">Copy Transcript</button>
</div>
</div>
<!-- pretty error -->
<div class="card error-card" id="errorCard" style="margin-top:24px; border-color: rgba(255,77,106,.25);">
<div class="card-label" style="color: var(--danger)">Error Response</div>
<div class="error-header">
<span class="error-badge" id="errorStatus">404</span>
<span class="error-title" id="errorTitle">Not Found</span>
</div>
<pre class="error-json" id="errorJson"></pre>
</div>
</div>
<div class="toast" id="toast"></div>
<script>
// ββ CONFIG β change this to your HF Space API endpoint ββ
const API_BASE = 'https://YOUR-HF-SPACE.hf.space'; // e.g. https://yourname-denoiser.hf.space
const DENOISE_ENDPOINT = `${API_BASE}/denoise`; // adjust to your route
/* ββββββββββββββββββββ TABS ββββββββββββββββββββ */
document.querySelectorAll('.tab-btn').forEach(btn => {
btn.addEventListener('click', () => {
document.querySelectorAll('.tab-btn').forEach(b => b.classList.remove('active'));
document.querySelectorAll('.tab-panel').forEach(p => p.classList.remove('active'));
btn.classList.add('active');
document.getElementById('panel-' + btn.dataset.tab).classList.add('active');
clearAudio();
});
});
/* ββββββββββββββββββββ UPLOAD ββββββββββββββββββββ */
let uploadedFile = null;
const dropZone = document.getElementById('dropZone');
const fileInput = document.getElementById('fileInput');
const filePreview = document.getElementById('filePreview');
const fileName = document.getElementById('fileName');
const fileSize = document.getElementById('fileSize');
dropZone.addEventListener('click', () => fileInput.click());
dropZone.addEventListener('dragover', e => { e.preventDefault(); dropZone.classList.add('drag-over'); });
dropZone.addEventListener('dragleave', () => dropZone.classList.remove('drag-over'));
dropZone.addEventListener('drop', e => {
e.preventDefault(); dropZone.classList.remove('drag-over');
if (e.dataTransfer.files[0]) setFile(e.dataTransfer.files[0]);
});
fileInput.addEventListener('change', () => { if (fileInput.files[0]) setFile(fileInput.files[0]); });
document.getElementById('fileRemove').addEventListener('click', () => {
uploadedFile = null; filePreview.classList.remove('visible'); updateSubmit();
});
function setFile(f) {
uploadedFile = f;
fileName.textContent = f.name;
fileSize.textContent = (f.size / 1024 / 1024).toFixed(2) + ' MB';
filePreview.classList.add('visible');
updateSubmit();
}
/* ββββββββββββββββββββ RECORDER ββββββββββββββββββββ */
let mediaRecorder, recordedBlob, timerInterval, seconds = 0;
const recordBtn = document.getElementById('recordBtn');
const recTimer = document.getElementById('recTimer');
const recStatus = document.getElementById('recStatus');
const waveform = document.getElementById('waveform');
const recordedPreview = document.getElementById('recordedPreview');
const recordedAudio = document.getElementById('recordedAudio');
recordBtn.addEventListener('click', async () => {
if (!mediaRecorder || mediaRecorder.state === 'inactive') {
try {
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
mediaRecorder = new MediaRecorder(stream);
const chunks = [];
mediaRecorder.ondataavailable = e => chunks.push(e.data);
mediaRecorder.onstop = () => {
recordedBlob = new Blob(chunks, { type: 'audio/webm' });
recordedAudio.src = URL.createObjectURL(recordedBlob);
recordedPreview.classList.add('visible');
updateSubmit();
stream.getTracks().forEach(t => t.stop());
};
mediaRecorder.start();
recordBtn.classList.add('recording');
waveform.classList.add('active');
recStatus.textContent = 'recordingβ¦'; recStatus.classList.add('live');
seconds = 0; recTimer.textContent = '00:00';
timerInterval = setInterval(() => {
seconds++;
recTimer.textContent = String(Math.floor(seconds/60)).padStart(2,'0') + ':' + String(seconds%60).padStart(2,'0');
}, 1000);
} catch { showToast('Microphone access denied'); }
} else {
mediaRecorder.stop(); clearInterval(timerInterval);
recordBtn.classList.remove('recording'); waveform.classList.remove('active');
recStatus.textContent = 'recording saved'; recStatus.classList.remove('live');
}
});
document.getElementById('reRecordBtn').addEventListener('click', () => {
recordedBlob = null; recordedPreview.classList.remove('visible');
recordedAudio.src = ''; recStatus.textContent = 'tap to record'; recTimer.textContent = '00:00';
updateSubmit();
});
function clearAudio() {
uploadedFile = null; recordedBlob = null;
filePreview.classList.remove('visible'); recordedPreview.classList.remove('visible');
updateSubmit();
}
function updateSubmit() {
document.getElementById('submitBtn').disabled = !(uploadedFile || recordedBlob);
}
/* ββββββββββββββββββββ PRETTY JSON ββββββββββββββββββββ */
function syntaxHighlight(obj) {
const json = JSON.stringify(obj, null, 2);
return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, match => {
if (/^"/.test(match)) {
if (/:$/.test(match)) return `<span class="json-key">${match}</span>`;
return `<span class="json-str">${match}</span>`;
}
if (/true|false/.test(match)) return `<span class="json-bool">${match}</span>`;
if (/null/.test(match)) return `<span class="json-null">${match}</span>`;
return `<span class="json-num">${match}</span>`;
});
}
function showError(statusCode, data) {
document.getElementById('resultCard').classList.remove('visible');
const card = document.getElementById('errorCard');
const badge = document.getElementById('errorStatus');
const title = document.getElementById('errorTitle');
const jsonEl = document.getElementById('errorJson');
badge.textContent = statusCode;
// derive human title
const titles = { 400:'Bad Request', 401:'Unauthorized', 403:'Forbidden',
404:'Not Found', 422:'Unprocessable Entity', 500:'Internal Server Error' };
title.textContent = data?.detail || titles[statusCode] || 'Error';
jsonEl.innerHTML = syntaxHighlight(data);
card.classList.add('visible');
card.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
}
/* ββββββββββββββββββββ PROGRESS ββββββββββββββββββββ */
function setStep(n) {
[1,2,3,4].forEach(i => {
const el = document.getElementById('step' + i);
el.classList.remove('done','active-step');
if (i < n) el.classList.add('done');
if (i === n) el.classList.add('active-step');
});
document.getElementById('progressFill').style.width = ((n-1)/3 * 100) + '%';
}
function showProgress(show) {
document.getElementById('progressTrack').classList.toggle('active', show);
document.getElementById('statusSteps').classList.toggle('active', show);
if (!show) document.getElementById('progressFill').style.width = '0%';
}
/* ββββββββββββββββββββ SUBMIT ββββββββββββββββββββ */
const submitBtn = document.getElementById('submitBtn');
submitBtn.addEventListener('click', async () => {
submitBtn.disabled = true;
submitBtn.innerHTML = `<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.2"><circle cx="12" cy="12" r="10"/><path d="M12 6v6l4 2"/></svg> Processingβ¦`;
document.getElementById('resultCard').classList.remove('visible');
document.getElementById('errorCard').classList.remove('visible');
showProgress(true);
setStep(1);
const formData = new FormData();
const audioBlob = uploadedFile || recordedBlob;
const audioName = uploadedFile ? uploadedFile.name : 'recording.webm';
formData.append('file', audioBlob, audioName);
try {
setStep(2);
await new Promise(r => setTimeout(r, 400)); // slight delay for UX step visibility
setStep(3);
const response = await fetch(DENOISE_ENDPOINT, {
method: 'POST',
body: formData,
});
const contentType = response.headers.get('Content-Type') || '';
let data;
if (contentType.includes('application/json')) {
data = await response.json();
} else {
data = { detail: await response.text() };
}
if (!response.ok) {
// ββ pretty print the error ββ
showProgress(false);
showError(response.status, data);
} else {
setStep(4);
// ββ happy path: expect { audio_url, transcript } ββ
const resultAudio = document.getElementById('resultAudio');
const transcriptBox = document.getElementById('transcriptBox');
const resultCard = document.getElementById('resultCard');
if (data.audio_url) {
resultAudio.src = data.audio_url;
} else if (data.audio_base64) {
resultAudio.src = 'data:audio/wav;base64,' + data.audio_base64;
}
transcriptBox.textContent = data.transcript || data.text || JSON.stringify(data, null, 2);
showProgress(false);
resultCard.classList.add('visible');
resultCard.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
}
} catch (err) {
showProgress(false);
showError('Network', { detail: err.message, type: 'NetworkError' });
}
submitBtn.disabled = false;
submitBtn.innerHTML = `<svg width="18" height="18" fill="none" stroke="currentColor" stroke-width="2.2" viewBox="0 0 24 24"><polygon points="5 3 19 12 5 21 5 3"/></svg> Process Audio`;
updateSubmit();
});
document.getElementById('copyBtn').addEventListener('click', () => {
navigator.clipboard.writeText(document.getElementById('transcriptBox').textContent)
.then(() => showToast('Copied!'));
});
/* ββββββββββββββββββββ TOAST ββββββββββββββββββββ */
const toast = document.getElementById('toast');
function showToast(msg) {
toast.textContent = msg;
toast.classList.add('show');
setTimeout(() => toast.classList.remove('show'), 2500);
}
</script>
</body>
</html> |