2023-07-20 20:10:12 +00:00
|
|
|
package backend
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2024-06-23 08:24:36 +00:00
|
|
|
"github.com/mudler/LocalAI/core/config"
|
2024-03-01 15:19:53 +00:00
|
|
|
|
2024-06-23 08:24:36 +00:00
|
|
|
"github.com/mudler/LocalAI/pkg/grpc/proto"
|
2024-08-24 00:20:28 +00:00
|
|
|
"github.com/mudler/LocalAI/pkg/model"
|
2024-06-23 08:24:36 +00:00
|
|
|
"github.com/mudler/LocalAI/pkg/utils"
|
2023-07-20 20:10:12 +00:00
|
|
|
)
|
|
|
|
|
2024-06-01 18:26:27 +00:00
|
|
|
func ModelTTS(
|
|
|
|
backend,
|
|
|
|
text,
|
|
|
|
modelFile,
|
2024-08-24 00:20:28 +00:00
|
|
|
voice,
|
2024-06-01 18:26:27 +00:00
|
|
|
language 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-11-08 20:54:25 +00:00
|
|
|
opts := ModelOptions(backendConfig, appConfig, model.WithBackendString(bb), model.WithModel(modelFile))
|
|
|
|
ttsModel, err := loader.Load(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-04-25 22:47:06 +00:00
|
|
|
if err := os.MkdirAll(appConfig.AudioDir, 0750); err != nil {
|
2023-07-20 20:10:12 +00:00
|
|
|
return "", nil, fmt.Errorf("failed creating audio directory: %s", err)
|
|
|
|
}
|
|
|
|
|
2024-08-24 00:20:28 +00:00
|
|
|
fileName := utils.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{
|
2024-08-24 00:20:28 +00:00
|
|
|
Text: text,
|
|
|
|
Model: modelPath,
|
|
|
|
Voice: voice,
|
|
|
|
Dst: filePath,
|
2024-06-01 18:26:27 +00:00
|
|
|
Language: &language,
|
2023-07-20 20:10:12 +00:00
|
|
|
})
|
2024-08-27 15:35:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
2023-07-20 20:10:12 +00:00
|
|
|
|
2024-06-01 18:26:27 +00:00
|
|
|
// return RPC error if any
|
|
|
|
if !res.Success {
|
|
|
|
return "", nil, fmt.Errorf(res.Message)
|
|
|
|
}
|
|
|
|
|
2023-07-20 20:10:12 +00:00
|
|
|
return filePath, res, err
|
|
|
|
}
|