Skip to content

Commit 09ada37

Browse files
authored
Merge pull request #34 from mutablelogic/v1
V1
2 parents 9b3dc74 + 0fb4c49 commit 09ada37

File tree

15 files changed

+130
-61
lines changed

15 files changed

+130
-61
lines changed

Makefile

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,31 @@ DOCKER_REGISTRY ?= ghcr.io/mutablelogic
1313
BUILD_TAG := ${DOCKER_REGISTRY}/go-whisper-${OS}-${ARCH}:${VERSION}
1414
ROOT_PATH := $(CURDIR)
1515
BUILD_DIR := build
16+
BUILD_FLAGS = -ldflags "-s -w $(BUILD_LD_FLAGS)"
17+
18+
# If GGML_CUDA is set, then add a cuda tag for the go ${BUILD FLAGS}
19+
ifeq ($(GGML_CUDA),1)
20+
BUILD_FLAGS += -tags cuda
21+
CUDA_DOCKER_ARCH ?= all
22+
endif
1623

1724
# Targets
1825
all: build server cli
1926

27+
# Generate the pkg-config files
28+
generate: mkdir go-tidy
29+
@echo "Generating pkg-config"
30+
@PKG_CONFIG_PATH=${ROOT_PATH}/${BUILD_DIR} go generate ./sys/whisper
31+
2032
# Make server
21-
server: mkdir go-tidy libwhisper libggml
33+
server: mkdir generate go-tidy libwhisper libggml
2234
@echo "Building whisper-server"
23-
@CGO_CFLAGS="-I${ROOT_PATH}/third_party/whisper.cpp/include -I${ROOT_PATH}/third_party/whisper.cpp/ggml/include" \
24-
CGO_LDFLAGS="-L${ROOT_PATH}/third_party/whisper.cpp" \
25-
${GO} build -o ${BUILD_DIR}/whisper-server ./cmd/server
35+
@PKG_CONFIG_PATH=${ROOT_PATH}/${BUILD_DIR} ${GO} build ${BUILD_FLAGS} -o ${BUILD_DIR}/whisper-server ./cmd/server
2636

2737
# Make cli
28-
cli: mkdir go-tidy libwhisper libggml
38+
cli: mkdir generate go-tidy libwhisper libggml
2939
@echo "Building whisper-cli"
30-
@CGO_CFLAGS="-I${ROOT_PATH}/third_party/whisper.cpp/include -I${ROOT_PATH}/third_party/whisper.cpp/ggml/include" \
31-
CGO_LDFLAGS="-L${ROOT_PATH}/third_party/whisper.cpp" \
32-
${GO} build -o ${BUILD_DIR}/whisper-cli ./cmd/cli
40+
@PKG_CONFIG_PATH=${ROOT_PATH}/${BUILD_DIR} ${GO} build ${BUILD_FLAGS} -o ${BUILD_DIR}/whisper-cli ./cmd/cli
3341

3442
# Build docker container
3543
docker: docker-dep submodule

cmd/server/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import (
1313
// Packages
1414
context "github.com/mutablelogic/go-server/pkg/context"
1515
httpserver "github.com/mutablelogic/go-server/pkg/httpserver"
16-
api "github.com/mutablelogic/go-whisper/pkg/api"
1716
whisper "github.com/mutablelogic/go-whisper/pkg/whisper"
17+
api "github.com/mutablelogic/go-whisper/pkg/whisper/api"
1818
sys "github.com/mutablelogic/go-whisper/sys/whisper"
1919
)
2020

go.mod

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,22 @@ go 1.22
44

55
require (
66
github.com/alecthomas/kong v0.9.0
7-
github.com/djthorpe/go-tablewriter v0.0.7
7+
github.com/djthorpe/go-errors v1.0.3
8+
github.com/djthorpe/go-tablewriter v0.0.8
89
github.com/go-audio/wav v1.1.0
9-
github.com/mutablelogic/go-client v1.0.8
10-
github.com/mutablelogic/go-server v1.4.11
10+
github.com/mutablelogic/go-client v1.0.9
11+
github.com/mutablelogic/go-server v1.4.13
1112
github.com/stretchr/testify v1.9.0
1213
)
1314

1415
require (
1516
github.com/davecgh/go-spew v1.1.1 // indirect
16-
github.com/djthorpe/go-errors v1.0.3 // indirect
1717
github.com/go-audio/audio v1.0.0 // indirect
1818
github.com/go-audio/riff v1.0.0 // indirect
19-
github.com/mattn/go-runewidth v0.0.15 // indirect
19+
github.com/mattn/go-runewidth v0.0.16 // indirect
2020
github.com/pmezard/go-difflib v1.0.0 // indirect
2121
github.com/rivo/uniseg v0.4.7 // indirect
22-
golang.org/x/sys v0.21.0 // indirect
23-
golang.org/x/term v0.21.0 // indirect
22+
golang.org/x/sys v0.22.0 // indirect
23+
golang.org/x/term v0.22.0 // indirect
2424
gopkg.in/yaml.v3 v3.0.1 // indirect
2525
)

go.sum

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ github.com/djthorpe/go-errors v1.0.3 h1:GZeMPkC1mx2vteXLI/gvxZS0Ee9zxzwD1mcYyKU5
1010
github.com/djthorpe/go-errors v1.0.3/go.mod h1:HtfrZnMd6HsX75Mtbv9Qcnn0BqOrrFArvCaj3RMnZhY=
1111
github.com/djthorpe/go-tablewriter v0.0.7 h1:jnNsJDjjLLCt0OAqB5DzGZN7V3beT1IpNMQ8GcOwZDU=
1212
github.com/djthorpe/go-tablewriter v0.0.7/go.mod h1:NVBvytpL+6fHfCKn0+3lSi15/G3A1HWf2cLNeHg6YBg=
13+
github.com/djthorpe/go-tablewriter v0.0.8 h1:uRhB9XVgK1n9tvVS7KMyxhxxGGtDvqC80toDTpW4DB4=
14+
github.com/djthorpe/go-tablewriter v0.0.8/go.mod h1:NVBvytpL+6fHfCKn0+3lSi15/G3A1HWf2cLNeHg6YBg=
1315
github.com/go-audio/audio v1.0.0 h1:zS9vebldgbQqktK4H0lUqWrG8P0NxCJVqcj7ZpNnwd4=
1416
github.com/go-audio/audio v1.0.0/go.mod h1:6uAu0+H2lHkwdGsAY+j2wHPNPpPoeg5AaEFh9FlA+Zs=
1517
github.com/go-audio/riff v1.0.0 h1:d8iCGbDvox9BfLagY94fBynxSPHO80LmZCaOsmKxokA=
@@ -20,10 +22,16 @@ github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUq
2022
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
2123
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
2224
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
25+
github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc=
26+
github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
2327
github.com/mutablelogic/go-client v1.0.8 h1:A3QtP0wdf+W3dE5k7dobwGYqqn4ZpIqRFu+h9vPoy7Y=
2428
github.com/mutablelogic/go-client v1.0.8/go.mod h1:aP9ecBd4R/acJEJSyp81U3mey9W3AHQV/G1XzfcrLx0=
29+
github.com/mutablelogic/go-client v1.0.9 h1:Eh4sjQOFDldP/L3IizqkcOD3WigZR+u1VaHTUM4ujYw=
30+
github.com/mutablelogic/go-client v1.0.9/go.mod h1:VLyB8j8IBJSK/FXvvqhmq93PRWDKkyLu8R7V2Vudb6A=
2531
github.com/mutablelogic/go-server v1.4.11 h1:feI9IyuK6pv7Gi7fbExfU51uDRMfQo0U9wo0vWN2wf8=
2632
github.com/mutablelogic/go-server v1.4.11/go.mod h1:9nenPAohKu8bFoRgwHJh+3s8h0kLFjUAb8KZvT1TQNU=
33+
github.com/mutablelogic/go-server v1.4.13 h1:k5LJJ/pCvyiw34UX341vRhliBOS6i7V65U/UICcOJOA=
34+
github.com/mutablelogic/go-server v1.4.13/go.mod h1:9nenPAohKu8bFoRgwHJh+3s8h0kLFjUAb8KZvT1TQNU=
2735
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
2836
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
2937
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
@@ -35,8 +43,12 @@ golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 h1:vr/HnozRka3pE4EsMEg1lgkXJ
3543
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842/go.mod h1:XtvwrStGgqGPLc4cjQfWqZHG1YFdYs6swckp8vpsjnc=
3644
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
3745
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
46+
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
47+
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
3848
golang.org/x/term v0.21.0 h1:WVXCp+/EBEHOj53Rvu+7KiT/iElMrO8ACK16SMZ3jaA=
3949
golang.org/x/term v0.21.0/go.mod h1:ooXLefLobQVslOqselCNF4SxFAaoS6KujMbsGzSDmX0=
50+
golang.org/x/term v0.22.0 h1:BbsgPEJULsl2fV/AT3v15Mjva5yXKQDyKf+TbDz7QJk=
51+
golang.org/x/term v0.22.0/go.mod h1:F3qCibpT5AMpCRfhfT53vVJwhLtIVHhB9XDjfFvnMI4=
4052
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
4153
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
4254
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

pkg/client/client.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/mutablelogic/go-client/pkg/multipart"
1313
"github.com/mutablelogic/go-server/pkg/httprequest"
1414
"github.com/mutablelogic/go-whisper/pkg/whisper"
15+
"github.com/mutablelogic/go-whisper/pkg/whisper/model"
1516
)
1617

1718
///////////////////////////////////////////////////////////////////////////////
@@ -37,9 +38,9 @@ func New(endpoint string, opts ...client.ClientOpt) (*Client, error) {
3738
///////////////////////////////////////////////////////////////////////////////
3839
// MODELS
3940

40-
func (c *Client) ListModels(ctx context.Context) ([]whisper.Model, error) {
41+
func (c *Client) ListModels(ctx context.Context) ([]model.Model, error) {
4142
var models struct {
42-
Models []whisper.Model `json:"models"`
43+
Models []model.Model `json:"models"`
4344
}
4445
if err := c.DoWithContext(ctx, client.MethodGet, &models, client.OptPath("models")); err != nil {
4546
return nil, err
@@ -52,12 +53,12 @@ func (c *Client) DeleteModel(ctx context.Context, model string) error {
5253
return c.DoWithContext(ctx, client.MethodDelete, nil, client.OptPath("models", model))
5354
}
5455

55-
func (c *Client) DownloadModel(ctx context.Context, path string, fn func(status string, cur, total int64)) (whisper.Model, error) {
56+
func (c *Client) DownloadModel(ctx context.Context, path string, fn func(status string, cur, total int64)) (model.Model, error) {
5657
var req struct {
5758
Path string `json:"path"`
5859
}
5960
type resp struct {
60-
whisper.Model
61+
model.Model
6162
Status string `json:"status"`
6263
Total int64 `json:"total,omitempty"`
6364
Completed int64 `json:"completed,omitempty"`
@@ -74,7 +75,7 @@ func (c *Client) DownloadModel(ctx context.Context, path string, fn func(status
7475

7576
var r resp
7677
if payload, err := client.NewJSONRequest(req); err != nil {
77-
return whisper.Model{}, err
78+
return model.Model{}, err
7879
} else if err := c.DoWithContext(ctx, payload, &r,
7980
client.OptPath("models"),
8081
client.OptQuery(query),
@@ -86,7 +87,7 @@ func (c *Client) DownloadModel(ctx context.Context, path string, fn func(status
8687
return nil
8788
}),
8889
); err != nil {
89-
return whisper.Model{}, err
90+
return model.Model{}, err
9091
}
9192

9293
// Return success

pkg/whisper/api/models.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@ import (
1313
"github.com/mutablelogic/go-server/pkg/httprequest"
1414
"github.com/mutablelogic/go-server/pkg/httpresponse"
1515
"github.com/mutablelogic/go-whisper/pkg/whisper"
16+
"github.com/mutablelogic/go-whisper/pkg/whisper/model"
1617
)
1718

1819
///////////////////////////////////////////////////////////////////////////////
1920
// TYPES
2021

2122
type respModels struct {
22-
Object string `json:"object,omitempty"`
23-
Models []*whisper.Model `json:"models"`
23+
Object string `json:"object,omitempty"`
24+
Models []*model.Model `json:"models"`
2425
}
2526

2627
type reqDownloadModel struct {
@@ -77,7 +78,7 @@ func DownloadModel(ctx context.Context, w http.ResponseWriter, r *http.Request,
7778

7879
// Download the model
7980
t := time.Now()
80-
model, err := service.DownloadModel(ctx, req.Name(), req.DestPath(), func(curBytes, totalBytes uint64) {
81+
model, err := service.DownloadModel(ctx, req.Name(), func(curBytes, totalBytes uint64) {
8182
if time.Since(t) > time.Second && query.Stream {
8283
t = time.Now()
8384
json.NewEncoder(w).Encode(respDownloadModelStatus{

pkg/whisper/api/transcribe.go

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,18 @@ package api
33
import (
44
"context"
55
"fmt"
6-
"log"
76
"mime/multipart"
87
"net/http"
98

109
// Packages
11-
"github.com/go-audio/wav"
10+
1211
"github.com/mutablelogic/go-server/pkg/httprequest"
1312
"github.com/mutablelogic/go-server/pkg/httpresponse"
1413
"github.com/mutablelogic/go-whisper/pkg/whisper"
14+
"github.com/mutablelogic/go-whisper/pkg/whisper/task"
15+
16+
// Namespace imports
17+
. "github.com/djthorpe/go-errors"
1518
)
1619

1720
///////////////////////////////////////////////////////////////////////////////
@@ -42,8 +45,6 @@ func TranscribeFile(ctx context.Context, service *whisper.Whisper, w http.Respon
4245
return
4346
}
4447

45-
log.Println(req)
46-
4748
// Get the model
4849
model := service.GetModelById(req.Model)
4950
if model == nil {
@@ -66,17 +67,15 @@ func TranscribeFile(ctx context.Context, service *whisper.Whisper, w http.Respon
6667
defer f.Close()
6768

6869
// Read samples
69-
buf, err := wav.NewDecoder(f).FullPCMBuffer()
70-
if err != nil {
71-
httpresponse.Error(w, http.StatusInternalServerError, err.Error())
72-
return
73-
}
70+
//buf, err := wav.NewDecoder(f).FullPCMBuffer()
71+
//if err != nil {
72+
// httpresponse.Error(w, http.StatusInternalServerError, err.Error())
73+
// return
74+
//}
7475

7576
// Get context for the model, perform transcription
7677
var result *whisper.Transcription
77-
if err := service.WithModelContext(model, func(ctx *whisper.Context) error {
78-
var err error
79-
78+
if err := service.WithModel(model, func(ctx *task.Context) error {
8079
// Set parameters for transcription & translation, default to english
8180
ctx.SetTranslate(translate)
8281
if req.Language != nil {
@@ -90,16 +89,17 @@ func TranscribeFile(ctx context.Context, service *whisper.Whisper, w http.Respon
9089
}
9190

9291
// Set prompt and temperature
93-
if req.Prompt != nil {
94-
ctx.SetPrompt(*req.Prompt)
95-
}
96-
if req.Temperature != nil {
97-
ctx.SetTemperature(*req.Temperature)
98-
}
99-
92+
/*
93+
if req.Prompt != nil {
94+
ctx.SetPrompt(*req.Prompt)
95+
}
96+
if req.Temperature != nil {
97+
ctx.SetTemperature(*req.Temperature)
98+
}
99+
*/
100100
// Perform the transcription, return any errors
101-
result, err = service.Transcribe(ctx, buf.AsFloat32Buffer().Data)
102-
return err
101+
//result, err = service.Transcribe(ctx, buf.AsFloat32Buffer().Data)
102+
return ErrNotImplemented
103103
}); err != nil {
104104
httpresponse.Error(w, http.StatusBadRequest, err.Error())
105105
return

sys/pkg-config/main.go

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ package main
33
import (
44
"flag"
55
"fmt"
6+
"os"
7+
"path/filepath"
68
)
79

810
var (
11+
flagDir = flag.String("dir", "${PKG_CONFIG_PATH}", "Destination directory")
912
flagPrefix = flag.String("prefix", "", "Prefix for the package")
1013
flagVersion = flag.String("version", "", "Version for the package")
1114
flagDescription = flag.String("description", "", "Description for the package")
@@ -15,5 +18,46 @@ var (
1518

1619
func main() {
1720
flag.Parse()
18-
fmt.Println("generate:", flag.Args())
21+
if flag.NArg() != 1 {
22+
fmt.Fprintln(os.Stderr, "Missing filename")
23+
os.Exit(-1)
24+
}
25+
dest := filepath.Join(os.ExpandEnv(*flagDir), flag.Arg(0))
26+
27+
var prefix string
28+
if *flagPrefix != "" {
29+
var err error
30+
prefix, err = filepath.Abs(*flagPrefix)
31+
if err != nil {
32+
fmt.Fprintln(os.Stderr, err)
33+
os.Exit(-1)
34+
}
35+
}
36+
37+
w, err := os.Create(dest)
38+
if err != nil {
39+
fmt.Fprintln(os.Stderr, err)
40+
os.Exit(-1)
41+
}
42+
defer w.Close()
43+
44+
// Write the package
45+
if prefix != "" {
46+
fmt.Fprintf(w, "prefix=%s\n\n", prefix)
47+
}
48+
fmt.Fprintf(w, "Name: %s\n", filepath.Base(dest))
49+
if *flagDescription != "" {
50+
fmt.Fprintf(w, "Description: %s\n", *flagDescription)
51+
} else {
52+
fmt.Fprintf(w, "Description: No description\n")
53+
}
54+
if *flagVersion != "" {
55+
fmt.Fprintf(w, "Version: %s\n", *flagVersion)
56+
}
57+
if *flagCompileFlags != "" {
58+
fmt.Fprintf(w, "Cflags: %s\n", *flagCompileFlags)
59+
}
60+
if *flagLinkerFlags != "" {
61+
fmt.Fprintf(w, "Libs: %s\n", *flagLinkerFlags)
62+
}
1963
}

sys/whisper/fullparams.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ import (
1111

1212
/*
1313
#cgo pkg-config: libwhisper
14-
#cgo darwin pkg-config: libwhisper-darwin
15-
#cgo linux pkg-config: libwhisper-linux
16-
1714
#include <whisper.h>
1815
#include <stdio.h>
1916
#include <stdbool.h>

sys/whisper/generate.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
package whisper
22

3+
///////////////////////////////////////////////////////////////////////////////
4+
// CGO
5+
6+
/*
7+
#cgo pkg-config: libwhisper
8+
#cgo darwin pkg-config: libwhisper-darwin
9+
#cgo linux pkg-config: libwhisper-linux
10+
#cgo arm64 pkg-config: cuda-12.2 cublas-12.2 cudart-12.2
11+
*/
12+
import "C"
13+
314
// Generate the whisper pkg-config files
4-
//go:generate go run ../pkg-config libwhisper.pc
5-
//go:generate go run ../pkg-config libwhisper-darwin.pc
6-
//go:generate go run ../pkg-config libwhisper-linux.pc
15+
// Setting the prefix to the base of the repository
16+
//go:generate go run ../pkg-config --version "0.0.0" --prefix "../.." --cflags "-I$DOLLAR{prefix}/third_party/whisper.cpp/include -I$DOLLAR{prefix}/third_party/whisper.cpp/ggml/include" --libs "-L$DOLLAR{prefix}/third_party/whisper.cpp -lwhisper -lggml -lm -lstdc++" libwhisper.pc
17+
//go:generate go run ../pkg-config --version "0.0.0" --libs "-framework Accelerate -framework Metal -framework Foundation -framework CoreGraphics" libwhisper-darwin.pc
18+
//go:generate go run ../pkg-config --version "0.0.0" --libs "-lgomp" libwhisper-linux.pc

0 commit comments

Comments
 (0)