# services/tts_service.py import requests from config import GROQ_API_KEY, GROQ_TTS_MODEL, GROQ_TTS_VOICE, GROQ_TTS_FORMAT def text_to_speech(text: str, output_file: str): """ Convertit du texte en audio via l'API Groq TTS. text : str -> texte à convertir output_file : str -> chemin du fichier de sortie (ex: 'output.wav') """ if not GROQ_API_KEY: raise RuntimeError("GROQ_API_KEY is not set in config") headers = { "Authorization": f"Bearer {GROQ_API_KEY}", "Content-Type": "application/json" } payload = { "text": text, "voice": GROQ_TTS_VOICE, "format": GROQ_TTS_FORMAT } url = f"https://api.groq.ai/v1/models/{GROQ_TTS_MODEL}/predict" response = requests.post(url, headers=headers, json=payload) response.raise_for_status() # Si erreur, lève une exception audio_content = response.content with open(output_file, "wb") as f: f.write(audio_content) return output_file