Skip to content

Commit

Permalink
Reorganized code
Browse files Browse the repository at this point in the history
  • Loading branch information
djthorpe committed Aug 1, 2024
1 parent 68eb4bb commit b83b27c
Show file tree
Hide file tree
Showing 13 changed files with 185 additions and 14 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ generate: mkdir go-tidy

# Make server
server: mkdir generate go-tidy libwhisper libggml
@echo "Building whisper-server"
@PKG_CONFIG_PATH=${ROOT_PATH}/${BUILD_DIR} ${GO} build ${BUILD_FLAGS} -o ${BUILD_DIR}/whisper-server ./cmd/server
@echo "Building whisper"
@PKG_CONFIG_PATH=${ROOT_PATH}/${BUILD_DIR} ${GO} build ${BUILD_FLAGS} -o ${BUILD_DIR}/whisper ./cmd/whisper

# Make cli
cli: mkdir generate go-tidy
Expand Down
2 changes: 1 addition & 1 deletion cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
// Packages
context "github.com/mutablelogic/go-server/pkg/context"
httpserver "github.com/mutablelogic/go-server/pkg/httpserver"
whisper "github.com/mutablelogic/go-whisper/pkg/whisper"
whisper "github.com/mutablelogic/go-whisper"
api "github.com/mutablelogic/go-whisper/pkg/whisper/api"
)

Expand Down
22 changes: 22 additions & 0 deletions cmd/whisper/download.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package main

import (
"log"

// Packages
"github.com/djthorpe/go-tablewriter"
)

type DownloadCmd struct {
Model string `arg:"" help:"Model to download"`
}

func (cmd *DownloadCmd) Run(ctx *Globals) error {
model, err := ctx.service.DownloadModel(ctx.ctx, cmd.Model, func(curBytes, totalBytes uint64) {
log.Printf("Downloaded %d of %d bytes", curBytes, totalBytes)
})
if err != nil {
return err
}
return ctx.writer.Write(model, tablewriter.OptHeader())
}
101 changes: 101 additions & 0 deletions cmd/whisper/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package main

import (
"context"
"os"
"path/filepath"
"syscall"

// Packages
kong "github.com/alecthomas/kong"
tablewriter "github.com/djthorpe/go-tablewriter"
ctx "github.com/mutablelogic/go-server/pkg/context"
whisper "github.com/mutablelogic/go-whisper"
)

type Globals struct {
NoGPU bool `name:"nogpu" help:"Disable GPU acceleration"`
Debug bool `name:"debug" help:"Enable debug output"`
Dir string `name:"dir" help:"Path to model store, uses ${WHISPER_DIR} " default:"${WHISPER_DIR}"`

// Writer, service and context
writer *tablewriter.Writer
service *whisper.Whisper
ctx context.Context
}

type CLI struct {
Globals
Models ModelsCmd `cmd:"models" help:"List models"`
Download DownloadCmd `cmd:"download" help:"Download a model"`
Server ServerCmd `cmd:"models" help:"Run the whisper server"`
}

func main() {
// The name of the executable
name, err := os.Executable()
if err != nil {
panic(err)
} else {
name = filepath.Base(name)
}

// Create a cli parser
cli := CLI{}
cmd := kong.Parse(&cli,
kong.Name(name),
kong.Description("speech transcription and translation service"),
kong.UsageOnError(),
kong.ConfigureHelp(kong.HelpOptions{Compact: true}),
kong.Vars{
"WHISPER_DIR": dirEnvOrDefault(name),
},
)

// Create a whisper server - set options
opts := []whisper.Opt{}
if cli.Globals.Debug {
opts = append(opts, whisper.OptDebug())
}
if cli.Globals.NoGPU {
opts = append(opts, whisper.OptNoGPU())
}

// Create directory if it doesn't exist
if err := os.MkdirAll(cli.Globals.Dir, 0755); err != nil {
cmd.FatalIfErrorf(err)
return
}

// Create a whisper server - create
service, err := whisper.New(cli.Globals.Dir, opts...)
if err != nil {
cmd.FatalIfErrorf(err)
return
} else {
cli.Globals.service = service
}
defer service.Close()

// Create a tablewriter object with text output
writer := tablewriter.New(os.Stdout, tablewriter.OptOutputText())
cli.Globals.writer = writer

// Create a context
cli.Globals.ctx = ctx.ContextForSignal(os.Interrupt, syscall.SIGQUIT)

// Run the command
if err := cmd.Run(&cli.Globals); err != nil {
cmd.FatalIfErrorf(err)
}
}

func dirEnvOrDefault(name string) string {
if dir := os.Getenv("WHISPER_DIR"); dir != "" {
return dir
}
if dir, err := os.UserCacheDir(); err == nil {
return filepath.Join(dir, name)
}
return os.TempDir()
}
12 changes: 12 additions & 0 deletions cmd/whisper/models.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

import (
// Packages
"github.com/djthorpe/go-tablewriter"
)

type ModelsCmd struct{}

func (*ModelsCmd) Run(ctx *Globals) error {
return ctx.writer.Write(ctx.service.ListModels(), tablewriter.OptHeader())
}
37 changes: 37 additions & 0 deletions cmd/whisper/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package main

import (
"log"
"net/http"

// Packages
"github.com/mutablelogic/go-server/pkg/httpserver"
"github.com/mutablelogic/go-whisper/pkg/whisper/api"
)

type ServerCmd struct {
Endpoint string `name:"endpoint" help:"Endpoint for the server" default:"/api/v1"`
Listen string `name:"listen" help:"Listen address for the server" default:"localhost:8080"`
}

func (cmd *ServerCmd) Run(ctx *Globals) error {
// Create a mux for serving requests, then register the endpoints with the mux
mux := http.NewServeMux()

// Register the endpoints
api.RegisterEndpoints(cmd.Endpoint, mux, ctx.service)

// Create a new HTTP server
log.Println("List address", cmd.Listen)
server, err := httpserver.Config{
Listen: cmd.Listen,
Router: mux,
}.New()
if err != nil {
return err
}

// Run the server until CTRL+C
log.Println("Press CTRL+C to exit")
return server.Run(ctx.ctx)
}
File renamed without changes.
2 changes: 1 addition & 1 deletion pkg/whisper/api/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
// Packages
"github.com/mutablelogic/go-server/pkg/httprequest"
"github.com/mutablelogic/go-server/pkg/httpresponse"
"github.com/mutablelogic/go-whisper/pkg/whisper"
"github.com/mutablelogic/go-whisper"
"github.com/mutablelogic/go-whisper/pkg/whisper/schema"
)

Expand Down
2 changes: 1 addition & 1 deletion pkg/whisper/api/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

// Packages
"github.com/mutablelogic/go-server/pkg/httpresponse"
"github.com/mutablelogic/go-whisper/pkg/whisper"
"github.com/mutablelogic/go-whisper"
)

/////////////////////////////////////////////////////////////////////////////
Expand Down
2 changes: 1 addition & 1 deletion pkg/whisper/api/transcribe.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
// Packages
"github.com/mutablelogic/go-server/pkg/httprequest"
"github.com/mutablelogic/go-server/pkg/httpresponse"
"github.com/mutablelogic/go-whisper/pkg/whisper"
"github.com/mutablelogic/go-whisper"
"github.com/mutablelogic/go-whisper/pkg/whisper/schema"
"github.com/mutablelogic/go-whisper/pkg/whisper/segmenter"
"github.com/mutablelogic/go-whisper/pkg/whisper/task"
Expand Down
3 changes: 1 addition & 2 deletions pkg/whisper/pool/contextpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import (
"fmt"

// Packages

"github.com/mutablelogic/go-whisper/pkg/whisper/schema"
schema "github.com/mutablelogic/go-whisper/pkg/whisper/schema"
task "github.com/mutablelogic/go-whisper/pkg/whisper/task"

// Namespace imports
Expand Down
4 changes: 2 additions & 2 deletions pkg/whisper/whisper.go → whisper.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (
"strings"

// Packages
"github.com/mutablelogic/go-media/pkg/ffmpeg"
ffmpeg "github.com/mutablelogic/go-media/pkg/ffmpeg"
model "github.com/mutablelogic/go-whisper/pkg/whisper/model"
pool "github.com/mutablelogic/go-whisper/pkg/whisper/pool"
"github.com/mutablelogic/go-whisper/pkg/whisper/schema"
schema "github.com/mutablelogic/go-whisper/pkg/whisper/schema"
task "github.com/mutablelogic/go-whisper/pkg/whisper/task"
whisper "github.com/mutablelogic/go-whisper/sys/whisper"

Expand Down
8 changes: 4 additions & 4 deletions pkg/whisper/whisper_test.go → whisper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

// Packages
wav "github.com/go-audio/wav"
whisper "github.com/mutablelogic/go-whisper/pkg/whisper"
whisper "github.com/mutablelogic/go-whisper"
task "github.com/mutablelogic/go-whisper/pkg/whisper/task"
assert "github.com/stretchr/testify/assert"

Expand All @@ -17,9 +17,9 @@ import (
)

const MODEL_TINY = "ggml-tiny.en-q5_1.bin"
const SAMPLE_EN = "../../samples/jfk.wav"
const SAMPLE_FR = "../../samples/OlivierL.wav"
const SAMPLE_DE = "../../samples/ge-podcast.wav"
const SAMPLE_EN = "samples/jfk.wav"
const SAMPLE_FR = "samples/OlivierL.wav"
const SAMPLE_DE = "samples/ge-podcast.wav"

func Test_whisper_001(t *testing.T) {
assert := assert.New(t)
Expand Down

0 comments on commit b83b27c

Please sign in to comment.