koishi-plugin-gpt-sovits-v2-api
Version:
Adapter for GPT-SoVITS v2
167 lines (163 loc) • 6.93 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
var __export = (target, all) => {
for (var name2 in all)
__defProp(target, name2, { get: all[name2], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var src_exports = {};
__export(src_exports, {
KoishiGptSovitsAPI: () => KoishiGptSovitsAPI,
default: () => src_default,
logger: () => logger,
name: () => name
});
module.exports = __toCommonJS(src_exports);
var import_koishi2 = require("koishi");
// src/config.ts
var import_koishi = require("koishi");
var BaseConfig = import_koishi.Schema.intersect([
import_koishi.Schema.object({
defaultEngine: import_koishi.Schema.const("GPT-SOVITS").description("默认引擎"),
max_length: import_koishi.Schema.number().default(256).description("最大长度")
}).description("基础配置"),
import_koishi.Schema.object({
endpoint: import_koishi.Schema.string().default("http://host.docker.internal:9880/").description("VITS 服务器地址"),
models: import_koishi.Schema.array(
import_koishi.Schema.object({
model_name: import_koishi.Schema.string().description("模型名称"),
gpt_model_path: import_koishi.Schema.string().description("GPT 模型文件路径"),
sovits_model_path: import_koishi.Schema.string().description("SOVITS 模型文件路径"),
reference_audio: import_koishi.Schema.string().description("参考音频路径"),
text_prompt: import_koishi.Schema.string().description("参考音频文本"),
text_prompt_lang: import_koishi.Schema.union([
import_koishi.Schema.const("zh"),
import_koishi.Schema.const("en"),
import_koishi.Schema.const("ja")
]).default("zh").description("参考音频文本语言")
}).role("table")
).default([]).description("模型配置列表")
}).description("GPT-SOVITS API 配置"),
import_koishi.Schema.object({
format: import_koishi.Schema.union([
import_koishi.Schema.const("ogg"),
import_koishi.Schema.const("wav"),
import_koishi.Schema.const("amr"),
import_koishi.Schema.const("mp3")
]).default("mp3").description("生成音频格式"),
default_lang: import_koishi.Schema.union([
import_koishi.Schema.const("zh"),
import_koishi.Schema.const("en"),
import_koishi.Schema.const("ja")
]).default("zh").description("默认生成音频语言,-lang 参数的回落值"),
speech_length: import_koishi.Schema.number().role("slider").min(0).max(2).step(0.1).default(1.4).description("语速, 越大越慢"),
cut_punc: import_koishi.Schema.string().default(",,.。!!??").description("分割符"),
top_k: import_koishi.Schema.number().default(15).description(""),
top_p: import_koishi.Schema.number().default(1).description(""),
temperature: import_koishi.Schema.number().default(1).description("")
}).description("参数设置"),
import_koishi.Schema.object({
waiting: import_koishi.Schema.boolean().default(true).description("消息反馈,会发送思考中..."),
recall: import_koishi.Schema.boolean().default(true).description("会撤回思考中"),
recall_time: import_koishi.Schema.number().default(5e3).description("撤回的时间")
}).description("拓展设置")
]);
// src/index.ts
var name = "adapter-gpt-sovits";
var logger = new import_koishi2.Logger(name);
var KoishiGptSovitsAPI = class {
static {
__name(this, "KoishiGptSovitsAPI");
}
http;
baseConfig;
constructor(ctx, config) {
this.baseConfig = config;
this.http = ctx.http.extend({
baseURL: config.endpoint
});
ctx.command("say <input:text>", "VITS 语音合成").action(async ({ session }, input) => 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.http.get("/set_model", {
params: {
gpt_model_path: modelConfig.gpt_model_path,
sovits_model_path: modelConfig.sovits_model_path
}
});
const response = await this.http.get("/", {
params: {
text: input,
text_language: language,
format: this.baseConfig.format,
speech_length: this.baseConfig.speech_length,
refer_wav_path: modelConfig.reference_audio,
prompt_text: modelConfig.text_prompt,
prompt_language: modelConfig.text_prompt_lang,
cut_punc: this.baseConfig.cut_punc,
top_k: this.baseConfig.top_k,
top_p: this.baseConfig.top_p,
temperature: this.baseConfig.temperature,
speed: 1
},
responseType: "arraybuffer"
});
return import_koishi2.h.audio(response, `audio/${this.baseConfig.format}`);
} catch (error) {
logger.error(error);
return `在处理请求时发生错误:${error.message || error}`;
}
}
};
var src_default = KoishiGptSovitsAPI;
((KoishiGptSovitsAPI2) => {
KoishiGptSovitsAPI2.Config = BaseConfig;
})(KoishiGptSovitsAPI || (KoishiGptSovitsAPI = {}));
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
KoishiGptSovitsAPI,
logger,
name
});