Skip to content

Commit

Permalink
swift api for sense voice
Browse files Browse the repository at this point in the history
  • Loading branch information
csukuangfj committed Jul 18, 2024
1 parent 7c80ca9 commit 7931f65
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,4 @@ sherpa-onnx-telespeech-ctc-*
*.fst
.ccache
lib*.a
sherpa-onnx-sense-voice-zh-en-ja-ko-yue-2024-07-17
18 changes: 16 additions & 2 deletions swift-api-examples/SherpaOnnx.swift
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,18 @@ func sherpaOnnxOfflineTdnnModelConfig(
)
}

func sherpaOnnxOfflineSenseVoiceModelConfig(
model: String = "",
language: String = "",
useInverseTextNormalization: Bool = false
) -> SherpaOnnxOfflineSenseVoiceModelConfig {
return SherpaOnnxOfflineSenseVoiceModelConfig(
model: toCPointer(model),
language: toCPointer(language),
use_itn: useInverseTextNormalization ? 1 : 0
)
}

func sherpaOnnxOfflineLMConfig(
model: String = "",
scale: Float = 1.0
Expand All @@ -378,7 +390,8 @@ func sherpaOnnxOfflineModelConfig(
modelType: String = "",
modelingUnit: String = "cjkchar",
bpeVocab: String = "",
teleSpeechCtc: String = ""
teleSpeechCtc: String = "",
senseVoice: SherpaOnnxOfflineSenseVoiceModelConfig = sherpaOnnxOfflineSenseVoiceModelConfig()
) -> SherpaOnnxOfflineModelConfig {
return SherpaOnnxOfflineModelConfig(
transducer: transducer,
Expand All @@ -393,7 +406,8 @@ func sherpaOnnxOfflineModelConfig(
model_type: toCPointer(modelType),
modeling_unit: toCPointer(modelingUnit),
bpe_vocab: toCPointer(bpeVocab),
telespeech_ctc: toCPointer(teleSpeechCtc)
telespeech_ctc: toCPointer(teleSpeechCtc),
sense_voice: senseVoice
)
}

Expand Down
19 changes: 18 additions & 1 deletion swift-api-examples/decode-file-non-streaming.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func run() {
var modelConfig: SherpaOnnxOfflineModelConfig
var modelType = "whisper"
// modelType = "paraformer"
// modelType = "sense_voice"

if modelType == "whisper" {
let encoder = "./sherpa-onnx-whisper-tiny.en/tiny.en-encoder.int8.onnx"
Expand Down Expand Up @@ -47,6 +48,19 @@ func run() {
debug: 0,
modelType: "paraformer"
)
} else if modelType == "sense_voice" {
let model = "./sherpa-onnx-sense-voice-zh-en-ja-ko-yue-2024-07-17/model.int8.onnx"
let tokens = "./sherpa-onnx-sense-voice-zh-en-ja-ko-yue-2024-07-17/tokens.txt"
let senseVoiceConfig = sherpaOnnxOfflineSenseVoiceModelConfig(
model: model,
useInverseTextNormalization: true
)

modelConfig = sherpaOnnxOfflineModelConfig(
tokens: tokens,
debug: 0,
senseVoice: senseVoiceConfig
)
} else {
print("Please specify a supported modelType \(modelType)")
return
Expand All @@ -63,7 +77,10 @@ func run() {

recognizer = SherpaOnnxOfflineRecognizer(config: &config)

let filePath = "./sherpa-onnx-whisper-tiny.en/test_wavs/0.wav"
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"
}
let fileURL: NSURL = NSURL(fileURLWithPath: filePath)
let audioFile = try! AVAudioFile(forReading: fileURL as URL)

Expand Down

0 comments on commit 7931f65

Please sign in to comment.