-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of github.com:djthorpe/go-whisper
- Loading branch information
Showing
56 changed files
with
570 additions
and
570 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
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:"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"` | ||
} | ||
|
||
//////////////////////////////////////////////////////////////////////////////// | ||
// 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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.Writeln("OK") | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.