Spaces:
Building
Building
# tts_module.py | |
import asyncio | |
import edge_tts | |
import tempfile | |
async def get_voices(): | |
voices = await edge_tts.list_voices() | |
return {f"{v['ShortName']} - {v['Locale']} ({v['Gender']})": v['ShortName'] for v in voices} | |
async def text_to_speech(text, voice, rate=0, pitch=0): | |
if not text.strip(): | |
raise ValueError("El texto no puede estar vacío.") | |
if not voice: | |
raise ValueError("Debes seleccionar una voz.") | |
voice_short_name = voice.split(" - ")[0] | |
rate_str = f"{rate:+d}%" | |
pitch_str = f"{pitch:+d}Hz" | |
communicate = edge_tts.Communicate(text, voice_short_name, rate=rate_str, pitch=pitch_str) | |
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file: | |
await communicate.save(tmp_file.name) | |
return tmp_file.name |