ChatTTS 开源文本转语音模型本地部署

1.下载模型文件

git lfs install

git clone https://www.modelscope.cn/pzc163/chatTTS.git ChatTTS-Model

2.下载chatTTS源码

git clone https://gitcode.com/2noise/ChatTTS.git ChatTTS

3.进入源码目录,批量安装Python依赖包

pip install -r requirements.txt

特别注意 :如果下载过程中,若出现找不到torch2.1.0 版本错误,请修改requirements.txt文件,把torch的版本修改为2.2.2后再次执行安装:

omegaconf~=2.3.0

torch~=2.2.2

tqdm

einops

vector_quantize_pytorch

transformers~=4.41.1

vocos

IPython

4.运行测试py文件,记得将路径换为自己的

# ChatTTS-01.py

import ChatTTS
import torch
import torchaudio

# 第一步下载的ChatTTS模型文件目录,请按照实际情况替换
MODEL_PATH = '/home/cxh/ChatTTS-Model'

# 初始化并加载模型,特别注意加载模型参数,官网样例代码已经过时,请使用下面代码
chat = ChatTTS.Chat()
chat.load_models(source='local', local_path='/home/cxh/ChatTTS-Model')

# 需要转化为音频的文本内容
text = '你好奥'

# 文本转为音频
wavs = chat.infer(text, use_decoder=True)

# 保存音频文件到本地文件(采样率为24000Hz)
torchaudio.save("./outputs/output-01.wav", torch.from_numpy(wavs[0]), 24000)

5.进行webui展示

import random

import ChatTTS
import gradio as gr
import numpy as np
import torch
from ChatTTS.infer.api import refine_text, infer_code

print('启动ChatTTS WebUI......')

# WebUI设置
WEB_HOST = '127.0.0.1'
WEB_PORT = 8089

MODEL_PATH = '/home/cxh/ChatTTS-Model'

chat = ChatTTS.Chat()
chat.load_models(source='local', local_path='/home/cxh/ChatTTS-Model')


def generate_seed():
    new_seed = random.randint(1, 100000000)
    return {
        "__type__": "update",
        "value": new_seed
    }


def generate_audio(text, temperature, top_P, top_K, audio_seed_input, text_seed_input, refine_text_flag):
    torch.manual_seed(audio_seed_input)
    rand_spk = torch.randn(768)
    params_infer_code = {
        'spk_emb': rand_spk,
        'temperature': temperature,
        'top_P': top_P,
        'top_K': top_K,
    }
    params_refine_text = {'prompt': '[oral_2][laugh_0][break_6]'}

    torch.manual_seed(text_seed_input)

    text_tokens = refine_text(chat.pretrain_models, text, **params_refine_text)['ids']
    text_tokens = [i[i < chat.pretrain_models['tokenizer'].convert_tokens_to_ids('[break_0]')] for i in text_tokens]
    text = chat.pretrain_models['tokenizer'].batch_decode(text_tokens)
    # result = infer_code(chat.pretrain_models, text, **params_infer_code, return_hidden=True)

    print(f'ChatTTS微调文本:{text}')

    wav = chat.infer(text,
                     params_refine_text=params_refine_text,
                     params_infer_code=params_infer_code,
                     use_decoder=True,
                     skip_refine_text=True,
                     )

    audio_data = np.array(wav[0]).flatten()
    sample_rate = 24000
    text_data = text[0] if isinstance(text, list) else text

    return [(sample_rate, audio_data), text_data]


def main():
    with gr.Blocks() as demo:
        default_text = "大家好,我是老牛同学,微信公众号:老牛同学。很高兴与您相遇,专注于编程技术、大模型及人工智能等相关技术分享,欢迎关注和转发,让我们共同启程智慧之旅!"
        text_input = gr.Textbox(label="输入文本", lines=4, placeholder="Please Input Text...", value=default_text)

        with gr.Row():
            refine_text_checkbox = gr.Checkbox(label="文本微调开关", value=True)
            temperature_slider = gr.Slider(minimum=0.00001, maximum=1.0, step=0.00001, value=0.8, label="语音温度参数")
            top_p_slider = gr.Slider(minimum=0.1, maximum=0.9, step=0.05, value=0.7, label="语音top_P采样参数")
            top_k_slider = gr.Slider(minimum=1, maximum=20, step=1, value=20, label="语音top_K采样参数")

        with gr.Row():
            audio_seed_input = gr.Number(value=42, label="语音随机数")
            generate_audio_seed = gr.Button("\U0001F3B2")
            text_seed_input = gr.Number(value=42, label="文本随机数")
            generate_text_seed = gr.Button("\U0001F3B2")

        generate_button = gr.Button("文本生成语音")

        text_output = gr.Textbox(label="微调文本", interactive=False)
        audio_output = gr.Audio(label="语音")

        generate_audio_seed.click(generate_seed,
                                  inputs=[],
                                  outputs=audio_seed_input)

        generate_text_seed.click(generate_seed,
                                 inputs=[],
                                 outputs=text_seed_input)

        generate_button.click(generate_audio,
                              inputs=[text_input, temperature_slider, top_p_slider, top_k_slider, audio_seed_input, text_seed_input, refine_text_checkbox],
                              outputs=[audio_output, text_output, ])

    # 启动WebUI
    demo.launch(server_name='127.0.0.1', server_port=8089, share=False, show_api=False, )


if __name__ == '__main__':
    main()

文章内容来源:
ChatTTS 开源文本转语音模型本地部署、API使用和搭建WebUI界面(建议收藏)

相关推荐
deephub30 分钟前
优化注意力层提升 Transformer 模型效率:通过改进注意力机制降低机器学习成本
人工智能·深度学习·transformer·大语言模型·注意力机制
搏博42 分钟前
神经网络问题之二:梯度爆炸(Gradient Explosion)
人工智能·深度学习·神经网络
KGback1 小时前
【论文解析】HAQ: Hardware-Aware Automated Quantization With Mixed Precision
人工智能
电子手信1 小时前
知识中台在多语言客户中的应用
大数据·人工智能·自然语言处理·数据挖掘·知识图谱
不高明的骗子1 小时前
【深度学习之一】2024最新pytorch+cuda+cudnn下载安装搭建开发环境
人工智能·pytorch·深度学习·cuda
Chef_Chen1 小时前
从0开始学习机器学习--Day33--机器学习阶段总结
人工智能·学习·机器学习
搏博1 小时前
神经网络问题之:梯度不稳定
人工智能·深度学习·神经网络
GL_Rain1 小时前
【OpenCV】Could NOT find TIFF (missing: TIFF_LIBRARY TIFF_INCLUDE_DIR)
人工智能·opencv·计算机视觉
shansjqun2 小时前
教学内容全覆盖:航拍杂草检测与分类
人工智能·分类·数据挖掘
狸克先生2 小时前
如何用AI写小说(二):Gradio 超简单的网页前端交互
前端·人工智能·chatgpt·交互