diff --git a/main.go b/main.go index 937fa8ea34f8..c3ebda4d0193 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 + }, + }, }, }