nwhisper
Version:
Native Node.js bindings for OpenAI's Whisper using whisper.cpp. High-performance local speech-to-text with custom model support.
83 lines • 4.27 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = autoDownloadModel;
const node_fs_1 = __importDefault(require("node:fs"));
const node_path_1 = __importDefault(require("node:path"));
const shelljs_1 = __importDefault(require("shelljs"));
const constants_1 = require("./constants");
async function autoDownloadModel(logger = console, autoDownloadModelName, withCuda = false, modelDir) {
const projectDir = process.cwd();
if (!autoDownloadModelName) {
throw new Error('[nwhisper] Error: Model name must be provided.');
}
if (!constants_1.MODELS_LIST.includes(autoDownloadModelName)) {
throw new Error('[nwhisper] Error: Provide a valid model name');
}
try {
// Determine download directory
const modelDirectory = modelDir || node_path_1.default.join(constants_1.WHISPER_CPP_PATH, 'models');
const modelFileName = constants_1.MODEL_OBJECT[autoDownloadModelName];
const modelFilePath = node_path_1.default.join(modelDirectory, modelFileName);
// Ensure download directory exists
if (!node_fs_1.default.existsSync(modelDirectory)) {
node_fs_1.default.mkdirSync(modelDirectory, { recursive: true });
logger.debug(`[nwhisper] Created model directory: ${modelDirectory}`);
}
// Check if model already exists
if (node_fs_1.default.existsSync(modelFilePath)) {
logger.debug(`[nwhisper] ${autoDownloadModelName} already exists in ${modelDirectory}. Skipping download.`);
return;
}
logger.debug(`[nwhisper] Auto-download Model: ${autoDownloadModelName} to ${modelDirectory}`);
// Always use whisper.cpp models directory for download script
const whisperModelsDir = node_path_1.default.join(constants_1.WHISPER_CPP_PATH, 'models');
shelljs_1.default.cd(whisperModelsDir);
let scriptPath = './download-ggml-model.sh';
if (process.platform === 'win32') {
scriptPath = 'download-ggml-model.cmd';
}
shelljs_1.default.chmod('+x', scriptPath);
const result = shelljs_1.default.exec(`${scriptPath} ${autoDownloadModelName}`);
if (result.code !== 0) {
throw new Error(`[nwhisper] Failed to download model: ${result.stderr}`);
}
// If using custom modelDir, move the downloaded model there
if (modelDir && modelDir !== whisperModelsDir) {
const sourceModelPath = node_path_1.default.join(whisperModelsDir, modelFileName);
if (node_fs_1.default.existsSync(sourceModelPath)) {
logger.debug(`[nwhisper] Moving model from ${sourceModelPath} to ${modelFilePath}`);
node_fs_1.default.copyFileSync(sourceModelPath, modelFilePath);
node_fs_1.default.unlinkSync(sourceModelPath); // Remove from original location
}
}
logger.debug('[nwhisper] Model downloaded. Attempting to build whisper.cpp...');
shelljs_1.default.cd('../');
// Configure CMake build
logger.debug('[nwhisper] Configuring CMake build...');
let configureCommand = 'cmake -B build';
if (withCuda) {
configureCommand += ' -DGGML_CUDA=1';
}
const configResult = shelljs_1.default.exec(configureCommand);
if (configResult.code !== 0) {
throw new Error(`[nwhisper] CMake configuration failed: ${configResult.stderr}`);
}
// Build the project
logger.debug('[nwhisper] Building whisper.cpp...');
const buildCommand = 'cmake --build build --config Release';
const buildResult = shelljs_1.default.exec(buildCommand);
if (buildResult.code !== 0) {
throw new Error(`[nwhisper] Build failed: ${buildResult.stderr}`);
}
logger.debug('[nwhisper] Model downloaded and built successfully');
}
catch (error) {
logger.error('[nwhisper] Error caught in autoDownloadModel:', error);
shelljs_1.default.cd(projectDir);
throw error;
}
}
//# sourceMappingURL=autoDownloadModel.js.map