-
Notifications
You must be signed in to change notification settings - Fork 508
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add streaming CTC HLG decoding for JNI.
- Loading branch information
1 parent
af8344c
commit 7b2a3a5
Showing
13 changed files
with
199 additions
and
4 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
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
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,58 @@ | ||
// Copyright 2024 Xiaomi Corporation | ||
|
||
// This file shows how to use an online CTC model, i.e., streaming CTC model, | ||
// to decode files. | ||
import com.k2fsa.sherpa.onnx.*; | ||
|
||
public class StreamingDecodeFileCtcHLG { | ||
public static void main(String[] args) { | ||
// please refer to | ||
// https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-streaming-zipformer-ctc-small-2024-03-18.tar.bz2 | ||
// to download model files | ||
String model = | ||
"./sherpa-onnx-streaming-zipformer-ctc-small-2024-03-18/ctc-epoch-30-avg-3-chunk-16-left-128.int8.onnx"; | ||
String tokens = "./sherpa-onnx-streaming-zipformer-ctc-small-2024-03-18/tokens.txt"; | ||
String hlg = "./sherpa-onnx-streaming-zipformer-ctc-small-2024-03-18/HLG.fst"; | ||
String waveFilename = "./sherpa-onnx-streaming-zipformer-ctc-small-2024-03-18/test_wavs/8k.wav"; | ||
|
||
WaveReader reader = new WaveReader(waveFilename); | ||
|
||
OnlineZipformer2CtcModelConfig ctc = | ||
OnlineZipformer2CtcModelConfig.builder().setModel(model).build(); | ||
|
||
OnlineModelConfig modelConfig = | ||
OnlineModelConfig.builder() | ||
.setZipformer2Ctc(ctc) | ||
.setTokens(tokens) | ||
.setNumThreads(1) | ||
.setDebug(true) | ||
.build(); | ||
|
||
OnlineCtcFstDecoderConfig ctcFstDecoderConfig = | ||
OnlineCtcFstDecoderConfig.builder().setGraph("hlg").build(); | ||
|
||
OnlineRecognizerConfig config = | ||
OnlineRecognizerConfig.builder() | ||
.setOnlineModelConfig(modelConfig) | ||
.setCtcFstDecoderConfig(ctcFstDecoderConfig) | ||
.build(); | ||
|
||
OnlineRecognizer recognizer = new OnlineRecognizer(config); | ||
OnlineStream stream = recognizer.createStream(); | ||
stream.acceptWaveform(reader.getSamples(), reader.getSampleRate()); | ||
|
||
float[] tailPaddings = new float[(int) (0.3 * reader.getSampleRate())]; | ||
stream.acceptWaveform(tailPaddings, reader.getSampleRate()); | ||
|
||
while (recognizer.isReady(stream)) { | ||
recognizer.decode(stream); | ||
} | ||
|
||
String text = recognizer.getResult(stream).getText(); | ||
|
||
System.out.printf("filename:%s\nresult:%s\n", waveFilename, text); | ||
|
||
stream.release(); | ||
recognizer.release(); | ||
} | ||
} |
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,36 @@ | ||
#!/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 ./sherpa-onnx-streaming-zipformer-ctc-small-2024-03-18/tokens.txt ]; then | ||
curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-streaming-zipformer-ctc-small-2024-03-18.tar.bz2 | ||
tar xvf sherpa-onnx-streaming-zipformer-ctc-small-2024-03-18.tar.bz2 | ||
rm sherpa-onnx-streaming-zipformer-ctc-small-2024-03-18.tar.bz2 | ||
fi | ||
|
||
java \ | ||
-Djava.library.path=$PWD/../build/lib \ | ||
-cp ../sherpa-onnx/java-api/build/sherpa-onnx.jar \ | ||
StreamingDecodeFileCtcHLG.java |
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
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
1 change: 1 addition & 0 deletions
1
sherpa-onnx/java-api/src/com/k2fsa/sherpa/onnx/OfflineNemoEncDecCtcModelConfig.java
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
43 changes: 43 additions & 0 deletions
43
sherpa-onnx/java-api/src/com/k2fsa/sherpa/onnx/OnlineCtcFstDecoderConfig.java
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,43 @@ | ||
// Copyright 2024 Xiaomi Corporation | ||
package com.k2fsa.sherpa.onnx; | ||
|
||
public class OnlineCtcFstDecoderConfig { | ||
private final String graph; | ||
private final int maxActive; | ||
|
||
private OnlineCtcFstDecoderConfig(Builder builder) { | ||
this.graph = builder.graph; | ||
this.maxActive = builder.maxActive; | ||
} | ||
|
||
public static Builder builder() { | ||
return new Builder(); | ||
} | ||
|
||
public String getGraph() { | ||
return graph; | ||
} | ||
|
||
public float getMaxActive() { | ||
return maxActive; | ||
} | ||
|
||
public static class Builder { | ||
private String graph = ""; | ||
private int maxActive = 3000; | ||
|
||
public OnlineCtcFstDecoderConfig build() { | ||
return new OnlineCtcFstDecoderConfig(this); | ||
} | ||
|
||
public Builder setGraph(String model) { | ||
this.graph = graph; | ||
return this; | ||
} | ||
|
||
public Builder setMaxActive(int maxActive) { | ||
this.maxActive = maxActive; | ||
return this; | ||
} | ||
} | ||
} |
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
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