From d01142173ad723348fb5cbd6bcb7821372fc6656 Mon Sep 17 00:00:00 2001 From: Fangjun Kuang Date: Wed, 3 Jan 2024 12:51:54 +0800 Subject: [PATCH] Add missing field for two-pass APK. (#511) --- .../java/com/k2fsa/sherpa/onnx/SherpaOnnx.kt | 5 ++ kotlin-api-examples/.gitignore | 2 +- kotlin-api-examples/SherpaOnnx2Pass.kt | 1 + kotlin-api-examples/run.sh | 18 +++++++ kotlin-api-examples/test-2pass.kt | 49 +++++++++++++++++++ 5 files changed, 74 insertions(+), 1 deletion(-) create mode 120000 kotlin-api-examples/SherpaOnnx2Pass.kt create mode 100644 kotlin-api-examples/test-2pass.kt diff --git a/android/SherpaOnnx2Pass/app/src/main/java/com/k2fsa/sherpa/onnx/SherpaOnnx.kt b/android/SherpaOnnx2Pass/app/src/main/java/com/k2fsa/sherpa/onnx/SherpaOnnx.kt index 9198d7dc0..08b3d8999 100644 --- a/android/SherpaOnnx2Pass/app/src/main/java/com/k2fsa/sherpa/onnx/SherpaOnnx.kt +++ b/android/SherpaOnnx2Pass/app/src/main/java/com/k2fsa/sherpa/onnx/SherpaOnnx.kt @@ -25,9 +25,14 @@ data class OnlineParaformerModelConfig( var decoder: String = "", ) +data class OnlineZipformer2CtcModelConfig( + var model: String = "", +) + data class OnlineModelConfig( var transducer: OnlineTransducerModelConfig = OnlineTransducerModelConfig(), var paraformer: OnlineParaformerModelConfig = OnlineParaformerModelConfig(), + var zipformer2Ctc: OnlineZipformer2CtcModelConfig = OnlineZipformer2CtcModelConfig(), var tokens: String, var numThreads: Int = 1, var debug: Boolean = false, diff --git a/kotlin-api-examples/.gitignore b/kotlin-api-examples/.gitignore index 196e33f05..681205b66 100644 --- a/kotlin-api-examples/.gitignore +++ b/kotlin-api-examples/.gitignore @@ -1,3 +1,3 @@ hs_err* -main.jar vits-zh-aishell3 +*.jar diff --git a/kotlin-api-examples/SherpaOnnx2Pass.kt b/kotlin-api-examples/SherpaOnnx2Pass.kt new file mode 120000 index 000000000..48756bcbc --- /dev/null +++ b/kotlin-api-examples/SherpaOnnx2Pass.kt @@ -0,0 +1 @@ +../android/SherpaOnnx2Pass/app/src/main/java/com/k2fsa/sherpa/onnx/SherpaOnnx.kt \ No newline at end of file diff --git a/kotlin-api-examples/run.sh b/kotlin-api-examples/run.sh index ddf412b20..283e8f44d 100755 --- a/kotlin-api-examples/run.sh +++ b/kotlin-api-examples/run.sh @@ -51,3 +51,21 @@ kotlinc-jvm -include-runtime -d main.jar Main.kt WaveReader.kt SherpaOnnx.kt fak ls -lh main.jar java -Djava.library.path=../build/lib -jar main.jar + +# For two-pass + +if [ ! -f ./sherpa-onnx-streaming-zipformer-en-20M-2023-02-17/encoder-epoch-99-avg-1.int8.onnx ]; then + wget https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-streaming-zipformer-en-20M-2023-02-17.tar.bz2 + tar xvf sherpa-onnx-streaming-zipformer-en-20M-2023-02-17.tar.bz2 + rm sherpa-onnx-streaming-zipformer-en-20M-2023-02-17.tar.bz2 +fi + +if [ ! -f ./sherpa-onnx-whisper-tiny.en/tiny.en-encoder.int8.onnx ]; then + wget https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-whisper-tiny.en.tar.bz2 + tar xvf sherpa-onnx-whisper-tiny.en.tar.bz2 + rm sherpa-onnx-whisper-tiny.en.tar.bz2 +fi + +kotlinc-jvm -include-runtime -d 2pass.jar test-2pass.kt WaveReader.kt SherpaOnnx2Pass.kt faked-asset-manager.kt +ls -lh 2pass.jar +java -Djava.library.path=../build/lib -jar 2pass.jar diff --git a/kotlin-api-examples/test-2pass.kt b/kotlin-api-examples/test-2pass.kt new file mode 100644 index 000000000..7ce5e4569 --- /dev/null +++ b/kotlin-api-examples/test-2pass.kt @@ -0,0 +1,49 @@ +package com.k2fsa.sherpa.onnx + +fun main() { + test2Pass() +} + +fun test2Pass() { + val firstPass = createFirstPass() + val secondPass = createSecondPass() + + val waveFilename = "./sherpa-onnx-streaming-zipformer-en-20M-2023-02-17/test_wavs/0.wav" + + var objArray = WaveReader.readWaveFromFile( + filename = waveFilename, + ) + var samples: FloatArray = objArray[0] as FloatArray + var sampleRate: Int = objArray[1] as Int + + firstPass.acceptWaveform(samples, sampleRate = sampleRate) + while (firstPass.isReady()) { + firstPass.decode() + } + + var text = firstPass.text + println("First pass text: $text") + + text = secondPass.decode(samples, sampleRate) + println("Second pass text: $text") +} + +fun createFirstPass(): SherpaOnnx { + val config = OnlineRecognizerConfig( + featConfig = getFeatureConfig(sampleRate = 16000, featureDim = 80), + modelConfig = getModelConfig(type = 1)!!, + endpointConfig = getEndpointConfig(), + enableEndpoint = true, + ) + + return SherpaOnnx(config = config) +} + +fun createSecondPass(): SherpaOnnxOffline { + val config = OfflineRecognizerConfig( + featConfig = getFeatureConfig(sampleRate = 16000, featureDim = 80), + modelConfig = getOfflineModelConfig(type = 2)!!, + ) + + return SherpaOnnxOffline(config = config) +}