2023-07-14 23:19:43 +00:00
|
|
|
package localai
|
2023-06-22 15:53:10 +00:00
|
|
|
|
|
|
|
import (
|
2023-07-14 23:19:43 +00:00
|
|
|
"context"
|
2023-06-22 15:53:10 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2023-07-14 23:19:43 +00:00
|
|
|
config "github.com/go-skynet/LocalAI/api/config"
|
|
|
|
|
|
|
|
"github.com/go-skynet/LocalAI/api/options"
|
2023-07-14 23:19:43 +00:00
|
|
|
"github.com/go-skynet/LocalAI/pkg/grpc/proto"
|
2023-06-22 15:53:10 +00:00
|
|
|
model "github.com/go-skynet/LocalAI/pkg/model"
|
|
|
|
"github.com/go-skynet/LocalAI/pkg/utils"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
type TTSRequest struct {
|
|
|
|
Model string `json:"model" yaml:"model"`
|
|
|
|
Input string `json:"input" yaml:"input"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateUniqueFileName(dir, baseName, ext string) string {
|
|
|
|
counter := 1
|
|
|
|
fileName := baseName + ext
|
|
|
|
|
|
|
|
for {
|
|
|
|
filePath := filepath.Join(dir, fileName)
|
|
|
|
_, err := os.Stat(filePath)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return fileName
|
|
|
|
}
|
|
|
|
|
|
|
|
counter++
|
|
|
|
fileName = fmt.Sprintf("%s_%d%s", baseName, counter, ext)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-14 23:19:43 +00:00
|
|
|
func TTSEndpoint(cm *config.ConfigLoader, o *options.Option) func(c *fiber.Ctx) error {
|
2023-06-22 15:53:10 +00:00
|
|
|
return func(c *fiber.Ctx) error {
|
|
|
|
|
|
|
|
input := new(TTSRequest)
|
|
|
|
// Get input data from the request body
|
|
|
|
if err := c.BodyParser(input); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-07-14 23:19:43 +00:00
|
|
|
piperModel, err := o.Loader.BackendLoader(
|
2023-07-14 23:19:43 +00:00
|
|
|
model.WithBackendString(model.PiperBackend),
|
|
|
|
model.WithModelFile(input.Model),
|
2023-07-14 23:19:43 +00:00
|
|
|
model.WithContext(o.Context),
|
2023-07-14 23:19:43 +00:00
|
|
|
model.WithAssetDir(o.AssetsDestination))
|
2023-06-22 15:53:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if piperModel == nil {
|
|
|
|
return fmt.Errorf("could not load piper model")
|
|
|
|
}
|
|
|
|
|
2023-07-14 23:19:43 +00:00
|
|
|
if err := os.MkdirAll(o.AudioDir, 0755); err != nil {
|
2023-07-14 23:19:43 +00:00
|
|
|
return fmt.Errorf("failed creating audio directory: %s", err)
|
2023-06-22 15:53:10 +00:00
|
|
|
}
|
|
|
|
|
2023-07-14 23:19:43 +00:00
|
|
|
fileName := generateUniqueFileName(o.AudioDir, "piper", ".wav")
|
|
|
|
filePath := filepath.Join(o.AudioDir, fileName)
|
2023-06-22 15:53:10 +00:00
|
|
|
|
2023-07-14 23:19:43 +00:00
|
|
|
modelPath := filepath.Join(o.Loader.ModelPath, input.Model)
|
2023-06-22 15:53:10 +00:00
|
|
|
|
2023-07-14 23:19:43 +00:00
|
|
|
if err := utils.VerifyPath(modelPath, o.Loader.ModelPath); err != nil {
|
2023-06-22 15:53:10 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-07-14 23:19:43 +00:00
|
|
|
if _, err := piperModel.TTS(context.Background(), &proto.TTSRequest{
|
|
|
|
Text: input.Input,
|
|
|
|
Model: modelPath,
|
|
|
|
Dst: filePath,
|
|
|
|
}); err != nil {
|
2023-06-22 15:53:10 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.Download(filePath)
|
|
|
|
}
|
|
|
|
}
|