Spaces:
Sleeping
Sleeping
Update audio_generator.py
Browse files- audio_generator.py +46 -45
audio_generator.py
CHANGED
|
@@ -1,53 +1,54 @@
|
|
| 1 |
import os
|
| 2 |
import edge_tts
|
| 3 |
import asyncio
|
| 4 |
-
from
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
async def generate_edge_audio(text, filename="output_audio.mp3"):
|
| 8 |
-
"""Free Microsoft Edge TTS implementation"""
|
| 9 |
try:
|
| 10 |
-
communicate = edge_tts.Communicate(
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
except Exception as e:
|
| 14 |
raise RuntimeError(f"EdgeTTS error: {str(e)}")
|
| 15 |
|
| 16 |
-
def generate_audio(text, filename="
|
| 17 |
-
"""
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
return asyncio.run(generate_edge_audio(text, filename))
|
| 21 |
-
except Exception as edge_error:
|
| 22 |
-
# If Edge fails, try ElevenLabs if configured
|
| 23 |
-
elevenlabs_key = os.getenv("ELEVENLABS_API_KEY")
|
| 24 |
-
if not elevenlabs_key:
|
| 25 |
-
raise RuntimeError(
|
| 26 |
-
"EdgeTTS failed and no ElevenLabs API key configured. "
|
| 27 |
-
f"Original error: {str(edge_error)}"
|
| 28 |
-
)
|
| 29 |
-
|
| 30 |
-
try:
|
| 31 |
-
# Verify ElevenLabs key
|
| 32 |
-
available_voices = voices()
|
| 33 |
-
if not available_voices:
|
| 34 |
-
raise RuntimeError("No ElevenLabs voices available")
|
| 35 |
-
|
| 36 |
-
audio = generate(
|
| 37 |
-
text=text,
|
| 38 |
-
voice=available_voices[0],
|
| 39 |
-
model="eleven_monolingual_v1",
|
| 40 |
-
api_key=elevenlabs_key
|
| 41 |
-
)
|
| 42 |
-
save(audio, filename)
|
| 43 |
-
return filename
|
| 44 |
-
except APIError as e:
|
| 45 |
-
if "requires a valid API key" in str(e):
|
| 46 |
-
raise RuntimeError("Invalid ElevenLabs API key")
|
| 47 |
-
raise RuntimeError(f"ElevenLabs error: {str(e)}")
|
| 48 |
-
except Exception as e:
|
| 49 |
-
raise RuntimeError(
|
| 50 |
-
f"Both EdgeTTS and ElevenLabs failed. "
|
| 51 |
-
f"Edge error: {str(edge_error)}. "
|
| 52 |
-
f"ElevenLabs error: {str(e)}"
|
| 53 |
-
)
|
|
|
|
| 1 |
import os
|
| 2 |
import edge_tts
|
| 3 |
import asyncio
|
| 4 |
+
from typing import Optional
|
| 5 |
+
|
| 6 |
+
# Available free voices (natural-sounding options)
|
| 7 |
+
VOICES = {
|
| 8 |
+
"female": {
|
| 9 |
+
"en-US": "en-US-AriaNeural", # Very natural
|
| 10 |
+
"en-GB": "en-GB-LibbyNeural", # British English
|
| 11 |
+
"es-ES": "es-ES-ElviraNeural" # Spanish
|
| 12 |
+
},
|
| 13 |
+
"male": {
|
| 14 |
+
"en-US": "en-US-GuyNeural",
|
| 15 |
+
"en-GB": "en-GB-RyanNeural",
|
| 16 |
+
"es-ES": "es-ES-AlvaroNeural"
|
| 17 |
+
}
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
async def generate_speech(
|
| 21 |
+
text: str,
|
| 22 |
+
output_file: str = "output.mp3",
|
| 23 |
+
voice: Optional[str] = None,
|
| 24 |
+
rate: str = "+0%", # Speed adjustment
|
| 25 |
+
volume: str = "+0%" # Volume adjustment
|
| 26 |
+
) -> str:
|
| 27 |
+
"""
|
| 28 |
+
Generate speech using free EdgeTTS
|
| 29 |
+
Args:
|
| 30 |
+
text: Input text (max 3000 chars)
|
| 31 |
+
output_file: Output path
|
| 32 |
+
voice: Voice code (e.g. 'en-US-AriaNeural')
|
| 33 |
+
rate: Speaking rate adjustment (-50% to +100%)
|
| 34 |
+
volume: Volume adjustment (-50% to +50%)
|
| 35 |
+
"""
|
| 36 |
+
if not voice:
|
| 37 |
+
voice = VOICES["female"]["en-US"] # Default to natural female voice
|
| 38 |
|
|
|
|
|
|
|
| 39 |
try:
|
| 40 |
+
communicate = edge_tts.Communicate(
|
| 41 |
+
text=text,
|
| 42 |
+
voice=voice,
|
| 43 |
+
rate=rate,
|
| 44 |
+
volume=volume
|
| 45 |
+
)
|
| 46 |
+
await communicate.save(output_file)
|
| 47 |
+
return output_file
|
| 48 |
except Exception as e:
|
| 49 |
raise RuntimeError(f"EdgeTTS error: {str(e)}")
|
| 50 |
|
| 51 |
+
def generate_audio(text: str, filename: str = "output.mp3") -> str:
|
| 52 |
+
"""Synchronous wrapper for async TTS"""
|
| 53 |
+
os.makedirs(os.path.dirname(filename) or ".", exist_ok=True)
|
| 54 |
+
return asyncio.run(generate_speech(text, filename))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|