2024-01-05 17:04:46 +00:00
|
|
|
package localai
|
|
|
|
|
|
|
|
import (
|
2024-02-10 20:37:03 +00:00
|
|
|
fiberContext "github.com/go-skynet/LocalAI/api/ctx"
|
2024-02-21 01:21:19 +00:00
|
|
|
"github.com/go-skynet/LocalAI/core/backend"
|
|
|
|
config "github.com/go-skynet/LocalAI/core/config"
|
2024-02-10 20:37:03 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2024-01-05 17:04:46 +00:00
|
|
|
|
2024-02-21 01:21:19 +00:00
|
|
|
"github.com/go-skynet/LocalAI/core/options"
|
2024-01-05 17:04:46 +00:00
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
type TTSRequest struct {
|
|
|
|
Model string `json:"model" yaml:"model"`
|
|
|
|
Input string `json:"input" yaml:"input"`
|
|
|
|
Backend string `json:"backend" yaml:"backend"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func TTSEndpoint(cm *config.ConfigLoader, o *options.Option) func(c *fiber.Ctx) error {
|
|
|
|
return func(c *fiber.Ctx) error {
|
|
|
|
|
|
|
|
input := new(TTSRequest)
|
2024-02-10 20:37:03 +00:00
|
|
|
|
2024-01-05 17:04:46 +00:00
|
|
|
// Get input data from the request body
|
|
|
|
if err := c.BodyParser(input); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-02-10 20:37:03 +00:00
|
|
|
modelFile, err := fiberContext.ModelFromContext(c, o.Loader, input.Model, false)
|
|
|
|
if err != nil {
|
|
|
|
modelFile = input.Model
|
|
|
|
log.Warn().Msgf("Model not found in context: %s", input.Model)
|
|
|
|
}
|
|
|
|
cfg, err := config.Load(modelFile, o.Loader.ModelPath, cm, false, 0, 0, false)
|
|
|
|
if err != nil {
|
|
|
|
modelFile = input.Model
|
|
|
|
log.Warn().Msgf("Model not found in context: %s", input.Model)
|
|
|
|
} else {
|
|
|
|
modelFile = cfg.Model
|
|
|
|
}
|
|
|
|
log.Debug().Msgf("Request for model: %s", modelFile)
|
|
|
|
|
|
|
|
if input.Backend != "" {
|
2024-02-15 16:33:06 +00:00
|
|
|
cfg.Backend = input.Backend
|
2024-02-10 20:37:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
filePath, _, err := backend.ModelTTS(cfg.Backend, input.Input, modelFile, o.Loader, o, *cfg)
|
2024-01-05 17:04:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return c.Download(filePath)
|
|
|
|
}
|
|
|
|
}
|