Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import edge_tts | |
| import asyncio | |
| import tempfile | |
| async def text_to_speech(text, voice, rate, pitch): | |
| if not text.strip(): | |
| return None | |
| voice_short_name = voice.split(" - ")[0] | |
| communicate = edge_tts.Communicate(text, voice_short_name, rate=f"{rate:+d}%", pitch=f"{pitch:+d}Hz") | |
| with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp: | |
| await communicate.save(tmp.name) | |
| return tmp.name | |
| async def build_ui(): | |
| voices = await edge_tts.list_voices() | |
| voice_map = {f"{v['ShortName']} - {v['Locale']}": v['ShortName'] for v in voices} | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# 🗣️ Text to Speech") | |
| text_in = gr.Textbox(label="Text", lines=4) | |
| voice = gr.Dropdown(choices=list(voice_map.keys()), label="Voice") | |
| rate = gr.Slider(-50, 50, 0, step=1, label="Rate (%)") | |
| pitch = gr.Slider(-20, 20, 0, step=1, label="Pitch (Hz)") | |
| out = gr.Audio(label="Generated Speech", type="filepath") | |
| btn = gr.Button("Generate") | |
| btn.click(lambda t, v, r, p: asyncio.run(text_to_speech(t, v, r, p)), | |
| inputs=[text_in, voice, rate, pitch], outputs=out) | |
| return demo | |
| demo = asyncio.run(build_ui()) |