Spaces:
Sleeping
Sleeping
import gradio as gr | |
import edge_tts | |
import asyncio | |
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, pitch): | |
if not text.strip(): | |
return None, "Please enter text to convert." | |
if not voice: | |
return None, "Please select a voice." | |
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) | |
# Save directly to mp3 file | |
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file: | |
await communicate.save(tmp_file.name) | |
return tmp_file.name, "" | |
async def tts_interface(text, voice, rate, pitch): | |
audio_path, warning_text = await text_to_speech(text, voice, rate, pitch) | |
# audio_path: str|None; warning_text: str | |
# Return audio_path, and a Gradio update dict for the Markdown | |
return audio_path, gr.update(value=warning_text) | |
async def create_demo(): | |
voices = await get_voices() | |
with gr.Blocks(analytics_enabled=False) as demo: | |
gr.Markdown("# 🎙️ Edge TTS Text-to-Speech") | |
with gr.Row(): | |
with gr.Column(scale=1): | |
gr.Markdown("## Text-to-Speech with Microsoft Edge TTS") | |
gr.Markdown(""" | |
Convert text to speech using Microsoft Edge TTS. | |
Adjust speech rate and pitch: 0 is default, positive values increase, negative values decrease. | |
""") | |
# (Promotional HTML omitted for brevity) | |
with gr.Column(scale=1): | |
# (Second promotional HTML omitted for brevity) | |
pass | |
with gr.Row(): | |
with gr.Column(): | |
text_input = gr.Textbox(label="Input Text", lines=5) | |
voice_dropdown = gr.Dropdown( | |
choices=[""] + list(voices.keys()), | |
label="Select Voice", | |
value="" | |
) | |
rate_slider = gr.Slider( | |
minimum=-50, maximum=50, value=0, | |
label="Speech Rate Adjustment (%)", step=1 | |
) | |
pitch_slider = gr.Slider( | |
minimum=-20, maximum=20, value=0, | |
label="Pitch Adjustment (Hz)", step=1 | |
) | |
generate_btn = gr.Button("Generate Speech", variant="primary") | |
audio_output = gr.Audio(label="Generated Audio", type="filepath") | |
warning_md = gr.Markdown("", label="Warning") | |
generate_btn.click( | |
fn=tts_interface, | |
inputs=[text_input, voice_dropdown, rate_slider, pitch_slider], | |
outputs=[audio_output, warning_md] | |
) | |
gr.Markdown( | |
"Experience the power of Edge TTS for text-to-speech conversion, " | |
"and explore our advanced Text-to-Video Converter for even more creative possibilities!" | |
) | |
return demo | |
async def main(): | |
demo = await create_demo() | |
demo.queue(default_concurrency_limit=50) | |
demo.launch(show_api=False) | |
if __name__ == "__main__": | |
asyncio.run(main()) | |