Add Kotlin and Java APIs for Moonshine v2 models (#3237)

Adds Android-side configuration and packaging support for Moonshine v2 (merged decoder) models so Kotlin/JNI and Java consumers can use the new model layout and ship additional language variants.

Changes:

- Extend Moonshine offline model config to include a v2 mergedDecoder model path and wire it through JNI.
- Add Moonshine v2 quantized model presets (idx 51–60) for multiple languages in the Kotlin offline model selector.
- Update Moonshine v2 export script + APK model list + GitHub Actions workflow to include new model variants and streamline publishing.
This commit is contained in:
Fangjun Kuang authored and GitHub committed 2026-02-27 19:23:12 +08:00
1 parent 3ff34bb8d6
commit 01e76fedce
13 files changed
+456 -17

No files matched your search

@@ -0,0 +1,53 @@
// Copyright 2024 Xiaomi Corporation
// This file shows how to use an offline Moonshine,
// i.e., non-streaming Moonshine v2 model,
// to decode files.
import com.k2fsa.sherpa.onnx.*;
public class NonStreamingDecodeFileMoonshineV2 {
public static void main(String[] args) {
// please refer to
// https://k2-fsa.github.io/sherpa/onnx/moonshine/index.html
// to download model files
String encoder = "./sherpa-onnx-moonshine-tiny-en-quantized-2026-02-27/encoder_model.ort";
String decoder =
"./sherpa-onnx-moonshine-tiny-en-quantized-2026-02-27/decoder_model_merged.ort";
String tokens = "./sherpa-onnx-moonshine-tiny-en-quantized-2026-02-27/tokens.txt";
String waveFilename = "./sherpa-onnx-moonshine-tiny-en-quantized-2026-02-27/test_wavs/0.wav";
WaveReader reader = new WaveReader(waveFilename);
OfflineMoonshineModelConfig moonshine =
OfflineMoonshineModelConfig.builder().setEncoder(encoder).setMergedDecoder(decoder).build();
OfflineModelConfig modelConfig =
OfflineModelConfig.builder()
.setMoonshine(moonshine)
.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();
}
}
+1
View File
@@ -30,6 +30,7 @@ This directory contains examples for the JAVA API of sherpa-onnx.
./run-non-streaming-decode-file-funasr-nano.sh
./run-non-streaming-decode-file-medasr-ctc.sh
./run-non-streaming-decode-file-moonshine.sh
./run-non-streaming-decode-file-moonshine-v2.sh
./run-non-streaming-decode-file-nemo-canary.sh
./run-non-streaming-decode-file-nemo.sh
./run-non-streaming-decode-file-omnilingual-asr-ctc.sh
@@ -0,0 +1,37 @@
#!/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-moonshine-tiny-en-quantized-2026-02-27/tokens.txt ]; then
curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-moonshine-tiny-en-quantized-2026-02-27.tar.bz2
tar xvf sherpa-onnx-moonshine-tiny-en-quantized-2026-02-27.tar.bz2
rm sherpa-onnx-moonshine-tiny-en-quantized-2026-02-27.tar.bz2
fi
java \
-Djava.library.path=$PWD/../build/lib \
-cp ../sherpa-onnx/java-api/build/sherpa-onnx.jar \
NonStreamingDecodeFileMoonshineV2.java