Spaces:
Sleeping
Sleeping
Qianhe Chen
commited on
addon.node : using whisper as a Node.js addon (#443)
Browse files* addon: implement node addon call whisper through cpp
* addon: modify the license to MIT
* addon: remove iostream
* addon: rename dir
* addon: fix typo
* addon: configure cmake to build when cmake-js is used
- examples/CMakeLists.txt +2 -0
- examples/addon.node/.gitignore +3 -0
- examples/addon.node/CMakeLists.txt +26 -0
- examples/addon.node/README.md +37 -0
- examples/addon.node/addon.cpp +421 -0
- examples/addon.node/index.js +27 -0
- examples/addon.node/package.json +12 -0
examples/CMakeLists.txt
CHANGED
|
@@ -24,6 +24,8 @@ if (EMSCRIPTEN)
|
|
| 24 |
add_subdirectory(command.wasm)
|
| 25 |
add_subdirectory(talk.wasm)
|
| 26 |
add_subdirectory(bench.wasm)
|
|
|
|
|
|
|
| 27 |
else()
|
| 28 |
add_subdirectory(main)
|
| 29 |
add_subdirectory(stream)
|
|
|
|
| 24 |
add_subdirectory(command.wasm)
|
| 25 |
add_subdirectory(talk.wasm)
|
| 26 |
add_subdirectory(bench.wasm)
|
| 27 |
+
elseif(CMAKE_JS_VERSION)
|
| 28 |
+
add_subdirectory(addon.node)
|
| 29 |
else()
|
| 30 |
add_subdirectory(main)
|
| 31 |
add_subdirectory(stream)
|
examples/addon.node/.gitignore
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
.idea
|
| 2 |
+
node_modules
|
| 3 |
+
build
|
examples/addon.node/CMakeLists.txt
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
set(TARGET whisper-addon)
|
| 2 |
+
|
| 3 |
+
# Base settings
|
| 4 |
+
#==================================================================
|
| 5 |
+
# env var supported by cmake-js
|
| 6 |
+
add_definitions(-DNAPI_VERSION=4)
|
| 7 |
+
include_directories(${CMAKE_JS_INC})
|
| 8 |
+
#==================================================================
|
| 9 |
+
|
| 10 |
+
add_library(${TARGET} SHARED ${CMAKE_JS_SRC} addon.cpp)
|
| 11 |
+
set_target_properties(${TARGET} PROPERTIES PREFIX "" SUFFIX ".node")
|
| 12 |
+
|
| 13 |
+
include(DefaultTargetOptions)
|
| 14 |
+
|
| 15 |
+
# Include N-API wrappers
|
| 16 |
+
#==================================================================
|
| 17 |
+
execute_process(COMMAND node -p "require('node-addon-api').include"
|
| 18 |
+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
|
| 19 |
+
OUTPUT_VARIABLE NODE_ADDON_API_DIR
|
| 20 |
+
)
|
| 21 |
+
string(REPLACE "\n" "" NODE_ADDON_API_DIR ${NODE_ADDON_API_DIR})
|
| 22 |
+
string(REPLACE "\"" "" NODE_ADDON_API_DIR ${NODE_ADDON_API_DIR})
|
| 23 |
+
target_include_directories(${TARGET} PRIVATE ${NODE_ADDON_API_DIR})
|
| 24 |
+
#==================================================================
|
| 25 |
+
|
| 26 |
+
target_link_libraries(${TARGET} ${CMAKE_JS_LIB} whisper ${CMAKE_THREAD_LIBS_INIT})
|
examples/addon.node/README.md
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# addon
|
| 2 |
+
|
| 3 |
+
This is an addon demo that can **perform whisper model reasoning in `node` and `electron` environments**, based on [cmake-js](https://github.com/cmake-js/cmake-js).
|
| 4 |
+
It can be used as a reference for using the whisper.cpp project in other node projects.
|
| 5 |
+
|
| 6 |
+
## Install
|
| 7 |
+
|
| 8 |
+
```shell
|
| 9 |
+
npm install
|
| 10 |
+
```
|
| 11 |
+
|
| 12 |
+
## Compile
|
| 13 |
+
|
| 14 |
+
Make sure it is in the project root directory and compiled with make-js.
|
| 15 |
+
|
| 16 |
+
```shell
|
| 17 |
+
npx cmake-js compile -T whisper-addon
|
| 18 |
+
```
|
| 19 |
+
|
| 20 |
+
For Electron addon and cmake-js options, you can see [cmake-js](https://github.com/cmake-js/cmake-js) and make very few configuration changes.
|
| 21 |
+
|
| 22 |
+
> Such as appointing special cmake path:
|
| 23 |
+
> ```shell
|
| 24 |
+
> npx cmake-js compile -c 'xxx/cmake' -T whisper-addon
|
| 25 |
+
> ```
|
| 26 |
+
|
| 27 |
+
## Run
|
| 28 |
+
|
| 29 |
+
```shell
|
| 30 |
+
cd examples/addon.node
|
| 31 |
+
|
| 32 |
+
node index.js --language='language' --model='model-path' --fname_inp='file-path'
|
| 33 |
+
```
|
| 34 |
+
|
| 35 |
+
Because this is a simple Demo, only the above parameters are set in the node environment.
|
| 36 |
+
|
| 37 |
+
Other parameters can also be specified in the node environment.
|
examples/addon.node/addon.cpp
ADDED
|
@@ -0,0 +1,421 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#include <string>
|
| 2 |
+
#include <thread>
|
| 3 |
+
#include <vector>
|
| 4 |
+
#include <cmath>
|
| 5 |
+
|
| 6 |
+
#include "napi.h"
|
| 7 |
+
|
| 8 |
+
#define DR_WAV_IMPLEMENTATION
|
| 9 |
+
#include "dr_wav.h"
|
| 10 |
+
|
| 11 |
+
#include "whisper.h"
|
| 12 |
+
|
| 13 |
+
struct whisper_params {
|
| 14 |
+
int32_t n_threads = std::min(4, (int32_t) std::thread::hardware_concurrency());
|
| 15 |
+
int32_t n_processors = 1;
|
| 16 |
+
int32_t offset_t_ms = 0;
|
| 17 |
+
int32_t offset_n = 0;
|
| 18 |
+
int32_t duration_ms = 0;
|
| 19 |
+
int32_t max_context = -1;
|
| 20 |
+
int32_t max_len = 0;
|
| 21 |
+
int32_t best_of = 5;
|
| 22 |
+
int32_t beam_size = -1;
|
| 23 |
+
|
| 24 |
+
float word_thold = 0.01f;
|
| 25 |
+
float entropy_thold = 2.4f;
|
| 26 |
+
float logprob_thold = -1.0f;
|
| 27 |
+
|
| 28 |
+
bool speed_up = false;
|
| 29 |
+
bool translate = false;
|
| 30 |
+
bool diarize = false;
|
| 31 |
+
bool output_txt = false;
|
| 32 |
+
bool output_vtt = false;
|
| 33 |
+
bool output_srt = false;
|
| 34 |
+
bool output_wts = false;
|
| 35 |
+
bool output_csv = false;
|
| 36 |
+
bool print_special = false;
|
| 37 |
+
bool print_colors = false;
|
| 38 |
+
bool print_progress = false;
|
| 39 |
+
bool no_timestamps = false;
|
| 40 |
+
|
| 41 |
+
std::string language = "en";
|
| 42 |
+
std::string prompt;
|
| 43 |
+
std::string model = "../../ggml-large.bin";
|
| 44 |
+
|
| 45 |
+
std::vector<std::string> fname_inp = {};
|
| 46 |
+
std::vector<std::string> fname_outp = {};
|
| 47 |
+
};
|
| 48 |
+
|
| 49 |
+
struct whisper_print_user_data {
|
| 50 |
+
const whisper_params * params;
|
| 51 |
+
|
| 52 |
+
const std::vector<std::vector<float>> * pcmf32s;
|
| 53 |
+
};
|
| 54 |
+
|
| 55 |
+
// 500 -> 00:05.000
|
| 56 |
+
// 6000 -> 01:00.000
|
| 57 |
+
std::string to_timestamp(int64_t t, bool comma = false) {
|
| 58 |
+
int64_t msec = t * 10;
|
| 59 |
+
int64_t hr = msec / (1000 * 60 * 60);
|
| 60 |
+
msec = msec - hr * (1000 * 60 * 60);
|
| 61 |
+
int64_t min = msec / (1000 * 60);
|
| 62 |
+
msec = msec - min * (1000 * 60);
|
| 63 |
+
int64_t sec = msec / 1000;
|
| 64 |
+
msec = msec - sec * 1000;
|
| 65 |
+
|
| 66 |
+
char buf[32];
|
| 67 |
+
snprintf(buf, sizeof(buf), "%02d:%02d:%02d%s%03d", (int) hr, (int) min, (int) sec, comma ? "," : ".", (int) msec);
|
| 68 |
+
|
| 69 |
+
return std::string(buf);
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
int timestamp_to_sample(int64_t t, int n_samples) {
|
| 73 |
+
return std::max(0, std::min((int) n_samples - 1, (int) ((t*WHISPER_SAMPLE_RATE)/100)));
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
void whisper_print_segment_callback(struct whisper_context * ctx, int n_new, void * user_data) {
|
| 77 |
+
const auto & params = *((whisper_print_user_data *) user_data)->params;
|
| 78 |
+
const auto & pcmf32s = *((whisper_print_user_data *) user_data)->pcmf32s;
|
| 79 |
+
|
| 80 |
+
const int n_segments = whisper_full_n_segments(ctx);
|
| 81 |
+
|
| 82 |
+
std::string speaker = "";
|
| 83 |
+
|
| 84 |
+
int64_t t0;
|
| 85 |
+
int64_t t1;
|
| 86 |
+
|
| 87 |
+
// print the last n_new segments
|
| 88 |
+
const int s0 = n_segments - n_new;
|
| 89 |
+
|
| 90 |
+
if (s0 == 0) {
|
| 91 |
+
printf("\n");
|
| 92 |
+
}
|
| 93 |
+
|
| 94 |
+
for (int i = s0; i < n_segments; i++) {
|
| 95 |
+
if (!params.no_timestamps || params.diarize) {
|
| 96 |
+
t0 = whisper_full_get_segment_t0(ctx, i);
|
| 97 |
+
t1 = whisper_full_get_segment_t1(ctx, i);
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
if (!params.no_timestamps) {
|
| 101 |
+
printf("[%s --> %s] ", to_timestamp(t0).c_str(), to_timestamp(t1).c_str());
|
| 102 |
+
}
|
| 103 |
+
|
| 104 |
+
if (params.diarize && pcmf32s.size() == 2) {
|
| 105 |
+
const int64_t n_samples = pcmf32s[0].size();
|
| 106 |
+
|
| 107 |
+
const int64_t is0 = timestamp_to_sample(t0, n_samples);
|
| 108 |
+
const int64_t is1 = timestamp_to_sample(t1, n_samples);
|
| 109 |
+
|
| 110 |
+
double energy0 = 0.0f;
|
| 111 |
+
double energy1 = 0.0f;
|
| 112 |
+
|
| 113 |
+
for (int64_t j = is0; j < is1; j++) {
|
| 114 |
+
energy0 += fabs(pcmf32s[0][j]);
|
| 115 |
+
energy1 += fabs(pcmf32s[1][j]);
|
| 116 |
+
}
|
| 117 |
+
|
| 118 |
+
if (energy0 > 1.1*energy1) {
|
| 119 |
+
speaker = "(speaker 0)";
|
| 120 |
+
} else if (energy1 > 1.1*energy0) {
|
| 121 |
+
speaker = "(speaker 1)";
|
| 122 |
+
} else {
|
| 123 |
+
speaker = "(speaker ?)";
|
| 124 |
+
}
|
| 125 |
+
|
| 126 |
+
//printf("is0 = %lld, is1 = %lld, energy0 = %f, energy1 = %f, %s\n", is0, is1, energy0, energy1, speaker.c_str());
|
| 127 |
+
}
|
| 128 |
+
|
| 129 |
+
// colorful print bug
|
| 130 |
+
//
|
| 131 |
+
const char * text = whisper_full_get_segment_text(ctx, i);
|
| 132 |
+
printf("%s%s", speaker.c_str(), text);
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
// with timestamps or speakers: each segment on new line
|
| 136 |
+
if (!params.no_timestamps || params.diarize) {
|
| 137 |
+
printf("\n");
|
| 138 |
+
}
|
| 139 |
+
|
| 140 |
+
fflush(stdout);
|
| 141 |
+
}
|
| 142 |
+
}
|
| 143 |
+
|
| 144 |
+
int run(whisper_params ¶ms, std::vector<std::vector<std::string>> &result) {
|
| 145 |
+
|
| 146 |
+
if (params.fname_inp.empty()) {
|
| 147 |
+
fprintf(stderr, "error: no input files specified\n");
|
| 148 |
+
return 2;
|
| 149 |
+
}
|
| 150 |
+
|
| 151 |
+
if (params.language != "auto" && whisper_lang_id(params.language.c_str()) == -1) {
|
| 152 |
+
fprintf(stderr, "error: unknown language '%s'\n", params.language.c_str());
|
| 153 |
+
exit(0);
|
| 154 |
+
}
|
| 155 |
+
|
| 156 |
+
// whisper init
|
| 157 |
+
|
| 158 |
+
struct whisper_context * ctx = whisper_init_from_file(params.model.c_str());
|
| 159 |
+
|
| 160 |
+
if (ctx == nullptr) {
|
| 161 |
+
fprintf(stderr, "error: failed to initialize whisper context\n");
|
| 162 |
+
return 3;
|
| 163 |
+
}
|
| 164 |
+
|
| 165 |
+
// initial prompt
|
| 166 |
+
std::vector<whisper_token> prompt_tokens;
|
| 167 |
+
|
| 168 |
+
if (!params.prompt.empty()) {
|
| 169 |
+
prompt_tokens.resize(1024);
|
| 170 |
+
prompt_tokens.resize(whisper_tokenize(ctx, params.prompt.c_str(), prompt_tokens.data(), prompt_tokens.size()));
|
| 171 |
+
|
| 172 |
+
fprintf(stderr, "\n");
|
| 173 |
+
fprintf(stderr, "initial prompt: '%s'\n", params.prompt.c_str());
|
| 174 |
+
fprintf(stderr, "initial tokens: [ ");
|
| 175 |
+
for (int i = 0; i < (int) prompt_tokens.size(); ++i) {
|
| 176 |
+
fprintf(stderr, "%d ", prompt_tokens[i]);
|
| 177 |
+
}
|
| 178 |
+
fprintf(stderr, "]\n");
|
| 179 |
+
}
|
| 180 |
+
|
| 181 |
+
for (int f = 0; f < (int) params.fname_inp.size(); ++f) {
|
| 182 |
+
const auto fname_inp = params.fname_inp[f];
|
| 183 |
+
const auto fname_outp = f < (int)params.fname_outp.size() && !params.fname_outp[f].empty() ? params.fname_outp[f] : params.fname_inp[f];
|
| 184 |
+
|
| 185 |
+
std::vector<float> pcmf32; // mono-channel F32 PCM
|
| 186 |
+
std::vector<std::vector<float>> pcmf32s; // stereo-channel F32 PCM
|
| 187 |
+
|
| 188 |
+
// WAV input
|
| 189 |
+
{
|
| 190 |
+
drwav wav;
|
| 191 |
+
std::vector<uint8_t> wav_data; // used for pipe input from stdin
|
| 192 |
+
|
| 193 |
+
if (fname_inp == "-") {
|
| 194 |
+
{
|
| 195 |
+
uint8_t buf[1024];
|
| 196 |
+
while (true)
|
| 197 |
+
{
|
| 198 |
+
const size_t n = fread(buf, 1, sizeof(buf), stdin);
|
| 199 |
+
if (n == 0) {
|
| 200 |
+
break;
|
| 201 |
+
}
|
| 202 |
+
wav_data.insert(wav_data.end(), buf, buf + n);
|
| 203 |
+
}
|
| 204 |
+
}
|
| 205 |
+
|
| 206 |
+
if (drwav_init_memory(&wav, wav_data.data(), wav_data.size(), nullptr) == false) {
|
| 207 |
+
fprintf(stderr, "error: failed to open WAV file from stdin\n");
|
| 208 |
+
return 4;
|
| 209 |
+
}
|
| 210 |
+
|
| 211 |
+
fprintf(stderr, "%s: read %zu bytes from stdin\n", __func__, wav_data.size());
|
| 212 |
+
}
|
| 213 |
+
else if (drwav_init_file(&wav, fname_inp.c_str(), nullptr) == false) {
|
| 214 |
+
fprintf(stderr, "error: failed to open '%s' as WAV file\n", fname_inp.c_str());
|
| 215 |
+
return 5;
|
| 216 |
+
}
|
| 217 |
+
|
| 218 |
+
if (wav.channels != 1 && wav.channels != 2) {
|
| 219 |
+
fprintf(stderr, "error: WAV file '%s' must be mono or stereo\n", fname_inp.c_str());
|
| 220 |
+
return 6;
|
| 221 |
+
}
|
| 222 |
+
|
| 223 |
+
if (params.diarize && wav.channels != 2 && params.no_timestamps == false) {
|
| 224 |
+
fprintf(stderr, "error: WAV file '%s' must be stereo for diarization and timestamps have to be enabled\n", fname_inp.c_str());
|
| 225 |
+
return 6;
|
| 226 |
+
}
|
| 227 |
+
|
| 228 |
+
if (wav.sampleRate != WHISPER_SAMPLE_RATE) {
|
| 229 |
+
fprintf(stderr, "error: WAV file '%s' must be %i kHz\n", fname_inp.c_str(), WHISPER_SAMPLE_RATE/1000);
|
| 230 |
+
return 8;
|
| 231 |
+
}
|
| 232 |
+
|
| 233 |
+
if (wav.bitsPerSample != 16) {
|
| 234 |
+
fprintf(stderr, "error: WAV file '%s' must be 16-bit\n", fname_inp.c_str());
|
| 235 |
+
return 9;
|
| 236 |
+
}
|
| 237 |
+
|
| 238 |
+
const uint64_t n = wav_data.empty() ? wav.totalPCMFrameCount : wav_data.size()/(wav.channels*wav.bitsPerSample/8);
|
| 239 |
+
|
| 240 |
+
std::vector<int16_t> pcm16;
|
| 241 |
+
pcm16.resize(n*wav.channels);
|
| 242 |
+
drwav_read_pcm_frames_s16(&wav, n, pcm16.data());
|
| 243 |
+
drwav_uninit(&wav);
|
| 244 |
+
|
| 245 |
+
// convert to mono, float
|
| 246 |
+
pcmf32.resize(n);
|
| 247 |
+
if (wav.channels == 1) {
|
| 248 |
+
for (uint64_t i = 0; i < n; i++) {
|
| 249 |
+
pcmf32[i] = float(pcm16[i])/32768.0f;
|
| 250 |
+
}
|
| 251 |
+
} else {
|
| 252 |
+
for (uint64_t i = 0; i < n; i++) {
|
| 253 |
+
pcmf32[i] = float(pcm16[2*i] + pcm16[2*i + 1])/65536.0f;
|
| 254 |
+
}
|
| 255 |
+
}
|
| 256 |
+
|
| 257 |
+
if (params.diarize) {
|
| 258 |
+
// convert to stereo, float
|
| 259 |
+
pcmf32s.resize(2);
|
| 260 |
+
|
| 261 |
+
pcmf32s[0].resize(n);
|
| 262 |
+
pcmf32s[1].resize(n);
|
| 263 |
+
for (uint64_t i = 0; i < n; i++) {
|
| 264 |
+
pcmf32s[0][i] = float(pcm16[2*i])/32768.0f;
|
| 265 |
+
pcmf32s[1][i] = float(pcm16[2*i + 1])/32768.0f;
|
| 266 |
+
}
|
| 267 |
+
}
|
| 268 |
+
}
|
| 269 |
+
|
| 270 |
+
// print system information
|
| 271 |
+
{
|
| 272 |
+
fprintf(stderr, "\n");
|
| 273 |
+
fprintf(stderr, "system_info: n_threads = %d / %d | %s\n",
|
| 274 |
+
params.n_threads*params.n_processors, std::thread::hardware_concurrency(), whisper_print_system_info());
|
| 275 |
+
}
|
| 276 |
+
|
| 277 |
+
// print some info about the processing
|
| 278 |
+
{
|
| 279 |
+
fprintf(stderr, "\n");
|
| 280 |
+
if (!whisper_is_multilingual(ctx)) {
|
| 281 |
+
if (params.language != "en" || params.translate) {
|
| 282 |
+
params.language = "en";
|
| 283 |
+
params.translate = false;
|
| 284 |
+
fprintf(stderr, "%s: WARNING: model is not multilingual, ignoring language and translation options\n", __func__);
|
| 285 |
+
}
|
| 286 |
+
}
|
| 287 |
+
fprintf(stderr, "%s: processing '%s' (%d samples, %.1f sec), %d threads, %d processors, lang = %s, task = %s, timestamps = %d ...\n",
|
| 288 |
+
__func__, fname_inp.c_str(), int(pcmf32.size()), float(pcmf32.size())/WHISPER_SAMPLE_RATE,
|
| 289 |
+
params.n_threads, params.n_processors,
|
| 290 |
+
params.language.c_str(),
|
| 291 |
+
params.translate ? "translate" : "transcribe",
|
| 292 |
+
params.no_timestamps ? 0 : 1);
|
| 293 |
+
|
| 294 |
+
fprintf(stderr, "\n");
|
| 295 |
+
}
|
| 296 |
+
|
| 297 |
+
// run the inference
|
| 298 |
+
{
|
| 299 |
+
whisper_full_params wparams = whisper_full_default_params(WHISPER_SAMPLING_GREEDY);
|
| 300 |
+
|
| 301 |
+
wparams.strategy = params.beam_size > 1 ? WHISPER_SAMPLING_BEAM_SEARCH : WHISPER_SAMPLING_GREEDY;
|
| 302 |
+
|
| 303 |
+
wparams.print_realtime = false;
|
| 304 |
+
wparams.print_progress = params.print_progress;
|
| 305 |
+
wparams.print_timestamps = !params.no_timestamps;
|
| 306 |
+
wparams.print_special = params.print_special;
|
| 307 |
+
wparams.translate = params.translate;
|
| 308 |
+
wparams.language = params.language.c_str();
|
| 309 |
+
wparams.n_threads = params.n_threads;
|
| 310 |
+
wparams.n_max_text_ctx = params.max_context >= 0 ? params.max_context : wparams.n_max_text_ctx;
|
| 311 |
+
wparams.offset_ms = params.offset_t_ms;
|
| 312 |
+
wparams.duration_ms = params.duration_ms;
|
| 313 |
+
|
| 314 |
+
wparams.token_timestamps = params.output_wts || params.max_len > 0;
|
| 315 |
+
wparams.thold_pt = params.word_thold;
|
| 316 |
+
wparams.entropy_thold = params.entropy_thold;
|
| 317 |
+
wparams.logprob_thold = params.logprob_thold;
|
| 318 |
+
wparams.max_len = params.output_wts && params.max_len == 0 ? 60 : params.max_len;
|
| 319 |
+
|
| 320 |
+
wparams.speed_up = params.speed_up;
|
| 321 |
+
|
| 322 |
+
wparams.greedy.best_of = params.best_of;
|
| 323 |
+
wparams.beam_search.beam_size = params.beam_size;
|
| 324 |
+
|
| 325 |
+
wparams.prompt_tokens = prompt_tokens.empty() ? nullptr : prompt_tokens.data();
|
| 326 |
+
wparams.prompt_n_tokens = prompt_tokens.empty() ? 0 : prompt_tokens.size();
|
| 327 |
+
|
| 328 |
+
whisper_print_user_data user_data = { ¶ms, &pcmf32s };
|
| 329 |
+
|
| 330 |
+
// this callback is called on each new segment
|
| 331 |
+
if (!wparams.print_realtime) {
|
| 332 |
+
wparams.new_segment_callback = whisper_print_segment_callback;
|
| 333 |
+
wparams.new_segment_callback_user_data = &user_data;
|
| 334 |
+
}
|
| 335 |
+
|
| 336 |
+
// example for abort mechanism
|
| 337 |
+
// in this example, we do not abort the processing, but we could if the flag is set to true
|
| 338 |
+
// the callback is called before every encoder run - if it returns false, the processing is aborted
|
| 339 |
+
{
|
| 340 |
+
static bool is_aborted = false; // NOTE: this should be atomic to avoid data race
|
| 341 |
+
|
| 342 |
+
wparams.encoder_begin_callback = [](struct whisper_context * /*ctx*/, void * user_data) {
|
| 343 |
+
bool is_aborted = *(bool*)user_data;
|
| 344 |
+
return !is_aborted;
|
| 345 |
+
};
|
| 346 |
+
wparams.encoder_begin_callback_user_data = &is_aborted;
|
| 347 |
+
}
|
| 348 |
+
|
| 349 |
+
if (whisper_full_parallel(ctx, wparams, pcmf32.data(), pcmf32.size(), params.n_processors) != 0) {
|
| 350 |
+
fprintf(stderr, "failed to process audio\n");
|
| 351 |
+
return 10;
|
| 352 |
+
}
|
| 353 |
+
}
|
| 354 |
+
}
|
| 355 |
+
|
| 356 |
+
const int n_segments = whisper_full_n_segments(ctx);
|
| 357 |
+
result.resize(n_segments);
|
| 358 |
+
for (int i = 0; i < n_segments; ++i) {
|
| 359 |
+
const char * text = whisper_full_get_segment_text(ctx, i);
|
| 360 |
+
const int64_t t0 = whisper_full_get_segment_t0(ctx, i);
|
| 361 |
+
const int64_t t1 = whisper_full_get_segment_t1(ctx, i);
|
| 362 |
+
|
| 363 |
+
result[i].emplace_back(to_timestamp(t0, true));
|
| 364 |
+
result[i].emplace_back(to_timestamp(t1, true));
|
| 365 |
+
result[i].emplace_back(text);
|
| 366 |
+
}
|
| 367 |
+
|
| 368 |
+
whisper_print_timings(ctx);
|
| 369 |
+
whisper_free(ctx);
|
| 370 |
+
|
| 371 |
+
return 0;
|
| 372 |
+
}
|
| 373 |
+
|
| 374 |
+
Napi::Object whisper(const Napi::CallbackInfo& info) {
|
| 375 |
+
Napi::Env env = info.Env();
|
| 376 |
+
if (info.Length() <= 0 || !info[0].IsObject()) {
|
| 377 |
+
Napi::TypeError::New(env, "object expected").ThrowAsJavaScriptException();
|
| 378 |
+
}
|
| 379 |
+
whisper_params params;
|
| 380 |
+
std::vector<std::vector<std::string>> result;
|
| 381 |
+
|
| 382 |
+
Napi::Object whisper_params = info[0].As<Napi::Object>();
|
| 383 |
+
std::string language = whisper_params.Get("language").As<Napi::String>();
|
| 384 |
+
std::string model = whisper_params.Get("model").As<Napi::String>();
|
| 385 |
+
std::string input = whisper_params.Get("fname_inp").As<Napi::String>();
|
| 386 |
+
|
| 387 |
+
params.language = language;
|
| 388 |
+
params.model = model;
|
| 389 |
+
params.fname_inp.emplace_back(input);
|
| 390 |
+
|
| 391 |
+
// run model
|
| 392 |
+
run(params, result);
|
| 393 |
+
|
| 394 |
+
fprintf(stderr, "RESULT:\n");
|
| 395 |
+
for (auto sentence:result) {
|
| 396 |
+
fprintf(stderr, "t0: %s, t1: %s, content: %s \n",
|
| 397 |
+
sentence[0].c_str(), sentence[1].c_str(), sentence[2].c_str());
|
| 398 |
+
}
|
| 399 |
+
|
| 400 |
+
Napi::Object res = Napi::Array::New(env, result.size());
|
| 401 |
+
for (u_int32_t i = 0; i < result.size(); ++i) {
|
| 402 |
+
Napi::Object tmp = Napi::Array::New(env, 3);
|
| 403 |
+
for (u_int32_t j = 0; j < 3; ++j) {
|
| 404 |
+
tmp[j] = Napi::String::New(env, result[i][j]);
|
| 405 |
+
}
|
| 406 |
+
res[i] = tmp;
|
| 407 |
+
}
|
| 408 |
+
|
| 409 |
+
return res;
|
| 410 |
+
}
|
| 411 |
+
|
| 412 |
+
|
| 413 |
+
Napi::Object Init(Napi::Env env, Napi::Object exports) {
|
| 414 |
+
exports.Set(
|
| 415 |
+
Napi::String::New(env, "whisper"),
|
| 416 |
+
Napi::Function::New(env, whisper)
|
| 417 |
+
);
|
| 418 |
+
return exports;
|
| 419 |
+
}
|
| 420 |
+
|
| 421 |
+
NODE_API_MODULE(whisper, Init);
|
examples/addon.node/index.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const path = require('path');
|
| 2 |
+
const { whisper } = require(path.join(__dirname, '../../build/Release/whisper-addon'));
|
| 3 |
+
|
| 4 |
+
const whisperParams = {
|
| 5 |
+
language: 'en',
|
| 6 |
+
model: path.join(__dirname, '../../models/ggml-base.en.bin'),
|
| 7 |
+
fname_inp: '',
|
| 8 |
+
};
|
| 9 |
+
|
| 10 |
+
const arguments = process.argv.slice(2);
|
| 11 |
+
const params = Object.fromEntries(
|
| 12 |
+
arguments.reduce((pre, item) => {
|
| 13 |
+
if (item.startsWith("--")) {
|
| 14 |
+
return [...pre, item.slice(2).split("=")];
|
| 15 |
+
}
|
| 16 |
+
return pre;
|
| 17 |
+
}, []),
|
| 18 |
+
);
|
| 19 |
+
|
| 20 |
+
for (const key in params) {
|
| 21 |
+
if (whisperParams.hasOwnProperty(key)) {
|
| 22 |
+
whisperParams[key] = params[key];
|
| 23 |
+
}
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
console.log('whisperParams =', whisperParams);
|
| 27 |
+
console.log(whisper(whisperParams));
|
examples/addon.node/package.json
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "whisper-addon",
|
| 3 |
+
"version": "0.0.0",
|
| 4 |
+
"description": "",
|
| 5 |
+
"main": "index.js",
|
| 6 |
+
"author": "Qanhe Chen",
|
| 7 |
+
"license": "MIT",
|
| 8 |
+
"devDependencies": {
|
| 9 |
+
"cmake-js": "^7.1.1",
|
| 10 |
+
"node-addon-api": "^5.0.0"
|
| 11 |
+
}
|
| 12 |
+
}
|