cs2764 commited on
Commit
e5d9894
·
verified ·
1 Parent(s): d007f6f

Upload app.py

Browse files

Setup default voice to ZH-xiaoxiao

Files changed (1) hide show
  1. app.py +85 -79
app.py CHANGED
@@ -1,79 +1,85 @@
1
- import gradio as gr
2
- import edge_tts
3
- import asyncio
4
- import tempfile
5
- import os
6
-
7
- async def get_voices():
8
- voices = await edge_tts.list_voices()
9
- return {f"{v['ShortName']} - {v['Locale']} ({v['Gender']})": v['ShortName'] for v in voices}
10
-
11
- async def text_to_speech(text, voice, rate, volume, pitch):
12
- if not text.strip():
13
- return None, "Please enter text to convert."
14
- if not voice:
15
- return None, "Please select a voice."
16
-
17
- voice_short_name = voice.split(" - ")[0]
18
- rate_str = f"{rate:+d}%"
19
- volume_str = f"{volume:+d}%"
20
- pitch_str = f"{pitch:+d}Hz"
21
- communicate = edge_tts.Communicate(text, voice_short_name, rate=rate_str, volume=volume_str, pitch=pitch_str)
22
- with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
23
- tmp_path = tmp_file.name
24
- await communicate.save(tmp_path)
25
- return tmp_path, None
26
-
27
- async def tts_interface(text, voice, rate, volume, pitch):
28
- audio, warning = await text_to_speech(text, voice, rate, volume, pitch)
29
- if warning:
30
- return audio, gr.Warning(warning)
31
- return audio, None
32
-
33
- async def create_demo():
34
- voices = await get_voices()
35
-
36
- description = """
37
- Convert text to speech using Microsoft Edge TTS. Adjust speech rate and pitch: 0 is default, positive values increase, negative values decrease.
38
-
39
- 🎥 **Exciting News: Introducing our Text-to-Video Converter!** 🎥
40
-
41
- Take your content creation to the next level with our cutting-edge Text-to-Video Converter!
42
- Transform your words into stunning, professional-quality videos in just a few clicks.
43
-
44
- ✨ Features:
45
- • Convert text to engaging videos with customizable visuals
46
- • Choose from 40+ languages and 300+ voices
47
- • Perfect for creating audiobooks, storytelling, and language learning materials
48
- • Ideal for educators, content creators, and language enthusiasts
49
- """
50
-
51
- demo = gr.Interface(
52
- fn=tts_interface,
53
- inputs=[
54
- gr.Textbox(label="Input Text", lines=5),
55
- gr.Dropdown(choices=[""] + list(voices.keys()), label="Select Voice", value=""),
56
- gr.Slider(minimum=-50, maximum=50, value=0, label="Speech Rate Adjustment (%)", step=1),
57
- gr.Slider(minimum=-50, maximum=50, value=0, label="Speech Volume Adjustment (%)", step=1),
58
- gr.Slider(minimum=-20, maximum=20, value=0, label="Pitch Adjustment (Hz)", step=1)
59
- ],
60
- outputs=[
61
- gr.Audio(label="Generated Audio", type="filepath"),
62
- gr.Markdown(label="Warning", visible=False)
63
- ],
64
- title="Edge TTS Text-to-Speech",
65
- description=description,
66
- article="Experience the power of Edge TTS for text-to-speech conversion, and explore our advanced Text-to-Video Converter for even more creative possibilities!",
67
- analytics_enabled=False,
68
- allow_flagging="manual",
69
- api_name=None
70
- )
71
- return demo
72
-
73
- async def main():
74
- demo = await create_demo()
75
- demo.queue(default_concurrency_limit=5)
76
- demo.launch(show_api=False)
77
-
78
- if __name__ == "__main__":
79
- asyncio.run(main())
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import edge_tts
3
+ import asyncio
4
+ import tempfile
5
+ import os
6
+
7
+ async def get_voices():
8
+ voices = await edge_tts.list_voices()
9
+ return {f"{v['ShortName']} - {v['Locale']} ({v['Gender']})": v['ShortName'] for v in voices}
10
+
11
+ async def text_to_speech(text, voice, rate, volume, pitch):
12
+ if not text.strip():
13
+ return None, "Please enter text to convert."
14
+ if not voice:
15
+ return None, "Please select a voice."
16
+
17
+ voice_short_name = voice.split(" - ")[0]
18
+ rate_str = f"{rate:+d}%"
19
+ volume_str = f"{volume:+d}%"
20
+ pitch_str = f"{pitch:+d}Hz"
21
+ communicate = edge_tts.Communicate(text, voice_short_name, rate=rate_str, volume=volume_str, pitch=pitch_str)
22
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
23
+ tmp_path = tmp_file.name
24
+ await communicate.save(tmp_path)
25
+ return tmp_path, None
26
+
27
+ async def tts_interface(text, voice, rate, volume, pitch):
28
+ audio, warning = await text_to_speech(text, voice, rate, volume, pitch)
29
+ if warning:
30
+ return audio, gr.Warning(warning)
31
+ return audio, None
32
+
33
+ async def create_demo():
34
+ voices = await get_voices()
35
+
36
+ description = """
37
+ Convert text to speech using Microsoft Edge TTS. Adjust speech rate and pitch: 0 is default, positive values increase, negative values decrease.
38
+
39
+ 🎥 **Exciting News: Introducing our Text-to-Video Converter!** 🎥
40
+
41
+ Take your content creation to the next level with our cutting-edge Text-to-Video Converter!
42
+ Transform your words into stunning, professional-quality videos in just a few clicks.
43
+
44
+ ✨ Features:
45
+ • Convert text to engaging videos with customizable visuals
46
+ • Choose from 40+ languages and 300+ voices
47
+ • Perfect for creating audiobooks, storytelling, and language learning materials
48
+ • Ideal for educators, content creators, and language enthusiasts
49
+ """
50
+
51
+ default_voice = ""
52
+ for voice_key in voices.keys():
53
+ if "XiaoxiaoNeural" in voice_key:
54
+ default_voice = voice_key
55
+ break
56
+
57
+ demo = gr.Interface(
58
+ fn=tts_interface,
59
+ inputs=[
60
+ gr.Textbox(label="Input Text", lines=5),
61
+ gr.Dropdown(choices=[""] + list(voices.keys()), label="Select Voice", value=default_voice),
62
+ gr.Slider(minimum=-50, maximum=50, value=0, label="Speech Rate Adjustment (%)", step=1),
63
+ gr.Slider(minimum=-50, maximum=50, value=0, label="Speech Volume Adjustment (%)", step=1),
64
+ gr.Slider(minimum=-20, maximum=20, value=0, label="Pitch Adjustment (Hz)", step=1)
65
+ ],
66
+ outputs=[
67
+ gr.Audio(label="Generated Audio", type="filepath"),
68
+ gr.Markdown(label="Warning", visible=False)
69
+ ],
70
+ title="Edge TTS Text-to-Speech",
71
+ description=description,
72
+ article="Experience the power of Edge TTS for text-to-speech conversion, and explore our advanced Text-to-Video Converter for even more creative possibilities!",
73
+ analytics_enabled=False,
74
+ allow_flagging="manual",
75
+ api_name=None
76
+ )
77
+ return demo
78
+
79
+ async def main():
80
+ demo = await create_demo()
81
+ demo.queue(default_concurrency_limit=5)
82
+ demo.launch(show_api=False)
83
+
84
+ if __name__ == "__main__":
85
+ asyncio.run(main())