File size: 1,002 Bytes
6a9fed6
 
 
 
2570129
 
 
6a9fed6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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}")