Stavros Panakakis commited on
Commit
c80d17a
·
unverified ·
1 Parent(s): 04d9c8d

go : add tests and update bindings (#2425)

Browse files

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

.github/workflows/bindings-go.yml CHANGED
@@ -13,10 +13,10 @@ jobs:
13
  ubuntu-latest:
14
  runs-on: ubuntu-latest
15
  steps:
16
- - uses: actions/setup-go@v3
17
  with:
18
- go-version: '^1.19'
19
- - uses: actions/checkout@v1
20
  - run: |
21
  cd bindings/go
22
  make test
 
13
  ubuntu-latest:
14
  runs-on: ubuntu-latest
15
  steps:
16
+ - uses: actions/setup-go@v5
17
  with:
18
+ go-version: '^1.23'
19
+ - uses: actions/checkout@v4
20
  - run: |
21
  cd bindings/go
22
  make test
bindings/go/go.mod CHANGED
@@ -1,10 +1,10 @@
1
  module github.com/ggerganov/whisper.cpp/bindings/go
2
 
3
- go 1.19
4
 
5
  require (
6
  github.com/go-audio/wav v1.1.0
7
- github.com/stretchr/testify v1.8.1
8
  )
9
 
10
  require (
 
1
  module github.com/ggerganov/whisper.cpp/bindings/go
2
 
3
+ go 1.23
4
 
5
  require (
6
  github.com/go-audio/wav v1.1.0
7
+ github.com/stretchr/testify v1.9.0
8
  )
9
 
10
  require (
bindings/go/go.sum CHANGED
@@ -1,4 +1,3 @@
1
- github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2
  github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
3
  github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4
  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=
9
  github.com/go-audio/wav v1.1.0/go.mod h1:mpe9qfwbScEbkd8uybLuIpTgHyrISw/OTuvjUW2iGtE=
10
  github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
11
  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=
19
  gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
20
  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=
22
  gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
23
  gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
 
 
1
  github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2
  github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3
  github.com/go-audio/audio v1.0.0 h1:zS9vebldgbQqktK4H0lUqWrG8P0NxCJVqcj7ZpNnwd4=
 
8
  github.com/go-audio/wav v1.1.0/go.mod h1:mpe9qfwbScEbkd8uybLuIpTgHyrISw/OTuvjUW2iGtE=
9
  github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
10
  github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
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=
 
 
 
 
 
13
  gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
14
  gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
 
15
  gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
16
  gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
bindings/go/pkg/whisper/context_test.go CHANGED
@@ -4,52 +4,90 @@ import (
4
  "os"
5
  "testing"
6
 
7
- // Packages
8
- whisper "github.com/ggerganov/whisper.cpp/bindings/go/pkg/whisper"
9
  assert "github.com/stretchr/testify/assert"
10
  )
11
 
12
- const (
13
- ModelPath = "../../models/ggml-tiny.bin"
14
- SamplePath = "../../samples/jfk.wav"
15
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
- func Test_Whisper_000(t *testing.T) {
 
 
 
 
 
 
 
 
18
  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
27
  model, err := whisper.New(ModelPath)
28
  assert.NoError(err)
29
  assert.NotNil(model)
30
- assert.NoError(model.Close())
31
 
32
- t.Log("languages=", model.Languages())
 
 
 
 
 
 
 
 
33
  }
34
 
35
- func Test_Whisper_001(t *testing.T) {
36
  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
 
 
 
 
 
45
  model, err := whisper.New(ModelPath)
46
  assert.NoError(err)
47
  assert.NotNil(model)
48
  defer model.Close()
49
 
50
- // Get context for decoding
51
- ctx, err := model.NewContext()
52
  assert.NoError(err)
53
- assert.NotNil(ctx)
54
 
 
 
55
  }
 
4
  "os"
5
  "testing"
6
 
7
+ "github.com/ggerganov/whisper.cpp/bindings/go/pkg/whisper"
8
+ "github.com/go-audio/wav"
9
  assert "github.com/stretchr/testify/assert"
10
  )
11
 
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)
40
 
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) {
50
  assert := assert.New(t)
51
+
 
 
 
 
 
 
 
52
  model, err := whisper.New(ModelPath)
53
  assert.NoError(err)
54
  assert.NotNil(model)
55
+ defer model.Close()
56
 
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)
66
  }
67
 
68
+ func TestProcess(t *testing.T) {
69
  assert := assert.New(t)
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
+
83
  model, err := whisper.New(ModelPath)
84
  assert.NoError(err)
85
  assert.NotNil(model)
86
  defer model.Close()
87
 
88
+ context, err := model.NewContext()
 
89
  assert.NoError(err)
 
90
 
91
+ err = context.Process(data, nil, nil)
92
+ assert.NoError(err)
93
  }
bindings/go/pkg/whisper/model_test.go ADDED
@@ -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 ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ package whisper_test
2
+
3
+ const (
4
+ ModelPath = "../../models/ggml-small.en.bin"
5
+ SamplePath = "../../samples/jfk.wav"
6
+ )