Skip to content

Adding in DetectedLanguage to go bindings #2947

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions bindings/go/pkg/whisper/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ func (context *context) Language() string {
return whisper.Whisper_lang_str(context.params.Language())
}

func (context *context) DetectedLanguage() string {
return whisper.Whisper_lang_str(context.model.ctx.Whisper_full_lang_id())
}

// Set translate flag
func (context *context) SetTranslate(v bool) {
context.params.SetTranslate(v)
Expand Down
31 changes: 31 additions & 0 deletions bindings/go/pkg/whisper/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,34 @@ func TestProcess(t *testing.T) {
err = context.Process(data, nil, nil, nil)
assert.NoError(err)
}

func TestDetectedLanguage(t *testing.T) {
assert := assert.New(t)

fh, err := os.Open(SamplePath)
assert.NoError(err)
defer fh.Close()

// Decode the WAV file - load the full buffer
dec := wav.NewDecoder(fh)
buf, err := dec.FullPCMBuffer()
assert.NoError(err)
assert.Equal(uint16(1), dec.NumChans)

data := buf.AsFloat32Buffer().Data

model, err := whisper.New(ModelPath)
assert.NoError(err)
assert.NotNil(model)
defer model.Close()

context, err := model.NewContext()
assert.NoError(err)

err = context.Process(data, nil, nil, nil)
assert.NoError(err)

expectedLanguage := "en"
actualLanguage := context.DetectedLanguage()
assert.Equal(expectedLanguage, actualLanguage)
}
1 change: 1 addition & 0 deletions bindings/go/pkg/whisper/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type Context interface {
SetTranslate(bool) // Set translate flag
IsMultilingual() bool // Return true if the model is multilingual.
Language() string // Get language
DetectedLanguage() string // Get detected language

SetOffset(time.Duration) // Set offset
SetDuration(time.Duration) // Set duration
Expand Down
Loading