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
特别注意 :如果下载过程中,若出现找不到torch
的2.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()