@snowdigital/whisper-node
Version:
NodeJS interface for whisper.cpp
75 lines • 4.04 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = autoDownloadModel;
const path_1 = __importDefault(require("path"));
const shelljs_1 = __importDefault(require("shelljs"));
const fs_1 = __importDefault(require("fs"));
const constants_1 = require("./constants");
function autoDownloadModel() {
return __awaiter(this, arguments, void 0, function* (logger = console, autoDownloadModelName, withCuda = false) {
const projectDir = process.cwd();
if (!autoDownloadModelName) {
throw new Error('[Whisper-node] Error: Model name must be provided.');
}
if (!constants_1.MODELS_LIST.includes(autoDownloadModelName)) {
throw new Error('[Whisper-node] Error: Provide a valid model name');
}
try {
const modelDirectory = path_1.default.join(constants_1.WHISPER_CPP_PATH, 'models');
shelljs_1.default.cd(modelDirectory);
const modelAlreadyExist = fs_1.default.existsSync(path_1.default.join(modelDirectory, constants_1.MODEL_OBJECT[autoDownloadModelName]));
if (modelAlreadyExist) {
logger.debug(`[Whisper-node] ${autoDownloadModelName} already exist. Skipping download.`);
return 'Models already exist. Skipping download.';
}
logger.debug(`[Whisper-node] Auto-download Model: ${autoDownloadModelName}`);
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(`[Whisper-node] Failed to download model: ${result.stderr}`);
}
logger.debug('[Whisper-node] Model downloaded. Attempting to build whisper.cpp...');
shelljs_1.default.cd('../');
// Configure CMake build
logger.debug('[Whisper-node] 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(`[Whisper-node] CMake configuration failed: ${configResult.stderr}`);
}
// Build the project
logger.debug('[Whisper-node] Building whisper.cpp...');
const buildCommand = 'cmake --build build --config Release';
const buildResult = shelljs_1.default.exec(buildCommand);
if (buildResult.code !== 0) {
throw new Error(`[Whisper-node] Build failed: ${buildResult.stderr}`);
}
return 'Model downloaded and built successfully';
}
catch (error) {
logger.error('[Whisper-node] Error caught in autoDownloadModel:', error);
shelljs_1.default.cd(projectDir);
throw error;
}
});
}
//# sourceMappingURL=autoDownloadModel.js.map