Skip to content

Commit e3103a8

Browse files
committed
Added some sync rails
1 parent 0e0660f commit e3103a8

File tree

5 files changed

+27
-9
lines changed

5 files changed

+27
-9
lines changed

cmd/server/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func main() {
5656

5757
// Create a whisper service
5858
log.Println("Storing models at", dir)
59-
whisper, err := whisper.New(dir)
59+
whisper, err := whisper.New(dir, whisper.OptMaxConcurrent(1))
6060
if err != nil {
6161
log.Println(err)
6262
os.Exit(-2)

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
3737
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
3838
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
3939
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
40-
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 h1:vr/HnozRka3pE4EsMEg1lgkXJkTFJCVUX+S/ZT6wYzM=
41-
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842/go.mod h1:XtvwrStGgqGPLc4cjQfWqZHG1YFdYs6swckp8vpsjnc=
40+
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 h1:2dVuKD2vS7b0QIHQbpyTISPd0LeHDbnYEryqj5Q1ug8=
41+
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56/go.mod h1:M4RDyNAINzryxdtnbRXRL/OHtkFuWGRjvuhBJpk2IlY=
4242
golang.org/x/image v0.18.0 h1:jGzIakQa/ZXI1I0Fxvaa9W7yP25TqT6cHIHn+6CqvSQ=
4343
golang.org/x/image v0.18.0/go.mod h1:4yyo5vMFQjVjUcVk4jEQcU9MGy/rulF5WvUILseCM2E=
4444
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=

pkg/whisper/segmenter/segmenter.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ func (s *Segmenter) Close() error {
5050
//////////////////////////////////////////////////////////////////////////////
5151
// PUBLIC METHODS
5252

53+
// TODO: segments are output through a callback, with the samples and a timestamp
54+
// TODO: we could do some basic silence and voice detection to segment to ensure
55+
// we don't overtax the CPU/GPU with silence and non-speech
56+
// TODO: We have hard-coded the sample format, sample rate and number of channels
57+
// here. We should make this configurable
5358
func (s *Segmenter) Decode(ctx context.Context) error {
5459
mapFunc := func(stream int, params *ffmpeg.Par) (*ffmpeg.Par, error) {
5560
if stream == s.reader.BestStream(media.AUDIO) {

pkg/whisper/task/context.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"fmt"
77
"path/filepath"
8+
"sync"
89

910
// Packages
1011
model "github.com/mutablelogic/go-whisper/pkg/whisper/model"
@@ -19,6 +20,8 @@ import (
1920

2021
// Context is used for running the transcription or translation
2122
type Context struct {
23+
sync.Mutex
24+
2225
model string
2326
whisper *whisper.Context
2427

@@ -36,6 +39,9 @@ func New() *Context {
3639

3740
// Init the context
3841
func (m *Context) Init(path string, model *model.Model, gpu int) error {
42+
m.Lock()
43+
defer m.Unlock()
44+
3945
// Check parameters
4046
if model == nil {
4147
return ErrBadParameter
@@ -143,25 +149,31 @@ func (task *Context) CanTranslate() bool {
143149
// TODO: We need a callback for segment progress.
144150
func (task *Context) Transcribe(ctx context.Context, samples []float32) error {
145151
// Set the 'abort' function
146-
task.params.SetAbortCallback(task.whisper, func() bool {
152+
/*task.params.SetAbortCallback(task.whisper, func() bool {
147153
select {
148154
case <-ctx.Done():
149155
return true
150156
default:
151157
return false
152158
}
153-
})
159+
})*/
154160

155161
// Set the 'progress' function
156-
task.params.SetProgressCallback(task.whisper, func(percent int) {
157-
fmt.Printf("Progress: %v\n", percent)
158-
})
162+
//task.params.SetProgressCallback(task.whisper, func(percent int) {
163+
// fmt.Printf("Progress: %v\n", percent)
164+
//})
159165

160166
// Perform the transcription
161167
if err := whisper.Whisper_full(task.whisper, task.params, samples); err != nil {
162168
return err
163169
}
164170

171+
// Get segments
172+
for i := 0; i < task.whisper.NumSegments(); i++ {
173+
segment := task.whisper.Segment(i)
174+
fmt.Printf("Segment: %v\n", segment.Text)
175+
}
176+
165177
// Return success
166178
return nil
167179
}

pkg/whisper/whisper.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"errors"
77
"fmt"
8+
"runtime"
89
"strings"
910

1011
// Packages
@@ -46,7 +47,7 @@ func New(path string, opt ...Opt) (*Whisper, error) {
4647
var o opts
4748

4849
// Set options
49-
o.MaxConcurrent = 1
50+
o.MaxConcurrent = runtime.NumCPU()
5051
for _, fn := range opt {
5152
if err := fn(&o); err != nil {
5253
return nil, err

0 commit comments

Comments
 (0)