From 8b72906f68dc3f6c2c4b860bb545b909e226455c Mon Sep 17 00:00:00 2001 From: Fangjun Kuang Date: Sun, 27 Oct 2024 08:14:41 +0800 Subject: [PATCH] Add Swift API for Moonshine models. --- swift-api-examples/SherpaOnnx.swift | 20 ++++++++++++++++-- .../decode-file-non-streaming.swift | 21 +++++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/swift-api-examples/SherpaOnnx.swift b/swift-api-examples/SherpaOnnx.swift index 783f59224..661ebba28 100644 --- a/swift-api-examples/SherpaOnnx.swift +++ b/swift-api-examples/SherpaOnnx.swift @@ -357,6 +357,20 @@ func sherpaOnnxOfflineWhisperModelConfig( ) } +func sherpaOnnxOfflineMoonshineModelConfig( + preprocessor: String = "", + encoder: String = "", + uncachedDecoder: String = "", + cachedDecoder: String = "" +) -> SherpaOnnxOfflineMoonshineModelConfig { + return SherpaOnnxOfflineMoonshineModelConfig( + preprocessor: toCPointer(preprocessor), + encoder: toCPointer(encoder), + uncached_decoder: toCPointer(uncachedDecoder), + cached_decoder: toCPointer(cachedDecoder) + ) +} + func sherpaOnnxOfflineTdnnModelConfig( model: String = "" ) -> SherpaOnnxOfflineTdnnModelConfig { @@ -401,7 +415,8 @@ func sherpaOnnxOfflineModelConfig( modelingUnit: String = "cjkchar", bpeVocab: String = "", teleSpeechCtc: String = "", - senseVoice: SherpaOnnxOfflineSenseVoiceModelConfig = sherpaOnnxOfflineSenseVoiceModelConfig() + senseVoice: SherpaOnnxOfflineSenseVoiceModelConfig = sherpaOnnxOfflineSenseVoiceModelConfig(), + moonshine: SherpaOnnxOfflineMoonshineModelConfig = sherpaOnnxOfflineMoonshineModelConfig() ) -> SherpaOnnxOfflineModelConfig { return SherpaOnnxOfflineModelConfig( transducer: transducer, @@ -417,7 +432,8 @@ func sherpaOnnxOfflineModelConfig( modeling_unit: toCPointer(modelingUnit), bpe_vocab: toCPointer(bpeVocab), telespeech_ctc: toCPointer(teleSpeechCtc), - sense_voice: senseVoice + sense_voice: senseVoice, + moonshine: moonshine ) } diff --git a/swift-api-examples/decode-file-non-streaming.swift b/swift-api-examples/decode-file-non-streaming.swift index a60777832..2e5e9f9ac 100644 --- a/swift-api-examples/decode-file-non-streaming.swift +++ b/swift-api-examples/decode-file-non-streaming.swift @@ -18,6 +18,7 @@ func run() { var modelType = "whisper" // modelType = "paraformer" // modelType = "sense_voice" + // modelType = "moonshine" if modelType == "whisper" { let encoder = "./sherpa-onnx-whisper-tiny.en/tiny.en-encoder.int8.onnx" @@ -61,6 +62,24 @@ func run() { debug: 0, senseVoice: senseVoiceConfig ) + } else if modelType == "moonshine" { + let preprocessor = "./sherpa-onnx-moonshine-tiny-en-int8/preprocess.onnx" + let encoder = "./sherpa-onnx-moonshine-tiny-en-int8/encode.int8.onnx" + let uncachedDecoder = "./sherpa-onnx-moonshine-tiny-en-int8/uncached_decode.int8.onnx" + let cachedDecoder = "./sherpa-onnx-moonshine-tiny-en-int8/cached_decode.int8.onnx" + let tokens = "./sherpa-onnx-moonshine-tiny-en-int8/tokens.txt" + let moonshine = sherpaOnnxOfflineMoonshineModelConfig( + preprocessor: preprocessor, + encoder: encoder, + uncachedDecoder: uncachedDecoder, + cachedDecoder: cachedDecoder + ) + + modelConfig = sherpaOnnxOfflineModelConfig( + tokens: tokens, + debug: 0, + moonshine: moonshine + ) } else { print("Please specify a supported modelType \(modelType)") return @@ -80,6 +99,8 @@ func run() { var filePath = "./sherpa-onnx-whisper-tiny.en/test_wavs/0.wav" if modelType == "sense_voice" { filePath = "./sherpa-onnx-sense-voice-zh-en-ja-ko-yue-2024-07-17/test_wavs/zh.wav" + } else if modelType == "moonshine" { + filePath = "./sherpa-onnx-moonshine-tiny-en-int8/test_wavs/0.wav" } let fileURL: NSURL = NSURL(fileURLWithPath: filePath) let audioFile = try! AVAudioFile(forReading: fileURL as URL)