Fangjun Kuang
Committed by GitHub

Publish pre-built jni libs for windows and osx (#1056)

@@ -3,7 +3,7 @@ name: linux-jni @@ -3,7 +3,7 @@ name: linux-jni
3 on: 3 on:
4 push: 4 push:
5 branches: 5 branches:
6 - - linux-jni 6 + - jni
7 tags: 7 tags:
8 - 'v[0-9]+.[0-9]+.[0-9]+*' 8 - 'v[0-9]+.[0-9]+.[0-9]+*'
9 workflow_dispatch: 9 workflow_dispatch:
@@ -33,6 +33,30 @@ jobs: @@ -33,6 +33,30 @@ jobs:
33 ls -lh 33 ls -lh
34 du -h -d1 . 34 du -h -d1 .
35 35
  36 + - name: Build jar
  37 + shell: bash
  38 + run: |
  39 + SHERPA_ONNX_VERSION=v$(grep "SHERPA_ONNX_VERSION" ./CMakeLists.txt | cut -d " " -f 2 | cut -d '"' -f 2)
  40 + cd sherpa-onnx/java-api
  41 + make
  42 + ls -lh build/
  43 + cp build/sherpa-onnx.jar ../../sherpa-onnx-$SHERPA_ONNX_VERSION.jar
  44 + cd ../..
  45 + ls -lh *.jar
  46 +
  47 + - uses: actions/upload-artifact@v4
  48 + with:
  49 + name: release-jni-linux-jar
  50 + path: ./*.jar
  51 +
  52 + - name: Release jar
  53 + if: (github.repository_owner == 'csukuangfj' || github.repository_owner == 'k2-fsa') && github.event_name == 'push' && contains(github.ref, 'refs/tags/')
  54 + uses: svenstaro/upload-release-action@v2
  55 + with:
  56 + file_glob: true
  57 + overwrite: true
  58 + file: ./*.jar
  59 +
36 - name: Build sherpa-onnx 60 - name: Build sherpa-onnx
37 uses: addnab/docker-run-action@v3 61 uses: addnab/docker-run-action@v3
38 with: 62 with:
@@ -78,6 +102,8 @@ jobs: @@ -78,6 +102,8 @@ jobs:
78 102
79 ls -lh lib 103 ls -lh lib
80 ls -lh bin 104 ls -lh bin
  105 + rm -rf ./install/pkgconfig
  106 + rm -rf ./install/share
81 107
82 - name: Display dependencies of sherpa-onnx for linux 108 - name: Display dependencies of sherpa-onnx for linux
83 shell: bash 109 shell: bash
@@ -99,9 +125,8 @@ jobs: @@ -99,9 +125,8 @@ jobs:
99 125
100 - uses: actions/upload-artifact@v4 126 - uses: actions/upload-artifact@v4
101 with: 127 with:
102 - name: release-jni  
103 - path: install/*  
104 - 128 + name: release-jni-linux
  129 + path: build/install/*
105 130
106 - name: Copy files 131 - name: Copy files
107 shell: bash 132 shell: bash
@@ -142,6 +167,7 @@ jobs: @@ -142,6 +167,7 @@ jobs:
142 mkdir -p jni 167 mkdir -p jni
143 168
144 cp -v ../sherpa-onnx-*.tar.bz2 ./jni 169 cp -v ../sherpa-onnx-*.tar.bz2 ./jni
  170 + cp -v ../*.jar ./jni
145 171
146 git status 172 git status
147 git lfs track "*.bz2" 173 git lfs track "*.bz2"
@@ -159,3 +185,4 @@ jobs: @@ -159,3 +185,4 @@ jobs:
159 file_glob: true 185 file_glob: true
160 overwrite: true 186 overwrite: true
161 file: sherpa-onnx-*.tar.bz2 187 file: sherpa-onnx-*.tar.bz2
  188 +
  1 +name: macos-jni
  2 +
  3 +on:
  4 + push:
  5 + branches:
  6 + - jni
  7 + tags:
  8 + - 'v[0-9]+.[0-9]+.[0-9]+*'
  9 +
  10 + workflow_dispatch:
  11 +
  12 +concurrency:
  13 + group: macos-jni-${{ github.ref }}
  14 + cancel-in-progress: true
  15 +
  16 +jobs:
  17 + macos_jni:
  18 + runs-on: ${{ matrix.os }}
  19 + name: ${{ matrix.arch }}
  20 + strategy:
  21 + fail-fast: false
  22 + matrix:
  23 + os: [macos-latest]
  24 + arch: [arm64, x86_64]
  25 +
  26 + steps:
  27 + - uses: actions/checkout@v4
  28 + with:
  29 + fetch-depth: 0
  30 +
  31 + - uses: actions/setup-java@v4
  32 + with:
  33 + distribution: 'temurin' # See 'Supported distributions' for available options
  34 + java-version: '21'
  35 +
  36 + - name: ccache
  37 + uses: hendrikmuhs/ccache-action@v1.2
  38 + with:
  39 + key: ${{ matrix.os }}-${{ matrix.arch }}
  40 +
  41 + - name: Configure CMake
  42 + shell: bash
  43 + run: |
  44 + export CMAKE_CXX_COMPILER_LAUNCHER=ccache
  45 + export PATH="/usr/lib/ccache:/usr/local/opt/ccache/libexec:$PATH"
  46 + cmake --version
  47 +
  48 + mkdir build
  49 + cd build
  50 + arch=${{ matrix.arch }}
  51 +
  52 + cmake \
  53 + -D BUILD_SHARED_LIBS=ON \
  54 + -D CMAKE_BUILD_TYPE=Release \
  55 + -DCMAKE_OSX_ARCHITECTURES=$arch \
  56 + -DSHERPA_ONNX_ENABLE_JNI=ON \
  57 + -DCMAKE_INSTALL_PREFIX=./install \
  58 + ..
  59 +
  60 + - name: Build sherpa-onnx for macos
  61 + shell: bash
  62 + run: |
  63 + export PATH="/usr/lib/ccache:/usr/local/opt/ccache/libexec:$PATH"
  64 +
  65 + cd build
  66 + make -j2
  67 + make install
  68 +
  69 + ls -lh lib
  70 + ls -lh bin
  71 +
  72 + file ./bin/sherpa-onnx
  73 +
  74 + rm -rf ./install/pkgconfig
  75 + rm -rf ./install/share
  76 +
  77 + - uses: actions/upload-artifact@v4
  78 + with:
  79 + name: release-jni-macos-${{ matrix.arch }}
  80 + path: build/install/*
  81 +
  82 + - name: Copy files
  83 + shell: bash
  84 + run: |
  85 + du -h -d1 .
  86 + SHERPA_ONNX_VERSION=v$(grep "SHERPA_ONNX_VERSION" ./CMakeLists.txt | cut -d " " -f 2 | cut -d '"' -f 2)
  87 +
  88 + arch=${{ matrix.arch }}
  89 + dst=sherpa-onnx-${SHERPA_ONNX_VERSION}-osx-$arch-jni
  90 + mkdir -p $dst
  91 +
  92 + cp -a build/install/bin $dst/
  93 + cp -a build/install/lib $dst/
  94 + cp -a build/install/include $dst/
  95 +
  96 + brew install tree
  97 +
  98 + tree $dst
  99 +
  100 + tar cjvf ${dst}.tar.bz2 $dst
  101 + du -h -d1 .
  102 +
  103 + - name: Publish to huggingface
  104 + if: (github.repository_owner == 'csukuangfj' || github.repository_owner == 'k2-fsa') && (github.event_name == 'push' || github.event_name == 'workflow_dispatch')
  105 + env:
  106 + HF_TOKEN: ${{ secrets.HF_TOKEN }}
  107 + uses: nick-fields/retry@v3
  108 + with:
  109 + max_attempts: 20
  110 + timeout_seconds: 200
  111 + shell: bash
  112 + command: |
  113 + git config --global user.email "csukuangfj@gmail.com"
  114 + git config --global user.name "Fangjun Kuang"
  115 +
  116 + rm -rf huggingface
  117 + export GIT_CLONE_PROTECTION_ACTIVE=false
  118 + GIT_LFS_SKIP_SMUDGE=1 git clone https://huggingface.co/csukuangfj/sherpa-onnx-libs huggingface
  119 +
  120 + cd huggingface
  121 + mkdir -p jni
  122 +
  123 + cp -v ../sherpa-onnx-*.tar.bz2 ./jni
  124 +
  125 + git status
  126 + git lfs track "*.bz2"
  127 +
  128 + git add .
  129 +
  130 + git commit -m "add more files"
  131 +
  132 + git push https://csukuangfj:$HF_TOKEN@huggingface.co/csukuangfj/sherpa-onnx-libs main
  133 +
  134 + - name: Release pre-compiled binaries and libs for linux x64
  135 + if: (github.repository_owner == 'csukuangfj' || github.repository_owner == 'k2-fsa') && github.event_name == 'push' && contains(github.ref, 'refs/tags/')
  136 + uses: svenstaro/upload-release-action@v2
  137 + with:
  138 + file_glob: true
  139 + overwrite: true
  140 + file: sherpa-onnx-*.tar.bz2
  1 +name: windows-x64-jni
  2 +
  3 +on:
  4 + push:
  5 + branches:
  6 + - jni
  7 + tags:
  8 + - 'v[0-9]+.[0-9]+.[0-9]+*'
  9 +
  10 + workflow_dispatch:
  11 +
  12 +concurrency:
  13 + group: windows-x64-jni-${{ github.ref }}
  14 + cancel-in-progress: true
  15 +
  16 +jobs:
  17 + windows_x64_jni:
  18 + name: windows x64 jni
  19 + runs-on: ${{ matrix.os }}
  20 + strategy:
  21 + fail-fast: false
  22 + matrix:
  23 + os: [windows-latest]
  24 +
  25 + steps:
  26 + - uses: actions/checkout@v4
  27 + with:
  28 + fetch-depth: 0
  29 +
  30 + - uses: actions/setup-java@v4
  31 + with:
  32 + distribution: 'temurin' # See 'Supported distributions' for available options
  33 + java-version: '21'
  34 +
  35 + - name: Configure CMake
  36 + shell: bash
  37 + run: |
  38 + mkdir build
  39 + cd build
  40 + cmake -A x64 -D CMAKE_BUILD_TYPE=Release -D BUILD_SHARED_LIBS=ON -DSHERPA_ONNX_ENABLE_JNI=ON -DCMAKE_INSTALL_PREFIX=./install ..
  41 +
  42 + - name: Build sherpa-onnx for windows
  43 + shell: bash
  44 + run: |
  45 + cd build
  46 + cmake --build . --config Release -- -m:2
  47 + cmake --build . --config Release --target install -- -m:2
  48 +
  49 + ls -lh ./bin/Release/sherpa-onnx.exe
  50 + rm -rf install/share
  51 + rm -rf install/lib/pkgconfig
  52 +
  53 + - uses: actions/upload-artifact@v4
  54 + with:
  55 + name: release-jni-windows-x64
  56 + path: build/install/*
  57 +
  58 + - name: Copy files
  59 + shell: bash
  60 + run: |
  61 + SHERPA_ONNX_VERSION=v$(grep "SHERPA_ONNX_VERSION" ./CMakeLists.txt | cut -d " " -f 2 | cut -d '"' -f 2)
  62 +
  63 + dst=sherpa-onnx-${SHERPA_ONNX_VERSION}-win-x64-jni
  64 + mkdir -p $dst
  65 +
  66 + cp -a build/install/bin $dst/
  67 + cp -a build/install/lib $dst/
  68 + cp -a build/install/include $dst/
  69 +
  70 + tar cjvf ${dst}.tar.bz2 $dst
  71 +
  72 + # https://huggingface.co/docs/hub/spaces-github-actions
  73 + - name: Publish to huggingface
  74 + if: (github.repository_owner == 'csukuangfj' || github.repository_owner == 'k2-fsa') && (github.event_name == 'push' || github.event_name == 'workflow_dispatch')
  75 + env:
  76 + HF_TOKEN: ${{ secrets.HF_TOKEN }}
  77 + uses: nick-fields/retry@v3
  78 + with:
  79 + max_attempts: 20
  80 + timeout_seconds: 200
  81 + shell: bash
  82 + command: |
  83 + git config --global user.email "csukuangfj@gmail.com"
  84 + git config --global user.name "Fangjun Kuang"
  85 +
  86 + rm -rf huggingface
  87 + export GIT_CLONE_PROTECTION_ACTIVE=false
  88 + GIT_LFS_SKIP_SMUDGE=1 git clone https://huggingface.co/csukuangfj/sherpa-onnx-libs huggingface
  89 +
  90 + cd huggingface
  91 + mkdir -p jni
  92 +
  93 + cp -v ../sherpa-onnx-*.tar.bz2 ./jni
  94 +
  95 + git status
  96 + git lfs track "*.bz2"
  97 +
  98 + git add .
  99 +
  100 + git commit -m "upload sherpa-onnx-${SHERPA_ONNX_VERSION}"
  101 +
  102 + git push https://csukuangfj:$HF_TOKEN@huggingface.co/csukuangfj/sherpa-onnx-libs main
  103 +
  104 + - name: Release pre-compiled binaries and libs for Windows x64
  105 + if: (github.repository_owner == 'csukuangfj' || github.repository_owner == 'k2-fsa') && github.event_name == 'push' && contains(github.ref, 'refs/tags/')
  106 + uses: svenstaro/upload-release-action@v2
  107 + with:
  108 + file_glob: true
  109 + overwrite: true
  110 + file: sherpa-onnx-*.tar.bz2
@@ -187,8 +187,9 @@ Java_com_k2fsa_sherpa_onnx_OfflineTts_generateWithCallbackImpl( @@ -187,8 +187,9 @@ Java_com_k2fsa_sherpa_onnx_OfflineTts_generateWithCallbackImpl(
187 SHERPA_ONNX_LOGE("string is: %s", p_text); 187 SHERPA_ONNX_LOGE("string is: %s", p_text);
188 188
189 std::function<int32_t(const float *, int32_t, float)> callback_wrapper = 189 std::function<int32_t(const float *, int32_t, float)> callback_wrapper =
190 - [env, callback](const float *samples, int32_t n, float /*progress*/) {  
191 - jclass cls = env->GetObjectClass(callback); 190 + [env, callback](const float *samples, int32_t n,
  191 + float /*progress*/) -> int {
  192 + jclass cls = env->GetObjectClass(callback);
192 193
193 #if 0 194 #if 0
194 // this block is for debugging only 195 // this block is for debugging only
@@ -207,22 +208,20 @@ Java_com_k2fsa_sherpa_onnx_OfflineTts_generateWithCallbackImpl( @@ -207,22 +208,20 @@ Java_com_k2fsa_sherpa_onnx_OfflineTts_generateWithCallbackImpl(
207 env->ReleaseStringUTFChars(classString, className); 208 env->ReleaseStringUTFChars(classString, className);
208 #endif 209 #endif
209 210
210 - jmethodID mid =  
211 - env->GetMethodID(cls, "invoke", "([F)Ljava/lang/Integer;");  
212 - if (mid == nullptr) {  
213 - SHERPA_ONNX_LOGE("Failed to get the callback. Ignore it.");  
214 - return 1;  
215 - }  
216 -  
217 - jfloatArray samples_arr = env->NewFloatArray(n);  
218 - env->SetFloatArrayRegion(samples_arr, 0, n, samples);  
219 -  
220 - jobject should_continue =  
221 - env->CallObjectMethod(callback, mid, samples_arr);  
222 - jclass jklass = env->GetObjectClass(should_continue);  
223 - jmethodID int_value_mid = env->GetMethodID(jklass, "intValue", "()I");  
224 - return env->CallIntMethod(should_continue, int_value_mid);  
225 - }; 211 + jmethodID mid = env->GetMethodID(cls, "invoke", "([F)Ljava/lang/Integer;");
  212 + if (mid == nullptr) {
  213 + SHERPA_ONNX_LOGE("Failed to get the callback. Ignore it.");
  214 + return 1;
  215 + }
  216 +
  217 + jfloatArray samples_arr = env->NewFloatArray(n);
  218 + env->SetFloatArrayRegion(samples_arr, 0, n, samples);
  219 +
  220 + jobject should_continue = env->CallObjectMethod(callback, mid, samples_arr);
  221 + jclass jklass = env->GetObjectClass(should_continue);
  222 + jmethodID int_value_mid = env->GetMethodID(jklass, "intValue", "()I");
  223 + return env->CallIntMethod(should_continue, int_value_mid);
  224 + };
226 225
227 auto audio = reinterpret_cast<sherpa_onnx::OfflineTts *>(ptr)->Generate( 226 auto audio = reinterpret_cast<sherpa_onnx::OfflineTts *>(ptr)->Generate(
228 p_text, sid, speed, callback_wrapper); 227 p_text, sid, speed, callback_wrapper);