> For the complete documentation index, see [llms.txt](https://docs.clore.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.clore.ai/guides/guides_v2-zh/yin-pin-yu-yu-yin/chattts.md).

# ChatTTS 对话语音

在 Clore.ai GPU 上运行带精细韵律控制的 ChatTTS 对话式文字转语音。

ChatTTS 是一个拥有 3 亿参数的生成式语音模型，针对对话场景进行了优化，例如 LLM 助手、聊天机器人和交互式语音应用。它能够生成听起来自然的语音，包含真实的停顿、笑声、填充词和语调——这些特征大多数 TTS 系统都难以复现。该模型支持英语和中文，并以 24 kHz 生成音频。

**GitHub：** [2noise/ChatTTS](https://github.com/2noise/ChatTTS) （3万+ 星标） **许可证：** AGPLv3+（代码），CC BY-NC 4.0（模型权重——非商业用途）

## 主要特性

* **对话韵律** —— 为对话调校的自然停顿、填充词和语调
* **细粒度控制标签** — `[oral_0-9]`, `[laugh_0-2]`, `[break_0-7]`, `[uv_break]`, `[lbreak]`
* **多说话人** —— 可随机采样说话人，或复用说话人嵌入以保持一致性
* **Temperature / top-P / top-K** —— 控制生成多样性
* **批量推理** —— 一次调用合成多段文本
* **轻量级** —— 约 3 亿参数，可在 4 GB VRAM 上运行

## 需求

| 组件     | 最低                | 推荐                  |
| ------ | ----------------- | ------------------- |
| GPU    | RTX 3060（4 GB 空闲） | RTX 3090 / RTX 4090 |
| 显存     | 4 GB              | 8 GB+               |
| 内存     | 8 GB              | 16 GB               |
| 磁盘     | 5 GB              | 10 GB               |
| Python | 3.9+              | 3.11                |
| CUDA   | 12.8+             | 12.8+               |

**Clore.ai 推荐：** RTX 3060（$0.03–0.07/小时）即可轻松运行 ChatTTS。若用于批量生产或更低延迟，选择 RTX 3090（$0.07–0.21/小时）。

## 安装

```bash
# 从 PyPI 安装
pip install ChatTTS torch torchaudio

# 或从源码安装以获取最新功能
git clone https://github.com/2noise/ChatTTS.git
cd ChatTTS
pip install -r requirements.txt

# 验证 GPU
python -c "import torch; print(torch.cuda.get_device_name(0))"
```

## 快速开始

```python
import ChatTTS
import torch
import torchaudio

# 初始化并加载模型（首次运行时会下载权重）
chat = ChatTTS.Chat()
chat.load(compile=False)  # 在预热后将 compile=True 可加快推理速度

texts = [
    "嘿！你今天过得怎么样？",
    "我整个上午都在做这个项目。进展很顺利。",
]

wavs = chat.infer(texts)

for i, wav in enumerate(wavs):
    audio_tensor = torch.from_numpy(wav)
    if audio_tensor.dim() == 1:
        audio_tensor = audio_tensor.unsqueeze(0)
    torchaudio.save(f"output_{i}.wav", audio_tensor, 24000)
    print(f"已保存 output_{i}.wav")
```

## 使用示例

### 一致的说话人声音

采样一个随机说话人嵌入，并在多次生成中复用它，以获得一致的声音：

```python
import ChatTTS
import torch
import torchaudio

chat = ChatTTS.Chat()
chat.load(compile=False)

# 采样一个说话人——将这个字符串保存起来，便于以后复用
rand_spk = chat.sample_random_speaker()

params_infer_code = ChatTTS.Chat.InferCodeParams(
    spk_emb=rand_spk,
    temperature=0.3,
    top_P=0.7,
    top_K=20,
)

params_refine_text = ChatTTS.Chat.RefineTextParams(
    prompt='[oral_2][laugh_0][break_4]',
)

texts = ["欢迎收听今天的节目。让我告诉你一些令人兴奋的事情。"]

wavs = chat.infer(
    texts,
    params_refine_text=params_refine_text,
    params_infer_code=params_infer_code,
)

audio = torch.from_numpy(wavs[0])
if audio.dim() == 1:
    audio = audio.unsqueeze(0)
torchaudio.save("consistent_speaker.wav", audio, 24000)
```

### 词级控制标签

将控制标签直接插入文本中，以实现精确的韵律控制：

```python
import ChatTTS
import torch
import torchaudio

chat = ChatTTS.Chat()
chat.load(compile=False)

# 标签：[uv_break] = 短停顿，[laugh] = 笑声，[lbreak] = 长停顿
text = 'What is [uv_break]your favorite food?[laugh][lbreak]'

rand_spk = chat.sample_random_speaker()
params = ChatTTS.Chat.InferCodeParams(spk_emb=rand_spk, temperature=0.3)

# skip_refine_text=True 可保留你手动添加的控制标签
wavs = chat.infer(text, skip_refine_text=True, params_infer_code=params)

audio = torch.from_numpy(wavs[0])
if audio.dim() == 1:
    audio = audio.unsqueeze(0)
torchaudio.save("controlled_output.wav", audio, 24000)
```

### 通过 WebUI 批量处理

ChatTTS 自带 Gradio 网页界面，便于交互式使用：

```bash
cd ChatTTS
python examples/web/webui.py --server_name 0.0.0.0 --server_port 7860
```

打开 `http_pub` Clore.ai 订单仪表盘中的 URL 以访问 UI。

## 给 Clore.ai 用户的建议

* **使用 `compile=True`** 在初步测试之后——PyTorch 编译会增加启动时间，但能显著加快重复推理
* **端口映射** —— 暴露端口 `7860/http` 在使用 WebUI 部署时
* **Docker 镜像** — 使用 `pytorch/pytorch:2.11.0-cuda12.8-cudnn9-runtime` 作为基础
* **说话人持久化** —— 保存 `rand_spk` 字符串到文件中，这样你就可以在不同会话之间复用声音，而无需重新采样
* **批量处理你的请求** — `chat.infer()` 接受文本列表并一起处理，这比逐个调用更高效
* **非商业许可** —— 模型权重采用 CC BY-NC 4.0；请根据你的使用场景检查许可要求

## 故障排查

| 问题                     | 解决方案                                                         |
| ---------------------- | ------------------------------------------------------------ |
| `CUDA 内存不足`            | 减少批量大小，或使用 ≥ 6 GB VRAM 的 GPU                                 |
| 模型下载较慢                 | 从 HuggingFace 预下载： `huggingface-cli download 2Noise/ChatTTS` |
| 音频有静电声/噪声              | 这是开源模型中的刻意设计（防滥用措施）；使用 `compile=True` 以获得更干净的输出              |
| `torchaudio.save` 维度错误 | 确保张量是二维的： `audio.unsqueeze(0)` 如有需要                          |
| 中文输出乱码                 | 确保输入文本使用 UTF-8 编码；安装 `WeTextProcessing` 以获得更好的规范化效果          |
| 首次推理较慢                 | 正常——模型编译和权重加载会在首次调用时发生；后续调用会更快                               |


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.clore.ai/guides/guides_v2-zh/yin-pin-yu-yu-yin/chattts.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
