Spaces:
Sleeping
Sleeping
Create tts_app.py
Browse files- tts_app.py +30 -0
tts_app.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import edge_tts
|
| 3 |
+
import asyncio
|
| 4 |
+
import tempfile
|
| 5 |
+
|
| 6 |
+
async def text_to_speech(text, voice, rate, pitch):
|
| 7 |
+
if not text.strip():
|
| 8 |
+
return None
|
| 9 |
+
voice_short_name = voice.split(" - ")[0]
|
| 10 |
+
communicate = edge_tts.Communicate(text, voice_short_name, rate=f"{rate:+d}%", pitch=f"{pitch:+d}Hz")
|
| 11 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp:
|
| 12 |
+
await communicate.save(tmp.name)
|
| 13 |
+
return tmp.name
|
| 14 |
+
|
| 15 |
+
async def build_ui():
|
| 16 |
+
voices = await edge_tts.list_voices()
|
| 17 |
+
voice_map = {f"{v['ShortName']} - {v['Locale']}": v['ShortName'] for v in voices}
|
| 18 |
+
with gr.Blocks() as demo:
|
| 19 |
+
gr.Markdown("# 🗣️ Text to Speech")
|
| 20 |
+
text_in = gr.Textbox(label="Text", lines=4)
|
| 21 |
+
voice = gr.Dropdown(choices=list(voice_map.keys()), label="Voice")
|
| 22 |
+
rate = gr.Slider(-50, 50, 0, step=1, label="Rate (%)")
|
| 23 |
+
pitch = gr.Slider(-20, 20, 0, step=1, label="Pitch (Hz)")
|
| 24 |
+
out = gr.Audio(label="Generated Speech", type="filepath")
|
| 25 |
+
btn = gr.Button("Generate")
|
| 26 |
+
btn.click(lambda t, v, r, p: asyncio.run(text_to_speech(t, v, r, p)),
|
| 27 |
+
inputs=[text_in, voice, rate, pitch], outputs=out)
|
| 28 |
+
return demo
|
| 29 |
+
|
| 30 |
+
demo = asyncio.run(build_ui())
|