yuping322 commited on
Commit
eebf6da
·
1 Parent(s): b09510b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +108 -0
app.py ADDED
@@ -0,0 +1,108 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import edge_tts
3
+ import asyncio
4
+ import os
5
+ # https://speech.platform.bing.com/consumer/speech/synthesize/readaloud/voices/list?trustedclienttoken=6A5AA1D4EAFF4E9FB37E23D68491D6F4
6
+ SUPPORTED_VOICES = {
7
+ 'Xiaoxiao-晓晓': 'zh-CN-XiaoxiaoNeural',
8
+ 'Xiaoyi-晓伊': 'zh-CN-XiaoyiNeural',
9
+ 'Yunjian-云健': 'zh-CN-YunjianNeural',
10
+ 'Yunxi-云希': 'zh-CN-YunxiNeural',
11
+ 'Yunxia-云夏': 'zh-CN-YunxiaNeural',
12
+ 'Yunyang-云扬': 'zh-CN-YunyangNeural',
13
+ 'liaoning-Xiaobei-晓北辽宁': 'zh-CN-liaoning-XiaobeiNeural',
14
+ 'shaanxi-Xiaoni-陕西晓妮': 'zh-CN-shaanxi-XiaoniNeural'
15
+ }
16
+
17
+ # 发音切换
18
+ def changeVoice(voices):
19
+ example = SUPPORTED_VOICES[voices]
20
+ example_file = os.path.join(os.path.dirname(__file__), "example/"+example+".wav")
21
+ return example_file
22
+
23
+ # 文本转语音
24
+ async def textToSpeech(text, voices, rate, volume):
25
+ output_file = "output.mp3"
26
+ voices = SUPPORTED_VOICES[voices]
27
+ if (rate >= 0):
28
+ rates = rate = "+" + str(rate) + "%"
29
+ else:
30
+ rates = str(rate) + "%"
31
+ if (volume >= 0):
32
+ volumes = "+" + str(volume) + "%"
33
+ else:
34
+ volumes = str(volume) + "%"
35
+ communicate = edge_tts.Communicate(text,
36
+ voices,
37
+ rate=rates,
38
+ volume=volumes,
39
+ proxy=None)
40
+ await communicate.save(output_file)
41
+ audio_file = os.path.join(os.path.dirname(__file__), "output.mp3")
42
+ if (os.path.exists(audio_file)):
43
+ return audio_file
44
+ else:
45
+ raise gr.Error("转换失败!")
46
+ return FileNotFoundError
47
+
48
+
49
+ # 清除转换结果
50
+ def clearSpeech():
51
+ output_file = os.path.join(os.path.dirname(__file__), "output.mp3")
52
+ if (os.path.exists(output_file)):
53
+ os.remove(output_file)
54
+ return None, None
55
+
56
+
57
+ with gr.Blocks(css="style.css", title="文本转语音") as demo:
58
+ gr.Markdown("""
59
+ # 微软Edge文本转语音
60
+ 调用edge-tts 进行转换
61
+ """)
62
+ with gr.Row():
63
+ with gr.Column():
64
+ text = gr.TextArea(label="文本", elem_classes="text-area")
65
+ btn = gr.Button("生成", elem_id="submit-btn")
66
+ with gr.Column():
67
+ voices = gr.Dropdown(choices=[
68
+ "Xiaoxiao-晓晓", "Xiaoyi-晓伊", "Yunjian-云健", "Yunxi-云希",
69
+ "Yunxia-云夏", "Yunyang-云扬", "liaoning-Xiaobei-晓北辽宁",
70
+ "shaanxi-Xiaoni-陕西晓妮"
71
+ ],
72
+ value="Xiaoxiao-晓晓",
73
+ label="发音",
74
+ info="请选择发音人",
75
+ interactive=True)
76
+
77
+ example = gr.Audio(label="试听",
78
+ value="example/zh-CN-XiaoxiaoNeural.wav",
79
+ interactive=False,
80
+ elem_classes="example")
81
+
82
+ voices.change(fn=changeVoice,inputs=voices,outputs=example)
83
+ rate = gr.Slider(-100,
84
+ 100,
85
+ step=1,
86
+ value=0,
87
+ label="语速增减",
88
+ info="加快或减慢语速",
89
+ interactive=True)
90
+
91
+ volume = gr.Slider(-100,
92
+ 100,
93
+ step=1,
94
+ value=0,
95
+ label="音调增减",
96
+ info="加大或减小音调",
97
+ interactive=True)
98
+ audio = gr.Audio(label="输出",
99
+ interactive=False,
100
+ elem_classes="audio")
101
+ clear = gr.Button("清除", elem_id="clear-btn")
102
+ btn.click(fn=textToSpeech,
103
+ inputs=[text, voices, rate, volume],
104
+ outputs=[audio])
105
+ clear.click(fn=clearSpeech, outputs=[text, audio])
106
+
107
+ if __name__ == "__main__":
108
+ demo.launch()