Qianhe Chen ggerganov commited on
Commit
398871a
·
unverified ·
1 Parent(s): eb55c82

ci : add node addon test and optimize compilation configuration (#468)

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

* ci: add addon.node test ci

* addon: remove build WHISPER_BUILD_TESTS

* addon: update build command

* addon: add test

* addon: add test file

* addon: adapt to compile on Windows

* addon: fix typo

* addon: reuse jfk.wav

Co-authored-by: Georgi Gerganov <[email protected]>

* addon: reuse jfk.wav

---------

Co-authored-by: Georgi Gerganov <[email protected]>

.github/workflows/examples.yml ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ name: Examples Tests
2
+ on:
3
+ push:
4
+ paths:
5
+ - examples/addon.node/**
6
+ - whisper.h
7
+ pull_request:
8
+ paths:
9
+ - examples/addon.node/**
10
+ - whisper.h
11
+
12
+ jobs:
13
+ addon_node-ubuntu-latest:
14
+ runs-on: ubuntu-latest
15
+ strategy:
16
+ matrix:
17
+ node-version: [ 16.x, 18.x ]
18
+ steps:
19
+ - name: Clone
20
+ uses: actions/checkout@v1
21
+
22
+ - name: Dependencies
23
+ run: |
24
+ sudo apt-get update
25
+ sudo apt-get install build-essential
26
+ sudo apt-get install cmake
27
+ sudo apt-get install libsdl2-dev
28
+
29
+ - name: Use Node.js ${{ matrix.node-version }}
30
+ uses: actions/setup-node@v1
31
+ with:
32
+ node-version: ${{ matrix.node-version }}
33
+ cache: 'npm'
34
+
35
+ - name: Install package.json dependencies
36
+ working-directory: ./examples/addon.node
37
+ run: npm install
38
+
39
+ - name: Compile addon.node
40
+ run: npx cmake-js compile -T whisper-addon -B Release
41
+
42
+ - name: Download test model
43
+ run: |
44
+ bash ./models/download-ggml-model.sh base.en
45
+ - name: Test
46
+ run: |
47
+ cd examples/addon.node
48
+ npm run test
CMakeLists.txt CHANGED
@@ -242,7 +242,7 @@ add_subdirectory(bindings)
242
  # programs, examples and tests
243
  #
244
 
245
- if (WHISPER_BUILD_TESTS)
246
  enable_testing()
247
  add_subdirectory(tests)
248
  endif ()
 
242
  # programs, examples and tests
243
  #
244
 
245
+ if (WHISPER_BUILD_TESTS AND NOT CMAKE_JS_VERSION)
246
  enable_testing()
247
  add_subdirectory(tests)
248
  endif ()
examples/addon.node/CMakeLists.txt CHANGED
@@ -24,3 +24,8 @@ target_include_directories(${TARGET} PRIVATE ${NODE_ADDON_API_DIR})
24
  #==================================================================
25
 
26
  target_link_libraries(${TARGET} ${CMAKE_JS_LIB} whisper ${CMAKE_THREAD_LIBS_INIT})
 
 
 
 
 
 
24
  #==================================================================
25
 
26
  target_link_libraries(${TARGET} ${CMAKE_JS_LIB} whisper ${CMAKE_THREAD_LIBS_INIT})
27
+
28
+ if(MSVC AND CMAKE_JS_NODELIB_DEF AND CMAKE_JS_NODELIB_TARGET)
29
+ # Generate node.lib
30
+ execute_process(COMMAND ${CMAKE_AR} /def:${CMAKE_JS_NODELIB_DEF} /out:${CMAKE_JS_NODELIB_TARGET} ${CMAKE_STATIC_LINKER_FLAGS})
31
+ endif()
examples/addon.node/README.md CHANGED
@@ -14,14 +14,14 @@ npm install
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
 
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 -B Release
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 -B Release
25
  > ```
26
 
27
  ## Run
examples/addon.node/__test__/whisper.spec.js ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const path = require('path');
2
+ const { whisper } = require(path.join(__dirname, '../../../build/Release/whisper-addon'));
3
+
4
+ const whisperParamsMock = {
5
+ language: 'en',
6
+ model: path.join(__dirname, '../../../models/ggml-base.en.bin'),
7
+ fname_inp: path.join(__dirname, '../../../samples/jfk.wav'),
8
+ };
9
+
10
+ describe("Run whisper.node", () => {
11
+
12
+ test("it should receive a non-empty value", () => {
13
+ expect(whisper(whisperParamsMock).length).toBeGreaterThan(0);
14
+ });
15
+ });
examples/addon.node/addon.cpp CHANGED
@@ -1,3 +1,4 @@
 
1
  #include <string>
2
  #include <thread>
3
  #include <vector>
@@ -398,9 +399,9 @@ Napi::Object whisper(const Napi::CallbackInfo& info) {
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;
 
1
+ #include <cstdint>
2
  #include <string>
3
  #include <thread>
4
  #include <vector>
 
399
  }
400
 
401
  Napi::Object res = Napi::Array::New(env, result.size());
402
+ for (uint64_t i = 0; i < result.size(); ++i) {
403
  Napi::Object tmp = Napi::Array::New(env, 3);
404
+ for (uint64_t j = 0; j < 3; ++j) {
405
  tmp[j] = Napi::String::New(env, result[i][j]);
406
  }
407
  res[i] = tmp;
examples/addon.node/package.json CHANGED
@@ -5,8 +5,12 @@
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
  }
 
5
  "main": "index.js",
6
  "author": "Qanhe Chen",
7
  "license": "MIT",
8
+ "scripts": {
9
+ "test": "jest"
10
+ },
11
  "devDependencies": {
12
  "cmake-js": "^7.1.1",
13
+ "jest": "^29.4.0",
14
  "node-addon-api": "^5.0.0"
15
  }
16
  }