> 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/kokoro-tts.md).

# Kokoro TTS

在 Clore.ai GPU 上运行 Kokoro TTS——一款超轻量级 8200 万参数的文字转语音模型。

Kokoro 是一款拥有 8200 万参数的文本转语音模型，性能远超其体量。尽管体积很小（VRAM 占用不到 2 GB），它仍能生成非常自然的英语语音，并且即使在入门级硬件上也能以实时或更快的速度运行。凭借 Apache 2.0 许可、内置多种音色风格以及对 CPU 推理的支持，Kokoro 非常适合实时应用、聊天机器人和边缘部署。

**HuggingFace：** [hexgrad/Kokoro-82M](https://huggingface.co/hexgrad/Kokoro-82M) **PyPI：** [kokoro](https://pypi.org/project/kokoro/) **许可证：** Apache 2.0

## 主要特性

* **8200 万参数** —— 现有最小的高质量 TTS 模型之一
* **< 2 GB VRAM** —— 几乎可在任何 GPU 上运行，甚至可以在 CPU 上运行
* **多种音色风格** —— 美式英语、英式英语；男声和女声
* **实时或更快** —— 适合流式传输的低延迟推理
* **流式生成** —— 在音频块生成时即可输出
* **多语言支持** —— 英语（主要）、日语（`misaki[ja]`）、中文（`misaki[zh]`)
* **Apache 2.0** — 可免费用于个人和商业用途

## 需求

| 组件     | 最低                 | 推荐       |
| ------ | ------------------ | -------- |
| GPU    | 任何具备 2 GB VRAM 的设备 | RTX 3060 |
| 显存     | 2 GB               | 4 GB     |
| 内存     | 4 GB               | 8 GB     |
| 磁盘     | 500 MB             | 1 GB     |
| Python | 3.9+               | 3.11     |
| 系统     | 已安装 espeak-ng      | —        |

**Clore.ai 推荐：** 一块 RTX 3060（$0.03–0.07/小时）就绰绰有余。Kokoro 甚至可以在仅 CPU 实例上运行，实现极具成本效益的 TTS。

## 安装

```bash
# 安装系统依赖
apt-get install -y espeak-ng

# 安装 Kokoro 和音频 I/O
pip install kokoro>=0.9.4 soundfile torch

# 日语支持（可选）
pip install misaki[ja]

# 中文支持（可选）
pip install misaki[zh]

# 验证
python -c "from kokoro import KPipeline; print('Kokoro 已就绪')"
```

## 快速开始

```python
from kokoro import KPipeline
import soundfile as sf

# 初始化流水线
# 'a' = 美式英语, 'b' = 英式英语
pipeline = KPipeline(lang_code='a')

text = """
Kokoro 是一款轻量级文本转语音模型，参数仅有八千二百万。
尽管体积很小，它仍能生成自然且富有表现力的语音。
"""

# 生成音频 — 音色选项：af_heart, af_bella, af_nicole, af_sarah, af_sky,
#                                  am_adam, am_michael, bf_emma, bf_isabella, bm_george, bm_lewis
generator = pipeline(text, voice='af_heart', speed=1.0)

for i, (graphemes, phonemes, audio) in enumerate(generator):
    sf.write(f'output_{i}.wav', audio, 24000)
    print(f"第 {i} 段：{graphemes[:50]}...")

print("完成！")
```

## 使用示例

### 多种音色对比

使用不同音色生成相同文本进行比较：

```python
from kokoro import KPipeline
import soundfile as sf

pipeline = KPipeline(lang_code='a')

text = "欢迎来到 Clore.ai，一个点对点 GPU 市场。"

voices = ['af_heart', 'af_bella', 'am_adam', 'am_michael']

for voice in voices:
    generator = pipeline(text, voice=voice, speed=1.0)
    for i, (gs, ps, audio) in enumerate(generator):
        sf.write(f'{voice}_{i}.wav', audio, 24000)
    print(f"已生成：{voice}")
```

### 英式英语与速度控制

```python
from kokoro import KPipeline
import soundfile as sf

# 'b' = 英式英语
pipeline = KPipeline(lang_code='b')

text = "下午好。这是英式英语合成的演示。"

# speed < 1.0 = 更慢, speed > 1.0 = 更快
generator = pipeline(text, voice='bf_emma', speed=0.85)

all_audio = []
for gs, ps, audio in generator:
    all_audio.append(audio)

import numpy as np
combined = np.concatenate(all_audio)
sf.write('british_slow.wav', combined, 24000)
print(f"总时长：{len(combined)/24000:.1f} 秒")
```

### 批量文件处理

处理多个文本并将其合并为单个有声书风格文件：

```python
from kokoro import KPipeline
import soundfile as sf
import numpy as np

pipeline = KPipeline(lang_code='a')

chapters = [
    "第一章。我们的旅程从这里开始。"，
    "太阳从群山上升起，在山谷中投下长长的影子。"，
    "她打开门，走进了未知。"，
]

all_audio = []
silence = np.zeros(int(24000 * 0.5))  # 章节之间 0.5 秒静音

for idx, text in enumerate(chapters):
    for gs, ps, audio in pipeline(text, voice='af_bella', speed=1.0):
        all_audio.append(audio)
    all_audio.append(silence)
    print(f"第 {idx+1} 章完成")

combined = np.concatenate(all_audio)
sf.write('audiobook.wav', combined, 24000)
print(f"总计：{len(combined)/24000:.1f} 秒")
```

## 给 Clore.ai 用户的建议

* **CPU 推理** —— Kokoro 足够小，可以在 CPU 上运行；适用于对成本敏感的工作负载或没有可用 GPU 的情况
* **流式输出** —— 生成器会在音频块产生时将其输出，从而支持网页应用中的实时播放
* **与 WhisperX 结合** —— 在语音流水线中使用 WhisperX 进行转写，使用 Kokoro 进行重新合成
* **Docker** — 使用 `pytorch/pytorch:2.11.0-cuda12.8-cudnn9-runtime` 并添加 `apt-get install -y espeak-ng` 为你的初创项目
* **音色一致性** —— 每个项目坚持使用同一个音色 ID，以获得一致的叙述者体验
* **成本效益** —— 在 RTX 3060 上仅需 $0.03–0.07/小时，Kokoro 是最便宜的自托管 TTS 解决方案之一

## 故障排查

| 问题                            | 解决方案                                             |
| ----------------------------- | ------------------------------------------------ |
| `未找到 espeak-ng`               | 运行 `apt-get install -y espeak-ng` （所需系统依赖）       |
| `ModuleNotFoundError: kokoro` | 使用以下方式安装 `pip install kokoro>=0.9.4 soundfile`   |
| 音频听起来很机械                      | 尝试更换音色（例如， `af_heart` 往往听起来最自然）                  |
| 日语/中文无法工作                     | 安装语言扩展包： `pip install misaki[ja]` 或 `misaki[zh]` |
| CPU 内存不足                      | 减少每次调用的文本长度；Kokoro 会流式输出音频块，因此内存占用保持受限           |
| 首次运行较慢                        | 模型权重在首次使用时下载（约 200 MB）；后续运行将是即时的                 |


---

# 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/kokoro-tts.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.
