Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,81 +1,70 @@
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
-
import edge_tts
|
4 |
import asyncio
|
|
|
5 |
import tempfile
|
6 |
|
7 |
-
#
|
8 |
def load_voices():
|
9 |
loop = asyncio.get_event_loop()
|
10 |
voices = loop.run_until_complete(edge_tts.list_voices())
|
11 |
-
return {
|
12 |
-
|
13 |
-
for v in voices
|
14 |
-
}
|
15 |
|
16 |
VOICES = load_voices()
|
17 |
|
18 |
-
#
|
19 |
-
async def
|
20 |
comm = edge_tts.Communicate(text, short_name, rate=rate_str, pitch=pitch_str)
|
21 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp:
|
22 |
await comm.save(tmp.name)
|
23 |
return tmp.name
|
24 |
|
25 |
-
#
|
26 |
-
def tts_interface(text,
|
27 |
if not text.strip():
|
28 |
-
return None, "🚨
|
29 |
-
if not
|
30 |
-
return None, "🚨
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
try:
|
35 |
-
|
36 |
-
|
37 |
)
|
38 |
-
return
|
39 |
except Exception as e:
|
40 |
-
return None, f"❌ TTS
|
41 |
|
42 |
-
#
|
43 |
-
def
|
44 |
with gr.Blocks(analytics_enabled=False) as demo:
|
45 |
-
gr.Markdown("# 🎙️ Edge TTS
|
46 |
-
|
47 |
-
gr.Markdown(
|
48 |
-
"**Convert your text to speech** using Microsoft Edge’s neural voices. "
|
49 |
-
"Adjust rate and pitch to fine-tune the output."
|
50 |
-
)
|
51 |
-
|
52 |
with gr.Row():
|
53 |
-
txt
|
54 |
-
vox
|
55 |
-
rate
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
warn_md = gr.Markdown("", label="Warnings / Errors")
|
61 |
|
62 |
btn.click(
|
63 |
fn=tts_interface,
|
64 |
-
inputs=[txt, vox, rate,
|
65 |
-
outputs=[
|
66 |
)
|
67 |
-
|
68 |
-
# Register API endpoints
|
69 |
-
demo.queue()
|
70 |
|
71 |
return demo
|
72 |
|
73 |
if __name__ == "__main__":
|
74 |
-
demo =
|
75 |
-
# 5) Bind to 0.0.0.0 and the port provided by HF Spaces (defaults to 7860 internally)
|
76 |
port = int(os.environ.get("PORT", 7860))
|
77 |
demo.launch(
|
78 |
server_name="0.0.0.0",
|
79 |
server_port=port,
|
80 |
-
|
81 |
)
|
|
|
1 |
import os
|
2 |
import gradio as gr
|
|
|
3 |
import asyncio
|
4 |
+
import edge_tts
|
5 |
import tempfile
|
6 |
|
7 |
+
# Load voices once
|
8 |
def load_voices():
|
9 |
loop = asyncio.get_event_loop()
|
10 |
voices = loop.run_until_complete(edge_tts.list_voices())
|
11 |
+
return {f"{v['ShortName']} - {v['Locale']} ({v['Gender']})": v['ShortName']
|
12 |
+
for v in voices}
|
|
|
|
|
13 |
|
14 |
VOICES = load_voices()
|
15 |
|
16 |
+
# Async TTS
|
17 |
+
async def _tts(text, short_name, rate_str, pitch_str):
|
18 |
comm = edge_tts.Communicate(text, short_name, rate=rate_str, pitch=pitch_str)
|
19 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp:
|
20 |
await comm.save(tmp.name)
|
21 |
return tmp.name
|
22 |
|
23 |
+
# Sync wrapper
|
24 |
+
def tts_interface(text, voice, rate, pitch):
|
25 |
if not text.strip():
|
26 |
+
return None, "🚨 Enter some text."
|
27 |
+
if not voice:
|
28 |
+
return None, "🚨 Select a voice."
|
29 |
+
name = voice.split(" - ")[0]
|
30 |
+
rate_s = f"{rate:+d}%"
|
31 |
+
pitch_s = f"{pitch:+d}Hz"
|
32 |
try:
|
33 |
+
path = asyncio.get_event_loop().run_until_complete(
|
34 |
+
_tts(text, name, rate_s, pitch_s)
|
35 |
)
|
36 |
+
return path, ""
|
37 |
except Exception as e:
|
38 |
+
return None, f"❌ TTS failed: {e}"
|
39 |
|
40 |
+
# Build UI
|
41 |
+
def create_app():
|
42 |
with gr.Blocks(analytics_enabled=False) as demo:
|
43 |
+
gr.Markdown("# 🎙️ Edge TTS in Hugging Face Space")
|
44 |
+
gr.Markdown("Convert text to speech with Microsoft Edge voices.")
|
|
|
|
|
|
|
|
|
|
|
45 |
with gr.Row():
|
46 |
+
txt = gr.Textbox(lines=5, label="Input Text")
|
47 |
+
vox = gr.Dropdown(list(VOICES.keys()), label="Voice")
|
48 |
+
rate = gr.Slider(-50, 50, value=0, label="Rate (%)")
|
49 |
+
pit = gr.Slider(-20, 20, value=0, label="Pitch (Hz)")
|
50 |
+
btn = gr.Button("Generate")
|
51 |
+
out_audio = gr.Audio(type="filepath", label="Audio")
|
52 |
+
warn = gr.Markdown("", label="Warning")
|
|
|
53 |
|
54 |
btn.click(
|
55 |
fn=tts_interface,
|
56 |
+
inputs=[txt, vox, rate, pit],
|
57 |
+
outputs=[out_audio, warn]
|
58 |
)
|
59 |
+
demo.queue() # Register /api endpoints
|
|
|
|
|
60 |
|
61 |
return demo
|
62 |
|
63 |
if __name__ == "__main__":
|
64 |
+
demo = create_app()
|
|
|
65 |
port = int(os.environ.get("PORT", 7860))
|
66 |
demo.launch(
|
67 |
server_name="0.0.0.0",
|
68 |
server_port=port,
|
69 |
+
ssr_mode=False # disable SSR introspection errors
|
70 |
)
|