Skip to content

Commit 5a33c04

Browse files
chenqianherock3125
authored andcommitted
addon.node : using whisper as a Node.js addon (ggml-org#443)
* 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
1 parent 43d13da commit 5a33c04

File tree

7 files changed

+528
-0
lines changed

7 files changed

+528
-0
lines changed

examples/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ if (EMSCRIPTEN)
2424
add_subdirectory(command.wasm)
2525
add_subdirectory(talk.wasm)
2626
add_subdirectory(bench.wasm)
27+
elseif(CMAKE_JS_VERSION)
28+
add_subdirectory(addon.node)
2729
else()
2830
add_subdirectory(main)
2931
add_subdirectory(stream)

examples/addon.node/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.idea
2+
node_modules
3+
build

examples/addon.node/CMakeLists.txt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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.

0 commit comments

Comments
 (0)