FastAPI-Backend-Models / services /tts_service.py
malek-messaoudii
feat: Update generate_speech function to use gTTS backend with MP3 format and modify voice parameter to accept language codes. Clean up tts_service.py by removing unused GROQ API key checks and related code.
a61d0cb
raw
history blame
938 Bytes
import uuid
from pathlib import Path
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 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)}")