Download app.py from fdbw/fzd2: direct link, hf CLI and curl.
- Browser
- Download file 1.31 kB
-
https://huggingface.co/spaces/fdbw/fzd2/resolve/main/app.py
- Command line
-
hf download hf://spaces/fdbw/fzd2/app.py
-
curl -L -o app.py https://huggingface.co/spaces/fdbw/fzd2/resolve/main/app.py
1.31 kB
| import gradio as gr | |
| from gtts import gTTS | |
| import os | |
| def text_to_speech(text, lang): | |
| if not text: | |
| return None | |
| # 映射界面显示的语言到 gTTS 缩写 | |
| lang_map = { | |
| "中文 (Chinese)": "zh-CN", | |
| "英语 (English)": "en", | |
| "法语 (French)": "fr", | |
| "日语 (Japanese)": "ja", | |
| "德语 (German)": "de", | |
| "西班牙语 (Spanish)": "es" | |
| } | |
| selected_lang = lang_map.get(lang, "en") | |
| # 生成语音 | |
| tts = gTTS(text=text, lang=selected_lang) | |
| output_file = "output.mp3" | |
| tts.save(output_file) | |
| return output_file | |
| # 定义 Gradio 界面 | |
| iface = gr.Interface( | |
| fn=text_to_speech, | |
| inputs=[ | |
| gr.Textbox(label="请输入文字", placeholder="在这里输入你想转换的内容..."), | |
| gr.Dropdown( | |
| choices=["中文 (Chinese)", "英语 (English)", "法语 (French)", "日语 (Japanese)", "德语 (German)", "西班牙语 (Spanish)"], | |
| label="选择语种", | |
| value="中文 (Chinese)" | |
| ) | |
| ], | |
| outputs=gr.Audio(label="生成声音", type="filepath"), | |
| title="多语种文字转语音 (TTS) 工具", | |
| description="输入文字并选择语言,点击提交即可生成并下载语音文件。" | |
| ) | |
| if __name__ == "__main__": | |
| iface.launch() |