mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-28 04:59:52 +01:00
feat: add TTSProvider class for CosyVoice text-to-speech functionality
This commit is contained in:
commit
970d212eed
16 changed files with 551 additions and 79 deletions
|
|
@ -105,11 +105,12 @@ function httpHandlerError(info, callBack) {
|
|||
/** 请求成功,退出该函数 可以根据项目需求来判断是否请求成功。这里判断的是status为200的时候是成功 */
|
||||
let networkError = false
|
||||
if (info.status === 200) {
|
||||
|
||||
if (info.data.code === 'success' || info.data.code === 0 || info.data.code === undefined) {
|
||||
return networkError
|
||||
}else if (info.data.code === 401) {
|
||||
goToPage(Constant.PAGE.LOGIN, true)
|
||||
console.log('触发 401,清除 Token 并跳转登录页');
|
||||
store.commit('clearAuth');
|
||||
window.location.href = '/login';
|
||||
return true
|
||||
} else {
|
||||
showDanger(info.data.msg)
|
||||
|
|
|
|||
|
|
@ -138,8 +138,8 @@ export default {
|
|||
.url(`${getServiceUrl()}/api/v1/user/change-password`)
|
||||
.method('PUT')
|
||||
.data({
|
||||
old_password: oldPassword,
|
||||
new_password: newPassword,
|
||||
password: oldPassword,
|
||||
newPassword: newPassword,
|
||||
})
|
||||
.success((res) => {
|
||||
RequestService.clearRequestTime();
|
||||
|
|
|
|||
|
Before Width: | Height: | Size: 13 KiB After Width: | Height: | Size: 13 KiB |
|
|
@ -1,57 +1,59 @@
|
|||
<template>
|
||||
<el-dialog :visible.sync="visible" width="400px" center>
|
||||
<div
|
||||
style="margin: 0 10px 10px;display: flex;align-items: center;gap: 10px;font-weight: 700;font-size: 20px;text-align: left;color: #3d4566;">
|
||||
<form>
|
||||
<el-dialog :visible.sync="value" width="400px" center>
|
||||
<div
|
||||
style="width: 40px;height: 40px;border-radius: 50%;background: #5778ff;display: flex;align-items: center;justify-content: center;">
|
||||
<img loading="lazy" src="@/assets/login/shield.png" alt="" style="width: 19px;height: 23px; filter: brightness(0) invert(1);" />
|
||||
</div>
|
||||
修改密码
|
||||
</div>
|
||||
<div style="height: 1px;background: #e8f0ff;"/>
|
||||
<div style="margin: 22px 15px;">
|
||||
<div style="font-weight: 400;font-size: 14px;text-align: left;color: #3d4566;">
|
||||
<div style="color: red;display: inline-block;">*</div>
|
||||
旧密码:
|
||||
<div style="height: 1px;background: #e8f0ff;"/>
|
||||
<div style="margin: 22px 15px;">
|
||||
<div style="font-weight: 400;font-size: 14px;text-align: left;color: #3d4566;">
|
||||
<div style="color: red;display: inline-block;">*</div>
|
||||
旧密码:
|
||||
</div>
|
||||
<div class="input-46" style="margin-top: 12px;">
|
||||
<el-input placeholder="请输入旧密码" v-model="oldPassword" type="password" show-password/>
|
||||
</div>
|
||||
<div style="font-weight: 400;font-size: 14px;text-align: left;color: #3d4566;margin-top: 12px;">
|
||||
<div style="color: red;display: inline-block;">*</div>
|
||||
新密码:
|
||||
</div>
|
||||
<div class="input-46" style="margin-top: 12px;">
|
||||
<el-input placeholder="请输入新密码" v-model="newPassword" type="password" show-password/>
|
||||
</div>
|
||||
<div style="font-weight: 400;font-size: 14px;text-align: left;color: #3d4566;margin-top: 12px;">
|
||||
<div style="color: red;display: inline-block;">*</div>
|
||||
确认新密码:
|
||||
</div>
|
||||
<div class="input-46" style="margin-top: 12px;">
|
||||
<el-input placeholder="请再次输入新密码" v-model="confirmNewPassword" type="password" show-password/>
|
||||
</div>
|
||||
</div>
|
||||
<div class="input-46" style="margin-top: 12px;">
|
||||
<el-input placeholder="请输入旧密码" v-model="oldPassword" type="password" show-password/>
|
||||
<div style="display: flex;margin: 15px 15px;gap: 7px;">
|
||||
<div class="dialog-btn" @click="confirm">
|
||||
确定
|
||||
</div>
|
||||
<div class="dialog-btn"
|
||||
style="background: #e6ebff;border: 1px solid #adbdff;color: #5778ff;"
|
||||
@click="cancel">
|
||||
取消
|
||||
</div>
|
||||
</div>
|
||||
<div style="font-weight: 400;font-size: 14px;text-align: left;color: #3d4566;margin-top: 12px;">
|
||||
<div style="color: red;display: inline-block;">*</div>
|
||||
新密码:
|
||||
</div>
|
||||
<div class="input-46" style="margin-top: 12px;">
|
||||
<el-input placeholder="请输入新密码" v-model="newPassword" type="password" show-password/>
|
||||
</div>
|
||||
<div style="font-weight: 400;font-size: 14px;text-align: left;color: #3d4566;margin-top: 12px;">
|
||||
<div style="color: red;display: inline-block;">*</div>
|
||||
确认新密码:
|
||||
</div>
|
||||
<div class="input-46" style="margin-top: 12px;">
|
||||
<el-input placeholder="请再次输入新密码" v-model="confirmNewPassword" type="password" show-password/>
|
||||
</div>
|
||||
</div>
|
||||
<div style="display: flex;margin: 15px 15px;gap: 7px;">
|
||||
<div class="dialog-btn" @click="confirm">
|
||||
确定
|
||||
</div>
|
||||
<div class="dialog-btn"
|
||||
style="background: #e6ebff;border: 1px solid #adbdff;color: #5778ff;"
|
||||
@click="cancel">
|
||||
取消
|
||||
</div>
|
||||
</div>
|
||||
</el-dialog>
|
||||
</el-dialog>
|
||||
</form>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import userApi from '@/apis/module/user';
|
||||
import { mapActions } from 'vuex';
|
||||
|
||||
export default {
|
||||
name: 'ChangePasswordDialog',
|
||||
props: {
|
||||
visible: {type: Boolean, required: true}
|
||||
value: {
|
||||
type: Boolean,
|
||||
required: true
|
||||
}
|
||||
},
|
||||
data() {
|
||||
return {
|
||||
|
|
@ -61,6 +63,7 @@ export default {
|
|||
}
|
||||
},
|
||||
methods: {
|
||||
...mapActions(['logout']), // 引入Vuex的logout action
|
||||
confirm() {
|
||||
if (!this.oldPassword.trim() || !this.newPassword.trim() || !this.confirmNewPassword.trim()) {
|
||||
this.$message.error('请填写所有字段');
|
||||
|
|
@ -75,22 +78,24 @@ export default {
|
|||
return;
|
||||
}
|
||||
|
||||
// 直接调用修改密码接口
|
||||
// 修改后的接口调用
|
||||
userApi.changePassword(this.oldPassword, this.newPassword, (res) => {
|
||||
if (res.code === 0) {
|
||||
this.$message.error(res.msg || '密码修改失败');
|
||||
} else {
|
||||
this.$message.success('密码修改成功');
|
||||
this.$emit('confirm', res);
|
||||
this.$emit('update:visible', false);
|
||||
this.resetForm();
|
||||
if (res.data.code === 0) {
|
||||
this.$message.success('密码修改成功,请重新登录');
|
||||
this.logout().then(() => {
|
||||
this.$router.push('/login');
|
||||
this.$emit('update:visible', false);
|
||||
});
|
||||
}else{
|
||||
this.$message.error(res.data.msg || '密码修改失败');
|
||||
}
|
||||
}, (err) => {
|
||||
this.$message.error(err.msg || '密码修改失败');
|
||||
});
|
||||
this.$emit('input', false);
|
||||
},
|
||||
cancel() {
|
||||
this.$emit('update:visible', false);
|
||||
this.$emit('input', false);
|
||||
this.resetForm();
|
||||
},
|
||||
resetForm() {
|
||||
|
|
|
|||
|
|
@ -10,7 +10,9 @@
|
|||
<!-- 中间导航菜单 -->
|
||||
<div class="header-center">
|
||||
<div class="equipment-management" :class="{ 'active-tab': $route.path === '/home' }" @click="goHome">
|
||||
<img loading="lazy" alt="" src="@/assets/header/roboot.png" :style="{ filter: $route.path === '/home' ? 'brightness(0) invert(1)' : 'None' }"/>
|
||||
|
||||
<img loading="lazy" alt="" src="@/assets/header/robot.png" :style="{ filter: $route.path === '/home' ? 'brightness(0) invert(1)' : 'None' }"/>
|
||||
|
||||
智能体管理
|
||||
</div>
|
||||
<div class="equipment-management" :class="{ 'active-tab': $route.path === '/user-management' }" @click="goUserManagement">
|
||||
|
|
@ -27,7 +29,7 @@
|
|||
<div class="header-right">
|
||||
<div class="search-container">
|
||||
<el-input
|
||||
v-model="serach"
|
||||
v-model="search"
|
||||
placeholder="输入名称搜索.."
|
||||
class="custom-search-input"
|
||||
@keyup.enter.native="handleSearch"
|
||||
|
|
@ -38,7 +40,7 @@
|
|||
<img loading="lazy" alt="" src="@/assets/home/avatar.png" class="avatar-img"/>
|
||||
<el-dropdown trigger="click" class="user-dropdown">
|
||||
<span class="el-dropdown-link">
|
||||
{{ userInfo.mobile || '加载中...' }}<i class="el-icon-arrow-down el-icon--right"></i>
|
||||
{{ userInfo.username || '加载中...' }}<i class="el-icon-arrow-down el-icon--right"></i>
|
||||
</span>
|
||||
<el-dropdown-menu slot="dropdown">
|
||||
<el-dropdown-item icon="el-icon-plus" @click.native="">个人中心</el-dropdown-item>
|
||||
|
|
@ -50,7 +52,7 @@
|
|||
</div>
|
||||
|
||||
<!-- 修改密码弹窗 -->
|
||||
<ChangePasswordDialog :visible.sync="isChangePasswordDialogVisible"/>
|
||||
<ChangePasswordDialog v-model="isChangePasswordDialogVisible"/>
|
||||
</el-header>
|
||||
</template>
|
||||
|
||||
|
|
@ -68,7 +70,7 @@ export default {
|
|||
props: ['devices'], // 接收父组件设备列表
|
||||
data() {
|
||||
return {
|
||||
serach: '',
|
||||
search: '',
|
||||
userInfo: {
|
||||
username: '',
|
||||
mobile: ''
|
||||
|
|
@ -99,7 +101,7 @@ export default {
|
|||
|
||||
// 处理搜索
|
||||
handleSearch() {
|
||||
const searchValue = this.serach.trim();
|
||||
const searchValue = this.search.trim();
|
||||
let filteredDevices;
|
||||
|
||||
if (!searchValue) {
|
||||
|
|
|
|||
|
|
@ -39,7 +39,7 @@ export default new Vuex.Store({
|
|||
logout({ commit }) {
|
||||
return new Promise((resolve) => {
|
||||
commit('clearAuth')
|
||||
resolve()
|
||||
window.location.href = '/login';
|
||||
})
|
||||
}
|
||||
},
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@
|
|||
</div>
|
||||
</el-header>
|
||||
<el-main style="position: relative;">
|
||||
<div class="login-box">
|
||||
<div class="login-box" @keyup.enter="login">
|
||||
<div
|
||||
style="display: flex;align-items: center;gap: 20px;margin-bottom: 39px;padding: 0 30px;">
|
||||
<img loading="lazy" alt="" src="@/assets/login/hi.png" style="width: 34px;height: 34px;"/>
|
||||
|
|
@ -87,8 +87,11 @@ export default {
|
|||
},
|
||||
methods: {
|
||||
fetchCaptcha() {
|
||||
console.log(this.$store.getters.getToken)
|
||||
if (this.$store.getters.getToken) {
|
||||
goToPage('/home')
|
||||
if (this.$route.path !== '/home'){
|
||||
this.$router.push('/home')
|
||||
}
|
||||
} else {
|
||||
this.captchaUuid = getUUID();
|
||||
|
||||
|
|
@ -96,10 +99,8 @@ export default {
|
|||
if (res.status === 200) {
|
||||
const blob = new Blob([res.data], {type: res.data.type});
|
||||
this.captchaUrl = URL.createObjectURL(blob);
|
||||
|
||||
} else {
|
||||
console.error('验证码加载异常:', error);
|
||||
showDanger('验证码加载失败,点击刷新')
|
||||
showDanger('验证码加载失败,点击刷新');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
@ -130,12 +131,13 @@ export default {
|
|||
|
||||
this.form.captchaId = this.captchaUuid
|
||||
Api.user.login(this.form, ({data}) => {
|
||||
console.log(data)
|
||||
showSuccess('登陆成功!')
|
||||
// 将令牌存储到 Vuex 中
|
||||
this.$store.commit('setToken', JSON.stringify(data.data))
|
||||
|
||||
goToPage('/home')
|
||||
if (data.code === 0) {
|
||||
showSuccess('登录成功!');
|
||||
this.$store.commit('setToken', JSON.stringify(data.data));
|
||||
goToPage('/home');
|
||||
} else {
|
||||
showDanger(data.msg || '登录失败');
|
||||
}
|
||||
})
|
||||
|
||||
// 重新获取验证码
|
||||
|
|
|
|||
|
|
@ -6,6 +6,11 @@ from config.settings import load_config
|
|||
SERVER_VERSION = "0.1.17"
|
||||
|
||||
|
||||
def get_logger(tag):
|
||||
"""获取预配置的日志记录器,避免循环导入"""
|
||||
# 基本配置,不依赖config
|
||||
return logger.bind(tag=tag)
|
||||
|
||||
def setup_logging():
|
||||
"""从配置文件中读取日志配置,并设置日志输出格式和级别"""
|
||||
config = load_config()
|
||||
|
|
|
|||
|
|
@ -6,10 +6,10 @@ import opuslib_next
|
|||
from pydub import AudioSegment
|
||||
from abc import ABC, abstractmethod
|
||||
from core.utils.tts import MarkdownCleaner
|
||||
from config.logger import get_logger
|
||||
|
||||
TAG = __name__
|
||||
logger = setup_logging()
|
||||
|
||||
logger = get_logger(TAG)
|
||||
|
||||
class TTSProviderBase(ABC):
|
||||
def __init__(self, config, delete_audio_file):
|
||||
|
|
@ -27,6 +27,7 @@ class TTSProviderBase(ABC):
|
|||
text = MarkdownCleaner.clean_markdown(text)
|
||||
while not os.path.exists(tmp_file) and max_repeat_time > 0:
|
||||
asyncio.run(self.text_to_speak(text, tmp_file))
|
||||
|
||||
if not os.path.exists(tmp_file):
|
||||
max_repeat_time = max_repeat_time - 1
|
||||
logger.bind(tag=TAG).error(f"语音生成失败: {text}:{tmp_file},再试{max_repeat_time}次")
|
||||
|
|
|
|||
107
main/xiaozhi-server/core/providers/tts/local_cosyvoice.py
Normal file
107
main/xiaozhi-server/core/providers/tts/local_cosyvoice.py
Normal file
|
|
@ -0,0 +1,107 @@
|
|||
import os
|
||||
import sys
|
||||
import uuid
|
||||
import requests
|
||||
from config.logger import get_logger
|
||||
from datetime import datetime
|
||||
from core.providers.tts.base import TTSProviderBase
|
||||
import torch
|
||||
import torchaudio
|
||||
|
||||
TAG = __name__
|
||||
logger = get_logger(TAG)
|
||||
|
||||
|
||||
class TTSProvider(TTSProviderBase):
|
||||
|
||||
def _initialize_model(self):
|
||||
# 保存原始 sys.path
|
||||
original_path = None
|
||||
|
||||
try:
|
||||
original_path = sys.path.copy()
|
||||
# 动态修改 sys.path
|
||||
sys.path.insert(0, self.matcha_tts_path)
|
||||
sys.path.insert(0, self.cosy_voice_path)
|
||||
|
||||
# 导入必要的模块
|
||||
from cosyvoice.cli.cosyvoice import CosyVoice2
|
||||
from cosyvoice.utils.file_utils import load_wav
|
||||
|
||||
# 初始化模型
|
||||
self.model = CosyVoice2(self.cosy_voice_model_dir,
|
||||
load_jit=False, load_trt=False, fp16=False)
|
||||
|
||||
# 保存导入的模块供之后使用
|
||||
self.CosyVoice2 = CosyVoice2
|
||||
self.prompt_speech_16k = load_wav(self.prompt_speech_16k, 16000)
|
||||
|
||||
return True
|
||||
except ImportError as e:
|
||||
logger.bind(tag=TAG).error(f"导入 CosyVoice 模块失败: {e}")
|
||||
raise ImportError(f"导入 CosyVoice 模块失败: {e}")
|
||||
finally:
|
||||
# 恢复原始 sys.path
|
||||
if original_path:
|
||||
sys.path = original_path
|
||||
|
||||
def __init__(self, config, delete_audio_file):
|
||||
super().__init__(config, delete_audio_file)
|
||||
self.cosy_voice_path = config.get("cosyvoice_path")
|
||||
self.cosy_voice_model_dir = config.get("cosyvoice_model_dir")
|
||||
self.matcha_tts_path = config.get("matcha_tts_path") if config.get(
|
||||
"matcha_tts_path") else f"{self.cosy_voice_path}/third_party/Matcha-TTS"
|
||||
|
||||
self.prompt_speech_16k = config.get("prompt_speech_16k") if config.get(
|
||||
"prompt_speech_16k") else f"{self.cosy_voice_path}/asset/zero_shot_prompt.wav"
|
||||
# 非必传参数,如果不传,则使用默认的16k采样率的提示音频
|
||||
self.prompt_speech_16k_text = config.get("prompt_speech_16k_text") if config.get(
|
||||
"prompt_speech_16k_text") else None
|
||||
|
||||
self._initialize_model()
|
||||
|
||||
def inference_to_single_file(self, inference_func, output_path, *args, **kwargs):
|
||||
"""
|
||||
执行推理并将结果保存为单个音频文件
|
||||
|
||||
参数:
|
||||
inference_func: 推理函数(如cosyvoice.inference_zero_shot)
|
||||
output_path: 输出文件路径
|
||||
*args, **kwargs: 传递给推理函数的参数
|
||||
|
||||
返回:
|
||||
合并后的语音张量
|
||||
"""
|
||||
speech_segments = []
|
||||
for segment in inference_func(*args, **kwargs):
|
||||
speech_segments.append(segment['tts_speech'])
|
||||
if speech_segments:
|
||||
combined_speech = torch.cat(speech_segments, dim=1)
|
||||
torchaudio.save(output_path, combined_speech, self.model.sample_rate)
|
||||
return combined_speech
|
||||
return None
|
||||
|
||||
def generate_filename(self):
|
||||
return os.path.join(self.output_file, f"tts-{datetime.now().date()}@{uuid.uuid4().hex}.{self.format}")
|
||||
|
||||
async def text_to_speak(self, text, output_file):
|
||||
try:
|
||||
if not self.prompt_speech_16k_text:
|
||||
self.inference_to_single_file(
|
||||
self.model.inference_cross_lingual,
|
||||
output_file,
|
||||
text,
|
||||
self.prompt_speech_16k,
|
||||
stream=False
|
||||
)
|
||||
else:
|
||||
self.inference_to_single_file(
|
||||
self.model.inference_zero_shot,
|
||||
output_file,
|
||||
text,
|
||||
self.prompt_speech_16k_text,
|
||||
self.prompt_speech_16k,
|
||||
stream=False
|
||||
)
|
||||
except Exception as e:
|
||||
logger.bind(tag=TAG).exception(f"CosyVoice TTS请求失败: {e}")
|
||||
|
|
@ -1,10 +1,10 @@
|
|||
import os
|
||||
import re
|
||||
import sys
|
||||
from config.logger import setup_logging
|
||||
from config.logger import get_logger
|
||||
import importlib
|
||||
|
||||
logger = setup_logging()
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
def create_instance(class_name, *args, **kwargs):
|
||||
|
|
|
|||
|
|
@ -12,8 +12,7 @@ model = AutoModel(
|
|||
hub="hf",
|
||||
)
|
||||
|
||||
# en
|
||||
res = model.generate(
|
||||
res0 = model.generate(
|
||||
input=f"{model.model_path}/example/en.mp3",
|
||||
cache={},
|
||||
language="auto", # "zn", "en", "yue", "ja", "ko", "nospeech"
|
||||
|
|
@ -22,6 +21,17 @@ res = model.generate(
|
|||
merge_vad=True, #
|
||||
merge_length_s=15,
|
||||
)
|
||||
text = rich_transcription_postprocess(res[0]["text"])
|
||||
print(text)
|
||||
|
||||
# en
|
||||
res = model.generate(
|
||||
input=f"{model.model_path}/example/zh.mp3",
|
||||
cache={},
|
||||
language="auto", # "zn", "en", "yue", "ja", "ko", "nospeech"
|
||||
use_itn=True,
|
||||
batch_size_s=60,
|
||||
merge_vad=True, #
|
||||
merge_length_s=15,
|
||||
)
|
||||
print(rich_transcription_postprocess(res[0]["text"]))
|
||||
print(rich_transcription_postprocess(res0[0]["text"]))
|
||||
|
||||
|
|
|
|||
|
|
@ -1,12 +1,12 @@
|
|||
pyyml==0.0.2
|
||||
torch==2.2.2
|
||||
torchaudio==2.2.2
|
||||
silero_vad==5.1.2
|
||||
websockets==14.2
|
||||
opuslib_next==1.1.2
|
||||
numpy==1.26.4
|
||||
pydub==0.25.1
|
||||
funasr==1.2.3
|
||||
torchaudio==2.2.2
|
||||
openai==1.61.0
|
||||
google-generativeai==0.8.4
|
||||
edge_tts==7.0.0
|
||||
|
|
|
|||
14
main/xiaozhi-server/test/unit/tts/cosyvoice_requirements.txt
Normal file
14
main/xiaozhi-server/test/unit/tts/cosyvoice_requirements.txt
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
HyperPyYAML==1.2.2
|
||||
openai-whisper==20231117
|
||||
inflect==7.3.1
|
||||
transformers==4.40.1
|
||||
conformer==0.3.2
|
||||
diffusers==0.29.0
|
||||
lightning==2.2.4
|
||||
rich==13.7.1
|
||||
gdown==5.1.0
|
||||
matplotlib==3.7.5
|
||||
wget==3.2
|
||||
pyarrow==19.0.1
|
||||
pyworld==0.3.4
|
||||
onnxruntime-gpu==1.21.0
|
||||
81
main/xiaozhi-server/test/unit/tts/local_cosyvoice_real.py
Normal file
81
main/xiaozhi-server/test/unit/tts/local_cosyvoice_real.py
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
import os
|
||||
import unittest
|
||||
import time
|
||||
from datetime import datetime
|
||||
import uuid
|
||||
import wave
|
||||
import torchaudio
|
||||
from core.providers.tts.local_cosyvoice import TTSProvider
|
||||
|
||||
|
||||
class TestRealTTSGeneration(unittest.TestCase):
|
||||
"""真实语音生成集成测试,需要实际的 CosyVoice 环境"""
|
||||
|
||||
def setUp(self):
|
||||
# 使用真实配置 - 请确保这些路径在您的环境中有效
|
||||
self.config = {
|
||||
"output_dir": "/tmp",
|
||||
"cosyvoice_path": "/home/shangjun/xt_workspace/python_workspace/CosyVoice",
|
||||
"cosyvoice_model_dir": "/home/shangjun/xt_workspace/python_workspace/CosyVoice/pretrained_models/CosyVoice2-0.5B",
|
||||
"prompt_speech_16k": "/home/shangjun/xt_workspace/python_workspace/CosyVoice/asset/zero_shot_prompt.wav",
|
||||
"prompt_speech_16k_text": "希望你以后能够做的比我还好呦。"
|
||||
}
|
||||
# 创建输出目录
|
||||
os.makedirs(self.config["output_dir"], exist_ok=True)
|
||||
|
||||
def test_real_tts_generation(self):
|
||||
"""使用真实模型生成语音文件"""
|
||||
# 初始化提供者,不删除生成的文件
|
||||
provider = TTSProvider(self.config, False)
|
||||
provider.format = "wav"
|
||||
|
||||
# 生成测试文本
|
||||
test_text = "王骀受了刖刑,被砍去了一只脚。孔子有个弟子叫常季,他见老师时提出了自己的疑问。他说:老师你看,王骀被砍去了一只脚,可是他的学识和品行好像都超过了先生您,至于跟平常人相比,好像水平就更高了。像他这样的人,运用心智是怎样的与众不同呢?孔子的学生觉得很是奇怪,这个人一只脚被砍掉了,但是他的名声却很大,很多人都喜欢跟他学习,这个学生感到很不理解,一见到老师就向老师提出自己心中的疑问。文中庄子又是借孔子之口,表达了自己这样的观点:说死和生都是人生中的大事,可是死和生都不能使王骀这样的人随之变化,你说王骀是个什么样的人呢?即使天翻过来地坠下去,他也不会因此而被毁灭,他通晓无所依凭的道理,当然也就不随物变迁,而是听任事物的变化而信守自己的宗本。孔子的这段话把常季给说晕了,他忍不住再问:老师您这些话是什么意思啊?孔子怎么回答的呢?这段话很重要,来看一下完整的译文:孔子说:“从事物千差万别的一面去看,邻近的肝胆虽处于一体之中,也像是楚国和越国那样相距甚远;如果从事物相同的一面来看,万事万物又都是同一的,没有差别的。像王骀这样的人,耳朵和眼睛最适宜何种声音和色彩这样的事,已经不在他考虑范围之内了。他让自己的心思自由自在地遨游在忘形、忘情的浑同境域之中,就把这些东西的差别都忘掉了。所以他看待自己丧失了一只脚这件事,就像是看待失落的土块一样。”学了前面《庄子》的几篇文章,这段话的观点我们已不陌生。另外,有没有觉得这段话的句式很熟悉?中学时我们就学过苏东坡的《前赤壁赋》,其中就有这样的句式:“自其变者而观之,则天地曾不能以一瞬;自其不变者而观之,则物与我皆无尽也。”可以说,东坡不仅化用了庄子的句式,而且思想也和庄子是一样的。"
|
||||
|
||||
# 执行文本到语音转换
|
||||
start_time = time.time()
|
||||
result_file = provider.to_tts(test_text)
|
||||
end_time = time.time()
|
||||
|
||||
# 输出生成信息
|
||||
print(f"语音生成耗时: {end_time - start_time:.2f}秒")
|
||||
print(f"生成的文件路径: {result_file}")
|
||||
|
||||
# 验证文件是否存在
|
||||
self.assertTrue(os.path.exists(result_file), "语音文件未成功生成")
|
||||
|
||||
# 验证文件格式
|
||||
self.assertTrue(result_file.endswith(".wav"), "生成的不是WAV文件")
|
||||
|
||||
# 验证文件内容
|
||||
try:
|
||||
# 检查音频文件属性
|
||||
audio_info = torchaudio.info(result_file)
|
||||
print(f"采样率: {audio_info.sample_rate}Hz")
|
||||
print(f"声道数: {audio_info.num_channels}")
|
||||
print(f"音频长度: {audio_info.num_frames / audio_info.sample_rate:.2f}秒")
|
||||
|
||||
# 加载音频文件
|
||||
waveform, sample_rate = torchaudio.load(result_file)
|
||||
|
||||
# 验证音频基本特性
|
||||
self.assertEqual(sample_rate, 24000, "采样率应为24kHz")
|
||||
self.assertTrue(waveform.size(0) > 0, "音频数据不应为空")
|
||||
self.assertTrue(waveform.size(1) > 0, "音频长度不应为0")
|
||||
|
||||
print(f"音频形状: {waveform.shape}")
|
||||
print(f"最大值: {waveform.max().item():.4f}, 最小值: {waveform.min().item():.4f}")
|
||||
|
||||
except Exception as e:
|
||||
self.fail(f"验证音频文件失败: {e}")
|
||||
|
||||
# 如果需要,可以在这里播放音频进行人工验证
|
||||
# import IPython.display as ipd
|
||||
# ipd.Audio(result_file)
|
||||
|
||||
def tearDown(self):
|
||||
# 清理临时文件(可选)
|
||||
# 注意:如果想保留文件以便检查,可以注释掉下面的代码
|
||||
# import shutil
|
||||
# shutil.rmtree(self.config["output_dir"])
|
||||
pass
|
||||
244
main/xiaozhi-server/test/unit/tts/local_cosyvoice_test.py
Normal file
244
main/xiaozhi-server/test/unit/tts/local_cosyvoice_test.py
Normal file
|
|
@ -0,0 +1,244 @@
|
|||
import unittest
|
||||
from unittest.mock import patch, MagicMock, AsyncMock
|
||||
import os
|
||||
import torch
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
import asyncio
|
||||
import numpy as np
|
||||
from core.providers.tts.base import TTSProviderBase
|
||||
from core.providers.tts.local_cosyvoice import TTSProvider
|
||||
|
||||
|
||||
class MockTTSProvider(TTSProviderBase):
|
||||
def generate_filename(self):
|
||||
return f"/tmp/mock_tts_{str(uuid.uuid4())}.wav"
|
||||
|
||||
async def text_to_speak(self, text, output_file):
|
||||
pass
|
||||
|
||||
|
||||
class TestTTSProviderBase(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.config = {"output_dir": "/tmp/audio"}
|
||||
self.provider = MockTTSProvider(self.config, True)
|
||||
|
||||
@patch('core.providers.tts.base.MarkdownCleaner.clean_markdown')
|
||||
@patch('asyncio.run')
|
||||
@patch('os.path.exists')
|
||||
def test_successful_tts_generation(self, mock_exists, mock_run, mock_clean):
|
||||
mock_clean.return_value = "清理后的文本"
|
||||
mock_exists.side_effect = [False, True, True]
|
||||
|
||||
result = self.provider.to_tts("你好世界")
|
||||
|
||||
self.assertIsNotNone(result)
|
||||
mock_run.assert_called_once()
|
||||
mock_clean.assert_called_once_with("你好世界")
|
||||
|
||||
@patch('core.providers.tts.base.MarkdownCleaner.clean_markdown')
|
||||
@patch('asyncio.run')
|
||||
@patch('os.path.exists')
|
||||
def test_tts_with_retries(self, mock_exists, mock_run, mock_clean):
|
||||
mock_clean.return_value = "清理后的文本"
|
||||
mock_exists.side_effect = [False, False, False, False, True]
|
||||
|
||||
result = self.provider.to_tts("你好世界")
|
||||
|
||||
self.assertIsNotNone(result)
|
||||
self.assertEqual(mock_run.call_count, 2)
|
||||
|
||||
@patch('core.providers.tts.base.MarkdownCleaner.clean_markdown')
|
||||
@patch('asyncio.run')
|
||||
@patch('os.path.exists')
|
||||
def test_tts_max_retries_exceeded(self, mock_exists, mock_run, mock_clean):
|
||||
mock_clean.return_value = "清理后的文本"
|
||||
mock_exists.return_value = False
|
||||
|
||||
result = self.provider.to_tts("你好世界")
|
||||
|
||||
self.assertEqual(mock_run.call_count, 5)
|
||||
|
||||
@patch('core.providers.tts.base.MarkdownCleaner.clean_markdown')
|
||||
@patch('asyncio.run')
|
||||
def test_tts_with_exception(self, mock_run, mock_clean):
|
||||
mock_clean.return_value = "清理后的文本"
|
||||
mock_run.side_effect = Exception("TTS 错误")
|
||||
|
||||
result = self.provider.to_tts("你好世界")
|
||||
|
||||
self.assertIsNone(result)
|
||||
|
||||
@patch('pydub.AudioSegment.from_file')
|
||||
@patch('opuslib_next.Encoder')
|
||||
def test_audio_to_opus_conversion(self, mock_encoder_class, mock_from_file):
|
||||
mock_audio = MagicMock()
|
||||
mock_audio.set_channels.return_value = mock_audio
|
||||
mock_audio.set_frame_rate.return_value = mock_audio
|
||||
mock_audio.set_sample_width.return_value = mock_audio
|
||||
mock_audio.__len__.return_value = 3000
|
||||
mock_audio.raw_data = b'\x00\x01' * 48000
|
||||
mock_from_file.return_value = mock_audio
|
||||
|
||||
mock_encoder = MagicMock()
|
||||
mock_encoder.encode.return_value = b'encoded_data'
|
||||
mock_encoder_class.return_value = mock_encoder
|
||||
|
||||
result, duration = self.provider.audio_to_opus_data("test.wav")
|
||||
|
||||
self.assertEqual(duration, 3.0)
|
||||
self.assertTrue(len(result) > 0)
|
||||
self.assertTrue(all(item == b'encoded_data' for item in result))
|
||||
|
||||
|
||||
class TestCosyVoiceTTSProvider(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.config = {
|
||||
"output_dir": "/tmp/audio",
|
||||
"cosyvoice_path": "/path/to/cosyvoice",
|
||||
"cosyvoice_model_dir": "/path/to/models"
|
||||
}
|
||||
self.mock_cosyvoice = MagicMock()
|
||||
self.mock_torch = MagicMock()
|
||||
self.mock_torchaudio = MagicMock()
|
||||
|
||||
self.patches = [
|
||||
patch('sys.path', new_callable=list),
|
||||
patch.dict('sys.modules', {
|
||||
'cosyvoice.cli.cosyvoice': MagicMock(),
|
||||
'cosyvoice.utils.file_utils': MagicMock(),
|
||||
'torch': self.mock_torch,
|
||||
'torchaudio': self.mock_torchaudio
|
||||
})
|
||||
]
|
||||
for p in self.patches:
|
||||
p.start()
|
||||
|
||||
def tearDown(self):
|
||||
for p in self.patches:
|
||||
p.stop()
|
||||
|
||||
@patch('core.providers.tts.local_cosyvoice.CosyVoice2')
|
||||
def test_initialization_with_defaults(self, mock_cosyvoice_class):
|
||||
provider = TTSProvider(self.config, True)
|
||||
|
||||
self.assertEqual(provider.cosy_voice_path, "/path/to/cosyvoice")
|
||||
self.assertEqual(provider.cosy_voice_model_dir, "/path/to/models")
|
||||
self.assertEqual(provider.matcha_tts_path, "/path/to/cosyvoice/third_party/Matcha-TTS")
|
||||
self.assertTrue(provider.prompt_speech_16k.endswith("/asset/zero_shot_prompt.wav"))
|
||||
self.assertIsNone(provider.prompt_speech_16k_text)
|
||||
|
||||
@patch('core.providers.tts.local_cosyvoice.CosyVoice2')
|
||||
def test_initialization_with_custom_values(self, mock_cosyvoice_class):
|
||||
custom_config = self.config.copy()
|
||||
custom_config.update({
|
||||
"matcha_tts_path": "/custom/matcha",
|
||||
"prompt_speech_16k": "/custom/prompt.wav",
|
||||
"prompt_speech_16k_text": "你好"
|
||||
})
|
||||
|
||||
provider = TTSProvider(custom_config, True)
|
||||
|
||||
self.assertEqual(provider.matcha_tts_path, "/custom/matcha")
|
||||
self.assertEqual(provider.prompt_speech_16k, "/custom/prompt.wav")
|
||||
self.assertEqual(provider.prompt_speech_16k_text, "你好")
|
||||
|
||||
@patch.object(TTSProvider, '_initialize_model')
|
||||
@patch('uuid.uuid4')
|
||||
@patch('core.providers.tts.local_cosyvoice.datetime') # 修改这一行
|
||||
def test_generate_filename(self, mock_datetime, mock_uuid, _):
|
||||
mock_datetime.now.return_value.date.return_value = "2023-01-01"
|
||||
mock_uuid.return_value.hex = "abcd1234"
|
||||
|
||||
provider = TTSProvider(self.config, True)
|
||||
provider.format = "wav"
|
||||
filename = provider.generate_filename()
|
||||
|
||||
self.assertEqual(filename, "/tmp/audio/tts-2023-01-01@abcd1234.wav")
|
||||
|
||||
@patch('core.providers.tts.local_cosyvoice.CosyVoice2')
|
||||
def test_inference_to_single_file(self, _):
|
||||
provider = TTSProvider(self.config, True)
|
||||
cosyvoice = MagicMock()
|
||||
cosyvoice.sample_rate = 16000
|
||||
|
||||
mock_speech1 = torch.tensor([0.1, 0.2])
|
||||
mock_speech2 = torch.tensor([0.3, 0.4])
|
||||
mock_inference_func = MagicMock()
|
||||
mock_inference_func.return_value = [
|
||||
{"tts_speech": mock_speech1},
|
||||
{"tts_speech": mock_speech2}
|
||||
]
|
||||
|
||||
with patch.object(torch, 'cat', return_value="combined_speech"):
|
||||
result = provider.inference_to_single_file(
|
||||
mock_inference_func, "/tmp/output.wav", "测试文本"
|
||||
)
|
||||
|
||||
mock_inference_func.assert_called_once_with("测试文本")
|
||||
self.assertEqual(result, "combined_speech")
|
||||
|
||||
@patch('core.providers.tts.local_cosyvoice.CosyVoice2')
|
||||
def test_inference_to_single_file_empty_result(self, _):
|
||||
provider = TTSProvider(self.config, True)
|
||||
|
||||
mock_inference_func = MagicMock()
|
||||
mock_inference_func.return_value = []
|
||||
|
||||
result = provider.inference_to_single_file(
|
||||
mock_inference_func, "/tmp/output.wav", "测试文本"
|
||||
)
|
||||
|
||||
self.assertIsNone(result)
|
||||
|
||||
@patch('core.providers.tts.local_cosyvoice.CosyVoice2')
|
||||
@patch('core.providers.tts.local_cosyvoice.cosyvoice', new_callable=MagicMock)
|
||||
@patch('core.providers.tts.local_cosyvoice.torch', new_callable=MagicMock)
|
||||
@patch('core.providers.tts.local_cosyvoice.torchaudio', new_callable=MagicMock)
|
||||
async def test_text_to_speak_with_prompt_text(self, _, __, mock_cosyvoice, ___):
|
||||
provider = TTSProvider(self.config, True)
|
||||
provider.prompt_speech_16k_text = "提示文本"
|
||||
|
||||
with patch.object(provider, 'inference_to_single_file') as mock_inference:
|
||||
await provider.text_to_speak("你好世界", "/tmp/output.wav")
|
||||
|
||||
mock_inference.assert_called_once_with(
|
||||
mock_cosyvoice.inference_cross_lingual,
|
||||
"/tmp/output.wav",
|
||||
"你好世界",
|
||||
provider.prompt_speech_16k_text,
|
||||
provider.prompt_speech_16k,
|
||||
stream=False
|
||||
)
|
||||
|
||||
@patch('core.providers.tts.local_cosyvoice.CosyVoice2')
|
||||
@patch('core.providers.tts.local_cosyvoice.cosyvoice', new_callable=MagicMock)
|
||||
@patch('core.providers.tts.local_cosyvoice.torch', new_callable=MagicMock)
|
||||
@patch('core.providers.tts.local_cosyvoice.torchaudio', new_callable=MagicMock)
|
||||
async def test_text_to_speak_without_prompt_text(self, _, __, mock_cosyvoice, ___):
|
||||
provider = TTSProvider(self.config, True)
|
||||
provider.prompt_speech_16k_text = None
|
||||
|
||||
with patch.object(provider, 'inference_to_single_file') as mock_inference:
|
||||
await provider.text_to_speak("你好世界", "/tmp/output.wav")
|
||||
|
||||
mock_inference.assert_called_once_with(
|
||||
mock_cosyvoice.inference_zero_shot,
|
||||
"/tmp/output.wav",
|
||||
"你好世界",
|
||||
provider.prompt_speech_16k,
|
||||
stream=False
|
||||
)
|
||||
|
||||
@patch('core.providers.tts.local_cosyvoice.CosyVoice2')
|
||||
async def test_text_to_speak_handles_exception(self, _):
|
||||
provider = TTSProvider(self.config, True)
|
||||
|
||||
with patch.object(provider, 'inference_to_single_file', side_effect=Exception("TTS错误")):
|
||||
try:
|
||||
await provider.text_to_speak("你好世界", "/tmp/output.wav")
|
||||
# 如果不抛出异常则测试通过
|
||||
except Exception:
|
||||
self.fail("text_to_speak方法没有正确处理异常")
|
||||
Loading…
Reference in a new issue