nwhisper
Version:
Native Node.js bindings for OpenAI's Whisper using whisper.cpp. High-performance local speech-to-text with custom model support.
106 lines • 4.37 kB
JavaScript
;
// npx nwhisper download
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const node_fs_1 = __importDefault(require("node:fs"));
const node_path_1 = __importDefault(require("node:path"));
const readline_sync_1 = __importDefault(require("readline-sync"));
const shelljs_1 = __importDefault(require("shelljs"));
const constants_1 = require("./constants");
const askForModel = async (logger = console) => {
const answer = readline_sync_1.default.question(`\n[nwhisper] Enter model name (e.g. 'tiny.en') or 'cancel' to exit\n(ENTER for tiny.en): `);
if (answer === 'cancel') {
logger.log('[nwhisper] Exiting model downloader.\n');
process.exit(0);
}
// User presses enter
else if (answer === '') {
logger.log('[nwhisper] Going with', constants_1.DEFAULT_MODEL);
return constants_1.DEFAULT_MODEL;
}
else if (!constants_1.MODELS_LIST.includes(answer)) {
logger.log('\n[nwhisper] FAIL: Name not found. Check your spelling OR quit wizard and use custom model.\n');
return await askForModel();
}
return answer;
};
const askIfUserWantToUseCuda = (logger = console) => {
const answer = readline_sync_1.default.question(`\n[nwhisper] Do you want to use CUDA for compilation? (y/n)\n(ENTER for n): `);
if (answer === 'y') {
logger.log('[nwhisper] Using CUDA for compilation.');
return true;
}
else {
logger.log('[nwhisper] Not using CUDA for compilation.');
return false;
}
};
async function downloadModel(logger = console) {
try {
shelljs_1.default.cd(node_path_1.default.join(constants_1.WHISPER_CPP_PATH, 'models'));
const anyModelExist = [];
constants_1.MODELS_LIST.forEach(model => {
if (!node_fs_1.default.existsSync(node_path_1.default.join(constants_1.WHISPER_CPP_PATH, 'models', constants_1.MODEL_OBJECT[model]))) {
// Model does not exist, skip
}
else {
anyModelExist.push(model);
}
});
if (anyModelExist.length > 0) {
logger.log('\n[nwhisper] Currently installed models:');
anyModelExist.forEach(model => logger.log(`- ${model}`));
logger.log('\n[nwhisper] You can install additional models from the list below.\n');
}
logger.log(`
| Model | Disk | RAM |
|----------------|--------|---------|
| tiny | 75 MB | ~390 MB |
| tiny.en | 75 MB | ~390 MB |
| base | 142 MB | ~500 MB |
| base.en | 142 MB | ~500 MB |
| small | 466 MB | ~1.0 GB |
| small.en | 466 MB | ~1.0 GB |
| medium | 1.5 GB | ~2.6 GB |
| medium.en | 1.5 GB | ~2.6 GB |
| large-v1 | 2.9 GB | ~4.7 GB |
| large | 2.9 GB | ~4.7 GB |
| large-v3-turbo | 1.5 GB | ~2.6 GB |
`);
if (!shelljs_1.default.which('./download-ggml-model.sh')) {
throw '[nwhisper] Error: Downloader not found.\n';
}
const modelName = await askForModel();
let scriptPath = './download-ggml-model.sh';
if (process.platform === 'win32')
scriptPath = 'download-ggml-model.cmd';
shelljs_1.default.chmod('+x', scriptPath);
shelljs_1.default.exec(`${scriptPath} ${modelName}`);
logger.log('[nwhisper] Attempting to build whisper.cpp...\n');
shelljs_1.default.cd('../');
const withCuda = askIfUserWantToUseCuda();
// Use CMake instead of make
logger.log('[nwhisper] Configuring CMake build...');
let configureCommand = 'cmake -B build';
if (withCuda) {
configureCommand += ' -DGGML_CUDA=1';
}
shelljs_1.default.exec(configureCommand);
logger.log('[nwhisper] Building with CMake...');
shelljs_1.default.exec('cmake --build build --config Release');
process.exit(0);
}
catch (error) {
logger.error('[nwhisper] Error Caught in downloadModel\n');
logger.error(error);
return error;
}
}
downloadModel().catch(error => {
console.error('Failed to download:', error);
process.exit(1);
});
//# sourceMappingURL=downloadModel.js.map