| 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 | |