Amanda Der Bedrosian Amanda Der Bedrosian commited on
Commit
1830e27
·
unverified ·
1 Parent(s): 2ccaffe

bindings.go : add DetectedLanguage to go bindings (#2947)

Browse files

Adding in DetectedLanguage(), a function to retrieve the detected
language that's populated by processing audio. Also adding in a unit
test to test the success.

Co-authored-by: Amanda Der Bedrosian <[email protected]>

bindings/go/pkg/whisper/context.go CHANGED
@@ -71,6 +71,10 @@ func (context *context) Language() string {
71
  return whisper.Whisper_lang_str(context.params.Language())
72
  }
73
 
 
 
 
 
74
  // Set translate flag
75
  func (context *context) SetTranslate(v bool) {
76
  context.params.SetTranslate(v)
 
71
  return whisper.Whisper_lang_str(context.params.Language())
72
  }
73
 
74
+ func (context *context) DetectedLanguage() string {
75
+ return whisper.Whisper_lang_str(context.model.ctx.Whisper_full_lang_id())
76
+ }
77
+
78
  // Set translate flag
79
  func (context *context) SetTranslate(v bool) {
80
  context.params.SetTranslate(v)
bindings/go/pkg/whisper/context_test.go CHANGED
@@ -91,3 +91,34 @@ func TestProcess(t *testing.T) {
91
  err = context.Process(data, nil, nil, nil)
92
  assert.NoError(err)
93
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91
  err = context.Process(data, nil, nil, nil)
92
  assert.NoError(err)
93
  }
94
+
95
+ func TestDetectedLanguage(t *testing.T) {
96
+ assert := assert.New(t)
97
+
98
+ fh, err := os.Open(SamplePath)
99
+ assert.NoError(err)
100
+ defer fh.Close()
101
+
102
+ // Decode the WAV file - load the full buffer
103
+ dec := wav.NewDecoder(fh)
104
+ buf, err := dec.FullPCMBuffer()
105
+ assert.NoError(err)
106
+ assert.Equal(uint16(1), dec.NumChans)
107
+
108
+ data := buf.AsFloat32Buffer().Data
109
+
110
+ model, err := whisper.New(ModelPath)
111
+ assert.NoError(err)
112
+ assert.NotNil(model)
113
+ defer model.Close()
114
+
115
+ context, err := model.NewContext()
116
+ assert.NoError(err)
117
+
118
+ err = context.Process(data, nil, nil, nil)
119
+ assert.NoError(err)
120
+
121
+ expectedLanguage := "en"
122
+ actualLanguage := context.DetectedLanguage()
123
+ assert.Equal(expectedLanguage, actualLanguage)
124
+ }
bindings/go/pkg/whisper/interface.go CHANGED
@@ -41,6 +41,7 @@ type Context interface {
41
  SetTranslate(bool) // Set translate flag
42
  IsMultilingual() bool // Return true if the model is multilingual.
43
  Language() string // Get language
 
44
 
45
  SetOffset(time.Duration) // Set offset
46
  SetDuration(time.Duration) // Set duration
 
41
  SetTranslate(bool) // Set translate flag
42
  IsMultilingual() bool // Return true if the model is multilingual.
43
  Language() string // Get language
44
+ DetectedLanguage() string // Get detected language
45
 
46
  SetOffset(time.Duration) // Set offset
47
  SetDuration(time.Duration) // Set duration