import gradio as gr import os import tempfile from extra import discusscode,wccode import requests from langs import langs from speakers import match_speakers from languages import UText header_markdown = f"""
# {UText("Gradio 版 OpenAI/Edge 文字转语音")}
""" def generate_voice(voice, text): print(voice,text) voice = voice.split(' #')[0] if not text: gr.Warning(UText("没有文本")) return None if not voice: gr.Warning(UText("请选择发音人")) return None key = os.getenv("KEY") API = os.getenv("API") if not API: API = "http://171.115.221.19:18503/freeTTS" url = f"{API}/{voice}" print(url) headers = { 'Authorization': f'Bearer {key}' } params = {'text': text} response = requests.get(url, headers=headers, params=params) if response.status_code == 200: # Success print(f"{voice}: \n{text} ") if len(response.content) == 0: gr.Warning(UText("声音文件为空")) return None # Save the received content as an MP3 file with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as temp_file: temp_file.write(response.content) temp_file_path = temp_file.name return temp_file_path else: # Error handling print(f"Error: {response.status_code}") gr.Warning(f"{response.json()}") print(response.json()) # You can also check the error details gr.Error(UText("遇到未知错误,联系作者处理")) return None def lang_chose_select(lang_chose,sex): return gr.Dropdown(choices=match_speakers(langs=lang_chose,sex=sex), label=UText('选择发音人'), value='',scale=6) with gr.Blocks() as demo: gr.Markdown(header_markdown) text = gr.TextArea(label=UText("文本"), placeholder=UText("在这里输入文本")) with gr.Row(): lang_chose = gr.Dropdown(choices=langs, label=UText('选择语言'), value=["Chinese"], multiselect=True,scale=12) sex = gr.Dropdown(choices=["Male","Female"], label=UText('选择性别'), value=["Male","Female"], multiselect=True,scale=6) voice = gr.Dropdown(choices=match_speakers(langs=["Chinese"],sex=["Male","Female"]), label=UText('选择发音人'), value='',scale=6) btn = gr.Button(UText("合成的语音"),scale=4) with gr.Row(): output_audio = gr.Audio(label=UText("合成的语音")) gr.Markdown(UText("tailer_markdown")) lang_chose.select(lang_chose_select,inputs=[lang_chose,sex], outputs=voice) sex.select(lang_chose_select,inputs=[lang_chose,sex], outputs=voice) btn.click(fn=generate_voice, inputs=[voice,text], outputs=output_audio, api_name="tts_button", concurrency_limit=None) demo.launch()