Python调用XTTS实现声音克隆,文字转语音

XTTS 是一个语音生成模型,不需要过多的训练数据,仅使用一个 6 秒的音频文件即可将语音克隆为不同的语言。

XTTS-v2 支持 17 种语言:

英语 (en)、西班牙语 (es)、法语 (fr)、德语 (de)、意大利语 (it)、葡萄牙语 (pt)、 波兰语 (pl)、土耳其语 (tr)、俄语 (ru)、荷兰语 (nl)、捷克语 (cs)、阿拉伯语 (ar)、中文 (zh-cn)、日语 (ja)、匈牙利语 (hu)、韩语 (ko) 印地语(hi)

下载模型

coqui/XTTS-v2 · HF MirrorWe’re on a journey to advance and democratize artificial intelligence through open source and open science.https://hf-mirror.com/coqui/XTTS-v2

模型使用示例

使用TTS API

from TTS.api import TTS
tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2", gpu=True)

# generate speech by cloning a voice using default settings
tts.tts_to_file(text="It took me quite a long time to develop a voice, and now that I have it I'm not going to be silent.",
                file_path="output.wav",
                speaker_wav="/path/to/target/speaker.wav",
                language="en")

直接使用模型

from TTS.tts.configs.xtts_config import XttsConfig
from TTS.tts.models.xtts import Xtts

config = XttsConfig()
config.load_json("/path/to/xtts/config.json")
model = Xtts.init_from_config(config)
model.load_checkpoint(config, checkpoint_dir="/path/to/xtts/", eval=True)
model.cuda()

outputs = model.synthesize(
    "It took me quite a long time to develop a voice and now that I have it I am not going to be silent.",
    config,
    speaker_wav="/data/TTS-public/_refclips/3.wav",
    gpt_cond_len=3,
    language="en",
)

使用 Docker 搭建XTTS流式服务

CUDA 12.1:

docker run --gpus=all -e COQUI_TOS_AGREED=1 -v /path/to/model/folder:/app/tts_models  --rm -p 8000:80 ghcr.io/coqui-ai/xtts-streaming-server:latest-cuda121

CUDA 11.8:

docker run --gpus=all -e COQUI_TOS_AGREED=1 -v /path/to/model/folder:/app/tts_models  --rm -p 8000:80 ghcr.io/coqui-ai/xtts-streaming-server:latest

CPU:

docker run -e COQUI_TOS_AGREED=1 -v /path/to/model/folder:/app/tts_models  --rm -p 8000:80 ghcr.io/coqui-ai/xtts-streaming-server:latest-cpu

确保将模型文件放在 /path/to/model/folder 目录下。

Docker 容器运行后,可以测试它是否正常工作。

xtts-streaming-server调用示例
git clone https://github.com/coqui-ai/xtts-streaming-server
cd xtts-streaming-server/test
python -m pip install -r requirements.txt
python test_streaming.py

作者:培根芝士

物联沃分享整理
物联沃-IOTWORD物联网 » Python调用XTTS实现声音克隆,文字转语音

发表回复