Yassine Mhirsi
feat: Introduce individual GROQ API keys for Topic, STT, TTS, and Chat services, enhancing configuration flexibility and service initialization checks across the application.
16153ee
| import requests | |
| import uuid | |
| import os | |
| from pathlib import Path | |
| from config import GROQ_TTS_API_KEY, GROQ_TTS_MODEL | |
| def text_to_speech( | |
| text: str, | |
| voice: str = "Aaliyah-PlayAI", | |
| fmt: str = "wav" | |
| ) -> str: | |
| """ | |
| Convert text to speech using Groq's TTS API (English only) | |
| """ | |
| if not GROQ_TTS_API_KEY: | |
| raise RuntimeError("GROQ_TTS_API_KEY is not set in config") | |
| if not text or not text.strip(): | |
| raise ValueError("Text cannot be empty") | |
| url = "https://api.groq.com/openai/v1/audio/speech" | |
| headers = { | |
| "Authorization": f"Bearer {GROQ_TTS_API_KEY}", | |
| "Content-Type": "application/json" | |
| } | |
| payload = { | |
| "model": GROQ_TTS_MODEL, | |
| "input": text.strip(), | |
| "voice": voice, | |
| "response_format": fmt | |
| } | |
| try: | |
| # Create temp directory for audio files | |
| temp_dir = Path("temp_audio") | |
| temp_dir.mkdir(exist_ok=True) | |
| # Unique filename | |
| output_filename = f"tts_{uuid.uuid4().hex[:8]}.{fmt}" | |
| output_path = temp_dir / output_filename | |
| # Call Groq API | |
| response = requests.post(url, headers=headers, json=payload, timeout=30) | |
| response.raise_for_status() | |
| # Save audio file | |
| with open(output_path, "wb") as f: | |
| f.write(response.content) | |
| return str(output_path) | |
| except requests.exceptions.RequestException as e: | |
| raise Exception(f"Groq TTS API error: {str(e)}") | |
| except Exception as e: | |
| raise Exception(f"Unexpected error in text_to_speech: {str(e)}") |