mirror of
https://github.com/ggerganov/whisper.cpp.git
synced 2025-06-08 10:47:16 +02:00
This commit add support for language detection in the Whisper Node.js addon example. It also updates the node addon to return an object instead of an array as the results. The motivation for this change is to enable the inclusion of the detected language in the result, in addition to the transcription segments. For example, when using the `detect_language` option, the result will now be: ```console { language: 'en' } ``` And if the `language` option is set to "auto", it will also return: ```console { language: 'en', transcription: [ [ '00:00:00.000', '00:00:07.600', ' And so my fellow Americans, ask not what your country can do for you,' ], [ '00:00:07.600', '00:00:10.600', ' ask what you can do for your country.' ] ] } ```
58 lines
1.3 KiB
JavaScript
58 lines
1.3 KiB
JavaScript
const path = require("path");
|
|
const { whisper } = require(path.join(
|
|
__dirname,
|
|
"../../build/Release/addon.node"
|
|
));
|
|
const { promisify } = require("util");
|
|
|
|
const whisperAsync = promisify(whisper);
|
|
|
|
const whisperParams = {
|
|
language: "en",
|
|
model: path.join(__dirname, "../../models/ggml-base.en.bin"),
|
|
fname_inp: path.join(__dirname, "../../samples/jfk.wav"),
|
|
use_gpu: true,
|
|
flash_attn: false,
|
|
no_prints: true,
|
|
comma_in_time: false,
|
|
translate: true,
|
|
no_timestamps: false,
|
|
detect_language: false,
|
|
audio_ctx: 0,
|
|
max_len: 0,
|
|
progress_callback: (progress) => {
|
|
console.log(`progress: ${progress}%`);
|
|
}
|
|
};
|
|
|
|
const arguments = process.argv.slice(2);
|
|
const params = Object.fromEntries(
|
|
arguments.reduce((pre, item) => {
|
|
if (item.startsWith("--")) {
|
|
const [key, value] = item.slice(2).split("=");
|
|
if (key === "audio_ctx") {
|
|
whisperParams[key] = parseInt(value);
|
|
} else if (key === "detect_language") {
|
|
whisperParams[key] = value === "true";
|
|
} else {
|
|
whisperParams[key] = value;
|
|
}
|
|
return pre;
|
|
}
|
|
return pre;
|
|
}, [])
|
|
);
|
|
|
|
for (const key in params) {
|
|
if (whisperParams.hasOwnProperty(key)) {
|
|
whisperParams[key] = params[key];
|
|
}
|
|
}
|
|
|
|
console.log("whisperParams =", whisperParams);
|
|
|
|
whisperAsync(whisperParams).then((result) => {
|
|
console.log();
|
|
console.log(result);
|
|
});
|