sovits-v2-api
Version:
GPT-SoVITS v2 api
110 lines (109 loc) • 4.44 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.KoishiGptSovitsAPI = exports.logger = exports.name = void 0;
const koishi_1 = require("koishi");
const client_1 = require("@gradio/client"); // 新增导入
const config_1 = require("./config");
exports.name = 'gpt-sovits';
exports.logger = new koishi_1.Logger(exports.name);
class KoishiGptSovitsAPI {
constructor(ctx, config) {
this.baseConfig = config;
this.http = ctx.http.extend({
baseURL: config.endpoint,
});
ctx.on('ready', async () => {
try {
this.gradioApp = await (0, client_1.client)(config.endpoint, {});
ctx.logger.info('GPT-SoVITS 连接成功');
}
catch (err) {
if (err instanceof Error) {
ctx.logger.error('连接失败: ' + err.message);
}
else {
ctx.logger.error('连接失败: 未知错误类型');
}
}
});
ctx.command('say <input:text>', 'VITS 语音合成')
.action(async ({ session }, input) => {
if (!session)
return "会话无效";
return this.handleSay(session, input);
});
}
listModels() {
return `可用的模型名称有:${this.baseConfig.models.map(model => model.model_name).join(', ')}`;
}
async handleSay(session, input) {
if (!input) {
return '请输入要合成的文本。';
}
input = input.trim();
if (input === 'list' || input.toLowerCase() === 'say list') {
return this.listModels();
}
let modelName;
let language = this.baseConfig.default_lang;
const modelMatch = input.match(/model#(\S+)/);
if (modelMatch) {
modelName = modelMatch[1];
input = input.replace(modelMatch[0], '').trim();
}
const langMatch = input.match(/lang#(\S+)/);
if (langMatch) {
language = langMatch[1];
input = input.replace(langMatch[0], '').trim();
}
if (input.length > this.baseConfig.max_length) {
return '输入文本过长,请减少文本长度。';
}
const modelConfig = this.baseConfig.models.find(model => model.model_name === modelName) || this.baseConfig.models[0];
if (!modelConfig) {
return `未找到可用的模型配置,请检查您指定的模型或使用 "say list" 命令查看可用模型。`;
}
try {
// 生成音频之前设置模型
await this.gradioApp.predict('/change_tts_inference', [
"", // 预训练BERT路径(留空)
"", // 预训练SSL路径(留空)
"0", // GPU卡号
modelConfig.gpt_model_path,
modelConfig.sovits_model_path,
false // 是否并行推理
]);
// 生成音频
const result = await this.gradioApp.predict('/tts_inference', [
input, // text
language, // text_language
modelConfig.reference_audio, // refer_wav_path
this.baseConfig.speech_length, // speech_length
this.baseConfig.cut_punc, // cut_punc
this.baseConfig.top_k, // top_k
this.baseConfig.top_p, // top_p
this.baseConfig.temperature, // temperature
1.0 // speed
]);
// 处理音频数据
let audioBuffer;
if (typeof result.data === 'string') {
audioBuffer = Buffer.from(result.data, 'base64');
}
else {
audioBuffer = Buffer.from(result.data);
}
return koishi_1.h.audio(audioBuffer, `audio/${this.baseConfig.format}`);
}
catch (error) {
const message = error instanceof Error ? error.message : String(error);
exports.logger.error(`合成失败: ${message}`);
return `处理失败: ${message}`;
}
}
}
exports.KoishiGptSovitsAPI = KoishiGptSovitsAPI;
exports.default = KoishiGptSovitsAPI;
(function (KoishiGptSovitsAPI) {
KoishiGptSovitsAPI.Config = config_1.BaseConfig;
})(KoishiGptSovitsAPI || (exports.KoishiGptSovitsAPI = KoishiGptSovitsAPI = {}));