Skip to content

Commit

Permalink
Publish node-addon-api wrapper for sherpa-onnx as npm packages (#829)
Browse files Browse the repository at this point in the history
  • Loading branch information
csukuangfj authored May 4, 2024
1 parent 2f9553d commit 4f758e6
Show file tree
Hide file tree
Showing 27 changed files with 773 additions and 15 deletions.
3 changes: 3 additions & 0 deletions .github/scripts/node-addon/README-optional.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Introduction

Please see [sherpa-onnx-node](https://www.npmjs.com/package/sherpa-onnx-node)
11 changes: 11 additions & 0 deletions .github/scripts/node-addon/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Introduction

Please see
https://github.com/k2-fsa/sherpa-onnx/blob/master/nodejs-addon-examples/README.md
for usages.


||Method|Support multiple threads|Minimum required node version|
|---|---|---|---|
|this package| https://github.com/nodejs/node-addon-api | Yes | v10|
|https://www.npmjs.com/package/sherpa-onnx| WebAssembly | No | v18|
1 change: 1 addition & 0 deletions .github/scripts/node-addon/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./sherpa-onnx.node');
8 changes: 8 additions & 0 deletions .github/scripts/node-addon/notes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Introduction

See also

- https://github.com/WonderInventions/node-webrtc/blob/develop/package.json
- https://stackoverflow.com/questions/15176082/npm-package-json-os-specific-dependency
- https://github.com/WonderInventions/node-webrtc/blob/develop/lib/binding.js
- cross-compiling https://github.com/nodejs/node-gyp/issues/829#issuecomment-665527032
53 changes: 53 additions & 0 deletions .github/scripts/node-addon/package-optional.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"name": "sherpa-onnx-PLATFORM-ARCH",
"version": "SHERPA_ONNX_VERSION",
"description": "Speech-to-text and text-to-speech using Next-gen Kaldi without internet connection",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/k2-fsa/sherpa-onnx.git"
},
"keywords": [
"speech to text",
"text to speech",
"transcription",
"real-time speech recognition",
"without internet connection",
"embedded systems",
"open source",
"zipformer",
"asr",
"tts",
"stt",
"c++",
"onnxruntime",
"onnx",
"ai",
"next-gen kaldi",
"offline",
"privacy",
"open source",
"vad",
"speaker id",
"language id",
"node-addon-api",
"streaming speech recognition",
"speech",
"recognition"
],
"author": "The next-gen Kaldi team",
"license": "Apache-2.0",
"bugs": {
"url": "https://github.com/k2-fsa/sherpa-onnx/issues"
},
"homepage": "https://github.com/k2-fsa/sherpa-onnx#readme",
"os": [
"PLATFORM"
],
"cpu": [
"ARCH"
]
}
52 changes: 52 additions & 0 deletions .github/scripts/node-addon/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"name": "sherpa-onnx-node",
"version": "SHERPA_ONNX_VERSION",
"description": "Speech-to-text and text-to-speech using Next-gen Kaldi without internet connection",
"main": "sherpa-onnx.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/k2-fsa/sherpa-onnx.git"
},
"keywords": [
"speech to text",
"text to speech",
"transcription",
"real-time speech recognition",
"without internet connection",
"embedded systems",
"open source",
"zipformer",
"asr",
"tts",
"stt",
"c++",
"onnxruntime",
"onnx",
"ai",
"next-gen kaldi",
"offline",
"privacy",
"open source",
"vad",
"speaker id",
"language id",
"node-addon-api",
"streaming speech recognition",
"speech",
"recognition"
],
"author": "The next-gen Kaldi team",
"license": "Apache-2.0",
"bugs": {
"url": "https://github.com/k2-fsa/sherpa-onnx/issues"
},
"homepage": "https://github.com/k2-fsa/sherpa-onnx#readme",
"optionalDependencies": {
"sherpa-onnx-darwin-arm64": "^SHERPA_ONNX_VERSION",
"sherpa-onnx-darwin-x64": "^SHERPA_ONNX_VERSION",
"sherpa-onnx-linux-x64": "^SHERPA_ONNX_VERSION"
}
}
37 changes: 37 additions & 0 deletions .github/scripts/node-addon/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env bash

sherpa_onnx_dir=$PWD
echo "sherpa_onnx_dir: $sherpa_onnx_dir"

src_dir=$sherpa_onnx_dir/.github/scripts/node-addon

platform=$(node -p "require('os').platform()")
arch=$(node -p "require('os').arch()")

SHERPA_ONNX_VERSION=$(grep "SHERPA_ONNX_VERSION" ./CMakeLists.txt | cut -d " " -f 2 | cut -d '"' -f 2)
echo "SHERPA_ONNX_VERSION $SHERPA_ONNX_VERSION"

if [ -z $owner ]; then
owner=k2-fsa
fi

sed -i.bak s/SHERPA_ONNX_VERSION/$SHERPA_ONNX_VERSION/g $src_dir/package-optional.json
sed -i.bak s/k2-fsa/$owner/g $src_dir/package-optional.json
sed -i.bak s/PLATFORM/$platform/g $src_dir/package-optional.json
sed -i.bak s/ARCH/$arch/g $src_dir/package-optional.json

git diff $src_dir/package-optional.json

dst=$sherpa_onnx_dir/sherpa-onnx-node
mkdir -p $dst

cp $src_dir/package-optional.json $dst/package.json
cp $src_dir/index.js $dst/
cp $src_dir/README-optional.md $dst/README.md

cp -fv build/install/lib/lib* $dst/ || true
cp -fv build/install/lib/*dll $dst/ || true

cp scripts/node-addon-api/build/Release/sherpa-onnx.node $dst/

ls $dst
23 changes: 23 additions & 0 deletions .github/scripts/test-nodejs-addon-npm.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env bash

set -ex

d=nodejs-addon-examples
echo "dir: $d"
cd $d
npm install --verbose
git status
ls -lh
ls -lh node_modules

export DYLD_LIBRARY_PATH=$PWD/node_modules/sherpa-onnx-darwin-x64:$DYLD_LIBRARY_PATH
export DYLD_LIBRARY_PATH=$PWD/node_modules/sherpa-onnx-darwin-arm64:$DYLD_LIBRARY_PATH
export LD_LIBRARY_PATH=$PWD/node_modules/sherpa-onnx-linux-x64:$LD_LIBRARY_PATH

curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20.tar.bz2
tar xvf sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20.tar.bz2
rm sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20.tar.bz2

node test_asr_streaming_transducer.js

rm -rf sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20
122 changes: 122 additions & 0 deletions .github/workflows/npm-addon-linux-x64.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
name: npm-addon-linux-x64

on:
workflow_dispatch:

concurrency:
group: npm-addon-linux-x64-${{ github.ref }}
cancel-in-progress: true

permissions:
contents: read
id-token: write

jobs:
npm-addon-linux-x64:
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: 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
- name: Build sherpa-onnx
uses: addnab/docker-run-action@v3
with:
image: quay.io/pypa/manylinux2014_x86_64
options: |
--volume ${{ github.workspace }}/:/shared/
shell: bash
run: |
uname -a
gcc --version
cmake --version
cd /shared
mkdir build
cd build
cmake \
-DCMAKE_INSTALL_PREFIX=./install \
-DBUILD_SHARED_LIBS=ON \
-DSHERPA_ONNX_ENABLE_WEBSOCKET=OFF \
-DSHERPA_ONNX_ENABLE_PORTAUDIO=OFF \
-DSHERPA_ONNX_ENABLE_BINARY=OFF \
..
make -j install
- name: Build sherpa-onnx node-addon
shell: bash
run: |
export PKG_CONFIG_PATH=$PWD/build/install:$PKG_CONFIG_PATH
sudo mkdir /shared
sudo ln -s $PWD/build /shared/
ls -lh /shared/build
pkg-config --cflags sherpa-onnx
pkg-config --libs sherpa-onnx
cd scripts/node-addon-api/
npm i
./node_modules/.bin/node-gyp configure build --verbose
- name: Prepare for publish
shell: bash
run: |
export PKG_CONFIG_PATH=$PWD/build/install:$PKG_CONFIG_PATH
owner=${{ github.repository_owner }}
export owner
echo "---"
ls -lh build/install/lib/
sudo chown -R runner ./build
echo "---"
ls -lh build/install/lib/
echo "---"
# find build/install/lib/ -maxdepth 1 -type l
# find build/install/lib/ -maxdepth 1 -type l -delete
#
# echo "---"
# ls -lh build/install/lib/
.github/scripts/node-addon/run.sh
- name: Display files to be published
shell: bash
run: |
ls -lh ./sherpa-onnx-node
- name: Publish
shell: bash
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
run: |
cd ./sherpa-onnx-node
npm install
npm ci
# see https://docs.npmjs.com/generating-provenance-statements
npm publish --provenance --access public
Loading

0 comments on commit 4f758e6

Please sign in to comment.