import uuid from pathlib import Path from config import GROQ_TTS_API_KEY, GROQ_TTS_MODEL from gtts import gTTS def text_to_speech( text: str, voice: str = "en", fmt: str = "mp3", ) -> str: """ Convert text to speech using gTTS (Google Translate, free). Only MP3 is supported. """ 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 } if not text or not text.strip(): raise ValueError("Text cannot be empty") if fmt != "mp3": raise ValueError("Only MP3 format is supported by the free TTS backend") try: temp_dir = Path("temp_audio") temp_dir.mkdir(exist_ok=True) output_filename = f"tts_{uuid.uuid4().hex[:8]}.{fmt}" output_path = temp_dir / output_filename # gTTS uses language codes; voice kept for compatibility. tts = gTTS(text=text.strip(), lang=voice or "en") tts.save(str(output_path)) return str(output_path) except Exception as e: raise Exception(f"Unexpected error in text_to_speech: {str(e)}")