Skip to content

Commit

Permalink
Updated whisper
Browse files Browse the repository at this point in the history
  • Loading branch information
djthorpe committed Aug 10, 2024
1 parent fbcd0cf commit 3e5ad11
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 7 deletions.
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ ifeq ($(GGML_CUDA),1)
endif

# Targets
all: whisper
all: whisper api

# Generate the pkg-config files
generate: mkdir go-tidy
Expand All @@ -42,6 +42,11 @@ whisper: mkdir generate go-tidy libwhisper libggml
@echo "Building whisper"
@PKG_CONFIG_PATH=${ROOT_PATH}/${BUILD_DIR} ${GO} build ${BUILD_FLAGS} -o ${BUILD_DIR}/whisper ./cmd/whisper

# Make api
api: mkdir go-tidy
@echo "Building api"
@${GO} build ${BUILD_FLAGS} -o ${BUILD_DIR}/api ./cmd/api

# Build docker container
docker: docker-dep submodule
@echo build docker image: ${BUILD_TAG} for ${OS}/${ARCH}
Expand Down
101 changes: 101 additions & 0 deletions cmd/api/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"
opt "github.com/mutablelogic/go-client"
ctx "github.com/mutablelogic/go-server/pkg/context"
client "github.com/mutablelogic/go-whisper/pkg/client"
)

////////////////////////////////////////////////////////////////////////////////
// TYPES

type Globals struct {
Url string `name:"url" help:"URL of whisper service (can be set from WHISPER_URL env)" default:"${WHISPER_URL}"`
Debug bool `name:"debug" help:"Enable debug output"`

// Writer, service and context
writer *tablewriter.Writer
client *client.Client
ctx context.Context
}

type CLI struct {
Globals

Ping PingCmd `cmd help:"Ping the whisper service"`
}

////////////////////////////////////////////////////////////////////////////////
// GLOBALS

const (
defaultEndpoint = "http://localhost:8080/api/v1"
)

////////////////////////////////////////////////////////////////////////////////
// MAIN

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 client"),
kong.UsageOnError(),
kong.ConfigureHelp(kong.HelpOptions{Compact: true}),
kong.Vars{
"WHISPER_URL": envOrDefault("WHISPER_URL", defaultEndpoint),
},
)

// Set whisper client options
opts := []opt.ClientOpt{}
if cli.Globals.Debug {
opts = append(opts, opt.OptTrace(os.Stderr, true))
}

// Create a whisper client
client, err := client.New(cli.Globals.Url, opts...)
if err != nil {
cmd.FatalIfErrorf(err)
return
} else {
cli.Globals.client = client
}

// 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 envOrDefault(name, def string) string {
if value := os.Getenv(name); value != "" {
return value
} else {
return def
}
}
10 changes: 10 additions & 0 deletions cmd/api/ping.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package main

type PingCmd struct{}

func (cmd *PingCmd) Run(ctx *Globals) error {
if err := ctx.client.Ping(ctx.ctx); err != nil {
return err
}
return ctx.writer.Write("OK")
}
3 changes: 2 additions & 1 deletion cmd/whisper/transcribe.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ func (cmd *TranscribeCmd) Run(ctx *Globals) error {
defer f.Close()

// Create a segmenter - read segments based on requested segment size
segmenter, err := segmenter.New(f, 0, whisper.SampleRate)
// TODO
segmenter, err := segmenter.NewReader(f, 0, whisper.SampleRate)
if err != nil {
return err
}
Expand Down
8 changes: 4 additions & 4 deletions etc/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ARG BASE_TAG=0.0.10-4-g6421fd2
ARG BASE_TAG=1.0.0
ARG BASE_DEV_CONTAINER=ghcr.io/mutablelogic/cuda-dev:${BASE_TAG}
ARG BASE_RUN_CONTAINER=ghcr.io/mutablelogic/cuda-rt:${BASE_TAG}
ARG CUDA_DOCKER_ARCH=all
Expand All @@ -14,7 +14,7 @@ ARG ARCH
ARG OS

RUN apt-get -y update \
&& apt-get -y install software-properties-common curl libgomp1 \
&& apt-get -y install software-properties-common curl \
&& add-apt-repository -y ppa:ubuntuhandbook1/ffmpeg6 \
&& apt-get -y update \
&& apt-get -y install libavformat-dev libavcodec-dev libavdevice-dev libavfilter-dev libavutil-dev libswscale-dev libswresample-dev
Expand All @@ -35,12 +35,12 @@ RUN make -j$(nproc)
# Setup runtime container
FROM ${BASE_RUN_CONTAINER} AS runtime
RUN apt-get -y update \
&& apt-get -y install software-properties-common libgomp1 \
&& apt-get -y install software-properties-common \
&& add-apt-repository -y ppa:ubuntuhandbook1/ffmpeg6 \
&& apt-get -y update \
&& apt-get -y install libavformat60 libavcodec60 libavdevice60 libavfilter9 libavutil58 libswscale7 libswresample4
COPY --from=build --chmod=755 /app/build/whisper /usr/local/bin/whisper
COPY --from=build /app/build/whisper /usr/local/bin/whisper
COPY --from=build --chmod=755 /app/build/api /usr/local/bin/api
COPY --chmod=755 etc/entrypoint.sh .

# Entrypoint when running the server
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/transcribe.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func TranscribeFile(ctx context.Context, service *whisper.Whisper, w http.Respon
defer f.Close()

// Create a segmenter - read segments based on requested segment size
segmenter, err := segmenter.New(f, req.SegmentDur(), whisper.SampleRate)
segmenter, err := segmenter.NewReader(f, req.SegmentDur(), whisper.SampleRate)
if err != nil {
httpresponse.Error(w, http.StatusBadRequest, err.Error())
return
Expand Down
7 changes: 7 additions & 0 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ func New(endpoint string, opts ...client.ClientOpt) (*Client, error) {
}
}

///////////////////////////////////////////////////////////////////////////////
// PING

func (c *Client) Ping(ctx context.Context) error {
return c.DoWithContext(ctx, client.MethodGet, nil, client.OptPath("health"))
}

///////////////////////////////////////////////////////////////////////////////
// MODELS

Expand Down

0 comments on commit 3e5ad11

Please sign in to comment.