轻量级tts,阅读英文

sequoia fb0eaf8716 修改程序说明 1 month ago
onnx 7f8c9baedc 把模型也放到仓库中 1 month ago
.gitignore 7f8c9baedc 把模型也放到仓库中 1 month ago
README.md 5de6276386 优化模型 1 month ago
benchmark_onnx_models.py 5de6276386 优化模型 1 month ago
doc.md fb0eaf8716 修改程序说明 1 month ago
requirements.txt 5de6276386 优化模型 1 month ago
speech_tts_cpu.py b6f1b87348 new 1 month ago
speech_tts_onnx_opt.py 7f8c9baedc 把模型也放到仓库中 1 month ago
start_onnx.sh 96d2d79a43 update opt onnx 1 month ago
start_tts_5.sh 37cb94baa4 onnx版本模型 1 month ago
tts_zh.py 0fde664b79 轻量级tts 2 months ago

README.md

TTS Server

这个仓库提供了 3 个基于 FastAPI 的 TTS 服务入口,分别对应中文直出、CPU 推理和 ONNX 优化推理。

文件说明

  • tts_zh.py:中文 TTS 接口,默认提供 /tts GET/POST。
  • speech_tts_cpu.py:CPU 版服务,提供 /generate/clear-cache/cache-info 等接口。
  • speech_tts_onnx_opt.py:ONNX 优化版服务,提供 /tts/generate/clear-cache/cache-info
  • start_tts_5.sh:启动 CPU 版服务。
  • start_onnx.sh:启动 ONNX 版服务。
  • benchmark_onnx_models.py:ONNX 模型性能测试脚本。
  • requirement.txt:Python 依赖清单。

安装依赖

建议使用 Python 3.11。

pip install -r requirement.txt

如果使用 ONNX 版本,还需要准备模型目录,默认路径如下:

  • TTS_ONNX_MODEL_DIR=/home/tts-server/onnx
  • TTS_ONNX_CONFIG_PATH=/home/tts-server/onnx/config.json
  • TTS_ONNX_VOICES_DIR=/home/tts-server/onnx/voices
  • TTS_ONNX_VOICES_V1_PATH=/home/tts-server/onnx/voices-v1.0.bin

默认模型名:

  • model_uint8.onnx

启动方式

1. 中文接口

uvicorn tts_zh:app --host 0.0.0.0 --port 18000 --workers 1

2. CPU 版

uvicorn speech_tts_cpu:app --host 0.0.0.0 --port 8028

或直接执行:

bash start_tts_5.sh

3. ONNX 优化版

uvicorn speech_tts_onnx_opt:app --host 0.0.0.0 --port 8028 --workers 1

或直接执行:

bash start_onnx.sh

常用环境变量

通用

  • CACHE_DIR:磁盘缓存目录,默认 ./audio_cache
  • LOG_LEVEL:日志级别,默认 INFO
  • MAX_CONCURRENT_REQUESTS:最大并发请求数
  • DISK_CACHE_SIZE:磁盘缓存保留数量
  • MEMORY_CACHE_SIZE:内存缓存大小

ONNX 版

  • TTS_ONNX_MODEL_NAME:模型文件名,默认 model_uint8.onnx
  • TTS_ONNX_MODEL_DIR:模型目录
  • TTS_ONNX_HF_MODEL_ID:默认 onnx-community/Kokoro-82M-ONNX
  • TTS_SAMPLE_RATE:采样率,默认 24000
  • ORT_INTRA_OP_THREADS:默认 2
  • ORT_INTER_OP_THREADS:默认 1

API 示例

/tts GET

curl "http://127.0.0.1:18000/tts?text=你好,欢迎使用语音服务&voice=zf_xiaoxiao&speed=1.0" --output tts.wav

/tts POST

curl -X POST "http://127.0.0.1:8028/tts" \
  -H "Content-Type: application/json" \
  -d '{"text":"Hello world","voice":"af_heart","speed":1.0}'

/generate POST

curl -X POST "http://127.0.0.1:8028/generate" \
  -H "Content-Type: application/json" \
  -d '{"text":"Hello world","voice":"af_heart","speed":1.0}'

说明

  • speech_tts_cpu.py 依赖 torch
  • speech_tts_onnx_opt.py 依赖 onnxruntimekokoro-onnx
  • ONNX 版本运行时如果本地缺少语音文件,会尝试从 Hugging Face 下载。