node-diarization
Version:
Transcription and diarization using Whisper and Pyannote with NodeJS
156 lines (155 loc) • 6.15 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.WhisperDiarization = void 0;
const diarization_1 = __importDefault(require("./src/diarization"));
const recognition_1 = __importStar(require("./src/recognition"));
const union_1 = __importStar(require("./src/union"));
const checks_1 = require("./src/checks");
const utils_1 = require("./src/utils");
const constants_1 = require("./src/constants");
const node_events_1 = require("node:events");
class WhisperDiarization extends node_events_1.EventEmitter {
constructor(filePath, options) {
super();
this.result = {};
this.finalResult = {
errors: [],
};
this.segmentChunk = '';
this.options = (0, utils_1.mergeOptions)(constants_1.DEFAULT_OPTIONS, options || {});
this.filePath = filePath;
}
checks() {
this.filePath = (0, checks_1.checkFileAndConvertPathToAbsolute)(this.filePath, 'No audio file exists');
(0, checks_1.checkTask)(this.options);
}
async initDiarization() {
// do diarization
if (this.options.consoleTextInfo) {
console.log('********** Diarization start **********');
}
this.result.dSR = await (0, diarization_1.default)(this.filePath, this.options);
this.finalResult.errors = this.finalResult.errors.concat(this.result.dSR.errors);
if (this.options?.diarization?.raw) {
this.finalResult.rawDiarization = this.result.dSR.data?.diarization;
}
if (!this.result.dSR.data) {
if (this.options.consoleTextInfo) {
console.log('Diarization failed, aborted');
}
return;
}
if (this.options.consoleTextInfo) {
console.log('Diarization done successful');
}
}
async initRecognition() {
// do recognition
if (this.options.consoleTextInfo) {
console.log('********** Recognition start **********');
}
this.result.rSR = await (0, recognition_1.default)(this.filePath, this.options, this);
this.finalResult.errors = this.finalResult.errors.concat(this.result.rSR.errors);
if (this.options?.recognition?.raw) {
this.finalResult.rawRecognition = this.result.rSR.data?.recognition;
}
if (!this.result.rSR.data) {
if (this.options.consoleTextInfo) {
console.log('Recognition failed, aborted');
}
return;
}
if (this.options.consoleTextInfo) {
console.log('Recognition done successful');
}
}
initUnion() {
if (this.result.rSR?.data?.recognition &&
this.result.dSR?.data?.diarization) {
this.finalResult.union = (0, union_1.default)(this.result.rSR?.data?.recognition, this.result.dSR?.data.diarization);
}
}
initEvents() {
// get the chunks from shell
// proceed union if diarization data exists
this.on('shell-stdout', data => {
if (!this.result.dSR?.data?.diarization) {
return;
}
const parsedSC = (0, recognition_1.parseSegmentsChunks)(this.segmentChunk + data);
this.segmentChunk = parsedSC.textChunk ?? this.segmentChunk + parsedSC.textChunk;
if (parsedSC.segments.length > 0) {
this.emit('data', (0, union_1.unionRecognitionAndDiarization)(parsedSC.segments, this.result.dSR?.data.diarization));
}
});
this.on('shell-close', _ => {
this.emit('end');
});
}
async init() {
this.checks();
if (this.options.checks?.proceed) {
await (0, checks_1.checkPythonScripts)(this.options);
}
this.initEvents();
if (!this.options?.tasks || this.options.tasks.some(t => t === 'diarization')) {
await this.initDiarization();
}
if (!this.options?.tasks || this.options.tasks.some(t => t === 'recognition')) {
await this.initRecognition();
}
this.initUnion();
return this.finalResult;
}
static async check(pythonOptions) {
const options = {
tasks: [],
checks: {
proceed: true,
},
consoleTextInfo: true,
};
if (pythonOptions) {
options.python = pythonOptions;
}
await (0, checks_1.checkPythonScripts)((0, utils_1.mergeOptions)(constants_1.DEFAULT_OPTIONS, options));
}
}
exports.WhisperDiarization = WhisperDiarization;
exports.default = WhisperDiarization;