node-diarization
Version:
Transcription and diarization using Whisper and Pyannote with NodeJS
90 lines (89 loc) • 3.36 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.formatCommandString = exports.shellExec = void 0;
const shelljs_1 = __importDefault(require("shelljs"));
const path_1 = __importDefault(require("path"));
const os_1 = __importDefault(require("os"));
const shellExec = (commandString, options, eventEmitter) => {
return new Promise((resolve, reject) => {
try {
//always run async, despite async option
const shellProcess = shelljs_1.default.exec(commandString, {
silent: options?.shell?.silent,
async: true,
});
const shellResult = {
errors: '',
data: '',
code: null,
};
// send chunk for parse
if (shellProcess.stdout) {
// chunks limited to 8192 bytes
// so json might be corrupted
shellProcess.stdout.on('data', (stdoutChunk) => {
shellResult.data += stdoutChunk;
if (eventEmitter) {
eventEmitter.emit('shell-stdout', stdoutChunk);
}
});
}
else {
shellResult.errors += 'No exec stdout\n';
}
if (shellProcess.stderr) {
shellProcess.stderr.on('data', (stderr) => {
shellResult.errors += stderr;
});
}
shellProcess.on('exit', code => {
if (eventEmitter) {
eventEmitter.emit('shell-close');
}
shellResult.code = code;
resolve(shellResult);
});
}
catch (e) {
reject(e);
}
});
};
exports.shellExec = shellExec;
const formatCommandString = (task, filePath, options) => {
let commandString = '';
// if venv path existed, (for linux and macOS users, generally)
if (options?.python?.venvPath) {
const platform = os_1.default.platform();
// for windows
if (platform === 'win32') {
commandString += `${options.python.venvPath}\\Scripts\\activate.bat && `;
// for linus and mac
}
else if (platform === 'darwin' || platform === 'linux') {
commandString += `. ${options.python.venvPath}/bin/activate && `;
}
else {
throw 'Platform is not supported, only Windows, MacOS or Linux is allowed';
}
}
commandString += `${options?.python?.var} ${path_1.default.resolve(path_1.default.join(__dirname, '..', 'py', task + '.py'))}`;
commandString += ' --file=' + filePath;
// excluded options fields from command arguments
const excludedFields = ['raw'];
if (options?.[task]) {
const currentTask = options[task];
Object.keys(currentTask).map((key) => {
if (excludedFields.some(f => f === key)) {
return;
}
commandString += ` --${key}=${currentTask[key]}`;
});
}
return commandString;
};
exports.formatCommandString = formatCommandString;
exports.default = exports.shellExec;