Skip to content

Commit

Permalink
Fixes for iOS
Browse files Browse the repository at this point in the history
  • Loading branch information
csukuangfj committed Sep 12, 2023
1 parent fd35324 commit 7506a07
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 8 deletions.
39 changes: 38 additions & 1 deletion ios-swiftui/SherpaOnnx2Pass/SherpaOnnx2Pass/Model.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func getStreamingZh14MZipformer20230223() -> SherpaOnnxOnlineModelConfig {
func getStreamingEn20MZipformer20230217() -> SherpaOnnxOnlineModelConfig {
let encoder = getResource("encoder-epoch-99-avg-1.int8", "onnx")
let decoder = getResource("decoder-epoch-99-avg-1", "onnx")
let joiner = getResource("joiner-epoch-99-avg-1.int8", "onnx")
let joiner = getResource("joiner-epoch-99-avg-1", "onnx")
let tokens = getResource("tokens", "txt")

return sherpaOnnxOnlineModelConfig(
Expand Down Expand Up @@ -91,6 +91,43 @@ func getNonStreamingZhParaformer20230328() -> SherpaOnnxOfflineModelConfig {
)
}

// https://k2-fsa.github.io/sherpa/onnx/pretrained_models/whisper/tiny.en.html#tiny-en
// English, int8 encoder and decoder
func getNonStreamingWhisperTinyEn() -> SherpaOnnxOfflineModelConfig {
let encoder = getResource("tiny.en-encoder.int8", "onnx")
let decoder = getResource("tiny.en-decoder.int8", "onnx")
let tokens = getResource("tiny.en-tokens", "txt")

return sherpaOnnxOfflineModelConfig(
tokens: tokens,
whisper: sherpaOnnxOfflineWhisperModelConfig(
encoder: encoder,
decoder: decoder
),
numThreads: 1,
modelType: "whisper"
)
}

// icefall-asr-multidataset-pruned_transducer_stateless7-2023-05-04 (English)
// https://k2-fsa.github.io/sherpa/onnx/pretrained_models/offline-transducer/zipformer-transducer-models.html#icefall-asr-multidataset-pruned-transducer-stateless7-2023-05-04-english

func getNonStreamingEnZipformer20230504() -> SherpaOnnxOfflineModelConfig {
let encoder = getResource("encoder-epoch-30-avg-4.int8", "onnx")
let decoder = getResource("decoder-epoch-30-avg-4", "onnx")
let joiner = getResource("joiner-epoch-30-avg-4", "onnx")
let tokens = getResource("non-streaming-zipformer-tokens", "txt")

return sherpaOnnxOfflineModelConfig(
tokens: tokens,
transducer: sherpaOnnxOfflineTransducerModelConfig(
encoder: encoder,
decoder: decoder,
joiner: joiner),
numThreads: 1,
modelType: "zipformer"
)
}

/// Please refer to
/// https://k2-fsa.github.io/sherpa/onnx/pretrained_models/index.html
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ class SherpaOnnxViewModel: ObservableObject {
var offlineRecognizer: SherpaOnnxOfflineRecognizer! = nil

var lastSentence: String = ""
let maxSentence: Int = 15
// let maxSentence: Int = 10 // for Chinese
let maxSentence: Int = 6 // for English

var results: String {
if sentences.isEmpty && lastSentence.isEmpty {
Expand Down Expand Up @@ -63,7 +64,9 @@ class SherpaOnnxViewModel: ObservableObject {
// You can also modify Model.swift to add new pre-trained models from
// https://k2-fsa.github.io/sherpa/onnx/pretrained_models/index.html
// let modelConfig = getBilingualStreamingZhEnZipformer20230220()
let modelConfig = getStreamingZh14MZipformer20230223()
/* let modelConfig = getStreamingZh14MZipformer20230223() */

let modelConfig = getStreamingEn20MZipformer20230217()

let featConfig = sherpaOnnxFeatureConfig(
sampleRate: 16000,
Expand All @@ -74,7 +77,11 @@ class SherpaOnnxViewModel: ObservableObject {
modelConfig: modelConfig,
enableEndpoint: true,
rule1MinTrailingSilence: 2.4,
rule2MinTrailingSilence: 1.2,

// rule2MinTrailingSilence: 1.2, // for Chinese

rule2MinTrailingSilence: 0.5, // for English

rule3MinUtteranceLength: 30,
decodingMethod: "greedy_search",
maxActivePaths: 4
Expand All @@ -83,7 +90,10 @@ class SherpaOnnxViewModel: ObservableObject {
}

private func initOfflineRecognizer() {
let modelConfig = getNonStreamingZhParaformer20230328()
// let modelConfig = getNonStreamingZhParaformer20230328()
let modelConfig = getNonStreamingWhisperTinyEn()

// let modelConfig = getNonStreamingEnZipformer20230504()

let featConfig = sherpaOnnxFeatureConfig(
sampleRate: 16000,
Expand Down Expand Up @@ -180,7 +190,9 @@ class SherpaOnnxViewModel: ObservableObject {
}
}

self.lastSentence = self.offlineRecognizer.decode(samples: Array(samples[0..<samples.count-12000])).text
// let num = 12000 // For Chinese
let num = 10000 // For English
self.lastSentence = self.offlineRecognizer.decode(samples: Array(samples[0..<samples.count-num])).text

let tmp = self.lastSentence
self.lastSentence = ""
Expand All @@ -189,8 +201,8 @@ class SherpaOnnxViewModel: ObservableObject {
self.updateLabel()

i = 0
if samples.count > 12000 {
i = samples.count - 12000
if samples.count > num {
i = samples.count - num
}
var tail: [Float] = Array(repeating: 0, count: samples.count - i)

Expand Down

0 comments on commit 7506a07

Please sign in to comment.