mirror of
https://github.com/mudler/LocalAI.git
synced 2024-12-27 00:01:07 +00:00
af9e5a2d05
* Revert "fix(fncall): fix regression introduced in #1963 (#2048)" This reverts commit6b06d4e0af
. * Revert "fix: action-tmate back to upstream, dead code removal (#2038)" This reverts commitfdec8a9d00
. * Revert "feat(grpc): return consumed token count and update response accordingly (#2035)" This reverts commite843d7df0e
. * Revert "refactor: backend/service split, channel-based llm flow (#1963)" This reverts commiteed5706994
. * feat(grpc): return consumed token count and update response accordingly Fixes: #1920 Signed-off-by: Ettore Di Giacinto <mudler@localai.io> --------- Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
90 lines
2.3 KiB
Go
90 lines
2.3 KiB
Go
package backend
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/go-skynet/LocalAI/core/config"
|
|
|
|
"github.com/go-skynet/LocalAI/pkg/grpc/proto"
|
|
model "github.com/go-skynet/LocalAI/pkg/model"
|
|
"github.com/go-skynet/LocalAI/pkg/utils"
|
|
)
|
|
|
|
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)
|
|
}
|
|
}
|
|
|
|
func ModelTTS(backend, text, modelFile, voice string, loader *model.ModelLoader, appConfig *config.ApplicationConfig, backendConfig config.BackendConfig) (string, *proto.Result, error) {
|
|
bb := backend
|
|
if bb == "" {
|
|
bb = model.PiperBackend
|
|
}
|
|
|
|
grpcOpts := gRPCModelOpts(backendConfig)
|
|
|
|
opts := modelOpts(config.BackendConfig{}, appConfig, []model.Option{
|
|
model.WithBackendString(bb),
|
|
model.WithModel(modelFile),
|
|
model.WithContext(appConfig.Context),
|
|
model.WithAssetDir(appConfig.AssetsDestination),
|
|
model.WithLoadGRPCLoadModelOpts(grpcOpts),
|
|
})
|
|
ttsModel, err := loader.BackendLoader(opts...)
|
|
if err != nil {
|
|
return "", nil, err
|
|
}
|
|
|
|
if ttsModel == nil {
|
|
return "", nil, fmt.Errorf("could not load piper model")
|
|
}
|
|
|
|
if err := os.MkdirAll(appConfig.AudioDir, 0755); err != nil {
|
|
return "", nil, fmt.Errorf("failed creating audio directory: %s", err)
|
|
}
|
|
|
|
fileName := generateUniqueFileName(appConfig.AudioDir, "tts", ".wav")
|
|
filePath := filepath.Join(appConfig.AudioDir, fileName)
|
|
|
|
// If the model file is not empty, we pass it joined with the model path
|
|
modelPath := ""
|
|
if modelFile != "" {
|
|
// 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 {
|
|
return "", nil, err
|
|
}
|
|
modelPath = mp
|
|
} else {
|
|
modelPath = modelFile
|
|
}
|
|
}
|
|
|
|
res, err := ttsModel.TTS(context.Background(), &proto.TTSRequest{
|
|
Text: text,
|
|
Model: modelPath,
|
|
Voice: voice,
|
|
Dst: filePath,
|
|
})
|
|
|
|
return filePath, res, err
|
|
}
|