当前位置:   article > 正文

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

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文件,记得将路径换为自己的

  1. # ChatTTS-01.py
  2. import ChatTTS
  3. import torch
  4. import torchaudio
  5. # 第一步下载的ChatTTS模型文件目录,请按照实际情况替换
  6. MODEL_PATH = '/home/cxh/ChatTTS-Model'
  7. # 初始化并加载模型,特别注意加载模型参数,官网样例代码已经过时,请使用下面代码
  8. chat = ChatTTS.Chat()
  9. chat.load_models(source='local', local_path='/home/cxh/ChatTTS-Model')
  10. # 需要转化为音频的文本内容
  11. text = '你好奥'
  12. # 文本转为音频
  13. wavs = chat.infer(text, use_decoder=True)
  14. # 保存音频文件到本地文件(采样率为24000Hz)
  15. torchaudio.save("./outputs/output-01.wav", torch.from_numpy(wavs[0]), 24000)

5.进行webui展示

  1. import random
  2. import ChatTTS
  3. import gradio as gr
  4. import numpy as np
  5. import torch
  6. from ChatTTS.infer.api import refine_text, infer_code
  7. print('启动ChatTTS WebUI......')
  8. # WebUI设置
  9. WEB_HOST = '127.0.0.1'
  10. WEB_PORT = 8089
  11. MODEL_PATH = '/home/cxh/ChatTTS-Model'
  12. chat = ChatTTS.Chat()
  13. chat.load_models(source='local', local_path='/home/cxh/ChatTTS-Model')
  14. def generate_seed():
  15. new_seed = random.randint(1, 100000000)
  16. return {
  17. "__type__": "update",
  18. "value": new_seed
  19. }
  20. def generate_audio(text, temperature, top_P, top_K, audio_seed_input, text_seed_input, refine_text_flag):
  21. torch.manual_seed(audio_seed_input)
  22. rand_spk = torch.randn(768)
  23. params_infer_code = {
  24. 'spk_emb': rand_spk,
  25. 'temperature': temperature,
  26. 'top_P': top_P,
  27. 'top_K': top_K,
  28. }
  29. params_refine_text = {'prompt': '[oral_2][laugh_0][break_6]'}
  30. torch.manual_seed(text_seed_input)
  31. text_tokens = refine_text(chat.pretrain_models, text, **params_refine_text)['ids']
  32. text_tokens = [i[i < chat.pretrain_models['tokenizer'].convert_tokens_to_ids('[break_0]')] for i in text_tokens]
  33. text = chat.pretrain_models['tokenizer'].batch_decode(text_tokens)
  34. # result = infer_code(chat.pretrain_models, text, **params_infer_code, return_hidden=True)
  35. print(f'ChatTTS微调文本:{text}')
  36. wav = chat.infer(text,
  37. params_refine_text=params_refine_text,
  38. params_infer_code=params_infer_code,
  39. use_decoder=True,
  40. skip_refine_text=True,
  41. )
  42. audio_data = np.array(wav[0]).flatten()
  43. sample_rate = 24000
  44. text_data = text[0] if isinstance(text, list) else text
  45. return [(sample_rate, audio_data), text_data]
  46. def main():
  47. with gr.Blocks() as demo:
  48. default_text = "大家好,我是老牛同学,微信公众号:老牛同学。很高兴与您相遇,专注于编程技术、大模型及人工智能等相关技术分享,欢迎关注和转发,让我们共同启程智慧之旅!"
  49. text_input = gr.Textbox(label="输入文本", lines=4, placeholder="Please Input Text...", value=default_text)
  50. with gr.Row():
  51. refine_text_checkbox = gr.Checkbox(label="文本微调开关", value=True)
  52. temperature_slider = gr.Slider(minimum=0.00001, maximum=1.0, step=0.00001, value=0.8, label="语音温度参数")
  53. top_p_slider = gr.Slider(minimum=0.1, maximum=0.9, step=0.05, value=0.7, label="语音top_P采样参数")
  54. top_k_slider = gr.Slider(minimum=1, maximum=20, step=1, value=20, label="语音top_K采样参数")
  55. with gr.Row():
  56. audio_seed_input = gr.Number(value=42, label="语音随机数")
  57. generate_audio_seed = gr.Button("\U0001F3B2")
  58. text_seed_input = gr.Number(value=42, label="文本随机数")
  59. generate_text_seed = gr.Button("\U0001F3B2")
  60. generate_button = gr.Button("文本生成语音")
  61. text_output = gr.Textbox(label="微调文本", interactive=False)
  62. audio_output = gr.Audio(label="语音")
  63. generate_audio_seed.click(generate_seed,
  64. inputs=[],
  65. outputs=audio_seed_input)
  66. generate_text_seed.click(generate_seed,
  67. inputs=[],
  68. outputs=text_seed_input)
  69. generate_button.click(generate_audio,
  70. inputs=[text_input, temperature_slider, top_p_slider, top_k_slider, audio_seed_input, text_seed_input, refine_text_checkbox],
  71. outputs=[audio_output, text_output, ])
  72. # 启动WebUI
  73. demo.launch(server_name='127.0.0.1', server_port=8089, share=False, show_api=False, )
  74. if __name__ == '__main__':
  75. main()

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/744005
推荐阅读
相关标签
  

闽ICP备14008679号