text2speech文生音频模型XTTS-V2部署带UI
模型下载链接,及前端代码效果链接见个人博客:
https://pylzzz.online
效果图:
python后端代码 flask框架
由于使用的是自己电脑的gpu运算,所以中间有转发的过程,利用内网穿透和虚拟局域网通信。
内网穿透教程可见个人博客
所需依赖 tts flask等
from TTS.tts.configs.xtts_config import XttsConfig
from TTS.tts.models.xtts import Xtts
import soundfile as sf
import numpy as np
from flask import Flask, request, jsonify, session, redirect, url_for
from flask_cors import CORS
import uuid
import subprocess
app = Flask(__name__)
CORS(app)
config = XttsConfig()
config.load_json("./XTTS-v2/config.json")
model = Xtts.init_from_config(config)
model.load_checkpoint(config, checkpoint_dir="./XTTS-v2/", eval=True)
model.cuda()
language_to_audio = {
"en": "./XTTS-v2/samples/en_sample.wav",
"es": "./XTTS-v2/samples/es_sample.wav",
"fr": "./XTTS-v2/samples/fr_sample.wav",
"de": "./XTTS-v2/samples/de_sample.wav",
"zh": "./XTTS-v2/samples/zh-cn-sample.wav",
"ja": "./XTTS-v2/samples/ja-sample.wav",
"pt": "./XTTS-v2/samples/pt_sample.wav",
"tr": "./XTTS-v2/samples/tr_sample.wav",
# 添加更多语言及对应的语音文件
}
@app.route('/api/xtts', methods=['POST'])
def xtts():
data = request.json
# 处理接收到的表单数据
print('Received form data:', data)
text = data.get("sentence")
lan = data.get("lant")
filename = data.get("ref_name")
url=f"https://pylzzz.online/wav/{filename}"
saveurl=f"/var/www/html/wav/{filename}"
if not filename:
saveurl = language_to_audio.get(lan)
else:
result = subprocess.run(['wget', '-O',saveurl ,url], capture_output=True, text=True)
outputs = model.synthesize(
text,
config,
speaker_wav=saveurl,
gpt_cond_len=3,
language=lan,
)
wav_data = outputs['wav']
# 提取采样率
sampling_rate = config.audio['output_sample_rate']
# 检查输出格式并将其保存为 WAV 文件
newfilename = f"{uuid.uuid4()}.wav"
if isinstance(wav_data, np.ndarray):
sf.write(f'/var/www/html/wav/{newfilename}', wav_data, sampling_rate)
else:
raise TypeError("Outputs should be a NumPy array")
return jsonify(message="Data received and forwarded", file=newfilename)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=3000)
标签:文生,data,v2,sample,V2,UI,XTTS,wav,config
From: https://blog.csdn.net/qq_43182415/article/details/140356500