Skip to content

Commit

Permalink
Add Java API for non-streaming ASR (#807)
Browse files Browse the repository at this point in the history
  • Loading branch information
csukuangfj authored Apr 24, 2024
1 parent c3a2e8a commit 83cd533
Show file tree
Hide file tree
Showing 24 changed files with 834 additions and 27 deletions.
19 changes: 14 additions & 5 deletions .github/workflows/run-java-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,9 @@ jobs:
make -j4
ls -lh lib
- name: Run java test
- name: Run java test (Streaming ASR)
shell: bash
run: |
export CMAKE_CXX_COMPILER_LAUNCHER=ccache
export PATH="/usr/lib/ccache:/usr/local/opt/ccache/libexec:$PATH"
cmake --version
cd ./java-api-examples
./run-streaming-decode-file-ctc.sh
# Delete model files to save space
Expand All @@ -109,3 +105,16 @@ jobs:
./run-streaming-decode-file-transducer.sh
rm -rf sherpa-onnx-streaming-*
- name: Run java test (Non-Streaming ASR)
shell: bash
run: |
cd ./java-api-examples
./run-non-streaming-decode-file-paraformer.sh
rm -rf sherpa-onnx-paraformer-zh-*
./run-non-streaming-decode-file-transducer.sh
rm -rf sherpa-onnx-zipformer-*
./run-non-streaming-decode-file-whisper.sh
rm -rf sherpa-onnx-whisper-*
1 change: 1 addition & 0 deletions java-api-examples/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
lib
hs_err*
!run-streaming*.sh
!run-non-streaming*.sh
49 changes: 49 additions & 0 deletions java-api-examples/NonStreamingDecodeFileParaformer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2024 Xiaomi Corporation

// This file shows how to use an offline paraformer, i.e., non-streaming paraformer,
// to decode files.
import com.k2fsa.sherpa.onnx.*;

public class NonStreamingDecodeFileTransducer {
public static void main(String[] args) {
// please refer to
// https://k2-fsa.github.io/sherpa/onnx/pretrained_models/offline-paraformer/paraformer-models.html#csukuangfj-sherpa-onnx-paraformer-zh-2023-03-28-chinese-english
// to download model files
String model = "./sherpa-onnx-paraformer-zh-2023-03-28/model.int8.onnx";
String tokens = "./sherpa-onnx-paraformer-zh-2023-03-28/tokens.txt";

String waveFilename = "./sherpa-onnx-paraformer-zh-2023-03-28/test_wavs/3-sichuan.wav";

WaveReader reader = new WaveReader(waveFilename);

OfflineParaformerModelConfig paraformer =
OfflineParaformerModelConfig.builder().setModel(model).build();

OfflineModelConfig modelConfig =
OfflineModelConfig.builder()
.setParaformer(paraformer)
.setTokens(tokens)
.setNumThreads(1)
.setDebug(true)
.build();

OfflineRecognizerConfig config =
OfflineRecognizerConfig.builder()
.setOfflineModelConfig(modelConfig)
.setDecodingMethod("greedy_search")
.build();

OfflineRecognizer recognizer = new OfflineRecognizer(config);
OfflineStream stream = recognizer.createStream();
stream.acceptWaveform(reader.getSamples(), reader.getSampleRate());

recognizer.decode(stream);

String text = recognizer.getResult(stream).getText();

System.out.printf("filename:%s\nresult:%s\n", waveFilename, text);

stream.release();
recognizer.release();
}
}
57 changes: 57 additions & 0 deletions java-api-examples/NonStreamingDecodeFileTransducer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright 2024 Xiaomi Corporation

// This file shows how to use an offline transducer, i.e., non-streaming transducer,
// to decode files.
import com.k2fsa.sherpa.onnx.*;

public class NonStreamingDecodeFileTransducer {
public static void main(String[] args) {
// please refer to
// https://k2-fsa.github.io/sherpa/onnx/pretrained_models/offline-transducer/zipformer-transducer-models.html#sherpa-onnx-zipformer-gigaspeech-2023-12-12-english
// to download model files
String encoder =
"./sherpa-onnx-zipformer-gigaspeech-2023-12-12/encoder-epoch-30-avg-1.int8.onnx";
String decoder = "./sherpa-onnx-zipformer-gigaspeech-2023-12-12/decoder-epoch-30-avg-1.onnx";
String joiner = "./sherpa-onnx-zipformer-gigaspeech-2023-12-12/joiner-epoch-30-avg-1.onnx";
String tokens = "./sherpa-onnx-zipformer-gigaspeech-2023-12-12/tokens.txt";

String waveFilename =
"./sherpa-onnx-zipformer-gigaspeech-2023-12-12/test_wavs/1089-134686-0001.wav";

WaveReader reader = new WaveReader(waveFilename);

OfflineTransducerModelConfig transducer =
OfflineTransducerModelConfig.builder()
.setEncoder(encoder)
.setDecoder(decoder)
.setJoiner(joiner)
.build();

OfflineModelConfig modelConfig =
OfflineModelConfig.builder()
.setTransducer(transducer)
.setTokens(tokens)
.setNumThreads(1)
.setDebug(true)
.build();

OfflineRecognizerConfig config =
OfflineRecognizerConfig.builder()
.setOfflineModelConfig(modelConfig)
.setDecodingMethod("greedy_search")
.build();

OfflineRecognizer recognizer = new OfflineRecognizer(config);
OfflineStream stream = recognizer.createStream();
stream.acceptWaveform(reader.getSamples(), reader.getSampleRate());

recognizer.decode(stream);

String text = recognizer.getResult(stream).getText();

System.out.printf("filename:%s\nresult:%s\n", waveFilename, text);

stream.release();
recognizer.release();
}
}
50 changes: 50 additions & 0 deletions java-api-examples/NonStreamingDecodeFileWhisper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright 2024 Xiaomi Corporation

// This file shows how to use an offline whisper, i.e., non-streaming whisper,
// to decode files.
import com.k2fsa.sherpa.onnx.*;

public class NonStreamingDecodeFileWhisper {
public static void main(String[] args) {
// please refer to
// https://k2-fsa.github.io/sherpa/onnx/pretrained_models/whisper/tiny.en.html
// to download model files
String encoder = "./sherpa-onnx-whisper-tiny.en/tiny.en-encoder.int8.onnx";
String decoder = "./sherpa-onnx-whisper-tiny.en/tiny.en-decoder.int8.onnx";
String tokens = "./sherpa-onnx-whisper-tiny.en/tiny.en-tokens.txt";

String waveFilename = "./sherpa-onnx-whisper-tiny.en/test_wavs/1.wav";

WaveReader reader = new WaveReader(waveFilename);

OfflineWhisperModelConfig whisper =
OfflineWhisperModelConfig.builder().setEncoder(encoder).setDecoder(decoder).build();

OfflineModelConfig modelConfig =
OfflineModelConfig.builder()
.setWhisper(whisper)
.setTokens(tokens)
.setNumThreads(1)
.setDebug(true)
.build();

OfflineRecognizerConfig config =
OfflineRecognizerConfig.builder()
.setOfflineModelConfig(modelConfig)
.setDecodingMethod("greedy_search")
.build();

OfflineRecognizer recognizer = new OfflineRecognizer(config);
OfflineStream stream = recognizer.createStream();
stream.acceptWaveform(reader.getSamples(), reader.getSampleRate());

recognizer.decode(stream);

String text = recognizer.getResult(stream).getText();

System.out.printf("filename:%s\nresult:%s\n", waveFilename, text);

stream.release();
recognizer.release();
}
}
10 changes: 10 additions & 0 deletions java-api-examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,18 @@ This directory contains examples for the JAVA API of sherpa-onnx.

# Usage

## Streaming Speech recognition

```
./run-streaming-decode-file-ctc.sh
./run-streaming-decode-file-paraformer.sh
./run-streaming-decode-file-transducer.sh
```

## Non-Streaming Speech recognition

```bash
./run-non-streaming-decode-file-paraformer.sh
./run-non-streaming-decode-file-transducer.sh
./run-non-streaming-decode-file-whisper.sh
```
3 changes: 0 additions & 3 deletions java-api-examples/StreamingDecodeFileCtc.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// Copyright 2022-2023 by zhaoming
// Copyright 2024 Xiaomi Corporation

// This file shows how to use an online CTC model, i.e., streaming CTC model,
Expand All @@ -16,8 +15,6 @@ public static void main(String[] args) {
String waveFilename = "./sherpa-onnx-streaming-zipformer-ctc-small-2024-03-18/test_wavs/8k.wav";

WaveReader reader = new WaveReader(waveFilename);
System.out.println(reader.getSampleRate());
System.out.println(reader.getSamples().length);

OnlineZipformer2CtcModelConfig ctc =
OnlineZipformer2CtcModelConfig.builder().setModel(model).build();
Expand Down
3 changes: 0 additions & 3 deletions java-api-examples/StreamingDecodeFileParaformer.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// Copyright 2022-2023 by zhaoming
// Copyright 2024 Xiaomi Corporation

// This file shows how to use an online paraformer, i.e., streaming paraformer,
Expand All @@ -16,8 +15,6 @@ public static void main(String[] args) {
String waveFilename = "./sherpa-onnx-streaming-paraformer-bilingual-zh-en/test_wavs/2.wav";

WaveReader reader = new WaveReader(waveFilename);
System.out.println(reader.getSampleRate());
System.out.println(reader.getSamples().length);

OnlineParaformerModelConfig paraformer =
OnlineParaformerModelConfig.builder().setEncoder(encoder).setDecoder(decoder).build();
Expand Down
2 changes: 0 additions & 2 deletions java-api-examples/StreamingDecodeFileTransducer.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ public static void main(String[] args) {
"./sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20/test_wavs/0.wav";

WaveReader reader = new WaveReader(waveFilename);
System.out.println(reader.getSampleRate());
System.out.println(reader.getSamples().length);

OnlineTransducerModelConfig transducer =
OnlineTransducerModelConfig.builder()
Expand Down
52 changes: 52 additions & 0 deletions java-api-examples/run-non-streaming-decode-file-paraformer.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env bash

set -ex

if [[ ! -f ../build/lib/libsherpa-onnx-jni.dylib && ! -f ../build/lib/libsherpa-onnx-jni.so ]]; then
mkdir -p ../build
pushd ../build
cmake \
-DSHERPA_ONNX_ENABLE_PYTHON=OFF \
-DSHERPA_ONNX_ENABLE_TESTS=OFF \
-DSHERPA_ONNX_ENABLE_CHECK=OFF \
-DBUILD_SHARED_LIBS=ON \
-DSHERPA_ONNX_ENABLE_PORTAUDIO=OFF \
-DSHERPA_ONNX_ENABLE_JNI=ON \
..

make -j4
ls -lh lib
popd
fi

if [ ! -f ../sherpa-onnx/java-api/build/sherpa-onnx.jar ]; then
pushd ../sherpa-onnx/java-api
make
popd
fi

if [[ ! -f ../build/lib/libsherpa-onnx-jni.dylib && ! -f ../build/lib/libsherpa-onnx-jni.so ]]; then
cmake \
-DSHERPA_ONNX_ENABLE_PYTHON=OFF \
-DSHERPA_ONNX_ENABLE_TESTS=OFF \
-DSHERPA_ONNX_ENABLE_CHECK=OFF \
-DBUILD_SHARED_LIBS=ON \
-DSHERPA_ONNX_ENABLE_PORTAUDIO=OFF \
-DSHERPA_ONNX_ENABLE_JNI=ON \
..

make -j4
ls -lh lib
fi

if [ ! -f ./sherpa-onnx-paraformer-zh-2023-03-28/tokens.txt ]; then
curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-paraformer-zh-2023-03-28.tar.bz2

tar xvf sherpa-onnx-paraformer-zh-2023-03-28.tar.bz2
rm sherpa-onnx-paraformer-zh-2023-03-28.tar.bz2
fi

java \
-Djava.library.path=$PWD/../build/lib \
-cp ../sherpa-onnx/java-api/build/sherpa-onnx.jar \
NonStreamingDecodeFileParaformer.java
52 changes: 52 additions & 0 deletions java-api-examples/run-non-streaming-decode-file-transducer.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env bash

set -ex

if [[ ! -f ../build/lib/libsherpa-onnx-jni.dylib && ! -f ../build/lib/libsherpa-onnx-jni.so ]]; then
mkdir -p ../build
pushd ../build
cmake \
-DSHERPA_ONNX_ENABLE_PYTHON=OFF \
-DSHERPA_ONNX_ENABLE_TESTS=OFF \
-DSHERPA_ONNX_ENABLE_CHECK=OFF \
-DBUILD_SHARED_LIBS=ON \
-DSHERPA_ONNX_ENABLE_PORTAUDIO=OFF \
-DSHERPA_ONNX_ENABLE_JNI=ON \
..

make -j4
ls -lh lib
popd
fi

if [ ! -f ../sherpa-onnx/java-api/build/sherpa-onnx.jar ]; then
pushd ../sherpa-onnx/java-api
make
popd
fi

if [[ ! -f ../build/lib/libsherpa-onnx-jni.dylib && ! -f ../build/lib/libsherpa-onnx-jni.so ]]; then
cmake \
-DSHERPA_ONNX_ENABLE_PYTHON=OFF \
-DSHERPA_ONNX_ENABLE_TESTS=OFF \
-DSHERPA_ONNX_ENABLE_CHECK=OFF \
-DBUILD_SHARED_LIBS=ON \
-DSHERPA_ONNX_ENABLE_PORTAUDIO=OFF \
-DSHERPA_ONNX_ENABLE_JNI=ON \
..

make -j4
ls -lh lib
fi

if [ ! -f ./sherpa-onnx-zipformer-gigaspeech-2023-12-12/tokens.txt ]; then
curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-zipformer-gigaspeech-2023-12-12.tar.bz2

tar xvf sherpa-onnx-zipformer-gigaspeech-2023-12-12.tar.bz2
rm sherpa-onnx-zipformer-gigaspeech-2023-12-12.tar.bz2
fi

java \
-Djava.library.path=$PWD/../build/lib \
-cp ../sherpa-onnx/java-api/build/sherpa-onnx.jar \
NonStreamingDecodeFileTransducer.java
Loading

0 comments on commit 83cd533

Please sign in to comment.