一个简单的用于运行 XTTSv2 的 FastAPI 服务器
本项目受 silero-api-server 启发,并使用了 XTTSv2。
该服务器是为 SillyTavern 创建的,但你可以将其用于自己的需求。
欢迎提交 PR 或将代码用于你自己的需求。
有一个 google collab 版本,如果你的电脑配置较低,可以使用它。
如果你在寻找普通 XTTS 的选项,请查看这里 https://github.com/daswer123/xtts-webui
最近我很少有时间做这个项目,所以我建议你了解一下 类似的项目
更新日志
你可以在 发布页面 上跟踪所有更改。
待办事项
- 允许通过生成请求和不同的端点更改生成参数
安装
简单安装:
pip install xtts-api-server
这将安装所有必要的依赖项,包括 仅支持 CPU 版本的 PyTorch
我建议您安装 GPU 版本 以提高处理速度(最多快 3 倍)
Windows
python -m venv venv
venv\Scripts\activate
pip install xtts-api-server
pip install torch==2.1.1+cu118 torchaudio==2.1.1+cu118 --index-url https://download.pytorch.org/whl/cu118
Linux
sudo apt install -y python3-dev python3-venv portaudio19-dev
python -m venv venv
source venv\bin\activate
pip install xtts-api-server
pip install torch==2.1.1+cu118 torchaudio==2.1.1+cu118 --index-url https://download.pytorch.org/whl/cu118
Manual
# Clone REPO
git clone https://github.com/daswer123/xtts-api-server
cd xtts-api-server
# Create virtual env
python -m venv venv
venv/scripts/activate or source venv/bin/activate
# Install deps
pip install -r requirements.txt
pip install torch==2.1.1+cu118 torchaudio==2.1.1+cu118 --index-url https://download.pytorch.org/whl/cu118
# Launch server
python -m xtts_api_server
使用 Docker Compose 运行 Docker 镜像
提供了一个 Dockerfile 用于构建 Docker 镜像,并提供了一个 docker-compose.yml 文件用于通过 Docker Compose 以服务方式运行服务器。
您可以使用以下命令构建镜像:
mkdir xtts-api-server
cd xtts-api-server
docker run -d daswer123/xtts-api-server
或
cd docker
docker compose build
然后你可以使用以下命令运行服务器:
docker compose up # or with -d to run in background
启动服务器
python -m xtts_api_server 将在默认 IP 和端口(localhost:8020)上运行
使用 --deepspeed 标志以快速处理结果(2-3 倍加速)
usage: xtts_api_server [-h] [-hs HOST] [-p PORT] [-sf SPEAKER_FOLDER] [-o OUTPUT] [-t TUNNEL_URL] [-ms MODEL_SOURCE] [--listen] [--use-cache] [--lowvram] [--deepspeed] [--streaming-mode] [--stream-play-sync]
Run XTTSv2 within a FastAPI application
options:
-h, --help show this help message and exit
-hs HOST, --host HOST
-p PORT, --port PORT
-d DEVICE, --device DEVICE `cpu` or `cuda`, you can specify which video card to use, for example, `cuda:0`
-sf SPEAKER_FOLDER, --speaker-folder The folder where you get the samples for tts
-o OUTPUT, --output Output folder
-mf MODELS_FOLDERS, --model-folder Folder where models for XTTS will be stored, finetuned models should be stored in this folder
-t TUNNEL_URL, --tunnel URL of tunnel used (e.g: ngrok, localtunnel)
-ms MODEL_SOURCE, --model-source ["api","apiManual","local"]
-v MODEL_VERSION, --version You can download the official model or your own model, official version you can find [here](https://huggingface.co/coqui/XTTS-v2/tree/main) the model version name is the same as the branch name [v2.0.2,v2.0.3, main] etc. Or you can load your model, just put model in models folder
--listen Allows the server to be used outside the local computer, similar to -hs 0.0.0.0
--use-cache Enables caching of results, your results will be saved and if there will be a repeated request, you will get a file instead of generation
--lowvram The mode in which the model will be stored in RAM and when the processing will move to VRAM, the difference in speed is small
--deepspeed allows you to speed up processing by several times, automatically downloads the necessary libraries
--streaming-mode Enables streaming mode, currently has certain limitations, as described below.
--streaming-mode-improve Enables streaming mode, includes an improved streaming mode that consumes 2gb more VRAM and uses a better tokenizer and more context.
--stream-play-sync Additional flag for streaming mod that allows you to play all audio one at a time without interruption
您可以将文件路径作为文本指定,然后路径会被计入,文件将被朗读
您可以加载自己的模型,为此您需要在 models 文件夹中创建一个文件夹,并使用配置文件加载模型,注意该文件夹中应包含 3 个文件 config.json vocab.json model.pth
如果您希望您的主机监听,请使用 -hs 0.0.0.0 或使用 --listen
需要 -t 或 --tunnel 标志,以便当您通过 get 获取扬声器时,获得正确的链接来收听预览。更多信息见 此处
Model-source 定义您希望以哪种格式使用 xtts:
local- 默认加载版本 2.0.2,但您可以通过 -v 标志指定版本,模型保存到 models 文件夹并使用XttsConfig和inference。apiManual- 默认加载版本 2.0.2,但您可以通过 -v 标志指定版本,模型保存到 models 文件夹并使用 TTS api 中的tts_to_file函数api- 将加载模型的最新版本。-v 标志不起作用。
所有版本的 XTTSv2 模型都可以在 此处 找到,模型版本名称与分支名称相同 [v2.0.2,v2.0.3, main] 等。
第一次运行或生成时,您可能需要确认您同意使用 XTTS。
关于流式模式
流式模式允许您几乎立即获取音频并播放。然而,它有一些限制。
现在,关于限制
- 只能在本地计算机上使用
- 从你的电脑播放音频
- 端点
tts_to_file不起作用,仅tts_to_audio有效,并且它返回 1 秒的静音。
你可以通过使用 -v 标志来指定 XTTS 模型的版本。
改进的流式模式适用于中文、日语、印地语等复杂语言,或者当你希望语言引擎在处理语音时考虑更多信息时。
--stream-play-sync 标志 - 允许你按队列顺序播放所有消息,如果你使用群聊则很有用。在 SillyTavern 中,你需要关闭流式传输才能正常工作
API 文档
API 文档可以从 http://localhost:8020/docs 访问
如何添加说话人
默认情况下,speakers 文件夹应出现在该文件夹中,你需要将包含语音样本的 wav 文件放在那里,你也可以创建一个文件夹并放入多个语音样本,这将给出更准确的结果
选择文件夹
你可以通过 API 更改说话人的文件夹和输出文件夹。
关于创建高质量语音克隆样本的说明
以下帖子是用户 Material1276 来自 reddit 的引用
关于制作优质样本的一些建议
时长保持在 7-9 秒左右。更长并不一定更好。
确保音频已降采样为单声道、22050Hz、16 位 wav 文件。否则处理速度会大幅降低,且似乎会导致质量较差的结果(基于几次测试)。其输出质量本来就是 24000Hz!
使用最新版本的 Audacity,选中你的片段,然后执行 Tracks > Resample to 22050Hz,接着执行 Tracks > Mix > Stereo to Mono。最后执行 File > Export Audio,将其保存为 22050Hz 的 WAV 文件
如果需要做任何音频清理,请在将其压缩为上述设置(单声道、22050Hz、16 位)之前进行。
确保你使用的片段没有背景噪音或音乐,例如许多电影在演员对话时都有安静的背景音乐。低质量音频会有需要清除的嘶嘶声。即使我们听不到,AI 也会捕捉到这些声音,并在一定程度上将其用于模拟语音,因此干净的音频至关重要!
尽量让你的片段是一段流畅的语音,就像附带的示例文件一样。不要有大的停顿、间隙或其他声音。最好选择能展现你试图模仿的人一定音域变化的片段。示例文件在 这里
确保片段不以呼吸声(吸气/呼气等)开始或结束。
使用 AI 生成的音频片段可能会引入不需要的声音,因为它本身已经是声音的复制/模拟,不过,这需要测试。
致谢
- 感谢作者 Kolja Beigel 的仓库 RealtimeTTS ,我在我的项目中使用了其中的一些代码。
- 感谢 erew123 关于创建样本的说明以及下载模型的代码
- 感谢 lendot 帮助修复多进程 bug 并添加使用多个样本作为说话人的代码