openai-whisper-js
Version:
openai-whisper-js is a Node.js wrapper for the OpenAI Whisper library, enabling seamless audio transcription using Whisper models. This package simplifies the process of interacting with Whisper by providing a JavaScript interface to execute transcription
90 lines (89 loc) • 4.3 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 });
const child_process_1 = require("child_process");
const fs_1 = require("fs");
const path_1 = __importDefault(require("path"));
class Whisper {
constructor() {
this.mappingArgs = {
modelName: (val) => {
return ['--model', val];
},
audio: (val) => {
if (typeof val === 'string') {
return val;
}
else if (Array.isArray(val)) {
return val.join(' ');
}
},
outDir: (val) => {
return ['--output_dir', val];
},
outputFormat: (val) => {
return ['--output_format', val];
},
verbose: (val) => {
return ['--verbose', val];
},
};
}
transcribe(options = { modelName: 'tiny', audio: '', debug: false }) {
return __awaiter(this, void 0, void 0, function* () {
return new Promise((resolve, reject) => {
const outDir = path_1.default.join(__dirname, '../../output');
const audio = options.audio;
const opt = Object.assign(Object.assign({}, options), { outDir, outputFormat: 'txt' });
delete opt.audio;
let args = Object.keys(opt).reduce((acc, key) => {
var _a, _b;
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return acc.concat(((_b = (_a = this === null || this === void 0 ? void 0 : this.mappingArgs) === null || _a === void 0 ? void 0 : _a[key]) === null || _b === void 0 ? void 0 : _b.call(_a, opt[key])) || '');
}, []);
args = args.concat(audio);
const whisperCommend = (0, child_process_1.spawn)(path_1.default.join(__dirname, '../../transcribe.sh'), args, {
shell: '/bin/bash',
});
if (options.debug) {
whisperCommend.stderr.on('data', (data) => {
console.log('DEBUG: stderr', (data === null || data === void 0 ? void 0 : data.toString()) || '');
});
whisperCommend.stdout.on('data', (data) => {
console.log('DEBUG: stdout', (data === null || data === void 0 ? void 0 : data.toString()) || '');
});
}
whisperCommend.on('exit', (code) => {
if (code === 0) {
try {
const pathParts = audio.split('/');
const [fileName] = pathParts[pathParts.length - 1].split('.');
const result = (0, fs_1.readFileSync)(`${outDir}/${fileName}.txt`, { encoding: 'utf-8' });
(0, fs_1.unlinkSync)(`${outDir}/${fileName}.txt`);
resolve(result);
}
catch (e) {
reject(new Error(`can not transcribe this audio ${audio}`));
}
}
else {
reject(new Error(`can not transcribe this audio ${audio}`));
}
});
});
});
}
}
exports.default = new Whisper();