2022-12-20 06:54:33 +00:00
|
|
|
package whisper_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
2024-09-20 12:36:12 +00:00
|
|
|
"github.com/ggerganov/whisper.cpp/bindings/go/pkg/whisper"
|
|
|
|
"github.com/go-audio/wav"
|
2022-12-20 06:54:33 +00:00
|
|
|
assert "github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2024-09-20 12:36:12 +00:00
|
|
|
func TestSetLanguage(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
model, err := whisper.New(ModelPath)
|
|
|
|
assert.NoError(err)
|
|
|
|
assert.NotNil(model)
|
|
|
|
defer model.Close()
|
|
|
|
|
|
|
|
context, err := model.NewContext()
|
|
|
|
assert.NoError(err)
|
|
|
|
|
|
|
|
// This returns an error since
|
|
|
|
// the model 'models/ggml-small.en.bin'
|
|
|
|
// that is loaded is not multilingual
|
|
|
|
err = context.SetLanguage("en")
|
|
|
|
assert.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContextModelIsMultilingual(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
model, err := whisper.New(ModelPath)
|
|
|
|
assert.NoError(err)
|
|
|
|
assert.NotNil(model)
|
|
|
|
defer model.Close()
|
|
|
|
|
|
|
|
context, err := model.NewContext()
|
|
|
|
assert.NoError(err)
|
2022-12-20 06:54:33 +00:00
|
|
|
|
2024-09-20 12:36:12 +00:00
|
|
|
isMultilingual := context.IsMultilingual()
|
|
|
|
|
|
|
|
// This returns false since
|
|
|
|
// the model 'models/ggml-small.en.bin'
|
|
|
|
// that is loaded is not multilingual
|
|
|
|
assert.False(isMultilingual)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLanguage(t *testing.T) {
|
2022-12-20 06:54:33 +00:00
|
|
|
assert := assert.New(t)
|
2024-09-20 12:36:12 +00:00
|
|
|
|
2022-12-20 06:54:33 +00:00
|
|
|
model, err := whisper.New(ModelPath)
|
|
|
|
assert.NoError(err)
|
|
|
|
assert.NotNil(model)
|
2024-09-20 12:36:12 +00:00
|
|
|
defer model.Close()
|
2022-12-20 06:54:33 +00:00
|
|
|
|
2024-09-20 12:36:12 +00:00
|
|
|
context, err := model.NewContext()
|
|
|
|
assert.NoError(err)
|
|
|
|
|
|
|
|
// This always returns en since
|
|
|
|
// the model 'models/ggml-small.en.bin'
|
|
|
|
// that is loaded is not multilingual
|
|
|
|
expectedLanguage := "en"
|
|
|
|
actualLanguage := context.Language()
|
|
|
|
assert.Equal(expectedLanguage, actualLanguage)
|
2022-12-20 06:54:33 +00:00
|
|
|
}
|
|
|
|
|
2024-09-20 12:36:12 +00:00
|
|
|
func TestProcess(t *testing.T) {
|
2022-12-20 06:54:33 +00:00
|
|
|
assert := assert.New(t)
|
2024-09-20 12:36:12 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2022-12-20 06:54:33 +00:00
|
|
|
model, err := whisper.New(ModelPath)
|
|
|
|
assert.NoError(err)
|
|
|
|
assert.NotNil(model)
|
|
|
|
defer model.Close()
|
|
|
|
|
2024-09-20 12:36:12 +00:00
|
|
|
context, err := model.NewContext()
|
2022-12-20 06:54:33 +00:00
|
|
|
assert.NoError(err)
|
|
|
|
|
2024-09-20 12:36:12 +00:00
|
|
|
err = context.Process(data, nil, nil)
|
|
|
|
assert.NoError(err)
|
2022-12-20 06:54:33 +00:00
|
|
|
}
|