From ab65f3a17d73eed3b4c0d0f72f867a79b52c74c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Espino?= Date: Sat, 14 Oct 2023 12:27:35 +0200 Subject: [PATCH] Addining the tts command line subcommand (#1169) This PR adds the tts (Text to Speach) command to the localai binary. This PR is related to the issue #816 --- main.go | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/main.go b/main.go index 937fa8ea..c3ebda4d 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,9 @@ package main import ( + "context" "encoding/json" + "errors" "fmt" "os" "os/signal" @@ -10,6 +12,7 @@ import ( "syscall" api "github.com/go-skynet/LocalAI/api" + "github.com/go-skynet/LocalAI/api/backend" "github.com/go-skynet/LocalAI/api/options" "github.com/go-skynet/LocalAI/internal" "github.com/go-skynet/LocalAI/pkg/gallery" @@ -274,6 +277,69 @@ For a list of compatible model, check out: https://localai.io/model-compatibilit }, }, }, + { + Name: "tts", + Usage: "Convert text to speech", + Flags: []cli.Flag{ + &cli.StringFlag{ + Name: "backend", + Value: "piper", + Aliases: []string{"b"}, + Usage: "Backend to run the TTS model", + }, + &cli.StringFlag{ + Name: "model", + Aliases: []string{"m"}, + Usage: "Model name to run the TTS", + Required: true, + }, + &cli.StringFlag{ + Name: "output-file", + Aliases: []string{"o"}, + Usage: "The path to write the output wav file", + }, + }, + Action: func(ctx *cli.Context) error { + modelOption := ctx.String("model") + if modelOption == "" { + return errors.New("--model parameter is required") + } + backendOption := ctx.String("backend") + if backendOption == "" { + backendOption = "piper" + } + outputFile := ctx.String("output-file") + outputDir := ctx.String("backend-assets-path") + if outputFile != "" { + outputDir = filepath.Dir(outputFile) + } + + text := strings.Join(ctx.Args().Slice(), " ") + + opts := &options.Option{ + Loader: model.NewModelLoader(ctx.String("models-path")), + Context: context.Background(), + AudioDir: outputDir, + AssetsDestination: ctx.String("backend-assets-path"), + } + + defer opts.Loader.StopAllGRPC() + + filePath, _, err := backend.ModelTTS(backendOption, text, modelOption, opts.Loader, opts) + if err != nil { + return err + } + if outputFile != "" { + if err := os.Rename(filePath, outputFile); err != nil { + return err + } + fmt.Printf("Generate file %s\n", outputFile) + } else { + fmt.Printf("Generate file %s\n", filePath) + } + return nil + }, + }, }, }