Skip to content

Commit

Permalink
go : support "auto" as an option when set language (ggerganov#462)
Browse files Browse the repository at this point in the history
Co-authored-by: Ming <ming@localhost>
  • Loading branch information
polarmoon and Ming authored Feb 4, 2023
1 parent 2919803 commit b2fc4c7
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
4 changes: 4 additions & 0 deletions bindings/go/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion bindings/go/pkg/whisper/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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())
}

Expand Down
2 changes: 1 addition & 1 deletion bindings/go/pkg/whisper/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b2fc4c7

Please sign in to comment.