Skip to content

Commit

Permalink
Addining the tts command line subcommand (#1169)
Browse files Browse the repository at this point in the history
This PR adds the tts (Text to Speach) command to the localai binary.

This PR is related to the issue #816
  • Loading branch information
jespino authored Oct 14, 2023
1 parent 4e23cbe commit ab65f3a
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package main

import (
"context"
"encoding/json"
"errors"
"fmt"
"os"
"os/signal"
Expand All @@ -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"
Expand Down Expand Up @@ -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
},
},
},
}

Expand Down

0 comments on commit ab65f3a

Please sign in to comment.