From ab1916fc598cc364b521a6d24752c4b092553e40 Mon Sep 17 00:00:00 2001 From: Qianhe Chen <54462604+chenqianhe@users.noreply.github.com> Date: Sun, 5 Feb 2023 21:02:08 +0800 Subject: [PATCH] ci : add node addon test and optimize compilation configuration (#468) * 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 * addon: reuse jfk.wav --------- Co-authored-by: Georgi Gerganov --- .github/workflows/examples.yml | 48 ++++++++++++++++++++ CMakeLists.txt | 2 +- examples/addon.node/CMakeLists.txt | 5 ++ examples/addon.node/README.md | 4 +- examples/addon.node/__test__/whisper.spec.js | 15 ++++++ examples/addon.node/addon.cpp | 5 +- examples/addon.node/package.json | 4 ++ 7 files changed, 78 insertions(+), 5 deletions(-) create mode 100644 .github/workflows/examples.yml create mode 100644 examples/addon.node/__test__/whisper.spec.js diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml new file mode 100644 index 00000000..ddaf5e9d --- /dev/null +++ b/.github/workflows/examples.yml @@ -0,0 +1,48 @@ +name: Examples Tests +on: + push: + paths: + - examples/addon.node/** + - whisper.h + pull_request: + paths: + - examples/addon.node/** + - whisper.h + +jobs: + addon_node-ubuntu-latest: + runs-on: ubuntu-latest + strategy: + matrix: + node-version: [ 16.x, 18.x ] + steps: + - name: Clone + uses: actions/checkout@v1 + + - name: Dependencies + run: | + sudo apt-get update + sudo apt-get install build-essential + sudo apt-get install cmake + sudo apt-get install libsdl2-dev + + - name: Use Node.js ${{ matrix.node-version }} + uses: actions/setup-node@v1 + with: + node-version: ${{ matrix.node-version }} + cache: 'npm' + + - name: Install package.json dependencies + working-directory: ./examples/addon.node + run: npm install + + - name: Compile addon.node + run: npx cmake-js compile -T whisper-addon -B Release + + - name: Download test model + run: | + bash ./models/download-ggml-model.sh base.en + - name: Test + run: | + cd examples/addon.node + npm run test diff --git a/CMakeLists.txt b/CMakeLists.txt index 5cb32fb0..d41252fd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -242,7 +242,7 @@ add_subdirectory(bindings) # programs, examples and tests # -if (WHISPER_BUILD_TESTS) +if (WHISPER_BUILD_TESTS AND NOT CMAKE_JS_VERSION) enable_testing() add_subdirectory(tests) endif () diff --git a/examples/addon.node/CMakeLists.txt b/examples/addon.node/CMakeLists.txt index 142d5eda..50c8faed 100644 --- a/examples/addon.node/CMakeLists.txt +++ b/examples/addon.node/CMakeLists.txt @@ -24,3 +24,8 @@ target_include_directories(${TARGET} PRIVATE ${NODE_ADDON_API_DIR}) #================================================================== target_link_libraries(${TARGET} ${CMAKE_JS_LIB} whisper ${CMAKE_THREAD_LIBS_INIT}) + +if(MSVC AND CMAKE_JS_NODELIB_DEF AND CMAKE_JS_NODELIB_TARGET) + # Generate node.lib + execute_process(COMMAND ${CMAKE_AR} /def:${CMAKE_JS_NODELIB_DEF} /out:${CMAKE_JS_NODELIB_TARGET} ${CMAKE_STATIC_LINKER_FLAGS}) +endif() diff --git a/examples/addon.node/README.md b/examples/addon.node/README.md index d14dde41..bdb1d256 100644 --- a/examples/addon.node/README.md +++ b/examples/addon.node/README.md @@ -14,14 +14,14 @@ npm install Make sure it is in the project root directory and compiled with make-js. ```shell -npx cmake-js compile -T whisper-addon +npx cmake-js compile -T whisper-addon -B Release ``` 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. > Such as appointing special cmake path: > ```shell -> npx cmake-js compile -c 'xxx/cmake' -T whisper-addon +> npx cmake-js compile -c 'xxx/cmake' -T whisper-addon -B Release > ``` ## Run diff --git a/examples/addon.node/__test__/whisper.spec.js b/examples/addon.node/__test__/whisper.spec.js new file mode 100644 index 00000000..b7d35406 --- /dev/null +++ b/examples/addon.node/__test__/whisper.spec.js @@ -0,0 +1,15 @@ +const path = require('path'); +const { whisper } = require(path.join(__dirname, '../../../build/Release/whisper-addon')); + +const whisperParamsMock = { + language: 'en', + model: path.join(__dirname, '../../../models/ggml-base.en.bin'), + fname_inp: path.join(__dirname, '../../../samples/jfk.wav'), +}; + +describe("Run whisper.node", () => { + + test("it should receive a non-empty value", () => { + expect(whisper(whisperParamsMock).length).toBeGreaterThan(0); + }); +}); diff --git a/examples/addon.node/addon.cpp b/examples/addon.node/addon.cpp index 053fb3a9..57c35148 100644 --- a/examples/addon.node/addon.cpp +++ b/examples/addon.node/addon.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -398,9 +399,9 @@ Napi::Object whisper(const Napi::CallbackInfo& info) { } Napi::Object res = Napi::Array::New(env, result.size()); - for (u_int32_t i = 0; i < result.size(); ++i) { + for (uint64_t i = 0; i < result.size(); ++i) { Napi::Object tmp = Napi::Array::New(env, 3); - for (u_int32_t j = 0; j < 3; ++j) { + for (uint64_t j = 0; j < 3; ++j) { tmp[j] = Napi::String::New(env, result[i][j]); } res[i] = tmp; diff --git a/examples/addon.node/package.json b/examples/addon.node/package.json index eaad69ce..bf51f0bb 100644 --- a/examples/addon.node/package.json +++ b/examples/addon.node/package.json @@ -5,8 +5,12 @@ "main": "index.js", "author": "Qanhe Chen", "license": "MIT", + "scripts": { + "test": "jest" + }, "devDependencies": { "cmake-js": "^7.1.1", + "jest": "^29.4.0", "node-addon-api": "^5.0.0" } }