INNER CODE UNIT · Python
create_audio_from_text_gtts
rohitg00/awesome-ai-apps · multimodal-apps/hedra-live-avatars-demo/hedra-avatar-backend/app/main.py:151
def create_audio_from_text_gtts(text: str, voice_id: Optional[str] = None) -> bytes:
"""Create audio from text using Google TTS"""
try:
logger.info(f"🎵 Creating gTTS audio: '{text[:50]}...' with voice: {voice_id}")
# Parse voice_id to extract language and tld
voice_info = next((v for v in get_gtts_voices() if v["id"] == voice_id), None)
if voice_info:
tts = gTTS(text=text, lang=voice_info["language"], tld=voice_info["tld"], slow=False)
else:
# Default to English US
tts = gTTS(text=text, lang='en', tld='com', slow=False)
# Create temporary file for MP3
with tempfile.NamedTemporaryFile(suffix='.mp3', delete=False) as temp_mp3:
tts.save(temp_mp3.name)
# Read the MP3 file