CoolFace
Apppublic

kemuriririn/IndexTTS

sourceHugging Faceupdated 1y agoView on Hugging Face
2likes
webui.py95 linesDownload Raw Back to root
1import spaces2import os3import shutil4import threading5import time6import sys7 8from huggingface_hub import snapshot_download9 10current_dir = os.path.dirname(os.path.abspath(__file__))11sys.path.append(current_dir)12sys.path.append(os.path.join(current_dir, "indextts"))13 14import gradio as gr15from indextts.infer import IndexTTS16from tools.i18n.i18n import I18nAuto17 18i18n = I18nAuto(language="zh_CN")19MODE = 'local'20snapshot_download("IndexTeam/IndexTTS-1.5",local_dir="checkpoints",)21tts = IndexTTS(model_dir="checkpoints", cfg_path="checkpoints/config.yaml")22 23os.makedirs("outputs/tasks",exist_ok=True)24os.makedirs("prompts",exist_ok=True)25 26@spaces.GPU27def infer(voice, text,output_path=None):28    if not tts:29        raise Exception("Model not loaded")30    if not output_path:31        output_path = os.path.join("outputs", f"spk_{int(time.time())}.wav")32    tts.infer(voice, text, output_path)33    return output_path34 35def tts_api(voice, text):36    try:37        output_path = infer(voice, text)38        with open(output_path, "rb") as f:39            audio_bytes = f.read()40        return (200, {}, audio_bytes)41    except Exception as e:42        return (500, {"error": str(e)}, None)43 44def gen_single(prompt, text):45    output_path = infer(prompt, text)46    return gr.update(value=output_path,visible=True)47 48def update_prompt_audio():49    update_button = gr.update(interactive=True)50    return update_button51 52with gr.Blocks() as demo:53    mutex = threading.Lock()54    gr.HTML('''55    <h2><center>IndexTTS: An Industrial-Level Controllable and Efficient Zero-Shot Text-To-Speech System</h2>56 57<p align="center">58<a href='https://arxiv.org/abs/2502.05512'><img src='https://img.shields.io/badge/ArXiv-2502.05512-red'></a>59    ''')60    with gr.Tab("音频生成"):61        with gr.Row():62            os.makedirs("prompts",exist_ok=True)63            prompt_audio = gr.Audio(label="请上传参考音频",key="prompt_audio",64                                    sources=["upload","microphone"],type="filepath")65            prompt_list = os.listdir("prompts")66            default = ''67            if prompt_list:68                default = prompt_list[0]69            input_text_single = gr.Textbox(label="请输入目标文本",key="input_text_single")70            gen_button = gr.Button("生成语音",key="gen_button",interactive=True)71            output_audio = gr.Audio(label="生成结果", visible=False,key="output_audio")72 73    prompt_audio.upload(update_prompt_audio,74                         inputs=[],75                         outputs=[gen_button])76 77    gen_button.click(gen_single,78                     inputs=[prompt_audio, input_text_single],79                     outputs=[output_audio])80 81    # 移除 Interface 相关内容,避免重复渲染82    # 只保留 Blocks demo,UI和API共用83    # 这样既有UI,也能通过Gradio HTTP API调用84    # 通过POST /run/predict即可API调用85 86    # 移除 add_api_route 和 mount_gradio_app,Spaces 不支持87 88def main():89    tts.load_normalizer()90    demo.launch(server_name="0.0.0.0", server_port=7860)91 92if __name__ == "__main__":93    main()94 95