whisper-node-server
Version:
Local audio transcription on CPU. Node.js bindings for OpenAI's Whisper. Modified from node-whisper
119 lines • 5.29 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.initializeWhisperCpp = exports.shellExec = void 0;
const child_process_1 = require("child_process");
const path_1 = __importDefault(require("path"));
const shelljs_1 = __importDefault(require("shelljs"));
const fs_1 = __importDefault(require("fs"));
function findWhisperCppDir() {
// Check if running from node_modules
const nodeModulesPath = path_1.default.join(process.cwd(), 'node_modules', 'whisper-node-server', 'lib', 'whisper.cpp');
const releaseBin = path_1.default.join(nodeModulesPath, 'build', 'bin', 'Release');
if (fs_1.default.existsSync(releaseBin)) {
return releaseBin;
}
if (fs_1.default.existsSync(nodeModulesPath)) {
return nodeModulesPath;
}
// Check if running from development directory
const devPath = path_1.default.join(__dirname, '..', 'lib', 'whisper.cpp');
if (fs_1.default.existsSync(devPath)) {
return devPath;
}
throw new Error('Could not find whisper.cpp directory');
}
const defaultOptions = {
cwd: findWhisperCppDir(),
silent: false
};
// For commands that need output returned
function shellExec(command, options = {}) {
const mergedOptions = Object.assign(Object.assign({}, defaultOptions), options);
return new Promise((resolve, reject) => {
const result = shelljs_1.default.exec(command, {
silent: mergedOptions.silent,
cwd: mergedOptions.cwd,
async: false
});
if (result.code === 0) {
resolve(result.stdout);
}
else {
reject(new Error(result.stderr));
}
});
}
exports.shellExec = shellExec;
// For long-running processes that need to be managed
function shell(command, options = {}) {
const mergedOptions = Object.assign(Object.assign({}, defaultOptions), options);
const [cmd, ...args] = command.split(' ').filter(Boolean);
// Ensure we use the full path to the executable
const cmdPath = path_1.default.join(mergedOptions.cwd, cmd);
if (!mergedOptions.silent) {
console.log('[whisper-node-server] Executing:', cmdPath, args.join(' '));
console.log('[whisper-node-server] Working directory:', mergedOptions.cwd);
}
const childProcess = (0, child_process_1.spawn)(cmdPath, args, {
cwd: mergedOptions.cwd,
stdio: mergedOptions.silent ? 'ignore' : 'inherit',
windowsHide: false
});
childProcess.on('error', (error) => {
console.error('[whisper-node-server] Process error:', error);
});
if (!mergedOptions.silent) {
childProcess.on('exit', (code) => {
if (code === 0) {
console.log('[whisper-node-server] Process completed successfully');
}
else {
console.error(`[whisper-node-server] Process exited with code ${code}`);
}
});
}
return childProcess;
}
exports.default = shell;
// Initialize whisper.cpp build if needed
function initializeWhisperCpp() {
return __awaiter(this, void 0, void 0, function* () {
try {
const whisperDir = findWhisperCppDir();
shelljs_1.default.cd(whisperDir);
if (!shelljs_1.default.which('make')) {
throw new Error("make command not found. Please install build tools.");
}
const serverExe = path_1.default.join(whisperDir, 'server.exe');
if (!fs_1.default.existsSync(serverExe)) {
console.log("[whisper-node-server] Whisper.cpp server not built. Running make...");
const result = shelljs_1.default.exec('make', { silent: false });
if (result.code !== 0) {
throw new Error("Failed to build whisper.cpp");
}
if (!fs_1.default.existsSync(serverExe)) {
throw new Error("Build completed but server.exe not found");
}
console.log("[whisper-node-server] Successfully built whisper.cpp");
}
}
catch (error) {
console.error("[whisper-node-server] Error initializing whisper.cpp:", error);
throw error;
}
});
}
exports.initializeWhisperCpp = initializeWhisperCpp;
//# sourceMappingURL=shell.js.map