2023-07-20 20:10:12 +00:00
|
|
|
package backend
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2024-03-01 15:19:53 +00:00
|
|
|
"github.com/go-skynet/LocalAI/core/config"
|
|
|
|
|
2023-07-20 20:10:12 +00:00
|
|
|
"github.com/go-skynet/LocalAI/pkg/grpc/proto"
|
2024-04-17 21:33:49 +00:00
|
|
|
model "github.com/go-skynet/LocalAI/pkg/model"
|
2023-07-20 20:10:12 +00:00
|
|
|
"github.com/go-skynet/LocalAI/pkg/utils"
|
|
|
|
)
|
|
|
|
|
2024-04-17 21:33:49 +00:00
|
|
|
func generateUniqueFileName(dir, baseName, ext string) string {
|
|
|
|
counter := 1
|
|
|
|
fileName := baseName + ext
|
2023-07-20 20:10:12 +00:00
|
|
|
|
2024-04-17 21:33:49 +00:00
|
|
|
for {
|
|
|
|
filePath := filepath.Join(dir, fileName)
|
|
|
|
_, err := os.Stat(filePath)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return fileName
|
2024-04-13 07:45:34 +00:00
|
|
|
}
|
|
|
|
|
2024-04-17 21:33:49 +00:00
|
|
|
counter++
|
|
|
|
fileName = fmt.Sprintf("%s_%d%s", baseName, counter, ext)
|
|
|
|
}
|
2023-07-20 20:10:12 +00:00
|
|
|
}
|
|
|
|
|
2024-04-17 21:33:49 +00:00
|
|
|
func ModelTTS(backend, text, modelFile, voice string, loader *model.ModelLoader, appConfig *config.ApplicationConfig, backendConfig config.BackendConfig) (string, *proto.Result, error) {
|
2023-08-07 20:41:07 +00:00
|
|
|
bb := backend
|
|
|
|
if bb == "" {
|
|
|
|
bb = model.PiperBackend
|
|
|
|
}
|
2024-02-10 20:37:03 +00:00
|
|
|
|
2024-03-01 15:19:53 +00:00
|
|
|
grpcOpts := gRPCModelOpts(backendConfig)
|
2024-02-10 20:37:03 +00:00
|
|
|
|
2024-04-17 21:33:49 +00:00
|
|
|
opts := modelOpts(config.BackendConfig{}, appConfig, []model.Option{
|
2023-08-07 20:41:07 +00:00
|
|
|
model.WithBackendString(bb),
|
|
|
|
model.WithModel(modelFile),
|
2024-03-01 15:19:53 +00:00
|
|
|
model.WithContext(appConfig.Context),
|
|
|
|
model.WithAssetDir(appConfig.AssetsDestination),
|
2024-02-10 20:37:03 +00:00
|
|
|
model.WithLoadGRPCLoadModelOpts(grpcOpts),
|
2023-08-18 23:49:33 +00:00
|
|
|
})
|
2024-03-14 22:08:34 +00:00
|
|
|
ttsModel, err := loader.BackendLoader(opts...)
|
2023-07-20 20:10:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
|
2024-03-14 22:08:34 +00:00
|
|
|
if ttsModel == nil {
|
2023-07-20 20:10:12 +00:00
|
|
|
return "", nil, fmt.Errorf("could not load piper model")
|
|
|
|
}
|
|
|
|
|
2024-03-01 15:19:53 +00:00
|
|
|
if err := os.MkdirAll(appConfig.AudioDir, 0755); err != nil {
|
2023-07-20 20:10:12 +00:00
|
|
|
return "", nil, fmt.Errorf("failed creating audio directory: %s", err)
|
|
|
|
}
|
|
|
|
|
2024-03-14 22:08:34 +00:00
|
|
|
fileName := generateUniqueFileName(appConfig.AudioDir, "tts", ".wav")
|
2024-03-01 15:19:53 +00:00
|
|
|
filePath := filepath.Join(appConfig.AudioDir, fileName)
|
2023-07-20 20:10:12 +00:00
|
|
|
|
2023-08-07 20:41:07 +00:00
|
|
|
// If the model file is not empty, we pass it joined with the model path
|
|
|
|
modelPath := ""
|
|
|
|
if modelFile != "" {
|
2024-03-14 22:08:34 +00:00
|
|
|
// If the model file is not empty, we pass it joined with the model path
|
|
|
|
// Checking first that it exists and is not outside ModelPath
|
|
|
|
// TODO: we should actually first check if the modelFile is looking like
|
|
|
|
// a FS path
|
|
|
|
mp := filepath.Join(loader.ModelPath, modelFile)
|
|
|
|
if _, err := os.Stat(mp); err == nil {
|
|
|
|
if err := utils.VerifyPath(mp, appConfig.ModelPath); err != nil {
|
2023-12-08 09:01:02 +00:00
|
|
|
return "", nil, err
|
|
|
|
}
|
2024-03-14 22:08:34 +00:00
|
|
|
modelPath = mp
|
2023-12-08 09:01:02 +00:00
|
|
|
} else {
|
|
|
|
modelPath = modelFile
|
2023-08-07 20:41:07 +00:00
|
|
|
}
|
2023-07-20 20:10:12 +00:00
|
|
|
}
|
|
|
|
|
2024-03-14 22:08:34 +00:00
|
|
|
res, err := ttsModel.TTS(context.Background(), &proto.TTSRequest{
|
2023-07-20 20:10:12 +00:00
|
|
|
Text: text,
|
|
|
|
Model: modelPath,
|
2024-03-14 22:08:34 +00:00
|
|
|
Voice: voice,
|
2023-07-20 20:10:12 +00:00
|
|
|
Dst: filePath,
|
|
|
|
})
|
|
|
|
|
|
|
|
return filePath, res, err
|
|
|
|
}
|