Skip to content

Commit 326b41a

Browse files
committed
Updated build
1 parent e0004fb commit 326b41a

File tree

14 files changed

+112
-55
lines changed

14 files changed

+112
-55
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.22
44

55
require (
66
github.com/alecthomas/kong v0.9.0
7+
github.com/djthorpe/go-errors v1.0.3
78
github.com/djthorpe/go-tablewriter v0.0.7
89
github.com/go-audio/wav v1.1.0
910
github.com/mutablelogic/go-client v1.0.8
@@ -13,7 +14,6 @@ require (
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
1919
github.com/mattn/go-runewidth v0.0.15 // indirect

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 cuda pkg-config: libcuda
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 "-lgomp1" libwhisper-linux.pc

sys/whisper/logging.go

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

1010
/*
1111
#cgo pkg-config: libwhisper
12-
#cgo darwin pkg-config: libwhisper-darwin
13-
#cgo linux pkg-config: libwhisper-linux
14-
1512
#include <whisper.h>
1613
#include <stdlib.h>
1714

0 commit comments

Comments
 (0)