Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -6,7 +6,10 @@ import os
|
|
6 |
|
7 |
async def get_voices():
|
8 |
voices = await edge_tts.list_voices()
|
9 |
-
return {
|
|
|
|
|
|
|
10 |
|
11 |
async def text_to_speech(text, voice, rate, pitch):
|
12 |
if not text.strip():
|
@@ -19,18 +22,17 @@ async def text_to_speech(text, voice, rate, pitch):
|
|
19 |
pitch_str = f"{pitch:+d}Hz"
|
20 |
communicate = edge_tts.Communicate(text, voice_short_name, rate=rate_str, pitch=pitch_str)
|
21 |
|
22 |
-
# Save directly to mp3 file
|
23 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
|
24 |
tmp_path = tmp_file.name
|
25 |
await communicate.save(tmp_path)
|
26 |
|
27 |
-
return tmp_path,
|
28 |
|
29 |
async def tts_interface(text, voice, rate, pitch):
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
return audio, None
|
34 |
|
35 |
async def create_demo():
|
36 |
voices = await get_voices()
|
@@ -42,84 +44,101 @@ async def create_demo():
|
|
42 |
with gr.Column(scale=1):
|
43 |
gr.Markdown("## Text-to-Speech with Microsoft Edge TTS")
|
44 |
gr.Markdown("""
|
45 |
-
|
46 |
-
|
47 |
""")
|
48 |
|
49 |
gr.HTML("""
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
""")
|
74 |
|
75 |
with gr.Column(scale=1):
|
76 |
gr.HTML("""
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
""")
|
103 |
|
104 |
with gr.Row():
|
105 |
with gr.Column():
|
106 |
text_input = gr.Textbox(label="Input Text", lines=5)
|
107 |
-
voice_dropdown = gr.Dropdown(
|
108 |
-
|
109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
110 |
|
111 |
generate_btn = gr.Button("Generate Speech", variant="primary")
|
112 |
|
113 |
-
audio_output = gr.Audio(
|
114 |
-
|
|
|
|
|
|
|
|
|
115 |
|
116 |
generate_btn.click(
|
117 |
fn=tts_interface,
|
118 |
inputs=[text_input, voice_dropdown, rate_slider, pitch_slider],
|
119 |
-
outputs=[audio_output, warning_md]
|
120 |
)
|
121 |
|
122 |
-
gr.Markdown(
|
|
|
|
|
|
|
123 |
|
124 |
return demo
|
125 |
|
|
|
6 |
|
7 |
async def get_voices():
|
8 |
voices = await edge_tts.list_voices()
|
9 |
+
return {
|
10 |
+
f"{v['ShortName']} - {v['Locale']} ({v['Gender']})": v['ShortName']
|
11 |
+
for v in voices
|
12 |
+
}
|
13 |
|
14 |
async def text_to_speech(text, voice, rate, pitch):
|
15 |
if not text.strip():
|
|
|
22 |
pitch_str = f"{pitch:+d}Hz"
|
23 |
communicate = edge_tts.Communicate(text, voice_short_name, rate=rate_str, pitch=pitch_str)
|
24 |
|
25 |
+
# Save directly to mp3 file
|
26 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
|
27 |
tmp_path = tmp_file.name
|
28 |
await communicate.save(tmp_path)
|
29 |
|
30 |
+
return tmp_path, ""
|
31 |
|
32 |
async def tts_interface(text, voice, rate, pitch):
|
33 |
+
audio_path, warning_text = await text_to_speech(text, voice, rate, pitch)
|
34 |
+
# Always return plain Python types: (str or None, warning string)
|
35 |
+
return audio_path, warning_text
|
|
|
36 |
|
37 |
async def create_demo():
|
38 |
voices = await get_voices()
|
|
|
44 |
with gr.Column(scale=1):
|
45 |
gr.Markdown("## Text-to-Speech with Microsoft Edge TTS")
|
46 |
gr.Markdown("""
|
47 |
+
Convert text to speech using Microsoft Edge TTS.
|
48 |
+
Adjust speech rate and pitch: 0 is default, positive values increase, negative values decrease.
|
49 |
""")
|
50 |
|
51 |
gr.HTML("""
|
52 |
+
<div style="margin: 20px 0; padding: 15px; border: 1px solid #4CAF50; border-radius: 10px; background-color: #f1f8e9;">
|
53 |
+
<p style="margin-top: 0;"><b>Looking for the new version with more features?</b></p>
|
54 |
+
<p>The new version includes:</p>
|
55 |
+
<ul>
|
56 |
+
<li><b>SRT Subtitle Support</b>: Upload SRT files or input SRT format text</li>
|
57 |
+
<li><b>File Upload</b>: Easily upload TXT or SRT files</li>
|
58 |
+
<li><b>Smart Format Detection</b>: Detects plain text or SRT format</li>
|
59 |
+
<li><b>MP3 Output</b>: Generate high-quality MP3 audio</li>
|
60 |
+
</ul>
|
61 |
+
<div style="text-align: center; margin-top: 15px;">
|
62 |
+
<a href="https://text-to-speech.wingetgui.com/" target="_blank"
|
63 |
+
style="display: inline-block;
|
64 |
+
background: linear-gradient(45deg, #4CAF50, #8BC34A);
|
65 |
+
color: white;
|
66 |
+
padding: 12px 30px;
|
67 |
+
text-decoration: none;
|
68 |
+
border-radius: 30px;
|
69 |
+
font-weight: bold;
|
70 |
+
font-size: 16px;
|
71 |
+
box-shadow: 0 4px 10px rgba(76, 175, 80, 0.3);
|
72 |
+
transition: all 0.3s ease;">Try New Version ➔</a>
|
73 |
+
</div>
|
74 |
+
</div>
|
75 |
""")
|
76 |
|
77 |
with gr.Column(scale=1):
|
78 |
gr.HTML("""
|
79 |
+
<div style="height: 100%; background-color: #f0f8ff; padding: 15px; border-radius: 10px;">
|
80 |
+
<h2 style="color: #1e90ff; margin-top: 0;">Turn Your Text Into Professional Videos!</h2>
|
81 |
+
<ul style="list-style-type: none; padding-left: 0;">
|
82 |
+
<li>✅ <b>40+ languages and 300+ voices supported</b></li>
|
83 |
+
<li>✅ <b>Custom backgrounds, music, and visual effects</b></li>
|
84 |
+
<li>✅ <b>Create engaging video content from simple text</b></li>
|
85 |
+
<li>✅ <b>Perfect for educators, content creators, and marketers</b></li>
|
86 |
+
</ul>
|
87 |
+
<div style="text-align: center; margin-top: 20px;">
|
88 |
+
<span style="font-size: 96px;">🎬</span>
|
89 |
+
<div style="margin-top: 15px;">
|
90 |
+
<a href="https://text2video.wingetgui.com/" target="_blank"
|
91 |
+
style="display: inline-block;
|
92 |
+
background: linear-gradient(45deg, #2196F3, #21CBF3);
|
93 |
+
color: white;
|
94 |
+
padding: 12px 30px;
|
95 |
+
text-decoration: none;
|
96 |
+
border-radius: 30px;
|
97 |
+
font-weight: bold;
|
98 |
+
font-size: 16px;
|
99 |
+
box-shadow: 0 4px 10px rgba(33, 150, 243, 0.3);
|
100 |
+
transition: all 0.3s ease;">Try Text-to-Video ➔</a>
|
101 |
+
</div>
|
102 |
+
</div>
|
103 |
+
</div>
|
104 |
""")
|
105 |
|
106 |
with gr.Row():
|
107 |
with gr.Column():
|
108 |
text_input = gr.Textbox(label="Input Text", lines=5)
|
109 |
+
voice_dropdown = gr.Dropdown(
|
110 |
+
choices=[""] + list(voices.keys()),
|
111 |
+
label="Select Voice",
|
112 |
+
value=""
|
113 |
+
)
|
114 |
+
rate_slider = gr.Slider(
|
115 |
+
minimum=-50, maximum=50, value=0,
|
116 |
+
label="Speech Rate Adjustment (%)", step=1
|
117 |
+
)
|
118 |
+
pitch_slider = gr.Slider(
|
119 |
+
minimum=-20, maximum=20, value=0,
|
120 |
+
label="Pitch Adjustment (Hz)", step=1
|
121 |
+
)
|
122 |
|
123 |
generate_btn = gr.Button("Generate Speech", variant="primary")
|
124 |
|
125 |
+
audio_output = gr.Audio(
|
126 |
+
label="Generated Audio",
|
127 |
+
type="filepath"
|
128 |
+
)
|
129 |
+
# Start with an empty Markdown for warnings
|
130 |
+
warning_md = gr.Markdown("", label="Warning")
|
131 |
|
132 |
generate_btn.click(
|
133 |
fn=tts_interface,
|
134 |
inputs=[text_input, voice_dropdown, rate_slider, pitch_slider],
|
135 |
+
outputs=[audio_output, warning_md.update]
|
136 |
)
|
137 |
|
138 |
+
gr.Markdown(
|
139 |
+
"Experience the power of Edge TTS for text-to-speech conversion, "
|
140 |
+
"and explore our advanced Text-to-Video Converter for even more creative possibilities!"
|
141 |
+
)
|
142 |
|
143 |
return demo
|
144 |
|