Skip to content

Commit fd5d96d

Browse files
committedSep 19, 2024
Add tests and updates for go bindings
Update Go version to ^1.23, the actions/setup-go to v5, actions/checkout to v4 and github.com/stretchr/testify to v1.9.0. Add test cases for the following model struct methods: - New - Close - NewContext - IsMultilingual - Languages Add test cases for the following context struct methods: - SetLanguage - IsMultilingual - Language - Process
1 parent 5b1ce40 commit fd5d96d

File tree

6 files changed

+164
-45
lines changed

6 files changed

+164
-45
lines changed
 

‎.github/workflows/bindings-go.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ jobs:
1313
ubuntu-latest:
1414
runs-on: ubuntu-latest
1515
steps:
16-
- uses: actions/setup-go@v3
16+
- uses: actions/setup-go@v5
1717
with:
18-
go-version: '^1.19'
19-
- uses: actions/checkout@v1
18+
go-version: '^1.23'
19+
- uses: actions/checkout@v4
2020
- run: |
2121
cd bindings/go
2222
make test

‎bindings/go/go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
module github.com/ggerganov/whisper.cpp/bindings/go
22

3-
go 1.19
3+
go 1.23
44

55
require (
66
github.com/go-audio/wav v1.1.0
7-
github.com/stretchr/testify v1.8.1
7+
github.com/stretchr/testify v1.9.0
88
)
99

1010
require (

‎bindings/go/go.sum

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
21
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
32
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
43
github.com/go-audio/audio v1.0.0 h1:zS9vebldgbQqktK4H0lUqWrG8P0NxCJVqcj7ZpNnwd4=
@@ -9,15 +8,9 @@ github.com/go-audio/wav v1.1.0 h1:jQgLtbqBzY7G+BM8fXF7AHUk1uHUviWS4X39d5rsL2g=
98
github.com/go-audio/wav v1.1.0/go.mod h1:mpe9qfwbScEbkd8uybLuIpTgHyrISw/OTuvjUW2iGtE=
109
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
1110
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
12-
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
13-
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
14-
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
15-
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
16-
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
17-
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
18-
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
11+
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
12+
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
1913
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
2014
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
21-
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
2215
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
2316
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

‎bindings/go/pkg/whisper/context_test.go

Lines changed: 64 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,52 +4,85 @@ import (
44
"os"
55
"testing"
66

7-
// Packages
8-
whisper "github.com/ggerganov/whisper.cpp/bindings/go/pkg/whisper"
7+
"github.com/ggerganov/whisper.cpp/bindings/go/pkg/whisper"
8+
"github.com/go-audio/wav"
99
assert "github.com/stretchr/testify/assert"
1010
)
1111

12-
const (
13-
ModelPath = "../../models/ggml-tiny.bin"
14-
SamplePath = "../../samples/jfk.wav"
15-
)
12+
func TestSetLanguage(t *testing.T) {
13+
assert := assert.New(t)
14+
15+
model, err := whisper.New(ModelPath)
16+
assert.NoError(err)
17+
defer model.Close()
18+
19+
context, err := model.NewContext()
20+
assert.NoError(err)
1621

17-
func Test_Whisper_000(t *testing.T) {
22+
// This returns an error since
23+
// the model 'models/ggml-small.en.bin'
24+
// that is loaded is not multilingual
25+
err = context.SetLanguage("en")
26+
assert.Error(err)
27+
}
28+
29+
func TestContextModelIsMultilingual(t *testing.T) {
1830
assert := assert.New(t)
19-
if _, err := os.Stat(ModelPath); os.IsNotExist(err) {
20-
t.Skip("Skipping test, model not found:", ModelPath)
21-
}
22-
if _, err := os.Stat(SamplePath); os.IsNotExist(err) {
23-
t.Skip("Skipping test, sample not found:", SamplePath)
24-
}
25-
26-
// Load model
31+
2732
model, err := whisper.New(ModelPath)
2833
assert.NoError(err)
29-
assert.NotNil(model)
30-
assert.NoError(model.Close())
34+
defer model.Close()
35+
36+
context, err := model.NewContext()
37+
assert.NoError(err)
3138

32-
t.Log("languages=", model.Languages())
39+
isMultilingual := context.IsMultilingual()
40+
41+
// This returns false since
42+
// the model 'models/ggml-small.en.bin'
43+
// that is loaded is not multilingual
44+
assert.False(isMultilingual)
3345
}
3446

35-
func Test_Whisper_001(t *testing.T) {
47+
func TestLanguage(t *testing.T) {
3648
assert := assert.New(t)
37-
if _, err := os.Stat(ModelPath); os.IsNotExist(err) {
38-
t.Skip("Skipping test, model not found:", ModelPath)
39-
}
40-
if _, err := os.Stat(SamplePath); os.IsNotExist(err) {
41-
t.Skip("Skipping test, sample not found:", SamplePath)
42-
}
43-
44-
// Load model
49+
4550
model, err := whisper.New(ModelPath)
4651
assert.NoError(err)
47-
assert.NotNil(model)
4852
defer model.Close()
4953

50-
// Get context for decoding
51-
ctx, err := model.NewContext()
54+
context, err := model.NewContext()
5255
assert.NoError(err)
53-
assert.NotNil(ctx)
5456

57+
// This always returns en since
58+
// the model 'models/ggml-small.en.bin'
59+
// that is loaded is not multilingual
60+
expectedLanguage := "en"
61+
actualLanguage := context.Language()
62+
assert.Equal(expectedLanguage, actualLanguage)
63+
}
64+
65+
func TestProcess(t *testing.T) {
66+
assert := assert.New(t)
67+
68+
fh, err := os.Open(SamplePath)
69+
assert.NoError(err)
70+
defer fh.Close()
71+
72+
// Decode the WAV file - load the full buffer
73+
dec := wav.NewDecoder(fh)
74+
buf, err := dec.FullPCMBuffer()
75+
assert.NoError(err)
76+
assert.Equal(uint16(1), dec.NumChans)
77+
78+
data := buf.AsFloat32Buffer().Data
79+
80+
model, err := whisper.New(ModelPath)
81+
assert.NoError(err)
82+
83+
context, err := model.NewContext()
84+
assert.NoError(err)
85+
86+
err = context.Process(data, nil, nil)
87+
assert.NoError(err)
5588
}

‎bindings/go/pkg/whisper/model_test.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package whisper_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/ggerganov/whisper.cpp/bindings/go/pkg/whisper"
7+
assert "github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestNew(t *testing.T) {
11+
assert := assert.New(t)
12+
t.Run("valid model path", func(t *testing.T) {
13+
model, err := whisper.New(ModelPath)
14+
assert.NoError(err)
15+
assert.NotNil(model)
16+
defer model.Close()
17+
18+
})
19+
20+
t.Run("invalid model path", func(t *testing.T) {
21+
invalidModelPath := "invalid-model-path.bin"
22+
model, err := whisper.New(invalidModelPath)
23+
assert.Error(err)
24+
assert.Nil(model)
25+
})
26+
}
27+
28+
func TestClose(t *testing.T) {
29+
assert := assert.New(t)
30+
31+
model, err := whisper.New(ModelPath)
32+
assert.NoError(err)
33+
34+
err = model.Close()
35+
assert.NoError(err)
36+
}
37+
38+
func TestNewContext(t *testing.T) {
39+
assert := assert.New(t)
40+
41+
model, err := whisper.New(ModelPath)
42+
assert.NoError(err)
43+
defer model.Close()
44+
45+
context, err := model.NewContext()
46+
assert.NoError(err)
47+
assert.NotNil(context)
48+
}
49+
50+
func TestIsMultilingual(t *testing.T) {
51+
assert := assert.New(t)
52+
53+
model, err := whisper.New(ModelPath)
54+
assert.NoError(err)
55+
defer model.Close()
56+
57+
isMultilingual := model.IsMultilingual()
58+
59+
// This returns false since
60+
// the model 'models/ggml-small.en.bin'
61+
// that is loaded is not multilingual
62+
assert.False(isMultilingual)
63+
}
64+
65+
func TestLanguages(t *testing.T) {
66+
assert := assert.New(t)
67+
68+
model, err := whisper.New(ModelPath)
69+
assert.NoError(err)
70+
defer model.Close()
71+
72+
expectedLanguages := []string{
73+
"en", "zh", "de", "es", "ru", "ko", "fr", "ja", "pt", "tr", "pl",
74+
"ca", "nl", "ar", "sv", "it", "id", "hi", "fi", "vi", "he", "uk",
75+
"el", "ms", "cs", "ro", "da", "hu", "ta", "no", "th", "ur", "hr",
76+
"bg", "lt", "la", "mi", "ml", "cy", "sk", "te", "fa", "lv", "bn",
77+
"sr", "az", "sl", "kn", "et", "mk", "br", "eu", "is", "hy", "ne",
78+
"mn", "bs", "kk", "sq", "sw", "gl", "mr", "pa", "si", "km", "sn",
79+
"yo", "so", "af", "oc", "ka", "be", "tg", "sd", "gu", "am", "yi",
80+
"lo", "uz", "fo", "ht", "ps", "tk", "nn", "mt", "sa", "lb", "my",
81+
"bo", "tl", "mg", "as", "tt", "haw", "ln", "ha", "ba", "jw", "su",
82+
}
83+
84+
actualLanguages := model.Languages()
85+
86+
assert.Equal(expectedLanguages, actualLanguages)
87+
}

‎bindings/go/pkg/whisper/util_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package whisper_test
2+
3+
const (
4+
ModelPath = "../../models/ggml-small.en.bin"
5+
SamplePath = "../../samples/jfk.wav"
6+
)

0 commit comments

Comments
 (0)