-
Notifications
You must be signed in to change notification settings - Fork 477
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Java API example for hotwords. (#1442)
- Loading branch information
1 parent
e0586f1
commit 1af8ad8
Showing
3 changed files
with
106 additions
and
0 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,3 +1,4 @@ | ||
lib | ||
hs_err* | ||
!run-*.sh | ||
./hotwords_cn.txt |
62 changes: 62 additions & 0 deletions
62
java-api-examples/NonStreamingDecodeFileTransducerHotwords.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,62 @@ | ||
// Copyright 2024 Xiaomi Corporation | ||
|
||
// This file shows how to use an offline transducer, i.e., non-streaming transducer, | ||
// to decode files with hotwords support. | ||
// | ||
// See also | ||
// https://k2-fsa.github.io/sherpa/onnx/hotwords/index.html#modeling-unit-is-cjkchar | ||
import com.k2fsa.sherpa.onnx.*; | ||
|
||
public class NonStreamingDecodeFileTransducerHotwords { | ||
public static void main(String[] args) { | ||
// please refer to | ||
// https://k2-fsa.github.io/sherpa/onnx/hotwords/index.html#modeling-unit-is-cjkchar | ||
// to download model files | ||
String encoder = | ||
"./sherpa-onnx-conformer-zh-stateless2-2023-05-23/encoder-epoch-99-avg-1.int8.onnx"; | ||
String decoder = "./sherpa-onnx-conformer-zh-stateless2-2023-05-23/decoder-epoch-99-avg-1.onnx"; | ||
String joiner = "./sherpa-onnx-conformer-zh-stateless2-2023-05-23/joiner-epoch-99-avg-1.onnx"; | ||
String tokens = "./sherpa-onnx-conformer-zh-stateless2-2023-05-23/tokens.txt"; | ||
|
||
String waveFilename = "./sherpa-onnx-conformer-zh-stateless2-2023-05-23/test_wavs/6.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) | ||
.setModelingUnit("cjkchar") | ||
.build(); | ||
|
||
OfflineRecognizerConfig config = | ||
OfflineRecognizerConfig.builder() | ||
.setOfflineModelConfig(modelConfig) | ||
.setDecodingMethod("modified_beam_search") | ||
.setHotwordsFile("./hotwords_cn.txt") | ||
.setHotwordsScore(2.0f) | ||
.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(); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
java-api-examples/run-non-streaming-decode-file-transducer-hotwords.sh
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 @@ | ||
#!/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-conformer-zh-stateless2-2023-05-23/tokens.txt ]; then | ||
wget https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-conformer-zh-stateless2-2023-05-23.tar.bz2 | ||
tar xvf sherpa-onnx-conformer-zh-stateless2-2023-05-23.tar.bz2 | ||
rm sherpa-onnx-conformer-zh-stateless2-2023-05-23.tar.bz2 | ||
fi | ||
|
||
if [ ! -f hotwords_cn.txt ]; then | ||
cat > hotwords_cn.txt <<EOF | ||
朱丽楠 | ||
EOF | ||
fi | ||
|
||
java \ | ||
-Djava.library.path=$PWD/../build/lib \ | ||
-cp ../sherpa-onnx/java-api/build/sherpa-onnx.jar \ | ||
NonStreamingDecodeFileTransducerHotwords.java |