File size: 1,310 Bytes
d5c57fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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()