-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathwhisper.go
192 lines (157 loc) · 4.44 KB
/
whisper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package whisper
import (
"context"
"encoding/json"
"errors"
"fmt"
"runtime"
"strings"
// Packages
ffmpeg "github.com/mutablelogic/go-media/pkg/ffmpeg"
pool "github.com/mutablelogic/go-whisper/pkg/pool"
schema "github.com/mutablelogic/go-whisper/pkg/schema"
store "github.com/mutablelogic/go-whisper/pkg/store"
task "github.com/mutablelogic/go-whisper/pkg/task"
whisper "github.com/mutablelogic/go-whisper/sys/whisper"
// Namespace imports
. "github.com/djthorpe/go-errors"
)
//////////////////////////////////////////////////////////////////////////////
// TYPES
// Whisper represents a whisper service for running transcription and translation
type Whisper struct {
pool *pool.ContextPool
store *store.Store
}
//////////////////////////////////////////////////////////////////////////////
// GLOBALS
const (
// This is the extension of the model files
extModel = ".bin"
// This is where the model is downloaded from
defaultModelUrl = "https://huggingface.co/ggerganov/whisper.cpp/resolve/main/?download=true"
// Sample Rate
SampleRate = whisper.SampleRate
)
//////////////////////////////////////////////////////////////////////////////
// LIFECYCLE
// Create a new whisper service with the path to the models directory
// and optional parameters
func New(path string, opt ...Opt) (*Whisper, error) {
var o opts
// Set options
o.MaxConcurrent = runtime.NumCPU()
for _, fn := range opt {
if err := fn(&o); err != nil {
return nil, err
}
}
// Create a new whisper service
w := new(Whisper)
if store, err := store.NewStore(path, extModel, defaultModelUrl); err != nil {
return nil, err
} else {
w.store = store
}
if pool := pool.NewContextPool(path, o.MaxConcurrent, o.gpu); pool == nil {
return nil, ErrInternalAppError
} else {
w.pool = pool
}
// Logging
if o.logfn != nil {
whisper.Whisper_log_set(func(level whisper.LogLevel, text string) {
if !o.debug && level > whisper.LogLevelError {
return
}
o.logfn(fmt.Sprintf("[%s] %s", level, strings.TrimSpace(text)))
})
ffmpeg.SetLogging(o.debug, func(text string) {
o.logfn(text)
})
}
// Return success
return w, nil
}
// Release all resources
func (w *Whisper) Close() error {
var result error
// Release pool resources
if w.pool != nil {
result = errors.Join(result, w.pool.Close())
}
// Set all to nil
w.pool = nil
w.store = nil
// Return any errors
return result
}
//////////////////////////////////////////////////////////////////////////////
// STRINGIFY
func (w *Whisper) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {
Store *store.Store `json:"store"`
Pool *pool.ContextPool `json:"pool"`
}{
Store: w.store,
Pool: w.pool,
})
}
func (w *Whisper) String() string {
data, err := json.MarshalIndent(w, "", " ")
if err != nil {
return err.Error()
}
return string(data)
}
//////////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS
// Return all models in the models directory
func (w *Whisper) ListModels() []*schema.Model {
return w.store.List()
}
// Get a model by its Id, returns nil if the model does not exist
func (w *Whisper) GetModelById(id string) *schema.Model {
return w.store.ById(id)
}
// Delete a model by its id
func (w *Whisper) DeleteModelById(id string) error {
model := w.store.ById(id)
if model == nil {
return ErrNotFound.Withf("%q", id)
}
// Empty the pool of this model
if err := w.pool.Drain(model); err != nil {
return err
}
// Delete the model
if err := w.store.Delete(model.Id); err != nil {
return err
}
// Return success
return nil
}
// Download a model by path, where the directory is the root of the model
// within the models directory. The model is returned immediately if it
// already exists in the store
func (w *Whisper) DownloadModel(ctx context.Context, path string, fn func(curBytes, totalBytes uint64)) (*schema.Model, error) {
return w.store.Download(ctx, path, fn)
}
// Get a task for the specified model, which may load the model or
// return an existing one. The context can then be used to run the Transcribe
// function, and after the context is returned to the pool.
func (w *Whisper) WithModel(model *schema.Model, fn func(task *task.Context) error) error {
if model == nil || fn == nil {
return ErrBadParameter
}
// Get a context from the pool
task, err := w.pool.Get(model)
if err != nil {
return err
}
defer w.pool.Put(task)
// Copy parameters
task.CopyParams()
// Execute the function
return fn(task)
}