Spaces:
Running
Running
| """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) | |