-
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.
- Loading branch information
Showing
13 changed files
with
185 additions
and
14 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,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()) | ||
} |
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,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() | ||
} |
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 | ||
|
||
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()) | ||
} |
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,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.
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
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