File size: 22,365 Bytes
62cb0ac
ee09b74
62cb0ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee09b74
62cb0ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e1d7139
 
 
62cb0ac
 
 
e1d7139
 
62cb0ac
e1d7139
62cb0ac
e1d7139
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62cb0ac
 
e1d7139
62cb0ac
e1d7139
62cb0ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e1d7139
62cb0ac
 
e1d7139
62cb0ac
 
1ee1e2b
 
62cb0ac
 
 
 
e1d7139
 
62cb0ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e1d7139
62cb0ac
 
 
 
e1d7139
62cb0ac
47ce372
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e77301
4ac44ba
 
2e77301
4ac44ba
 
 
 
 
 
2e77301
 
 
 
47ce372
2e77301
47ce372
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62cb0ac
 
 
 
 
 
 
 
 
 
 
 
ee09b74
62cb0ac
c2b4e33
 
 
 
a172f6f
62cb0ac
c2b4e33
62cb0ac
c2b4e33
 
 
 
 
62cb0ac
c2b4e33
 
 
 
 
 
 
62cb0ac
 
 
 
 
 
 
 
 
 
 
 
c2b4e33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62cb0ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e1d7139
 
62cb0ac
 
 
 
 
 
 
e1d7139
62cb0ac
 
ee09b74
 
 
 
 
62cb0ac
ee09b74
62cb0ac
ee09b74
62cb0ac
ee09b74
 
 
 
 
62cb0ac
ee09b74
 
 
 
 
 
 
62cb0ac
ee09b74
 
 
 
 
e69dae9
 
 
 
 
ee09b74
 
 
 
 
 
 
 
 
 
 
 
62cb0ac
4ac44ba
 
47ce372
 
4ac44ba
47ce372
 
 
4ac44ba
47ce372
 
fc2c77a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47ce372
fc2c77a
 
47ce372
 
62cb0ac
 
47ce372
62cb0ac
 
e69dae9
 
62cb0ac
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
"""
Gradio App for Pro-TeVA Yoruba Tone Recognition
Hugging Face Spaces deployment
"""

import gradio as gr
from speechbrain.inference.interfaces import foreign_class
import numpy as np
import matplotlib.pyplot as plt
import torch
import config

# ============ CONFIGURATION ============

# Import tone info from config
TONE_INFO = config.TONE_INFO

# ============ MODEL LOADING ============

print("Loading Pro-TeVA tone recognition model...")
print(f"Checkpoint folder: {config.CHECKPOINT_FOLDER}")

try:
    tone_recognizer = foreign_class(
        source="./",
        pymodule_file="custom_interface.py",
        classname="ProTeVaToneRecognizer",
        hparams_file="inference.yaml",
        savedir=config.PRETRAINED_MODEL_DIR
    )
    print("βœ“ Model loaded successfully!")

    # Validate configuration
    if config.validate_config():
        print(f"βœ“ Space detection: {'ENABLED' if config.ENABLE_SPACE_DETECTION else 'DISABLED'}")
        if config.ENABLE_SPACE_DETECTION:
            print(f"  Method: {config.SPACE_DETECTION_METHOD}")
except Exception as e:
    print(f"βœ— Error loading model: {e}")
    tone_recognizer = None

# ============ HELPER FUNCTIONS ============

def format_tone_sequence(tone_indices, tone_names):
    """Format tone sequence with colors and symbols"""
    if not tone_indices:
        return "No tones detected"

    formatted = []
    for idx, name in zip(tone_indices, tone_names):
        info = config.get_tone_info(idx)
        formatted.append(f"{info['name']} ({info['symbol']})")

    return " β†’ ".join(formatted)

def create_f0_comparison_plot(f0_extracted, f0_predicted):
    """Create F0 comparison plot showing both extracted and predicted contours"""
    if f0_extracted is None or f0_predicted is None:
        return None

    # Convert to numpy
    if isinstance(f0_extracted, torch.Tensor):
        f0_extracted_numpy = f0_extracted.cpu().numpy().flatten()
    else:
        f0_extracted_numpy = np.array(f0_extracted).flatten()

    if isinstance(f0_predicted, torch.Tensor):
        f0_predicted_numpy = f0_predicted.cpu().numpy().flatten()
    else:
        f0_predicted_numpy = np.array(f0_predicted).flatten()

    # Create plot with both contours
    fig, ax = plt.subplots(figsize=(12, 5))

    # Normalized time axis
    time_extracted = np.arange(len(f0_extracted_numpy)) / len(f0_extracted_numpy)
    time_predicted = np.arange(len(f0_predicted_numpy)) / len(f0_predicted_numpy)

    # Plot both F0 contours
    ax.plot(time_extracted, f0_extracted_numpy, linewidth=2.5, color='#3498db',
            label='Extracted F0 (TorchYIN)', alpha=0.8)
    ax.plot(time_predicted, f0_predicted_numpy, linewidth=2.5, color='#e74c3c',
            linestyle='--', label='Predicted F0 (Decoder)', alpha=0.8)

    # Configure plot
    ax.set_xlabel('Normalized Time', fontsize=12)
    ax.set_ylabel('F0 (Hz)', fontsize=12)
    ax.set_title('F0 Comparison: Extracted vs Predicted', fontsize=14, fontweight='bold')
    ax.grid(True, alpha=0.3)
    ax.legend(loc='upper right', fontsize=11, framealpha=0.9)
    plt.tight_layout()

    return fig

def create_tone_visualization(tone_indices):
    """Create visual representation of tone sequence"""
    if not tone_indices:
        return None

    fig, ax = plt.subplots(figsize=(max(12, len(tone_indices) * 0.8), 3))

    # Prepare data
    x_positions = []
    colors = []
    labels = []

    position = 0
    for idx in tone_indices:
        info = config.get_tone_info(idx)

        # Space tokens get different visual treatment
        if idx == 4:
            # Draw vertical line for space
            ax.axvline(x=position - 0.25, color=info['color'],
                      linewidth=3, linestyle='--', alpha=0.7)
        else:
            x_positions.append(position)
            colors.append(info['color'])
            labels.append(info['symbol'])
            position += 1

    # Draw tone bars
    if x_positions:
        ax.bar(x_positions, [1] * len(x_positions), color=colors, alpha=0.7,
               edgecolor='black', linewidth=2, width=0.8)

        # Add tone symbols
        for i, (pos, label) in enumerate(zip(x_positions, labels)):
            ax.text(pos, 0.5, label, ha='center', va='center',
                   fontsize=20, fontweight='bold')

    # Configure plot
    if x_positions:
        ax.set_xlim(-0.5, max(x_positions) + 0.5)
    ax.set_ylim(0, 1.2)
    if x_positions:
        ax.set_xticks(x_positions)
        ax.set_xticklabels([f"T{i+1}" for i in range(len(x_positions))])
    ax.set_ylabel('Tone', fontsize=12)
    ax.set_title('Tone Sequence Visualization (| = word boundary)',
                 fontsize=14, fontweight='bold')
    ax.set_yticks([])
    plt.tight_layout()

    return fig

# ============ PREDICTION FUNCTION ============

def predict_tone(audio_file):
    """Main prediction function for Gradio interface"""
    if tone_recognizer is None:
        return "❌ Model not loaded. Please check configuration.", None, None, ""

    if audio_file is None:
        return "⚠️ Please provide an audio file", None, None, ""

    try:
        # Get predictions (now returns both F0 values)
        tone_indices, tone_names, f0_extracted, f0_predicted = tone_recognizer.classify_file(audio_file)

        # Format output
        tone_text = format_tone_sequence(tone_indices, tone_names)

        # Create visualizations - combined F0 comparison plot
        f0_comparison_plot = create_f0_comparison_plot(f0_extracted, f0_predicted)
        tone_viz = create_tone_visualization(tone_indices)

        # Create statistics
        num_tones = len([t for t in tone_indices if t != 4])
        num_spaces = len([t for t in tone_indices if t == 4])

        stats = f"""
πŸ“Š **Prediction Statistics:**
- Total tones detected: {num_tones}
- Word boundaries detected: {num_spaces}
- Sequence length: {len(tone_indices)}

🎡 **Tone Distribution:**
- High tones (H): {tone_indices.count(1)}
- Low tones (B): {tone_indices.count(2)}
- Mid tones (M): {tone_indices.count(3)}

βš™οΈ **Detection Settings:**
- Space detection: {'ENABLED' if config.ENABLE_SPACE_DETECTION else 'DISABLED'}
{f"- Method: {config.SPACE_DETECTION_METHOD}" if config.ENABLE_SPACE_DETECTION else ""}
        """

        return tone_text, f0_comparison_plot, tone_viz, stats

    except Exception as e:
        import traceback
        error_details = traceback.format_exc()
        return f"❌ Error during prediction: {str(e)}\n\n{error_details}", None, None, ""

# ============ JSON API FUNCTION ============

def predict_tone_json(audio_file):
    """API endpoint that returns pure JSON response for programmatic access"""
    if tone_recognizer is None:
        return {
            "success": False,
            "error": "Model not loaded. Please check configuration."
        }

    if audio_file is None:
        return {
            "success": False,
            "error": "No audio file provided"
        }

    try:
        # Handle different input types from Gradio API
        # gr.File returns the file path as a string
        if isinstance(audio_file, str):
            file_path = audio_file
        elif hasattr(audio_file, 'name'):
            # File-like object with name attribute
            file_path = audio_file.name
        elif isinstance(audio_file, dict):
            # FileData format from API - prefer 'path' over 'name'/'orig_name'
            file_path = audio_file.get('path') or audio_file.get('name', str(audio_file))
        else:
            # Try to get path attribute or convert to string
            file_path = getattr(audio_file, 'path', str(audio_file))

        # Get predictions
        tone_indices, tone_names, f0_extracted, f0_predicted = tone_recognizer.classify_file(file_path)

        # Convert F0 tensors to lists for JSON serialization
        if hasattr(f0_extracted, 'cpu'):
            f0_extracted_list = f0_extracted.cpu().numpy().flatten().tolist()
        else:
            f0_extracted_list = list(np.array(f0_extracted).flatten())

        if hasattr(f0_predicted, 'cpu'):
            f0_predicted_list = f0_predicted.cpu().numpy().flatten().tolist()
        else:
            f0_predicted_list = list(np.array(f0_predicted).flatten())

        # Build response
        return {
            "success": True,
            "tone_sequence": [
                {
                    "index": idx,
                    "label": name,
                    "name": config.get_tone_info(idx)["name"],
                    "symbol": config.get_tone_info(idx)["symbol"]
                }
                for idx, name in zip(tone_indices, tone_names)
            ],
            "tone_string": " β†’ ".join(tone_names),
            "statistics": {
                "total_tones": len([t for t in tone_indices if t != 4]),
                "word_boundaries": len([t for t in tone_indices if t == 4]),
                "sequence_length": len(tone_indices),
                "high_tones": tone_indices.count(1),
                "low_tones": tone_indices.count(2),
                "mid_tones": tone_indices.count(3)
            },
            "f0_data": {
                "extracted": f0_extracted_list,
                "predicted": f0_predicted_list,
                "length": len(f0_extracted_list)
            },
            "settings": {
                "space_detection_enabled": config.ENABLE_SPACE_DETECTION,
                "space_detection_method": config.SPACE_DETECTION_METHOD if config.ENABLE_SPACE_DETECTION else None
            }
        }

    except Exception as e:
        import traceback
        return {
            "success": False,
            "error": str(e),
            "traceback": traceback.format_exc()
        }

# ============ GRADIO INTERFACE ============

custom_css = """
.gradio-container {
    font-family: 'Arial', sans-serif;
}
.output-text {
    font-size: 18px;
    font-weight: bold;
}
"""

with gr.Blocks(css=custom_css, title="Pro-TeVA Tone Recognition") as demo:

    with gr.Row():
        with gr.Column(scale=2):
            gr.Markdown(
                f"""
                # Pro-TeVA: Prototype-based Explainable Tone Recognition for Yoruba

                Upload an audio file or record your voice to detect Yoruba tone patterns.

                **Yoruba Tones:**
                - **High Tone (H)** (β—ŒΜ): Syllable with high pitch
                - **Low Tone (B)** (β—ŒΜ€): Syllable with low pitch
                - **Mid Tone (M)** (β—Œ): Syllable with neutral/middle pitch
                - **Space ( | )**: Word boundary (detected automatically)

                **Space Detection:** {config.SPACE_DETECTION_METHOD if config.ENABLE_SPACE_DETECTION else 'OFF'}
                """
            )

        with gr.Column(scale=1):
            gr.Markdown("### 🎧 Audio Examples")
            gr.Markdown("**Click on an example to load it**")

    with gr.Row():
        with gr.Column(scale=1):
            gr.Markdown("### 🎀 Input Audio")

            audio_input = gr.Audio(
                sources=["microphone", "upload"],
                type="filepath",
                label="Record or Upload Audio",
                waveform_options={"show_controls": True}
            )

            # Female voice examples
            gr.Markdown("**πŸ‘© Female Voice (Yof):**")
            gr.Examples(
                examples=[
                    ["examples/yof_00295_00024634140.wav"],
                    ["examples/yof_00295_00151151204.wav"],
                    ["examples/yof_00295_00427144639.wav"],
                    ["examples/yof_00295_00564596981.wav"],
                ],
                inputs=audio_input,
                label="",
                examples_per_page=4
            )

            # Male voice examples
            gr.Markdown("**πŸ‘¨ Male Voice (Yom):**")
            gr.Examples(
                examples=[
                    ["examples/yom_08784_01544027142.wav"],
                    ["examples/yom_08784_01792196659.wav"],
                    ["examples/yom_09334_00045442417.wav"],
                    ["examples/yom_09334_00091591408.wav"],
                ],
                inputs=audio_input,
                label="",
                examples_per_page=4
            )

            predict_btn = gr.Button("πŸ” Predict Tones", variant="primary", size="lg")

            gr.Markdown(
                """
                ### πŸ“ Tips:
                - Speak clearly in Yoruba
                - Keep recordings under 10 seconds
                - Avoid background noise
                - Pause slightly between words for better boundary detection
                """
            )

        with gr.Column(scale=2):
            gr.Markdown("### 🎯 Results")

            tone_output = gr.Textbox(
                label="Predicted Tone Sequence",
                lines=3,
                elem_classes="output-text"
            )

            stats_output = gr.Markdown(label="Statistics")

            with gr.Tabs():
                with gr.Tab("F0 Comparison"):
                    f0_comparison_plot = gr.Plot(label="Extracted vs Predicted F0")

                with gr.Tab("Tone Visualization"):
                    tone_viz = gr.Plot(label="Tone Sequence")

    predict_btn.click(
        fn=predict_tone,
        inputs=audio_input,
        outputs=[tone_output, f0_comparison_plot, tone_viz, stats_output]
    )

    with gr.Row():
        with gr.Column(scale=1):
            gr.Markdown(
                f"""
                ---

                **About Pro-TeVA:**

                **Pro-TeVA** (Prototype-based Temporal Variational Autoencoder) is an explainable neural model for tone recognition.

                Unlike black-box models, Pro-TeVA provides transparency through:
                - Interpretable F0 (pitch) features
                - Visualizable tone prototypes
                - F0 reconstruction for explainability
                - High performance: 17.74% Tone Error Rate

                **Model Architecture:**
                - Feature Extractor: HuBERT (Orange/SSA-HuBERT-base-60k)
                - Encoder: {config.RNN_LAYERS}-layer Bidirectional GRU ({config.RNN_NEURONS} neurons)
                - Variational Autoencoder: Compact latent representations
                - Prototype Layer: {config.N_PROTOTYPES} learnable tone prototypes
                - Decoder: F0 reconstruction (VanillaNN)
                - Output: CTC-based sequence prediction

                **Space Detection:**
                - Method: {config.SPACE_DETECTION_METHOD if config.ENABLE_SPACE_DETECTION else 'Disabled'}
                - Uses F0 contours, silence patterns, and tone duration
                - Automatically detects word boundaries in continuous speech

                **API Access:**
                - REST API enabled for programmatic access
                - Use Gradio client: `pip install gradio_client`
                - See README for full API documentation

                Built with ❀️ using SpeechBrain and Gradio

                **Model Checkpoint:** {config.CHECKPOINT_FOLDER}
                """
            )

        with gr.Column(scale=1):
            gr.Image(
                value="proteva_archi.png",
                label="Pro-TeVA Architecture",
                show_label=True
            )

    # JSON API interface - use gr.Textbox to receive raw file path from API
    # This avoids Gradio's file component preprocessing issues
    json_api = gr.Interface(
        fn=predict_tone_json,
        inputs=gr.Textbox(label="Audio File Path", placeholder="Path to uploaded audio file"),
        outputs=gr.JSON(label="Prediction Result"),
        api_name="predict_json",
        title="Pro-TeVA JSON API",
        description="Upload file first via /gradio_api/upload, then pass the returned path here"
    )

# API Documentation tab
with gr.Blocks() as api_docs:
    gr.Markdown(
        """
        # API Documentation

        Pro-TeVA provides two API endpoints for programmatic access to tone recognition.

        ---

        ## Available Endpoints

        | Endpoint | Description | Output Type |
        |----------|-------------|-------------|
        | `/predict` | UI endpoint with visualizations | Text + Plots |
        | `/predict_json` | Pure JSON for APIs | Structured JSON |

        ---

        ## JSON API Endpoint (Recommended)

        **Endpoint:** `/predict_json`

        This is the recommended endpoint for programmatic access as it returns pure JSON data.

        ### Input

        - **audio_file**: Audio file (WAV, MP3, FLAC)
        - Recommended: 16kHz sample rate, mono
        - Max duration: ~10 seconds

        ### Output Schema

        ```json
        {
          "success": true,
          "tone_sequence": [
            {
              "index": 1,
              "label": "H",
              "name": "High Tone",
              "symbol": "β—ŒΜ"
            }
          ],
          "tone_string": "H β†’ B β†’ M",
          "statistics": {
            "total_tones": 3,
            "word_boundaries": 1,
            "sequence_length": 4,
            "high_tones": 1,
            "low_tones": 1,
            "mid_tones": 1
          },
          "f0_data": {
            "extracted": [120.5, 125.3, ...],
            "predicted": [118.2, 123.8, ...],
            "length": 100
          },
          "settings": {
            "space_detection_enabled": true,
            "space_detection_method": "combined"
          }
        }
        ```

        ---

        ## Python Examples

        ### Installation

        ```bash
        pip install gradio_client
        ```

        ### Basic Usage

        ```python
        from gradio_client import Client

        # Connect to Pro-TeVA
        client = Client("https://huggingface.co/spaces/Obiang/Pro-TeVA")

        # Get JSON response
        result = client.predict(
            audio_file="path/to/audio.wav",
            api_name="/predict_json"
        )

        # Parse results
        print(f"Success: {result['success']}")
        print(f"Tones: {result['tone_string']}")
        print(f"Statistics: {result['statistics']}")
        ```

        ### Batch Processing

        ```python
        from gradio_client import Client

        client = Client("https://huggingface.co/spaces/Obiang/Pro-TeVA")

        audio_files = ["audio1.wav", "audio2.wav", "audio3.wav"]

        for audio_path in audio_files:
            result = client.predict(
                audio_file=audio_path,
                api_name="/predict_json"
            )

            if result['success']:
                print(f"{audio_path}: {result['tone_string']}")
            else:
                print(f"{audio_path}: Error - {result['error']}")
        ```

        ---

        ## cURL Examples

        ### Step 1: Submit Request

        ```bash
        curl -X POST "https://Obiang-Pro-TeVA.hf.space/call/predict_json" \\
          -H "Content-Type: application/json" \\
          -d '{
            "data": ["https://example.com/audio.wav"]
          }'
        ```

        **Response:**
        ```json
        {"event_id": "abc123def456"}
        ```

        ### Step 2: Get Results

        ```bash
        curl -N "https://Obiang-Pro-TeVA.hf.space/call/predict_json/abc123def456"
        ```

        **Response (Server-Sent Events):**
        ```
        event: complete
        data: {"success": true, "tone_sequence": [...], ...}
        ```

        ### One-liner with jq

        ```bash
        # Submit and get event_id
        EVENT_ID=$(curl -s -X POST "https://Obiang-Pro-TeVA.hf.space/call/predict_json" \\
          -H "Content-Type: application/json" \\
          -d '{"data": ["audio.wav"]}' | jq -r '.event_id')

        # Get results
        curl -N "https://Obiang-Pro-TeVA.hf.space/call/predict_json/$EVENT_ID"
        ```

        ---

        ## JavaScript Example

        ```javascript
        import { client } from "@gradio/client";

        async function predictTones(audioBlob) {
          const app = await client("https://huggingface.co/spaces/Obiang/Pro-TeVA");

          const result = await app.predict("/predict_json", {
            audio_file: audioBlob
          });

          console.log("Tones:", result.data.tone_string);
          console.log("Statistics:", result.data.statistics);

          return result.data;
        }
        ```

        ---

        ## Error Handling

        ### Error Response Schema

        ```json
        {
          "success": false,
          "error": "Error message here",
          "traceback": "Full error traceback..."
        }
        ```

        ### Python Error Handling

        ```python
        from gradio_client import Client

        try:
            client = Client("https://huggingface.co/spaces/Obiang/Pro-TeVA")
            result = client.predict(
                audio_file="audio.wav",
                api_name="/predict_json"
            )

            if result['success']:
                print(f"Tones: {result['tone_string']}")
            else:
                print(f"Error: {result['error']}")

        except Exception as e:
            print(f"Connection error: {str(e)}")
        ```

        ---

        ## Rate Limits

        - Hugging Face Spaces: Standard rate limits apply
        - Free tier: Suitable for development and testing
        - For high-volume usage: Consider deploying your own instance

        ---

        ## Tone Labels Reference

        | Index | Label | Name | Symbol |
        |-------|-------|------|--------|
        | 0 | BLANK | CTC Blank | - |
        | 1 | H | High Tone | β—ŒΜ |
        | 2 | B | Low Tone | β—ŒΜ€ |
        | 3 | M | Mid Tone | β—Œ |
        | 4 | SPACE | Word Boundary | \\| |

        ---

        ## Support

        For questions or issues, please open an issue on the repository or check the README for more details.
        """
    )

# Combine all interfaces
app = gr.TabbedInterface(
    [demo, json_api, api_docs],
    ["Tone Recognition", "JSON API", "API Documentation"],
    title="Pro-TeVA: Prototype-based Explainable Tone Recognition for Yoruba"
)

if __name__ == "__main__":
    app.launch(
        share=config.GRADIO_SHARE,
        server_name=config.GRADIO_SERVER_NAME,
        server_port=config.GRADIO_SERVER_PORT,
        show_api=config.ENABLE_API
    )