Skip to content

Commit

Permalink
Merge pull request #61 from mutablelogic/v1
Browse files Browse the repository at this point in the history
Updated api
  • Loading branch information
djthorpe authored Aug 10, 2024
2 parents a76d738 + 317ef01 commit d4b7fdc
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 20 deletions.
12 changes: 12 additions & 0 deletions cmd/api/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

type DeleteCmd struct {
Model string `arg:"" name:"model" help:"Model to delete"`
}

func (cmd *DeleteCmd) Run(ctx *Globals) error {
if err := ctx.client.DeleteModel(ctx.ctx, cmd.Model); err != nil {
return err
}
return nil
}
22 changes: 22 additions & 0 deletions cmd/api/download.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package main

import (
"fmt"

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

type DownloadCmd struct {
Model string `arg:"" name:"model" help:"Model to download (must end in .bin)"`
}

func (cmd *DownloadCmd) Run(ctx *Globals) error {
model, err := ctx.client.DownloadModel(ctx.ctx, cmd.Model, func(status string, cur, total int64) {
pct := fmt.Sprintf("%02d%%", int(100*float64(cur)/float64(total)))
ctx.writer.Writeln(pct, status)
})
if err != nil {
return err
}
return ctx.writer.Write(model, tablewriter.OptHeader())
}
5 changes: 4 additions & 1 deletion cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ type Globals struct {
type CLI struct {
Globals

Ping PingCmd `cmd help:"Ping the whisper service"`
Ping PingCmd `cmd:"ping" help:"Ping the whisper service"`
Models ModelsCmd `cmd:"models" help:"List models"`
Download DownloadCmd `cmd:"download" help:"Download a model"`
Delete DeleteCmd `cmd:"delete" help:"Delete a model"`
}

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

import "github.com/djthorpe/go-tablewriter"

type ModelsCmd struct{}

func (cmd *ModelsCmd) Run(ctx *Globals) error {
if models, err := ctx.client.ListModels(ctx.ctx); err != nil {
return err
} else {
return ctx.writer.Write(models, tablewriter.OptHeader())
}
}
2 changes: 1 addition & 1 deletion cmd/api/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ func (cmd *PingCmd) Run(ctx *Globals) error {
if err := ctx.client.Ping(ctx.ctx); err != nil {
return err
}
return ctx.writer.Write("OK")
return ctx.writer.Writeln("OK")
}
10 changes: 5 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ go 1.22
require (
github.com/alecthomas/kong v0.9.0
github.com/djthorpe/go-errors v1.0.3
github.com/djthorpe/go-tablewriter v0.0.8
github.com/djthorpe/go-tablewriter v0.0.10
github.com/go-audio/wav v1.1.0
github.com/mutablelogic/go-client v1.0.9
github.com/mutablelogic/go-media v1.6.11
github.com/mutablelogic/go-media v1.6.12
github.com/mutablelogic/go-server v1.4.15
github.com/stretchr/testify v1.9.0
)
Expand All @@ -20,8 +20,8 @@ require (
github.com/mattn/go-runewidth v0.0.16 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/term v0.22.0 // indirect
golang.org/x/exp v0.0.0-20240808152545-0cdaa3abc0fa // indirect
golang.org/x/sys v0.24.0 // indirect
golang.org/x/term v0.23.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
20 changes: 10 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/djthorpe/go-errors v1.0.3 h1:GZeMPkC1mx2vteXLI/gvxZS0Ee9zxzwD1mcYyKU5jD0=
github.com/djthorpe/go-errors v1.0.3/go.mod h1:HtfrZnMd6HsX75Mtbv9Qcnn0BqOrrFArvCaj3RMnZhY=
github.com/djthorpe/go-tablewriter v0.0.8 h1:uRhB9XVgK1n9tvVS7KMyxhxxGGtDvqC80toDTpW4DB4=
github.com/djthorpe/go-tablewriter v0.0.8/go.mod h1:NVBvytpL+6fHfCKn0+3lSi15/G3A1HWf2cLNeHg6YBg=
github.com/djthorpe/go-tablewriter v0.0.10 h1:/nL5NWJba4zrQoleIEyw4NwBYY9b3XTGM9EV+c+VWhU=
github.com/djthorpe/go-tablewriter v0.0.10/go.mod h1:NVBvytpL+6fHfCKn0+3lSi15/G3A1HWf2cLNeHg6YBg=
github.com/go-audio/audio v1.0.0 h1:zS9vebldgbQqktK4H0lUqWrG8P0NxCJVqcj7ZpNnwd4=
github.com/go-audio/audio v1.0.0/go.mod h1:6uAu0+H2lHkwdGsAY+j2wHPNPpPoeg5AaEFh9FlA+Zs=
github.com/go-audio/riff v1.0.0 h1:d8iCGbDvox9BfLagY94fBynxSPHO80LmZCaOsmKxokA=
Expand All @@ -26,8 +26,8 @@ github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6T
github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mutablelogic/go-client v1.0.9 h1:Eh4sjQOFDldP/L3IizqkcOD3WigZR+u1VaHTUM4ujYw=
github.com/mutablelogic/go-client v1.0.9/go.mod h1:VLyB8j8IBJSK/FXvvqhmq93PRWDKkyLu8R7V2Vudb6A=
github.com/mutablelogic/go-media v1.6.11 h1:czwRvuWIaqDArZrHv0e7nEIjXJkCbnNWkrQzkTOs96w=
github.com/mutablelogic/go-media v1.6.11/go.mod h1:HulNT0yyH63a3FRlbuzNDakhOypYrmtFVkHEXZjDgAY=
github.com/mutablelogic/go-media v1.6.12 h1:+FaqSz6GpQduF4KEDdYMxTCdtkJFOOaMJ/4wd3L59wI=
github.com/mutablelogic/go-media v1.6.12/go.mod h1:HulNT0yyH63a3FRlbuzNDakhOypYrmtFVkHEXZjDgAY=
github.com/mutablelogic/go-server v1.4.15 h1:jOvVdDmVK+PGCMBAk5atKHVonnccwy/b4dWwWFAOTso=
github.com/mutablelogic/go-server v1.4.15/go.mod h1:9nenPAohKu8bFoRgwHJh+3s8h0kLFjUAb8KZvT1TQNU=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand All @@ -37,14 +37,14 @@ github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 h1:2dVuKD2vS7b0QIHQbpyTISPd0LeHDbnYEryqj5Q1ug8=
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56/go.mod h1:M4RDyNAINzryxdtnbRXRL/OHtkFuWGRjvuhBJpk2IlY=
golang.org/x/exp v0.0.0-20240808152545-0cdaa3abc0fa h1:ELnwvuAXPNtPk1TJRuGkI9fDTwym6AYBu0qzT8AcHdI=
golang.org/x/exp v0.0.0-20240808152545-0cdaa3abc0fa/go.mod h1:akd2r19cwCdwSwWeIdzYQGa/EZZyqcOdwWiwj5L5eKQ=
golang.org/x/image v0.18.0 h1:jGzIakQa/ZXI1I0Fxvaa9W7yP25TqT6cHIHn+6CqvSQ=
golang.org/x/image v0.18.0/go.mod h1:4yyo5vMFQjVjUcVk4jEQcU9MGy/rulF5WvUILseCM2E=
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.22.0 h1:BbsgPEJULsl2fV/AT3v15Mjva5yXKQDyKf+TbDz7QJk=
golang.org/x/term v0.22.0/go.mod h1:F3qCibpT5AMpCRfhfT53vVJwhLtIVHhB9XDjfFvnMI4=
golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg=
golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.23.0 h1:F6D4vR+EHoL9/sWAWgAR1H2DcHr4PareCbAaCo1RpuU=
golang.org/x/term v0.23.0/go.mod h1:DgV24QBUrK6jhZXl+20l6UWznPlwAHm1Q1mGHtydmSk=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
26 changes: 23 additions & 3 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package client

import (
"context"
"errors"
"io"
"net/url"
"os"
Expand Down Expand Up @@ -86,9 +87,28 @@ func (c *Client) DownloadModel(ctx context.Context, path string, fn func(status
client.OptPath("models"),
client.OptQuery(query),
client.OptNoTimeout(),
client.OptJsonStreamCallback(func(v any) error {
if v, ok := v.(*resp); ok && fn != nil {
fn(v.Status, v.Completed, v.Total)
client.OptTextStreamCallback(func(evt client.TextStreamEvent) error {
switch evt.Event {
case "progress":
var r resp
if err := evt.Json(&r); err != nil {
return err
} else {
fn(r.Status, r.Completed, r.Total)
}
case "error":
var errstr string
if evt.Event == "error" {
if err := evt.Json(&errstr); err != nil {
return err
} else {
return errors.New(errstr)
}
}
case "ok":
if err := evt.Json(&r); err != nil {
return err
}
}
return nil
}),
Expand Down

0 comments on commit d4b7fdc

Please sign in to comment.