Skip to content

Commit a2392e0

Browse files
StavrospanakakisiThalay
authored andcommitted
go : add tests and update bindings (ggml-org#2425)
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 9d9e38f commit a2392e0

File tree

6 files changed

+171
-43
lines changed

6 files changed

+171
-43
lines changed

.github/workflows/bindings-go.yml

+3-3
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

+2-2
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

+2-9
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

+67-29
Original file line numberDiff line numberDiff line change
@@ -4,52 +4,90 @@ 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+
assert.NotNil(model)
18+
defer model.Close()
19+
20+
context, err := model.NewContext()
21+
assert.NoError(err)
22+
23+
// This returns an error since
24+
// the model 'models/ggml-small.en.bin'
25+
// that is loaded is not multilingual
26+
err = context.SetLanguage("en")
27+
assert.Error(err)
28+
}
29+
30+
func TestContextModelIsMultilingual(t *testing.T) {
31+
assert := assert.New(t)
32+
33+
model, err := whisper.New(ModelPath)
34+
assert.NoError(err)
35+
assert.NotNil(model)
36+
defer model.Close()
37+
38+
context, err := model.NewContext()
39+
assert.NoError(err)
1640

17-
func Test_Whisper_000(t *testing.T) {
41+
isMultilingual := context.IsMultilingual()
42+
43+
// This returns false since
44+
// the model 'models/ggml-small.en.bin'
45+
// that is loaded is not multilingual
46+
assert.False(isMultilingual)
47+
}
48+
49+
func TestLanguage(t *testing.T) {
1850
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
51+
2752
model, err := whisper.New(ModelPath)
2853
assert.NoError(err)
2954
assert.NotNil(model)
30-
assert.NoError(model.Close())
55+
defer model.Close()
3156

32-
t.Log("languages=", model.Languages())
57+
context, err := model.NewContext()
58+
assert.NoError(err)
59+
60+
// This always returns en since
61+
// the model 'models/ggml-small.en.bin'
62+
// that is loaded is not multilingual
63+
expectedLanguage := "en"
64+
actualLanguage := context.Language()
65+
assert.Equal(expectedLanguage, actualLanguage)
3366
}
3467

35-
func Test_Whisper_001(t *testing.T) {
68+
func TestProcess(t *testing.T) {
3669
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
70+
71+
fh, err := os.Open(SamplePath)
72+
assert.NoError(err)
73+
defer fh.Close()
74+
75+
// Decode the WAV file - load the full buffer
76+
dec := wav.NewDecoder(fh)
77+
buf, err := dec.FullPCMBuffer()
78+
assert.NoError(err)
79+
assert.Equal(uint16(1), dec.NumChans)
80+
81+
data := buf.AsFloat32Buffer().Data
82+
4583
model, err := whisper.New(ModelPath)
4684
assert.NoError(err)
4785
assert.NotNil(model)
4886
defer model.Close()
4987

50-
// Get context for decoding
51-
ctx, err := model.NewContext()
88+
context, err := model.NewContext()
5289
assert.NoError(err)
53-
assert.NotNil(ctx)
5490

91+
err = context.Process(data, nil, nil)
92+
assert.NoError(err)
5593
}

bindings/go/pkg/whisper/model_test.go

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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+
assert.NotNil(model)
34+
35+
err = model.Close()
36+
assert.NoError(err)
37+
}
38+
39+
func TestNewContext(t *testing.T) {
40+
assert := assert.New(t)
41+
42+
model, err := whisper.New(ModelPath)
43+
assert.NoError(err)
44+
assert.NotNil(model)
45+
defer model.Close()
46+
47+
context, err := model.NewContext()
48+
assert.NoError(err)
49+
assert.NotNil(context)
50+
}
51+
52+
func TestIsMultilingual(t *testing.T) {
53+
assert := assert.New(t)
54+
55+
model, err := whisper.New(ModelPath)
56+
assert.NoError(err)
57+
assert.NotNil(model)
58+
defer model.Close()
59+
60+
isMultilingual := model.IsMultilingual()
61+
62+
// This returns false since
63+
// the model 'models/ggml-small.en.bin'
64+
// that is loaded is not multilingual
65+
assert.False(isMultilingual)
66+
}
67+
68+
func TestLanguages(t *testing.T) {
69+
assert := assert.New(t)
70+
71+
model, err := whisper.New(ModelPath)
72+
assert.NoError(err)
73+
assert.NotNil(model)
74+
defer model.Close()
75+
76+
expectedLanguages := []string{
77+
"en", "zh", "de", "es", "ru", "ko", "fr", "ja", "pt", "tr", "pl",
78+
"ca", "nl", "ar", "sv", "it", "id", "hi", "fi", "vi", "he", "uk",
79+
"el", "ms", "cs", "ro", "da", "hu", "ta", "no", "th", "ur", "hr",
80+
"bg", "lt", "la", "mi", "ml", "cy", "sk", "te", "fa", "lv", "bn",
81+
"sr", "az", "sl", "kn", "et", "mk", "br", "eu", "is", "hy", "ne",
82+
"mn", "bs", "kk", "sq", "sw", "gl", "mr", "pa", "si", "km", "sn",
83+
"yo", "so", "af", "oc", "ka", "be", "tg", "sd", "gu", "am", "yi",
84+
"lo", "uz", "fo", "ht", "ps", "tk", "nn", "mt", "sa", "lb", "my",
85+
"bo", "tl", "mg", "as", "tt", "haw", "ln", "ha", "ba", "jw", "su",
86+
}
87+
88+
actualLanguages := model.Languages()
89+
90+
assert.Equal(expectedLanguages, actualLanguages)
91+
}

bindings/go/pkg/whisper/util_test.go

+6
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)