Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,7 +3,7 @@ import edge_tts
|
|
3 |
import asyncio
|
4 |
import tempfile
|
5 |
|
6 |
-
# 1)
|
7 |
def load_voices():
|
8 |
loop = asyncio.get_event_loop()
|
9 |
voices = loop.run_until_complete(edge_tts.list_voices())
|
@@ -14,14 +14,14 @@ def load_voices():
|
|
14 |
|
15 |
VOICES = load_voices()
|
16 |
|
17 |
-
# 2) Async
|
18 |
async def _text_to_speech(text, short_name, rate_str, pitch_str):
|
19 |
-
|
20 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp:
|
21 |
-
await
|
22 |
return tmp.name
|
23 |
|
24 |
-
# 3)
|
25 |
def tts_interface(text, voice_choice, rate, pitch):
|
26 |
if not text.strip():
|
27 |
return None, "🚨 Please enter some text."
|
@@ -31,7 +31,6 @@ def tts_interface(text, voice_choice, rate, pitch):
|
|
31 |
rate_str = f"{rate:+d}%"
|
32 |
pitch_str = f"{pitch:+d}Hz"
|
33 |
try:
|
34 |
-
# Run the async TTS call synchronously
|
35 |
audio_path = asyncio.get_event_loop().run_until_complete(
|
36 |
_text_to_speech(text, short_name, rate_str, pitch_str)
|
37 |
)
|
@@ -39,35 +38,34 @@ def tts_interface(text, voice_choice, rate, pitch):
|
|
39 |
except Exception as e:
|
40 |
return None, f"❌ TTS failed: {e}"
|
41 |
|
42 |
-
# 4) Build
|
43 |
def create_demo():
|
44 |
with gr.Blocks(analytics_enabled=False) as demo:
|
45 |
gr.Markdown("# 🎙️ Edge TTS on Hugging Face Spaces")
|
46 |
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
""")
|
52 |
|
53 |
with gr.Row():
|
54 |
-
txt
|
55 |
-
vox
|
56 |
-
rate
|
57 |
pitch = gr.Slider(-20, 20, value=0, label="Pitch (Hz)")
|
58 |
|
59 |
-
btn
|
60 |
audio_out = gr.Audio(type="filepath", label="Audio Output")
|
61 |
-
warn_md
|
62 |
|
63 |
-
#
|
64 |
btn.click(
|
65 |
fn=tts_interface,
|
66 |
inputs=[txt, vox, rate, pitch],
|
67 |
outputs=[audio_out, warn_md]
|
68 |
)
|
69 |
|
70 |
-
# Enable
|
71 |
demo.queue()
|
72 |
|
73 |
return demo
|
@@ -75,5 +73,4 @@ Adjust rate and pitch to fine-tune the output.
|
|
75 |
# 5) Launch
|
76 |
if __name__ == "__main__":
|
77 |
demo = create_demo()
|
78 |
-
|
79 |
-
demo.launch()
|
|
|
3 |
import asyncio
|
4 |
import tempfile
|
5 |
|
6 |
+
# 1) Load voices once at startup
|
7 |
def load_voices():
|
8 |
loop = asyncio.get_event_loop()
|
9 |
voices = loop.run_until_complete(edge_tts.list_voices())
|
|
|
14 |
|
15 |
VOICES = load_voices()
|
16 |
|
17 |
+
# 2) Async TTS worker
|
18 |
async def _text_to_speech(text, short_name, rate_str, pitch_str):
|
19 |
+
comm = edge_tts.Communicate(text, short_name, rate=rate_str, pitch=pitch_str)
|
20 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp:
|
21 |
+
await comm.save(tmp.name)
|
22 |
return tmp.name
|
23 |
|
24 |
+
# 3) Synchronous wrapper for Gradio callback
|
25 |
def tts_interface(text, voice_choice, rate, pitch):
|
26 |
if not text.strip():
|
27 |
return None, "🚨 Please enter some text."
|
|
|
31 |
rate_str = f"{rate:+d}%"
|
32 |
pitch_str = f"{pitch:+d}Hz"
|
33 |
try:
|
|
|
34 |
audio_path = asyncio.get_event_loop().run_until_complete(
|
35 |
_text_to_speech(text, short_name, rate_str, pitch_str)
|
36 |
)
|
|
|
38 |
except Exception as e:
|
39 |
return None, f"❌ TTS failed: {e}"
|
40 |
|
41 |
+
# 4) Build Gradio Blocks UI
|
42 |
def create_demo():
|
43 |
with gr.Blocks(analytics_enabled=False) as demo:
|
44 |
gr.Markdown("# 🎙️ Edge TTS on Hugging Face Spaces")
|
45 |
|
46 |
+
gr.Markdown(
|
47 |
+
"**Convert your text to speech** using Microsoft Edge's neural voices. "
|
48 |
+
"Adjust rate and pitch to fine-tune the output."
|
49 |
+
)
|
|
|
50 |
|
51 |
with gr.Row():
|
52 |
+
txt = gr.Textbox(label="Input Text", lines=5, placeholder="Type something…")
|
53 |
+
vox = gr.Dropdown(choices=list(VOICES.keys()), label="Voice")
|
54 |
+
rate = gr.Slider(-50, 50, value=0, label="Rate (%)")
|
55 |
pitch = gr.Slider(-20, 20, value=0, label="Pitch (Hz)")
|
56 |
|
57 |
+
btn = gr.Button("Generate Speech")
|
58 |
audio_out = gr.Audio(type="filepath", label="Audio Output")
|
59 |
+
warn_md = gr.Markdown("", label="Warnings / Errors")
|
60 |
|
61 |
+
# Wire the callback and register the /api endpoints
|
62 |
btn.click(
|
63 |
fn=tts_interface,
|
64 |
inputs=[txt, vox, rate, pitch],
|
65 |
outputs=[audio_out, warn_md]
|
66 |
)
|
67 |
|
68 |
+
# Enable queuing so that Gradio registers its API
|
69 |
demo.queue()
|
70 |
|
71 |
return demo
|
|
|
73 |
# 5) Launch
|
74 |
if __name__ == "__main__":
|
75 |
demo = create_demo()
|
76 |
+
demo.launch()
|
|