File size: 4,297 Bytes
3e08670
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Exemples d'utilisation de l'API MMS ASR/TTS"""

import requests
import json

BASE_URL = "http://localhost:7860"

# Exemples de textes dans différentes langues
EXAMPLES = {
    "beh": {
        "text": "Àbọ̀ wa",
        "translation": "Hello"
    },
    "bba": {
        "text": "A gbà kú",
        "translation": "Good morning"
    },
    "ddn": {
        "text": "Sàlaam alaikum",
        "translation": "Peace be upon you"
    },
    "ewe": {
        "text": "Woé gbé o",
        "translation": "Hello"
    },
    "gej": {
        "text": "A-kúma",
        "translation": "Good morning"
    },
    "tbz": {
        "text": "Salaam",
        "translation": "Hello"
    },
    "yor": {
        "text": "Àbọ̀ wa",
        "translation": "Hello"
    },
    "eng": {
        "text": "Hello world",
        "translation": "Hello world"
    }
}

def test_tts():
    """Test TTS pour toutes les langues"""
    print("=" * 60)
    print("TEST TTS (Text-to-Speech)")
    print("=" * 60)

    for lang, data in EXAMPLES.items():
        print(f"\n📢 Langue: {lang} ({data['translation']})")
        print(f"   Texte: {data['text']}")

        try:
            response = requests.post(
                f"{BASE_URL}/tts",
                json={"text": data["text"], "language": lang},
                timeout=60
            )

            if response.status_code == 200:
                result = response.json()
                audio_size = len(result["audio"]) // 2  # hex = 2 chars per byte
                print(f"   ✅ Audio généré: {audio_size} bytes")
                print(f"   Sample rate: {result['sample_rate']} Hz")

                # Sauvegarde l'audio
                with open(f"output_{lang}.wav", "wb") as f:
                    f.write(bytes.fromhex(result["audio"]))
                print(f"   Fichier: output_{lang}.wav")
            else:
                print(f"   ❌ Erreur {response.status_code}: {response.json()}")
        except Exception as e:
            print(f"   ❌ Exception: {e}")

def test_health():
    """Teste la santé du service"""
    print("=" * 60)
    print("TEST SANTÉ DU SERVICE")
    print("=" * 60)

    try:
        response = requests.get(f"{BASE_URL}/health")
        data = response.json()
        print(f"✅ Status: {data['status']}")
        print(f"   Device: {data['device']}")
    except Exception as e:
        print(f"❌ Erreur: {e}")

def test_supported_languages():
    """Liste les langues supportées"""
    print("=" * 60)
    print("LANGUES SUPPORTÉES")
    print("=" * 60)

    try:
        response = requests.get(f"{BASE_URL}/supported-languages")
        data = response.json()
        print(f"\n🎤 ASR: {data['asr']}")
        print(f"\n📢 TTS: {', '.join(data['tts'])}")
        print(f"\n📝 Codes de langue:")
        for code, name in data['language_codes'].items():
            print(f"   {code:5} -> {name}")
    except Exception as e:
        print(f"❌ Erreur: {e}")

def test_asr_sample():
    """Test ASR (nécessite un fichier audio)"""
    print("\n" + "=" * 60)
    print("TEST ASR (Automatic Speech Recognition)")
    print("=" * 60)

    audio_path = "sample_audio.wav"
    try:
        with open(audio_path, "rb") as f:
            files = {"audio": f}
            response = requests.post(
                f"{BASE_URL}/asr",
                files=files,
                data={"language": "eng"},
                timeout=60
            )

        if response.status_code == 200:
            result = response.json()
            print(f"✅ Transcription: {result['transcription']}")
            print(f"   Langue: {result['language']}")
            print(f"   Durée: {result['audio_length']:.2f}s")
        else:
            print(f"❌ Erreur {response.status_code}: {response.json()}")
    except FileNotFoundError:
        print(f"⚠️  Fichier audio introuvable: {audio_path}")
        print("   Pour tester l'ASR, fournir un fichier audio valide")
    except Exception as e:
        print(f"❌ Erreur: {e}")

if __name__ == "__main__":
    print("\n🚀 MMS ASR/TTS API - Exemples d'utilisation\n")

    # Tests
    test_health()
    test_supported_languages()
    test_tts()
    test_asr_sample()

    print("\n" + "=" * 60)
    print("Tests terminés!")
    print("=" * 60)