File size: 26,006 Bytes
f587ca1
2132455
 
f587ca1
 
2132455
 
f587ca1
2132455
f587ca1
2132455
 
 
 
f587ca1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7ceaf75
f587ca1
 
 
 
2132455
f587ca1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2132455
 
f587ca1
 
 
 
 
 
 
 
 
2132455
f587ca1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7ceaf75
 
f587ca1
7ceaf75
 
 
 
 
 
f587ca1
 
 
 
 
 
 
7ceaf75
f587ca1
 
 
7ceaf75
 
f587ca1
 
7ceaf75
 
 
 
f587ca1
7ceaf75
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f587ca1
 
 
7ceaf75
f587ca1
 
7ceaf75
 
f587ca1
7ceaf75
 
 
f587ca1
7ceaf75
f587ca1
7ceaf75
 
f587ca1
7ceaf75
 
 
f587ca1
7ceaf75
 
 
 
 
 
 
 
 
 
 
 
 
 
f587ca1
7ceaf75
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f587ca1
7ceaf75
 
 
 
 
 
 
 
 
 
f587ca1
 
7ceaf75
 
f587ca1
 
7ceaf75
f587ca1
 
 
 
7ceaf75
 
f587ca1
 
 
 
 
 
7ceaf75
 
f587ca1
 
 
 
 
 
 
 
 
 
 
7ceaf75
f587ca1
 
7ceaf75
f587ca1
7ceaf75
f587ca1
7ceaf75
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f587ca1
7ceaf75
f587ca1
 
 
7ceaf75
f587ca1
7ceaf75
f587ca1
 
7ceaf75
f587ca1
7ceaf75
f587ca1
 
 
7ceaf75
f587ca1
 
 
7ceaf75
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f587ca1
 
 
 
 
7ceaf75
 
f587ca1
 
7ceaf75
f587ca1
 
 
 
 
 
 
 
7ceaf75
f587ca1
7ceaf75
 
 
 
f587ca1
 
7ceaf75
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2132455
f587ca1
 
2132455
f587ca1
7ceaf75
f587ca1
7ceaf75
f587ca1
7ceaf75
f587ca1
 
7ceaf75
 
 
 
f587ca1
7ceaf75
f587ca1
 
2132455
f587ca1
 
7ceaf75
f587ca1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7ceaf75
 
 
 
 
f587ca1
 
 
 
 
 
 
 
 
 
7ceaf75
 
 
 
 
 
 
 
f587ca1
 
 
 
 
 
2132455
f587ca1
 
 
 
 
 
 
 
 
 
2132455
f587ca1
 
 
 
 
7ceaf75
 
 
 
 
 
f587ca1
 
 
 
 
 
7ceaf75
2132455
f587ca1
 
 
7ceaf75
f587ca1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7ceaf75
f587ca1
 
 
 
7ceaf75
f587ca1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7ceaf75
f587ca1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7ceaf75
 
 
 
f587ca1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
import atexit
import os
import tempfile
import uuid
from pathlib import Path

import torch
from flask import Flask, jsonify, render_template_string, request, send_from_directory

from classes import ESC50_CLASSES
from preprocessing import preprocess_audio
from utils import load_compressed_model, load_model, predict


BASE_DIR = Path(__file__).resolve().parent
ORIGINAL_MODEL_PATH = BASE_DIR / "weights" / "esc50_model.pth"
COMPRESSED_MODEL_PATH = BASE_DIR / "weights" / "esc50_model_compressed.pth"
STATS_PATH = BASE_DIR / "stats" / "esc50_mel_stats.json"
SAMPLES_DIR = BASE_DIR / "samples"
UPLOAD_DIR = Path(tempfile.gettempdir()) / "soundedge_uploads"
UPLOAD_DIR.mkdir(parents=True, exist_ok=True)

ALLOWED_EXTENSIONS = {".wav"}
LOW_CONFIDENCE_THRESHOLD = 0.6
MAX_UPLOAD_BYTES = 5 * 1024 * 1024

app = Flask(__name__)
app.config["MAX_CONTENT_LENGTH"] = MAX_UPLOAD_BYTES

_gpu_device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
_model_cache = {"Original": None, "Compressed": None}


def _safe_label(raw_name: str) -> str:
    return raw_name.replace("_", " ").title()


def _sample_files() -> list[str]:
    if not SAMPLES_DIR.is_dir():
        return []
    return sorted([f.name for f in SAMPLES_DIR.iterdir() if f.suffix.lower() == ".wav"])


def _validate_wav_filename(filename: str) -> bool:
    ext = Path(filename).suffix.lower()
    return ext in ALLOWED_EXTENSIONS


def _get_active_model(model_choice: str):
    if model_choice == "Compressed":
        if _model_cache["Compressed"] is None:
            _model_cache["Compressed"] = load_compressed_model(
                str(ORIGINAL_MODEL_PATH),
                str(COMPRESSED_MODEL_PATH),
                num_classes=len(ESC50_CLASSES),
            )
        return _model_cache["Compressed"], torch.device("cpu")

    if _model_cache["Original"] is None:
        _model_cache["Original"] = load_model(
            str(ORIGINAL_MODEL_PATH),
            _gpu_device,
            num_classes=len(ESC50_CLASSES),
        )
    return _model_cache["Original"], _gpu_device


def _prediction_payload(model_choice: str, source_path: Path):
    model, device = _get_active_model(model_choice)
    input_tensor = preprocess_audio(str(source_path), str(STATS_PATH))
    top_class, top_prob, all_probs = predict(model, input_tensor, device)

    return {
        "topClass": top_class,
        "topClassLabel": _safe_label(top_class),
        "topProbability": top_prob,
        "topProbabilityPct": round(top_prob * 100, 2),
        "lowConfidence": top_prob < LOW_CONFIDENCE_THRESHOLD,
        "top3": [
            {
                "className": p["class_name"],
                "classLabel": _safe_label(p["class_name"]),
                "probability": p["probability"],
                "probabilityPct": round(p["probability"] * 100, 2),
            }
            for p in all_probs[:3]
        ],
        "allProbs": [
            {
                "className": p["class_name"],
                "classLabel": _safe_label(p["class_name"]),
                "probability": p["probability"],
                "probabilityPct": round(p["probability"] * 100, 2),
            }
            for p in all_probs
        ],
    }


@app.get("/")
def index():
    classes = [_safe_label(c) for c in ESC50_CLASSES]
    samples = [{"name": s, "label": _safe_label(Path(s).stem)} for s in _sample_files()]
    return render_template_string(
        PAGE_TEMPLATE,
        class_badges=classes,
        samples=samples,
    )


@app.get("/samples/<path:filename>")
def serve_sample(filename: str):
    return send_from_directory(SAMPLES_DIR, filename)


@app.get("/uploads/<path:filename>")
def serve_uploaded_file(filename: str):
    return send_from_directory(UPLOAD_DIR, filename)


@app.post("/upload")
def upload_audio():
    uploaded = request.files.get("file")
    if uploaded is None:
        return jsonify({"error": "Missing file field. Use multipart form key 'file'."}), 400

    filename = uploaded.filename or ""
    if not filename:
        return jsonify({"error": "No file selected."}), 400
    if not _validate_wav_filename(filename):
        return jsonify({"error": "Only .wav files are supported."}), 400

    file_id = uuid.uuid4().hex
    stored_name = f"{file_id}.wav"
    destination = UPLOAD_DIR / stored_name
    uploaded.save(destination)

    return jsonify(
        {
            "fileId": file_id,
            "filename": filename,
            "audioUrl": f"/uploads/{stored_name}",
        }
    )


@app.post("/predict")
def predict_audio():
    payload = request.get_json(silent=True) or {}
    source_type = payload.get("source", "upload")
    model_choice = payload.get("model", "Original")
    if model_choice not in ("Original", "Compressed"):
        return jsonify({"error": "Invalid model. Use 'Original' or 'Compressed'."}), 400

    try:
        if source_type == "upload":
            file_id = payload.get("fileId", "")
            if not file_id:
                return jsonify({"error": "Missing fileId for uploaded source."}), 400
            source_path = UPLOAD_DIR / f"{file_id}.wav"
            if not source_path.exists():
                return jsonify({"error": "Uploaded file not found. Upload again."}), 404
        elif source_type == "sample":
            sample_name = payload.get("sampleName", "")
            if not sample_name:
                return jsonify({"error": "Missing sampleName for sample source."}), 400
            source_path = SAMPLES_DIR / sample_name
            if not source_path.exists() or source_path.suffix.lower() != ".wav":
                return jsonify({"error": "Sample not found."}), 404
        else:
            return jsonify({"error": "Invalid source. Use 'upload' or 'sample'."}), 400

        result = _prediction_payload(model_choice=model_choice, source_path=source_path)
        return jsonify(result)
    except Exception as exc:
        return jsonify({"error": f"Error during inference: {exc}"}), 500


@app.get("/health")
def health():
    return jsonify({"status": "ok"})


def _cleanup_uploads():
    if not UPLOAD_DIR.exists():
        return
    for wav in UPLOAD_DIR.glob("*.wav"):
        try:
            wav.unlink()
        except OSError:
            pass


atexit.register(_cleanup_uploads)


PAGE_TEMPLATE = """
<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>SoundEdge - Environmental Sound Classification</title>
    <style>
        :root {
            --bg: #f1f5f9;
            --text: #1f2937;
            --muted: #64748b;
            --line: #d8dee8;
            --chip-bg: #e9edf3;
            --chip-border: #d8e0ec;
            --panel: transparent;
            --blue: #0b75b8;
            --violet: #6d46e8;
            --danger-bg: #fff1f2;
            --danger: #dc2626;
        }
        * { box-sizing: border-box; }
        body {
            margin: 0;
            font-family: "Segoe UI", "Helvetica Neue", Helvetica, Arial, sans-serif;
            background: var(--bg);
            color: var(--text);
        }
        .container {
            width: min(1020px, 92vw);
            margin: 1.4rem auto 3rem;
        }
        .hero {
            background: linear-gradient(135deg, #d8e7fb 0%, #dfe4f8 100%);
            border: 1px solid #b7d1f5;
            border-radius: 14px;
            padding: 2rem 1.4rem 1.6rem;
            text-align: center;
            margin-bottom: 1.9rem;
        }
        .hero h1 {
            margin: 0;
            font-size: clamp(2.2rem, 3.8vw, 3.6rem);
            font-weight: 800;
            background: linear-gradient(90deg, #1f82c4, #4f48d9);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
        }
        .hero p {
            margin: 1rem 0 0;
            color: #445d78;
            font-size: 2.1vmin;
            min-font-size: 1rem;
        }
        .hero-icon {
            font-size: clamp(2rem, 4vmin, 2.8rem);
            margin-right: 0.55rem;
            vertical-align: 0.28em;
        }
        .section {
            border-top: 1px solid var(--line);
            padding-top: 1.9rem;
            margin-top: 1.7rem;
        }
        .section:first-of-type {
            border-top: 0;
            padding-top: 0;
            margin-top: 0;
        }
        .panel {
            background: var(--panel);
            border: 0;
        }
        .section-title {
            font-size: 1.72rem;
            letter-spacing: 0.12em;
            text-transform: uppercase;
            color: var(--violet);
            font-weight: 800;
            margin: 0 0 1rem;
        }
        .badge-grid { display: flex; flex-wrap: wrap; gap: 0.46rem; }
        .badge {
            background: var(--chip-bg);
            border: 1px solid var(--chip-border);
            border-radius: 8px;
            padding: 0.32rem 0.74rem;
            font-size: 1rem;
            color: #425569;
        }
        .model-row {
            display: flex;
            gap: 1.2rem;
            align-items: center;
            font-size: 1.08rem;
        }
        .model-row label {
            display: inline-flex;
            align-items: center;
            gap: 0.45rem;
            cursor: pointer;
        }
        .tabs {
            display: flex;
            gap: 1rem;
            border-bottom: 2px solid var(--line);
            margin-bottom: 1.05rem;
        }
        .tab-btn {
            border: 0;
            background: transparent;
            color: #334155;
            font-size: 1.08rem;
            padding: 0.32rem 0.1rem 0.7rem;
            border-bottom: 3px solid transparent;
            cursor: pointer;
        }
        .tab-btn.active {
            color: #0f73c6;
            border-color: #0f73c6;
        }
        .tab-panel {
            display: none;
        }
        .tab-panel.active {
            display: block;
        }
        .guide-box {
            background: #eff8ff;
            border: 1px solid #b9def9;
            border-radius: 12px;
            padding: 0.95rem 1rem;
            color: #36516e;
            margin-bottom: 0.95rem;
            font-size: 0.95rem;
        }
        .guide-title {
            font-weight: 700;
            color: #0c5f9e;
            margin-bottom: 0.45rem;
        }
        .input-grid {
            display: flex;
            gap: 0.75rem;
            align-items: center;
            flex-wrap: wrap;
        }
        .upload-input {
            border: 1px solid #ccd7e4;
            border-radius: 9px;
            background: #f7fafc;
            padding: 0.42rem;
            min-width: 280px;
        }
        .sample-select {
            width: 100%;
            border-radius: 10px;
            border: 1px solid #d6deea;
            font-size: 1.2rem;
            color: #374b60;
            background: #eef2f7;
            padding: 0.58rem 0.8rem;
        }
        .audio-wrap {
            margin-top: 0.8rem;
            width: 100%;
            max-width: 100%;
            border-radius: 999px;
            background: #e8edf4;
            padding: 0.3rem 0.65rem;
        }
        audio {
            width: 100%;
            height: 38px;
        }
        .sample-btn {
            margin-top: 0.95rem;
            width: 100%;
            border: 1px solid #cfd9e5;
            background: #f2f5f8;
            color: #233548;
            border-radius: 11px;
            padding: 0.72rem 0.9rem;
            font-size: 2.1vmin;
            cursor: pointer;
        }
        .btn {
            border: 1px solid #0b77bb;
            background: #0b77bb;
            color: #fff;
            border-radius: 10px;
            padding: 0.57rem 1rem;
            font-weight: 600;
            cursor: pointer;
        }
        .btn.secondary {
            border-color: #6d46e8;
            background: #6d46e8;
        }
        .btn:disabled {
            opacity: 0.55;
            cursor: not-allowed;
        }
        .status {
            margin-top: 0.6rem;
            font-size: 0.96rem;
            color: var(--muted);
            min-height: 1.2rem;
        }
        .error {
            color: var(--danger);
            background: var(--danger-bg);
            border: 1px solid #fecdd3;
            border-radius: 10px;
            padding: 0.65rem;
            margin-top: 0.7rem;
        }
        #resultsPanel { display: none; }
        .result-card {
            background: linear-gradient(135deg, #dbeafe 0%, #ede9fe 100%);
            border: 1px solid #5f96ff;
            border-radius: 14px;
            padding: 1.9rem;
            text-align: center;
            margin-bottom: 1.2rem;
        }
        .result-label {
            font-size: 1.35rem;
            letter-spacing: 0.12em;
            color: #6a7c90;
            text-transform: uppercase;
        }
        .result-class {
            margin: 0.58rem 0 0;
            color: #0d6ba9;
            font-size: clamp(2.1rem, 4vmin, 3.4rem);
            font-weight: 800;
        }
        .result-prob {
            margin-top: 0.58rem;
            color: #6d46e8;
            font-size: clamp(1.35rem, 2.6vmin, 2rem);
            font-weight: 600;
        }
        .bar-row { margin-top: 0.9rem; }
        .bar-label {
            display: flex;
            justify-content: space-between;
            font-size: 1.1rem;
            margin-bottom: 0.25rem;
            color: #3a4b5c;
        }
        .bar-bg {
            height: 14px;
            border-radius: 999px;
            background: #cfd6df;
            overflow: hidden;
        }
        .bar-fill {
            height: 14px;
            border-radius: 999px;
            background: linear-gradient(90deg, #0284c7, #7c3aed);
        }
        .all-prob-list {
            margin-top: 0.75rem;
            max-height: 420px;
            overflow-y: auto;
            padding-right: 0.25rem;
        }
        .all-prob-row {
            margin-bottom: 0.48rem;
        }
        .all-prob-head {
            display: flex;
            justify-content: space-between;
            font-size: 0.98rem;
            color: #3f5061;
            margin-bottom: 0.16rem;
        }
        .all-prob-bg {
            height: 8px;
            border-radius: 999px;
            background: #d9e0e8;
            overflow: hidden;
        }
        .all-prob-fill {
            height: 8px;
            border-radius: 999px;
            background: #0b86c8;
        }
        @media (max-width: 760px) {
            .section-title { font-size: 1.14rem; }
            .badge { font-size: 0.85rem; }
            .hero p { font-size: 0.98rem; }
            .sample-select { font-size: 1.18rem; }
            .sample-btn { font-size: 0.95rem; }
            .upload-input { min-width: 100%; }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="hero">
            <h1><span class="hero-icon">🔊</span>SoundEdge</h1>
            <p>Environmental Sound Classification - upload a short audio clip and let the model identify the sound.</p>
        </div>

        <div class="panel section">
            <div class="section-title">Supported Sound Classes</div>
            <div class="badge-grid">
                {% for cls in class_badges %}
                    <span class="badge">{{ cls }}</span>
                {% endfor %}
            </div>
        </div>

        <div class="panel section">
            <div class="section-title">Select Model</div>
            <div class="model-row">
                <label><input type="radio" name="model" value="Original" checked> Original</label>
                <label><input type="radio" name="model" value="Compressed"> Compressed</label>
            </div>
        </div>

        <div class="panel section">
            <div class="section-title">Choose Audio Input</div>
            <div class="tabs">
                <button id="tabUpload" class="tab-btn active" type="button">⬆️ Upload a File</button>
                <button id="tabSample" class="tab-btn" type="button">🎵 Try a Sample</button>
            </div>

            <div id="uploadPanel" class="tab-panel active">
                <div class="guide-box">
                    <div class="guide-title">Upload Guide</div>
                    <div>Format: WAV (.wav) only.</div>
                    <div>Duration: Around 5 seconds gives best results.</div>
                    <div>Size: Keep under 5 MB.</div>
                </div>
                <div class="input-grid">
                    <input id="uploadInput" class="upload-input" type="file" accept=".wav,audio/wav" />
                    <button id="uploadBtn" class="btn" type="button">Upload</button>
                    <button id="predictUploadedBtn" class="btn secondary" type="button" disabled>Classify Uploaded Audio</button>
                </div>
                <div class="audio-wrap" id="uploadedAudioWrap" style="display:none;">
                    <audio id="uploadedAudio" controls></audio>
                </div>
                <div id="uploadStatus" class="status"></div>
                <div id="uploadError"></div>
            </div>

            <div id="samplePanel" class="tab-panel">
                {% if samples %}
                    <select id="sampleSelect" class="sample-select">
                        {% for sample in samples %}
                            <option value="{{ sample.name }}">{{ sample.label }}</option>
                        {% endfor %}
                    </select>
                    <div class="audio-wrap">
                        <audio id="sampleAudio" controls></audio>
                    </div>
                    <button id="predictSampleBtn" class="sample-btn" type="button">Classify this sample ></button>
                {% else %}
                    <div class="status">No sample files found in the samples folder.</div>
                {% endif %}
                <div id="sampleStatus" class="status"></div>
            </div>
        </div>

        <div id="resultsPanel" class="panel section"></div>
    </div>

    <script>
        let uploadedFileId = null;
        const uploadInput = document.getElementById("uploadInput");
        const uploadBtn = document.getElementById("uploadBtn");
        const predictUploadedBtn = document.getElementById("predictUploadedBtn");
        const uploadStatus = document.getElementById("uploadStatus");
        const uploadError = document.getElementById("uploadError");
        const uploadedAudio = document.getElementById("uploadedAudio");
        const sampleSelect = document.getElementById("sampleSelect");
        const sampleAudio = document.getElementById("sampleAudio");
        const sampleStatus = document.getElementById("sampleStatus");
        const predictSampleBtn = document.getElementById("predictSampleBtn");
        const resultsPanel = document.getElementById("resultsPanel");
        const uploadedAudioWrap = document.getElementById("uploadedAudioWrap");
        const tabUpload = document.getElementById("tabUpload");
        const tabSample = document.getElementById("tabSample");
        const uploadPanel = document.getElementById("uploadPanel");
        const samplePanel = document.getElementById("samplePanel");

        function currentModel() {
            const selected = document.querySelector('input[name="model"]:checked');
            return selected ? selected.value : "Original";
        }

        function setError(target, message) {
            target.innerHTML = message ? `<div class="error">${message}</div>` : "";
        }

        function setTab(mode) {
            const isUpload = mode === "upload";
            tabUpload?.classList.toggle("active", isUpload);
            tabSample?.classList.toggle("active", !isUpload);
            uploadPanel?.classList.toggle("active", isUpload);
            samplePanel?.classList.toggle("active", !isUpload);
        }

        function renderResult(result) {
            resultsPanel.style.display = "block";
            if (result.lowConfidence) {
                resultsPanel.innerHTML = `
                    <div class="error" style="margin-top:0;">
                        Unable to confidently identify the sound. Please upload a clearer clip and try again.
                    </div>
                `;
                return;
            }

            let top3Html = "";
            result.top3.forEach(item => {
                top3Html += `
                    <div class="bar-row">
                        <div class="bar-label"><span>${item.classLabel}</span><span>${item.probabilityPct.toFixed(2)}%</span></div>
                        <div class="bar-bg"><div class="bar-fill" style="width:${item.probabilityPct}%;"></div></div>
                    </div>
                `;
            });

            let allHtml = "";
            result.allProbs.forEach(item => {
                allHtml += `
                    <div class="all-prob-row">
                        <div class="all-prob-head"><span>${item.classLabel}</span><span>${item.probabilityPct.toFixed(1)}%</span></div>
                        <div class="all-prob-bg"><div class="all-prob-fill" style="width:${item.probabilityPct}%;"></div></div>
                    </div>
                `;
            });

            resultsPanel.innerHTML = `
                <div class="result-card">
                    <div class="result-label">Predicted Sound</div>
                    <h2 class="result-class">${result.topClassLabel}</h2>
                    <div class="result-prob">Confidence ${result.topProbabilityPct.toFixed(1)}%</div>
                </div>
                <div class="section-title" style="margin-top:1rem;">Top 3 Predictions</div>
                ${top3Html}
                <div class="section-title" style="margin-top:1.2rem;">All Class Probabilities</div>
                <div class="all-prob-list">${allHtml}</div>
            `;
        }

        async function predict(payload, statusEl) {
            statusEl.textContent = "Analysing audio...";
            const resp = await fetch("/predict", {
                method: "POST",
                headers: { "Content-Type": "application/json" },
                body: JSON.stringify({ ...payload, model: currentModel() })
            });
            const data = await resp.json();
            if (!resp.ok) {
                throw new Error(data.error || "Prediction failed.");
            }
            statusEl.textContent = "Classification completed.";
            renderResult(data);
        }

        uploadInput?.addEventListener("change", () => {
            setError(uploadError, "");
            uploadStatus.textContent = "";
            uploadedFileId = null;
            predictUploadedBtn.disabled = true;

            const file = uploadInput.files && uploadInput.files[0];
            if (!file) {
                uploadedAudioWrap.style.display = "none";
                return;
            }
            const objectUrl = URL.createObjectURL(file);
            uploadedAudio.src = objectUrl;
            uploadedAudioWrap.style.display = "block";
        });

        uploadBtn?.addEventListener("click", async () => {
            setError(uploadError, "");
            uploadStatus.textContent = "";
            const file = uploadInput.files && uploadInput.files[0];
            if (!file) {
                setError(uploadError, "Select a WAV file before uploading.");
                return;
            }

            const formData = new FormData();
            formData.append("file", file);

            try {
                uploadStatus.textContent = "Uploading...";
                const resp = await fetch("/upload", { method: "POST", body: formData });
                const data = await resp.json();
                if (!resp.ok) {
                    throw new Error(data.error || "Upload failed.");
                }
                uploadedFileId = data.fileId;
                uploadedAudio.src = data.audioUrl;
                uploadedAudioWrap.style.display = "block";
                predictUploadedBtn.disabled = false;
                uploadStatus.textContent = `Uploaded: ${data.filename}`;
            } catch (err) {
                setError(uploadError, err.message || "Upload failed.");
                uploadStatus.textContent = "";
            }
        });

        predictUploadedBtn?.addEventListener("click", async () => {
            setError(uploadError, "");
            if (!uploadedFileId) {
                setError(uploadError, "Upload a file first.");
                return;
            }
            try {
                await predict({ source: "upload", fileId: uploadedFileId }, uploadStatus);
            } catch (err) {
                setError(uploadError, err.message || "Prediction failed.");
                uploadStatus.textContent = "";
            }
        });

        if (sampleSelect && sampleAudio) {
            function syncSampleAudio() {
                sampleAudio.src = `/samples/${encodeURIComponent(sampleSelect.value)}`;
            }
            sampleSelect.addEventListener("change", syncSampleAudio);
            syncSampleAudio();
        }

        tabUpload?.addEventListener("click", () => setTab("upload"));
        tabSample?.addEventListener("click", () => setTab("sample"));
        setTab("sample");

        predictSampleBtn?.addEventListener("click", async () => {
            sampleStatus.textContent = "";
            try {
                await predict({ source: "sample", sampleName: sampleSelect.value }, sampleStatus);
            } catch (err) {
                sampleStatus.textContent = err.message || "Prediction failed.";
            }
        });
    </script>
</body>
</html>
"""


if __name__ == "__main__":
    port = int(os.environ.get("PORT", "8501"))
    host = os.environ.get("HOST", "127.0.0.1")
    app.run(host=host, port=port, debug=False)