Skip to content

Commit 315af70

Browse files
authored
Merge pull request #31 from mutablelogic/v1
Reorganized bindings and service
2 parents b0fc36d + 7b42a9b commit 315af70

32 files changed

+2286
-1276
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ server: mkdir go-tidy libwhisper libggml
2525
${GO} build -o ${BUILD_DIR}/whisper-server ./cmd/server
2626

2727
# Make cli
28-
cli: mkdir go-tidy
28+
cli: mkdir go-tidy libwhisper libggml
2929
@echo "Building whisper-cli"
3030
@CGO_CFLAGS="-I${ROOT_PATH}/third_party/whisper.cpp/include -I${ROOT_PATH}/third_party/whisper.cpp/ggml/include" \
3131
CGO_LDFLAGS="-L${ROOT_PATH}/third_party/whisper.cpp" \

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ require (
77
github.com/djthorpe/go-tablewriter v0.0.7
88
github.com/go-audio/wav v1.1.0
99
github.com/mutablelogic/go-client v1.0.8
10-
github.com/mutablelogic/go-server v1.4.10
10+
github.com/mutablelogic/go-server v1.4.11
1111
github.com/stretchr/testify v1.9.0
1212
)
1313

go.sum

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,17 @@ github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZ
2222
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
2323
github.com/mutablelogic/go-client v1.0.8 h1:A3QtP0wdf+W3dE5k7dobwGYqqn4ZpIqRFu+h9vPoy7Y=
2424
github.com/mutablelogic/go-client v1.0.8/go.mod h1:aP9ecBd4R/acJEJSyp81U3mey9W3AHQV/G1XzfcrLx0=
25-
github.com/mutablelogic/go-server v1.4.10 h1:iItbUmynlfsIpRHjZXXGJHnYCHFmRgokkn/DIbInR6k=
26-
github.com/mutablelogic/go-server v1.4.10/go.mod h1:q5zurZQ/DcdGnssptGXxtady5o/ZdhL/5VPnKKED1J0=
25+
github.com/mutablelogic/go-server v1.4.11 h1:feI9IyuK6pv7Gi7fbExfU51uDRMfQo0U9wo0vWN2wf8=
26+
github.com/mutablelogic/go-server v1.4.11/go.mod h1:9nenPAohKu8bFoRgwHJh+3s8h0kLFjUAb8KZvT1TQNU=
2727
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
2828
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
2929
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
3030
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
3131
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
3232
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
3333
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
34+
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 h1:vr/HnozRka3pE4EsMEg1lgkXJkTFJCVUX+S/ZT6wYzM=
35+
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842/go.mod h1:XtvwrStGgqGPLc4cjQfWqZHG1YFdYs6swckp8vpsjnc=
3436
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
3537
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
3638
golang.org/x/term v0.21.0 h1:WVXCp+/EBEHOj53Rvu+7KiT/iElMrO8ACK16SMZ3jaA=

pkg/whisper/context/context.go

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package pool
2+
3+
import (
4+
5+
// Packages
6+
7+
"path/filepath"
8+
9+
model "github.com/mutablelogic/go-whisper/pkg/whisper/model"
10+
"github.com/mutablelogic/go-whisper/sys/whisper"
11+
12+
// Namespace imports
13+
. "github.com/djthorpe/go-errors"
14+
)
15+
16+
//////////////////////////////////////////////////////////////////////////////
17+
// TYPES
18+
19+
// Context is used for running the transcription or translation
20+
type Context struct {
21+
Model *model.Model
22+
23+
whisper *whisper.Context
24+
params whisper.FullParams
25+
}
26+
27+
//////////////////////////////////////////////////////////////////////////////
28+
// LIFECYCLE
29+
30+
// Create a new context object
31+
func New(path string, model *model.Model) (*Context, error) {
32+
ctx := new(Context)
33+
34+
// Init the context
35+
if err := ctx.Init(path, model); err != nil {
36+
return nil, err
37+
}
38+
39+
// Init the transcription with default parameters
40+
ctx.params = whisper.DefaultFullParams(whisper.SAMPLING_GREEDY)
41+
42+
// Return success
43+
return ctx, nil
44+
}
45+
46+
// Init the context
47+
func (m *Context) Init(path string, model *model.Model) error {
48+
// Check parameters
49+
if model == nil {
50+
return ErrBadParameter
51+
}
52+
53+
// Get a context
54+
ctx := whisper.Whisper_init_from_file_with_params(filepath.Join(path, model.Path), whisper.DefaultContextParams())
55+
if ctx == nil {
56+
return ErrInternalAppError.With("whisper_init_from_file_with_params")
57+
}
58+
59+
// Set resources
60+
m.whisper = ctx
61+
m.Model = model
62+
63+
// Return success
64+
return nil
65+
}
66+
67+
// Close the context and release all resources
68+
func (m *Context) Close() error {
69+
var result error
70+
71+
// Do nothing if nil
72+
if m == nil {
73+
return nil
74+
}
75+
76+
// Release resources
77+
if m.whisper != nil {
78+
whisper.Whisper_free(m.whisper)
79+
}
80+
m.whisper = nil
81+
m.Model = nil
82+
83+
// Return any errors
84+
return result
85+
}
86+
87+
//////////////////////////////////////////////////////////////////////////////
88+
// PUBLIC METHODS
89+
90+
// Transcribe samples. The samples should be 16KHz float32 samples in
91+
// a single channel.
92+
// TODO: We need a low-latency streaming version of this function.
93+
// TODO: We need a callback for segment progress.
94+
func (ctx *Context) Transcribe(samples []float32) error {
95+
// Perform the transcription
96+
return whisper.Whisper_full(ctx.whisper, ctx.params, samples)
97+
}
File renamed without changes.

pkg/whisper/model.go

Lines changed: 0 additions & 158 deletions
This file was deleted.

pkg/whisper/model/model.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package model
2+
3+
import (
4+
"encoding/json"
5+
)
6+
7+
//////////////////////////////////////////////////////////////////////////////
8+
// TYPES
9+
10+
type Model struct {
11+
Id string `json:"id" writer:",width:28,wrap"`
12+
Object string `json:"object,omitempty" writer:"-"`
13+
Path string `json:"path,omitempty" writer:",width:40,wrap"`
14+
Created int64 `json:"created,omitempty"`
15+
OwnedBy string `json:"owned_by,omitempty"`
16+
}
17+
18+
//////////////////////////////////////////////////////////////////////////////
19+
// STRINGIFY
20+
21+
func (m *Model) String() string {
22+
data, err := json.MarshalIndent(m, "", " ")
23+
if err != nil {
24+
return err.Error()
25+
}
26+
return string(data)
27+
}

0 commit comments

Comments
 (0)