UNPKG

gpt-sovits-sdk

Version:

Node.js SDK for GPT-SoVITS API

204 lines (203 loc) 7.94 kB
"use strict"; /** * GPT-SoVITS SDK 测试脚本 * * 这个脚本专注于测试获取可用模型和参考音频的API端点 */ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); const index_1 = require("./index"); const path = __importStar(require("path")); const fs = __importStar(require("fs")); // 创建输出目录 const outputDir = path.join(__dirname, '../test-output'); if (!fs.existsSync(outputDir)) { fs.mkdirSync(outputDir, { recursive: true }); } // 创建客户端实例 const client = (0, index_1.createClient)({ baseUrl: 'http://127.0.0.1:9880', // 默认API地址 timeout: 60000, // 设置超时时间为60秒 debug: true, // 启用调试输出 retries: 2 // 设置重试次数 }); /** * 测试获取可用模型 */ async function testGetModels() { console.log('=== 测试获取可用模型 ==='); try { console.log('发送请求获取模型列表...'); const response = await client.getModels(); console.log('\n模型列表响应状态:'); console.log(`- 状态码: ${response.code}`); console.log(`- 消息: ${response.message}`); // 检查响应结构 if (!response || !response.data) { console.log('\n❌ 响应中没有data字段'); return false; } console.log('\ndata字段检查:'); console.log('- 是否包含gpt_models字段:', 'gpt_models' in response.data); console.log('- 是否包含sovits_models字段:', 'sovits_models' in response.data); // 检查模型数量 const gptModels = response.data.gpt_models || []; const sovitsModels = response.data.sovits_models || []; console.log('\n模型数量:'); console.log('- GPT模型数量:', gptModels.length); console.log('- SoVITS模型数量:', sovitsModels.length); // 如果有模型,显示第一个模型的详细信息 if (gptModels.length > 0) { console.log('\n第一个GPT模型详情:'); console.log(JSON.stringify(gptModels[0], null, 2)); } if (sovitsModels.length > 0) { console.log('\n第一个SoVITS模型详情:'); console.log(JSON.stringify(sovitsModels[0], null, 2)); } console.log('\n✅ 获取模型测试成功'); return true; } catch (error) { console.error('\n❌ 获取模型测试失败:', error); if (error instanceof Error) { console.error('错误信息:', error.message); console.error('错误堆栈:', error.stack); } return false; } } /** * 测试API连接 */ async function testConnection() { console.log('=== 测试API连接 ==='); try { const rootInfo = await client.getRoot(); console.log('API根信息:', rootInfo); const healthInfo = await client.getHealth(); console.log('API健康状态:', healthInfo); console.log('✅ API连接测试成功'); return true; } catch (error) { console.error('❌ API连接测试失败:', error); return false; } } /** * 测试获取参考音频 */ async function testGetReferenceAudios() { console.log('=== 测试获取参考音频 ==='); try { console.log('发送请求获取参考音频列表...'); const response = await client.getReferenceAudios(); console.log('\n参考音频列表响应状态:'); console.log(`- 状态码: ${response.code}`); console.log(`- 消息: ${response.message}`); // 检查响应结构 if (!response || !response.data) { console.log('\n❌ 响应中没有data字段'); return false; } console.log('\ndata字段检查:'); console.log('- 是否包含audios字段:', 'audios' in response.data); console.log('- 是否包含directories字段:', 'directories' in response.data); // 检查音频数量 const audios = response.data.audios || []; const directories = response.data.directories || []; console.log('\n音频和子目录数量:'); console.log('- 音频数量:', audios.length); console.log('- 子目录数量:', directories.length); // 如果有子目录,显示子目录列表 if (directories.length > 0) { console.log('\n子目录列表:'); directories.forEach((dir, index) => { console.log(` ${index + 1}. ${dir}`); }); // 测试获取第一个子目录中的音频 if (directories.length > 0) { console.log(`\n测试获取子目录 "${directories[0]}" 中的音频...`); const subdirResponse = await client.getReferenceAudios(directories[0]); if (subdirResponse && subdirResponse.data && subdirResponse.data.audios) { const subdirAudios = subdirResponse.data.audios || []; console.log(`- 子目录 "${directories[0]}" 中的音频数量:`, subdirAudios.length); // 如果子目录中有音频,显示第一个音频的详细信息 if (subdirAudios.length > 0) { console.log('\n子目录中第一个音频详情:'); console.log(JSON.stringify(subdirAudios[0], null, 2)); } } } } // 如果有音频,显示第一个音频的详细信息 if (audios.length > 0) { console.log('\n第一个音频详情:'); console.log(JSON.stringify(audios[0], null, 2)); } console.log('\n✅ 获取参考音频测试成功'); return true; } catch (error) { console.error('\n❌ 获取参考音频测试失败:', error); if (error instanceof Error) { console.error('错误信息:', error.message); console.error('错误堆栈:', error.stack); } return false; } } /** * 运行测试 */ async function runTests() { console.log('开始测试 GPT-SoVITS SDK...\n'); // 测试API连接 const connectionSuccess = await testConnection(); if (!connectionSuccess) { console.error('API连接失败,终止测试'); return; } // 测试获取可用模型 await testGetModels(); // 测试获取参考音频 await testGetReferenceAudios(); console.log('\n测试完成!'); } // 执行测试 runTests().catch(error => { console.error('测试过程中发生错误:', error); });