gcw_mXoV1I2Z/chatterbox
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

Chatterbox TTS — 昇腾 NPU 适配

ResembleAI/chatterbox 文本转语音(TTS)模型的 昇腾 NPU (Ascend 910 / torch-npu) 适配工程。

  • 模型: ResembleAI/chatterbox(ChatterboxTTS,0.5B Llama backbone + S3Gen 声码器)
  • 协议: MIT
  • 设备: npu:0(ASCEND_VISIBLE_DEVICES=12)
  • 框架: PyTorch 2.9 + torch_npu 2.9 + transformers

特性

  • 全流程运行在昇腾 NPU(T3 LM 文本→token 生成 + S3Gen token→波形解码),全域零 CUDA 语句。
  • 双模式推理:
    • CLI: python inference.py --text "合成文本"(兼容 --img 别名)
    • FastAPI: uvicorn inference:app --host 0.0.0.0 --port 8000,POST /predict 接收 {"text": "..."},返回 wav 路径 / base64 / 时长 / 采样率等 JSON。
  • 内置声音条件(conds.pt),开箱即用,无需参考音频。
  • 输出带 Resemble AI Perth 水印(默认启用,与官方行为一致)。

环境要求

组件版本
硬件Ascend 910(≥1 卡)
CANN≥ 8.0(本机 8.5.1)
Python3.10 / 3.11
PyTorch2.9.0
torch_npu2.9.0.post1
export PIP_INDEX_URL=https://repo.huaweicloud.com/repository/pypi/simple/
pip install -r requirements.txt

权重

权重缓存于 ./model_weights/chatterbox(本地优先,非空直接复用)。

export HF_ENDPOINT=https://hf-mirror.com
python -c "
from huggingface_hub import snapshot_download
snapshot_download('ResembleAI/chatterbox', local_dir='./model_weights/chatterbox',
                  allow_patterns=['ve.safetensors','t3_cfg.safetensors','s3gen.safetensors','tokenizer.json','conds.pt','README.md'])
"

CLI 使用

export ASCEND_VISIBLE_DEVICES=12
python inference.py --text "Hello, this is a test of the chatterbox text to speech model running on Ascend NPU."

输出 JSON(含 wav_path、base64_wav、duration_s、sample_rate 等),合成 wav 保存于工程目录 output.wav。

FastAPI 使用

export ASCEND_VISIBLE_DEVICES=12
uvicorn inference:app --host 0.0.0.0 --port 8000 &
curl -X POST http://127.0.0.1:8000/predict \
  -H 'Content-Type: application/json' \
  -d '{"text": "Hello, this is a test of the chatterbox text to speech model running on Ascend NPU."}'

文件说明

文件说明
inference.py双模式推理脚本(CLI + FastAPI)
requirements.txtNPU 运行依赖
model_weights/chatterbox/本地权重缓存
assets/agent_workflow.pngNPU 部署安装日志截图
assets/npu_device_call.pngCLI 推理成功截图
assets/model_result.pngFastAPI 返回 JSON 截图

适配要点

  1. 入口脚本顶部 import torch_npu + torch.npu.set_device(0) + DEVICE="npu:0",try-except 捕获 NPU 初始化异常。
  2. 模型与全部输入 Tensor 迁移 npu:0,全域零 CUDA 语句。
  3. ChatterboxTTS.from_local 加载权重(safetensors 加载到 CPU 后 .to("npu:0"))。
  4. 水印器 perth 为纯 numpy 实现,CPU 运行,不依赖 GPU。
  5. 采样率 24000 Hz(S3GEN_SR)。