sreepathi-ravikumar commited on
Commit
8147c82
·
verified ·
1 Parent(s): 9029957

Update audio_generator.py

Browse files
Files changed (1) hide show
  1. audio_generator.py +24 -22
audio_generator.py CHANGED
@@ -1,25 +1,27 @@
1
  import os
2
- from elevenlabs import generate, save, voices
 
3
 
4
- def generate_audio(text, filename="output_audio.mp3"):
5
- api_key = os.getenv("ELEVENLABS_API_KEY")
6
- if not api_key:
7
- raise RuntimeError("Missing ElevenLabs API Key")
8
-
9
- # Get available voices
10
- available_voices = voices()
11
- if not available_voices:
12
- raise RuntimeError("No voices available in your ElevenLabs account")
13
 
14
- # Use the first available voice
15
- voice = available_voices[0]
16
- print(f"Using voice: {voice.name}") # For debugging
17
-
18
- audio = generate(
19
- text=text,
20
- voice=voice,
21
- model="eleven_monolingual_v1",
22
- api_key=api_key
23
- )
24
- save(audio, filename)
25
- return filename
 
 
 
 
1
  import os
2
+ import edge_tts
3
+ import asyncio
4
 
5
+ async def generate_edge_audio(text, filename="output_audio.mp3"):
6
+ try:
7
+ communicate = edge_tts.Communicate(text, "en-US-AriaNeural")
8
+ await communicate.save(filename)
9
+ return filename
10
+ except Exception as e:
11
+ raise RuntimeError(f"EdgeTTS error: {str(e)}")
 
 
12
 
13
+ def generate_audio(text, filename="output_audio.mp3"):
14
+ try:
15
+ return asyncio.run(generate_edge_audio(text, filename))
16
+ except Exception as e:
17
+ # Fallback to ElevenLabs if configured
18
+ if os.getenv("ELEVENLABS_API_KEY"):
19
+ from elevenlabs import generate, save, voices
20
+ try:
21
+ voice = voices()[0]
22
+ audio = generate(text=text, voice=voice)
23
+ save(audio, filename)
24
+ return filename
25
+ except Exception as e:
26
+ raise RuntimeError(f"Both EdgeTTS and ElevenLabs failed: {str(e)}")
27
+ raise