File size: 762 Bytes
587fe4f
1e7709f
 
 
 
587fe4f
 
2da4544
1e7709f
 
587fe4f
 
 
 
2da4544
587fe4f
1e7709f
 
 
 
587fe4f
 
1e7709f
587fe4f
 
1e7709f
587fe4f
 
1e7709f
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
import requests
import uuid
from config import GROQ_API_KEY, GROQ_TTS_MODEL

def text_to_speech(text: str, voice: str, fmt: str):
    if not GROQ_API_KEY:
        raise RuntimeError("GROQ_API_KEY is not set in config")

    url = "https://api.groq.ai/openai/v1/audio/speech"

    headers = {
        "Authorization": f"Bearer {GROQ_API_KEY}",
        "Content-Type": "application/json"
    }

    payload = {
        "model": GROQ_TTS_MODEL,
        "voice": voice,
        "format": fmt,
        "input": text
    }

    output_file = f"audio_{uuid.uuid4()}.{fmt}"

    response = requests.post(url, headers=headers, json=payload)
    response.raise_for_status()

    with open(output_file, "wb") as f:
        f.write(response.content)

    return output_file