From b2fc4c701001d0b1cab1c1185e31458a290e0b32 Mon Sep 17 00:00:00 2001 From: polarmoon <90010972+polarmoon@users.noreply.github.com> Date: Fri, 3 Feb 2023 23:09:27 -0800 Subject: [PATCH] go : support "auto" as an option when set language (#462) Co-authored-by: Ming --- bindings/go/params.go | 4 ++++ bindings/go/pkg/whisper/context.go | 9 ++++++++- bindings/go/pkg/whisper/interface.go | 2 +- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/bindings/go/params.go b/bindings/go/params.go index 08757d7..c413895 100644 --- a/bindings/go/params.go +++ b/bindings/go/params.go @@ -49,6 +49,10 @@ func (p *Params) SetSpeedup(v bool) { // Set language id func (p *Params) SetLanguage(lang int) error { + if lang == -1 { + p.language = nil + return nil + } str := C.whisper_lang_str(C.int(lang)) if str == nil { return ErrInvalidLanguage diff --git a/bindings/go/pkg/whisper/context.go b/bindings/go/pkg/whisper/context.go index a4fe513..0a6e9cb 100644 --- a/bindings/go/pkg/whisper/context.go +++ b/bindings/go/pkg/whisper/context.go @@ -46,7 +46,10 @@ func (context *context) SetLanguage(lang string) error { if !context.model.IsMultilingual() { return ErrModelNotMultilingual } - if id := context.model.ctx.Whisper_lang_id(lang); id < 0 { + + if lang == "auto" { + context.params.SetLanguage(-1) + } else if id := context.model.ctx.Whisper_lang_id(lang); id < 0 { return ErrUnsupportedLanguage } else if err := context.params.SetLanguage(id); err != nil { return err @@ -61,6 +64,10 @@ func (context *context) IsMultilingual() bool { // Get language func (context *context) Language() string { + id := context.params.Language() + if id == -1 { + return "auto" + } return whisper.Whisper_lang_str(context.params.Language()) } diff --git a/bindings/go/pkg/whisper/interface.go b/bindings/go/pkg/whisper/interface.go index 4242963..a1d3f68 100644 --- a/bindings/go/pkg/whisper/interface.go +++ b/bindings/go/pkg/whisper/interface.go @@ -29,7 +29,7 @@ type Model interface { // Context is the speach recognition context. type Context interface { - SetLanguage(string) error // Set the language to use for speech recognition. + SetLanguage(string) error // Set the language to use for speech recognition, use "auto" for auto detect language. SetTranslate(bool) // Set translate flag IsMultilingual() bool // Return true if the model is multilingual. Language() string // Get language