File size: 1,001 Bytes
587fe4f
 
 
c7fc3b6
587fe4f
 
 
 
 
 
 
 
2da4544
587fe4f
 
 
 
2da4544
587fe4f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# 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