-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add WebAssembly + NodeJS example. (#311)
- Loading branch information
1 parent
185f5c7
commit 41ebcfb
Showing
19 changed files
with
629 additions
and
461 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,16 @@ | ||
name: nodejs | ||
|
||
on: | ||
push: | ||
branches: | ||
- master | ||
paths: | ||
- '.github/workflows/nodejs.yaml' | ||
- 'CMakeLists.txt' | ||
- 'cmake/**' | ||
- 'nodejs-examples/**' | ||
- 'sherpa-ncnn/csrc/*' | ||
pull_request: | ||
branches: | ||
- master | ||
paths: | ||
- '.github/workflows/nodejs.yaml' | ||
- 'CMakeLists.txt' | ||
- 'cmake/**' | ||
- 'nodejs-examples/**' | ||
- 'sherpa-ncnn/csrc/*' | ||
workflow_dispatch: | ||
|
||
schedule: | ||
# minute (0-59) | ||
# hour (0-23) | ||
# day of the month (1-31) | ||
# month (1-12) | ||
# day of the week (0-6) | ||
# nightly test at 22:50 UTC time every day | ||
- cron: "50 22 * * *" | ||
|
||
concurrency: | ||
group: nodejs-${{ github.ref }} | ||
|
@@ -33,63 +25,48 @@ jobs: | |
strategy: | ||
fail-fast: false | ||
matrix: | ||
os: [ubuntu-latest, macos-latest] | ||
os: [ubuntu-latest, macos-latest, windows-latest] | ||
python-version: ["3.8"] | ||
|
||
steps: | ||
- uses: actions/checkout@v2 | ||
- uses: actions/checkout@v4 | ||
with: | ||
fetch-depth: 0 | ||
|
||
- name: Setup Python ${{ matrix.python-version }} | ||
uses: actions/setup-python@v2 | ||
uses: actions/setup-python@v5 | ||
with: | ||
python-version: ${{ matrix.python-version }} | ||
|
||
- uses: actions/setup-node@v3 | ||
- uses: actions/setup-node@v4 | ||
with: | ||
node-version: 13 | ||
registry-url: 'https://registry.npmjs.org' | ||
|
||
- name: Display node version | ||
shell: bash | ||
run: | | ||
node --version | ||
npm --version | ||
cd nodejs-examples | ||
npm install [email protected] -g | ||
npm install [email protected] | ||
npm --version | ||
- name: Install npm packages | ||
shell: bash | ||
run: | | ||
cd nodejs-examples | ||
npm install ffi-napi ref-struct-napi wav | ||
npm install npm | ||
npm --version | ||
npm list | ||
- name: ccache | ||
uses: hendrikmuhs/[email protected] | ||
with: | ||
key: ${{ matrix.os }}-shared | ||
|
||
- name: Download model | ||
shell: bash | ||
run: | | ||
cd nodejs-examples | ||
GIT_LFS_SKIP_SMUDGE=1 git clone https://huggingface.co/csukuangfj/sherpa-ncnn-streaming-zipformer-bilingual-zh-en-2023-02-13 | ||
cd sherpa-ncnn-streaming-zipformer-bilingual-zh-en-2023-02-13 | ||
git lfs pull --include "*.bin" | ||
ls -lh | ||
curl -OL https://github.com/k2-fsa/sherpa-ncnn/releases/download/models/sherpa-ncnn-streaming-zipformer-bilingual-zh-en-2023-02-13.tar.bz2 | ||
tar xvf sherpa-ncnn-streaming-zipformer-bilingual-zh-en-2023-02-13.tar.bz2 | ||
rm sherpa-ncnn-streaming-zipformer-bilingual-zh-en-2023-02-13.tar.bz2 | ||
ls -lh sherpa-ncnn-streaming-zipformer-bilingual-zh-en-2023-02-13 | ||
- name: Test | ||
shell: bash | ||
run: | | ||
export CMAKE_CXX_COMPILER_LAUNCHER=ccache | ||
export PATH="/usr/lib/ccache:/usr/local/opt/ccache/libexec:$PATH" | ||
cmake --version | ||
cd nodejs-examples | ||
ls -lh | ||
./run.sh | ||
node ./decode-file.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
name: npm | ||
|
||
on: | ||
workflow_dispatch: | ||
|
||
concurrency: | ||
group: npm-${{ github.ref }} | ||
cancel-in-progress: true | ||
|
||
permissions: | ||
contents: read | ||
id-token: write | ||
|
||
jobs: | ||
nodejs: | ||
runs-on: ${{ matrix.os }} | ||
strategy: | ||
fail-fast: false | ||
matrix: | ||
os: [ubuntu-latest] | ||
python-version: ["3.8"] | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
with: | ||
fetch-depth: 0 | ||
|
||
- name: Install emsdk | ||
uses: mymindstorm/setup-emsdk@v14 | ||
|
||
- name: View emsdk version | ||
shell: bash | ||
run: | | ||
emcc -v | ||
echo "--------------------" | ||
emcc --check | ||
- name: Setup Python ${{ matrix.python-version }} | ||
uses: actions/setup-python@v5 | ||
with: | ||
python-version: ${{ matrix.python-version }} | ||
|
||
- uses: actions/setup-node@v4 | ||
with: | ||
registry-url: 'https://registry.npmjs.org' | ||
|
||
- name: Display node version | ||
shell: bash | ||
run: | | ||
node --version | ||
npm --version | ||
cd nodejs-examples | ||
npm install npm | ||
npm --version | ||
- name: Build nodejs package | ||
shell: bash | ||
env: | ||
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} | ||
run: | | ||
./build-wasm-simd-for-nodejs.sh | ||
cp -v build-wasm-simd-for-nodejs/install/bin/wasm/sherpa-ncnn-wasm-main.js ./scripts/nodejs | ||
cp -v build-wasm-simd-for-nodejs/install/bin/wasm/sherpa-ncnn-wasm-main.wasm ./scripts/nodejs | ||
cp -v build-wasm-simd-for-nodejs/install/bin/wasm/sherpa-ncnn.js ./scripts/nodejs | ||
SHERPA_NCNN_VERSION=$(grep "SHERPA_NCNN_VERSION" ./CMakeLists.txt | cut -d " " -f 2 | cut -d '"' -f 2) | ||
echo "SHERPA_NCNN_VERSION $SHERPA_NCNN_VERSION" | ||
cd scripts/nodejs | ||
owner=${{ github.repository_owner }} | ||
echo "owner: $owner" | ||
sed -i.bak s/SHERPA_NCNN_VERSION/$SHERPA_NCNN_VERSION/g ./package.json | ||
sed -i.bak s/k2-fsa/$owner/g ./package.json | ||
rm package.json.bak | ||
git diff . | ||
npm install | ||
npm ci | ||
# see https://docs.npmjs.com/generating-provenance-statements | ||
npm publish --provenance --access public |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -116,3 +116,4 @@ generate-int8-*.sh | |
cmake-build-release | ||
cmake-build-debug | ||
|
||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
#!/usr/bin/env bash | ||
# Copyright (c) 2024 Xiaomi Corporation | ||
# | ||
# This script is to build sherpa-ncnn for WebAssembly (NodeJS) | ||
# | ||
# See also | ||
# https://github.com/Tencent/ncnn/wiki/how-to-build#build-for-webassembly | ||
# | ||
# Please refer to | ||
# https://k2-fsa.github.io/sherpa/ncnn/wasm/index.html | ||
# for more details. | ||
|
||
set -ex | ||
|
||
if [ x"$EMSCRIPTEN" == x"" ]; then | ||
if ! command -v emcc &> /dev/null; then | ||
echo "Please install emscripten first" | ||
echo "" | ||
echo "You can use the following commands to install it:" | ||
echo "" | ||
echo "git clone https://github.com/emscripten-core/emsdk.git" | ||
echo "cd emsdk" | ||
echo "git pull" | ||
echo "./emsdk install latest" | ||
echo "./emsdk activate latest" | ||
echo "source ./emsdk_env.sh" | ||
exit 1 | ||
else | ||
EMSCRIPTEN=$(dirname $(realpath $(which emcc))) | ||
fi | ||
fi | ||
|
||
export EMSCRIPTEN=$EMSCRIPTEN | ||
echo "EMSCRIPTEN: $EMSCRIPTEN" | ||
if [ ! -f $EMSCRIPTEN/cmake/Modules/Platform/Emscripten.cmake ]; then | ||
echo "Cannot find $EMSCRIPTEN/cmake/Modules/Platform/Emscripten.cmake" | ||
echo "Please make sure you have installed emsdk correctly" | ||
exit 1 | ||
fi | ||
|
||
mkdir -p build-wasm-simd-for-nodejs | ||
pushd build-wasm-simd-for-nodejs | ||
|
||
export SHERPA_NCNN_IS_USING_BUILD_WASM_SH=ON | ||
|
||
cmake \ | ||
-DCMAKE_INSTALL_PREFIX=./install \ | ||
-DCMAKE_BUILD_TYPE=Release \ | ||
-DCMAKE_TOOLCHAIN_FILE=$EMSCRIPTEN/cmake/Modules/Platform/Emscripten.cmake \ | ||
-DNCNN_THREADS=OFF \ | ||
-DNCNN_OPENMP=OFF \ | ||
-DNCNN_SIMPLEOMP=OFF \ | ||
-DNCNN_RUNTIME_CPU=OFF \ | ||
-DNCNN_SSE2=ON \ | ||
-DNCNN_AVX2=OFF \ | ||
-DNCNN_AVX=OFF \ | ||
-DNCNN_BUILD_TOOLS=OFF \ | ||
-DNCNN_BUILD_EXAMPLES=OFF \ | ||
-DNCNN_BUILD_BENCHMARK=OFF \ | ||
\ | ||
-DSHERPA_NCNN_ENABLE_WASM=ON \ | ||
-DSHERPA_NCNN_ENABLE_WASM_FOR_NODEJS=ON \ | ||
-DBUILD_SHARED_LIBS=OFF \ | ||
-DSHERPA_NCNN_ENABLE_PYTHON=OFF \ | ||
-DSHERPA_NCNN_ENABLE_PORTAUDIO=OFF \ | ||
-DSHERPA_NCNN_ENABLE_JNI=OFF \ | ||
-DSHERPA_NCNN_ENABLE_BINARY=OFF \ | ||
-DSHERPA_NCNN_ENABLE_TEST=OFF \ | ||
-DSHERPA_NCNN_ENABLE_C_API=ON \ | ||
-DSHERPA_NCNN_ENABLE_GENERATE_INT8_SCALE_TABLE=OFF \ | ||
-DSHERPA_NCNN_ENABLE_FFMPEG_EXAMPLES=OFF \ | ||
.. | ||
|
||
make -j2 | ||
make install | ||
ls -lh install/bin/wasm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1 @@ | ||
install | ||
node_modules | ||
package.json | ||
package-lock.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# Introduction | ||
|
||
This folder contains examples about how to use the sherpa-ncnn WebAssembly module | ||
with nodejs for speech recognition. | ||
|
||
- [decode-file.js](./decode-file.js) it shows how to decode a file | ||
- [real-time-speech-recognition-microphone.js](./real-time-speech-recognition-microphone.js) it shows | ||
how to do real-time speech recognition with a microphone | ||
|
||
## Usage | ||
|
||
### Install dependencies | ||
|
||
```bash | ||
cd ./nodejs-examples | ||
npm i | ||
``` | ||
|
||
### Download a model | ||
|
||
Please visit <https://github.com/k2-fsa/sherpa-ncnn/releases/tag/models> to | ||
select more models. | ||
|
||
The following is an example: | ||
|
||
```bash | ||
cd ./nodejs-examples | ||
wget https://github.com/k2-fsa/sherpa-ncnn/releases/download/models/sherpa-ncnn-streaming-zipformer-bilingual-zh-en-2023-02-13.tar.bz2 | ||
tar xvf sherpa-ncnn-streaming-zipformer-bilingual-zh-en-2023-02-13.tar.bz2 | ||
rm sherpa-ncnn-streaming-zipformer-bilingual-zh-en-2023-02-13.tar.bz2 | ||
``` | ||
|
||
### Decode a file | ||
|
||
```bash | ||
cd ./nodejs-examples | ||
|
||
node ./decode-file.js | ||
``` | ||
|
||
### Real-time speech recognition from a microphone | ||
|
||
```bash | ||
cd ./nodejs-examples | ||
|
||
node ./real-time-speech-recognition-microphone.js | ||
``` |
Oops, something went wrong.