import os import logging from elevenlabs import stream from elevenlabs.client import ElevenLabs from dotenv import load_dotenv load_dotenv() AUDIO_DIR = "audio_outputs" logger = logging.getLogger(__name__) client = ElevenLabs(api_key=os.getenv("ELEVENLABS_API_KEY")) def generate_audio(text: str, voice_id: str, audio_key: str): try: logger.info("🎯 Starting ElevenLabs audio generation") audio_stream = client.text_to_speech.convert_as_stream( text=text, voice_id=voice_id, model_id="eleven_multilingual_v2" ) os.makedirs(AUDIO_DIR, exist_ok=True) output_path = os.path.join(AUDIO_DIR, f"{audio_key}.mp3") with open(output_path, "wb") as f: for chunk in audio_stream: if isinstance(chunk, bytes): f.write(chunk) logger.info(f"✅ Audio saved to {output_path}") except Exception as e: logger.error(f"🔥 Error generating audio: {e}")