Obiang commited on
Commit
fc2c77a
·
1 Parent(s): 47ce372

Add API Documentation tab with examples and usage guide

Browse files
Files changed (1) hide show
  1. app.py +247 -4
app.py CHANGED
@@ -421,7 +421,7 @@ with gr.Blocks(css=custom_css, title="Pro-TeVA Tone Recognition") as demo:
421
  show_label=True
422
  )
423
 
424
- # Hidden JSON API interface
425
  json_api = gr.Interface(
426
  fn=predict_tone_json,
427
  inputs=gr.Audio(type="filepath", label="Audio File"),
@@ -431,10 +431,253 @@ with gr.Blocks(css=custom_css, title="Pro-TeVA Tone Recognition") as demo:
431
  description="Returns pure JSON response for programmatic access"
432
  )
433
 
434
- # Combine both interfaces
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
435
  app = gr.TabbedInterface(
436
- [demo, json_api],
437
- ["Tone Recognition", "JSON API"],
438
  title="Pro-TeVA: Prototype-based Explainable Tone Recognition for Yoruba"
439
  )
440
 
 
421
  show_label=True
422
  )
423
 
424
+ # JSON API interface
425
  json_api = gr.Interface(
426
  fn=predict_tone_json,
427
  inputs=gr.Audio(type="filepath", label="Audio File"),
 
431
  description="Returns pure JSON response for programmatic access"
432
  )
433
 
434
+ # API Documentation tab
435
+ with gr.Blocks() as api_docs:
436
+ gr.Markdown(
437
+ """
438
+ # API Documentation
439
+
440
+ Pro-TeVA provides two API endpoints for programmatic access to tone recognition.
441
+
442
+ ---
443
+
444
+ ## Available Endpoints
445
+
446
+ | Endpoint | Description | Output Type |
447
+ |----------|-------------|-------------|
448
+ | `/predict` | UI endpoint with visualizations | Text + Plots |
449
+ | `/predict_json` | Pure JSON for APIs | Structured JSON |
450
+
451
+ ---
452
+
453
+ ## JSON API Endpoint (Recommended)
454
+
455
+ **Endpoint:** `/predict_json`
456
+
457
+ This is the recommended endpoint for programmatic access as it returns pure JSON data.
458
+
459
+ ### Input
460
+
461
+ - **audio_file**: Audio file (WAV, MP3, FLAC)
462
+ - Recommended: 16kHz sample rate, mono
463
+ - Max duration: ~10 seconds
464
+
465
+ ### Output Schema
466
+
467
+ ```json
468
+ {
469
+ "success": true,
470
+ "tone_sequence": [
471
+ {
472
+ "index": 1,
473
+ "label": "H",
474
+ "name": "High Tone",
475
+ "symbol": "◌́"
476
+ }
477
+ ],
478
+ "tone_string": "H → B → M",
479
+ "statistics": {
480
+ "total_tones": 3,
481
+ "word_boundaries": 1,
482
+ "sequence_length": 4,
483
+ "high_tones": 1,
484
+ "low_tones": 1,
485
+ "mid_tones": 1
486
+ },
487
+ "f0_data": {
488
+ "extracted": [120.5, 125.3, ...],
489
+ "predicted": [118.2, 123.8, ...],
490
+ "length": 100
491
+ },
492
+ "settings": {
493
+ "space_detection_enabled": true,
494
+ "space_detection_method": "combined"
495
+ }
496
+ }
497
+ ```
498
+
499
+ ---
500
+
501
+ ## Python Examples
502
+
503
+ ### Installation
504
+
505
+ ```bash
506
+ pip install gradio_client
507
+ ```
508
+
509
+ ### Basic Usage
510
+
511
+ ```python
512
+ from gradio_client import Client
513
+
514
+ # Connect to Pro-TeVA
515
+ client = Client("https://huggingface.co/spaces/Obiang/Pro-TeVA")
516
+
517
+ # Get JSON response
518
+ result = client.predict(
519
+ audio_file="path/to/audio.wav",
520
+ api_name="/predict_json"
521
+ )
522
+
523
+ # Parse results
524
+ print(f"Success: {result['success']}")
525
+ print(f"Tones: {result['tone_string']}")
526
+ print(f"Statistics: {result['statistics']}")
527
+ ```
528
+
529
+ ### Batch Processing
530
+
531
+ ```python
532
+ from gradio_client import Client
533
+
534
+ client = Client("https://huggingface.co/spaces/Obiang/Pro-TeVA")
535
+
536
+ audio_files = ["audio1.wav", "audio2.wav", "audio3.wav"]
537
+
538
+ for audio_path in audio_files:
539
+ result = client.predict(
540
+ audio_file=audio_path,
541
+ api_name="/predict_json"
542
+ )
543
+
544
+ if result['success']:
545
+ print(f"{audio_path}: {result['tone_string']}")
546
+ else:
547
+ print(f"{audio_path}: Error - {result['error']}")
548
+ ```
549
+
550
+ ---
551
+
552
+ ## cURL Examples
553
+
554
+ ### Step 1: Submit Request
555
+
556
+ ```bash
557
+ curl -X POST "https://Obiang-Pro-TeVA.hf.space/call/predict_json" \\
558
+ -H "Content-Type: application/json" \\
559
+ -d '{
560
+ "data": ["https://example.com/audio.wav"]
561
+ }'
562
+ ```
563
+
564
+ **Response:**
565
+ ```json
566
+ {"event_id": "abc123def456"}
567
+ ```
568
+
569
+ ### Step 2: Get Results
570
+
571
+ ```bash
572
+ curl -N "https://Obiang-Pro-TeVA.hf.space/call/predict_json/abc123def456"
573
+ ```
574
+
575
+ **Response (Server-Sent Events):**
576
+ ```
577
+ event: complete
578
+ data: {"success": true, "tone_sequence": [...], ...}
579
+ ```
580
+
581
+ ### One-liner with jq
582
+
583
+ ```bash
584
+ # Submit and get event_id
585
+ EVENT_ID=$(curl -s -X POST "https://Obiang-Pro-TeVA.hf.space/call/predict_json" \\
586
+ -H "Content-Type: application/json" \\
587
+ -d '{"data": ["audio.wav"]}' | jq -r '.event_id')
588
+
589
+ # Get results
590
+ curl -N "https://Obiang-Pro-TeVA.hf.space/call/predict_json/$EVENT_ID"
591
+ ```
592
+
593
+ ---
594
+
595
+ ## JavaScript Example
596
+
597
+ ```javascript
598
+ import { client } from "@gradio/client";
599
+
600
+ async function predictTones(audioBlob) {
601
+ const app = await client("https://huggingface.co/spaces/Obiang/Pro-TeVA");
602
+
603
+ const result = await app.predict("/predict_json", {
604
+ audio_file: audioBlob
605
+ });
606
+
607
+ console.log("Tones:", result.data.tone_string);
608
+ console.log("Statistics:", result.data.statistics);
609
+
610
+ return result.data;
611
+ }
612
+ ```
613
+
614
+ ---
615
+
616
+ ## Error Handling
617
+
618
+ ### Error Response Schema
619
+
620
+ ```json
621
+ {
622
+ "success": false,
623
+ "error": "Error message here",
624
+ "traceback": "Full error traceback..."
625
+ }
626
+ ```
627
+
628
+ ### Python Error Handling
629
+
630
+ ```python
631
+ from gradio_client import Client
632
+
633
+ try:
634
+ client = Client("https://huggingface.co/spaces/Obiang/Pro-TeVA")
635
+ result = client.predict(
636
+ audio_file="audio.wav",
637
+ api_name="/predict_json"
638
+ )
639
+
640
+ if result['success']:
641
+ print(f"Tones: {result['tone_string']}")
642
+ else:
643
+ print(f"Error: {result['error']}")
644
+
645
+ except Exception as e:
646
+ print(f"Connection error: {str(e)}")
647
+ ```
648
+
649
+ ---
650
+
651
+ ## Rate Limits
652
+
653
+ - Hugging Face Spaces: Standard rate limits apply
654
+ - Free tier: Suitable for development and testing
655
+ - For high-volume usage: Consider deploying your own instance
656
+
657
+ ---
658
+
659
+ ## Tone Labels Reference
660
+
661
+ | Index | Label | Name | Symbol |
662
+ |-------|-------|------|--------|
663
+ | 0 | BLANK | CTC Blank | - |
664
+ | 1 | H | High Tone | ◌́ |
665
+ | 2 | B | Low Tone | ◌̀ |
666
+ | 3 | M | Mid Tone | ◌ |
667
+ | 4 | SPACE | Word Boundary | \\| |
668
+
669
+ ---
670
+
671
+ ## Support
672
+
673
+ For questions or issues, please open an issue on the repository or check the README for more details.
674
+ """
675
+ )
676
+
677
+ # Combine all interfaces
678
  app = gr.TabbedInterface(
679
+ [demo, json_api, api_docs],
680
+ ["Tone Recognition", "JSON API", "API Documentation"],
681
  title="Pro-TeVA: Prototype-based Explainable Tone Recognition for Yoruba"
682
  )
683