sysf commited on
Commit
d853483
1 Parent(s): cea48f0

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +125 -0
app.py ADDED
@@ -0,0 +1,125 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+
3
+
4
+ import azure.cognitiveservices.speech as speechsdk
5
+ import gradio as gr
6
+ import io
7
+
8
+ dict = {
9
+ "中文": "zh-CN",
10
+ "英语": "en-US",
11
+ "法语": "fr-FR",
12
+ "西班牙语": "es-ES",
13
+ "阿拉伯语": "ar-SA",
14
+ "葡萄牙语": "pt-PT",
15
+ "泰语": "th-TH",
16
+ "越南语": "vi-VN",
17
+ "俄语": "ru-RU",
18
+ "日语": "ja-JP",
19
+ "德语": "de-DE",
20
+ "印度尼西亚语": "id-ID",
21
+ "韩语": "ko-KR",
22
+ "菲律宾语": "fil-PH",
23
+ "意大利语": "it-IT",
24
+ "荷兰语": "nl-NL",
25
+ "波兰语": "pl-PL",
26
+ "瑞典语": "sv-SE",
27
+ "希伯来语":"he-IL",
28
+ "土耳其语": "tr-TR",
29
+ "马来语": "ms-MY",
30
+ "匈牙利语": "hu-HU",
31
+ "希腊语": "el-GR",
32
+ "捷克语": "cs-CZ",
33
+ "丹麦语": "da-DK",
34
+ "挪威语": "nb-NO",
35
+ "芬兰语": "fi-FI",
36
+ "斯洛文尼亚语": "sl-SI",
37
+ "爱沙尼亚语": "et-EE",
38
+ "拉脱维亚语": "lv-LV",
39
+ "立陶宛语": "lt-LT",
40
+ "克罗地亚语": "hr-HR",
41
+ "罗马尼亚语": "ro-RO",
42
+ "斯洛伐克语": "sk-SK",
43
+ "保加利亚语": "bg-BG",
44
+ "塞尔维亚语": "sr-RS",
45
+ "乌克兰语": "uk-UA",
46
+ "繁体中文": "zh-TW",
47
+ "印地语": "hi-IN",
48
+ "挪威博克马尔语": "nb-NO",
49
+ "波斯语": "fa-IR",
50
+ "罗马语": "rm-CH",
51
+ "斯瓦希里语": "sw-KE",
52
+ "孟加拉语": "bn-BD",
53
+ "波斯尼亚语": "bs-BA",
54
+ "加泰罗尼亚语": "ca-ES",
55
+ "克里奥尔语": "ht-HT",
56
+ "爱尔兰语": "ga-IE",
57
+ "卡纳达语": "kn-IN",
58
+ "哈萨克语": "kk-KZ",
59
+ "马其顿语": "mk-MK",
60
+ "马拉雅拉姆语": "ml-IN",
61
+ "毛利语": "mi-NZ",
62
+ "尼泊尔语": "ne-NP",
63
+ "普什图语": "ps-AF",
64
+ "旁遮普语": "pa-IN",
65
+ "萨摩亚语": "sm-WS",
66
+ "斯洛伐克语": "sk-SK",
67
+ "索马里语": "so-SO",
68
+ "瑞典语": "sv-SE",
69
+ "塔加洛语": "tl-PH",
70
+ "塔吉克语": "tg-TJ",
71
+ "泰米尔语": "ta-IN",
72
+ "泰卢固语": "te-IN",
73
+ "图库尔语": "tk-TM",
74
+ "乌尔都语": "ur-PK",
75
+ "乌兹别克语": "uz-UZ",
76
+ "威尔士语": "cy-GB",
77
+ "科西嘉语": "co-FR",
78
+ "弗里西语": "fy-NL",
79
+ "加利西亚语": "gl-ES",
80
+ }
81
+
82
+
83
+
84
+ def text_to_speech(text, language_code):
85
+ # Replace with your own subscription key and region identifier from Azure portal
86
+ subscription_key = "8e5ccacd6d424577b2dff566c0cd027f"
87
+ region = "eastus"
88
+
89
+ # Creates an instance of a speech config with specified subscription key and service region.
90
+ speech_config = speechsdk.SpeechConfig(subscription=subscription_key, region=region, speech_recognition_language=dict[language_code])
91
+
92
+ # Creates a speech synthesizer using the default speaker as audio output.
93
+ speech_synthesizer = speechsdk.SpeechSynthesizer(speech_config=speech_config)
94
+
95
+ # Synthesizes the received text to speech.
96
+ result = speech_synthesizer.speak_text_async(text).get()
97
+
98
+ # Checks result.
99
+ if result.reason == speechsdk.ResultReason.SynthesizingAudioCompleted:
100
+ audio_stream = io.BytesIO(result.audio_data)
101
+ file_path = save_audio(audio_stream)
102
+ return "Speech synthesized to speaker for text [{}]".format(text), file_path
103
+ elif result.reason == speechsdk.ResultReason.Canceled:
104
+ cancellation_details = result.cancellation_details
105
+ return "Speech synthesis canceled: {}".format(cancellation_details.reason), None
106
+ if cancellation_details.reason == speechsdk.CancellationReason.Error:
107
+ if cancellation_details.error_details:
108
+ return "Error details: {}".format(cancellation_details.error_details), None
109
+ return "Did you update the subscription info?", None
110
+
111
+
112
+ def save_audio(audio_stream):
113
+ file_path = "speech.wav"
114
+ with open(file_path, "wb") as f:
115
+ f.write(audio_stream.read())
116
+ return file_path
117
+
118
+
119
+ input_text = gr.inputs.Textbox(lines=5, label="Input Text")
120
+ output_text = gr.outputs.Textbox(label="Output Text")
121
+ output_audio = gr.outputs.Audio(type="filepath", label="导出文件")
122
+ language = gr.inputs.Dropdown(choices=list(dict.keys()), label="Language")
123
+ interface = gr.Interface(fn=text_to_speech, inputs=[input_text, language], outputs=[output_text, output_audio], title="微软文字转语音")
124
+
125
+ interface.launch(share=True)