Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from gtts import gTTS
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
def text_to_speech(text, lang):
|
| 6 |
+
if not text:
|
| 7 |
+
return None
|
| 8 |
+
|
| 9 |
+
# 映射界面显示的语言到 gTTS 缩写
|
| 10 |
+
lang_map = {
|
| 11 |
+
"中文 (Chinese)": "zh-CN",
|
| 12 |
+
"英语 (English)": "en",
|
| 13 |
+
"法语 (French)": "fr",
|
| 14 |
+
"日语 (Japanese)": "ja",
|
| 15 |
+
"德语 (German)": "de",
|
| 16 |
+
"西班牙语 (Spanish)": "es"
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
selected_lang = lang_map.get(lang, "en")
|
| 20 |
+
|
| 21 |
+
# 生成语音
|
| 22 |
+
tts = gTTS(text=text, lang=selected_lang)
|
| 23 |
+
output_file = "output.mp3"
|
| 24 |
+
tts.save(output_file)
|
| 25 |
+
|
| 26 |
+
return output_file
|
| 27 |
+
|
| 28 |
+
# 定义 Gradio 界面
|
| 29 |
+
iface = gr.Interface(
|
| 30 |
+
fn=text_to_speech,
|
| 31 |
+
inputs=[
|
| 32 |
+
gr.Textbox(label="请输入文字", placeholder="在这里输入你想转换的内容..."),
|
| 33 |
+
gr.Dropdown(
|
| 34 |
+
choices=["中文 (Chinese)", "英语 (English)", "法语 (French)", "日语 (Japanese)", "德语 (German)", "西班牙语 (Spanish)"],
|
| 35 |
+
label="选择语种",
|
| 36 |
+
value="中文 (Chinese)"
|
| 37 |
+
)
|
| 38 |
+
],
|
| 39 |
+
outputs=gr.Audio(label="生成声音", type="filepath"),
|
| 40 |
+
title="多语种文字转语音 (TTS) 工具",
|
| 41 |
+
description="输入文字并选择语言,点击提交即可生成并下载语音文件。"
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
if __name__ == "__main__":
|
| 45 |
+
iface.launch()
|