| package localai |
|
|
| import ( |
| "path/filepath" |
|
|
| "github.com/labstack/echo/v4" |
| "github.com/mudler/LocalAI/core/backend" |
| "github.com/mudler/LocalAI/core/config" |
| "github.com/mudler/LocalAI/core/http/middleware" |
| "github.com/mudler/LocalAI/pkg/model" |
|
|
| "github.com/mudler/LocalAI/core/schema" |
| "github.com/mudler/xlog" |
|
|
| "github.com/mudler/LocalAI/pkg/utils" |
| ) |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| func TTSEndpoint(cl *config.ModelConfigLoader, ml *model.ModelLoader, appConfig *config.ApplicationConfig) echo.HandlerFunc { |
| return func(c echo.Context) error { |
| input, ok := c.Get(middleware.CONTEXT_LOCALS_KEY_LOCALAI_REQUEST).(*schema.TTSRequest) |
| if !ok || input.Model == "" { |
| return echo.ErrBadRequest |
| } |
|
|
| cfg, ok := c.Get(middleware.CONTEXT_LOCALS_KEY_MODEL_CONFIG).(*config.ModelConfig) |
| if !ok || cfg == nil { |
| return echo.ErrBadRequest |
| } |
|
|
| xlog.Debug("LocalAI TTS Request received", "model", input.Model) |
|
|
| if cfg.Backend == "" && input.Backend != "" { |
| cfg.Backend = input.Backend |
| } |
|
|
| if input.Language != "" { |
| cfg.Language = input.Language |
| } |
|
|
| if input.Voice != "" { |
| cfg.Voice = input.Voice |
| } |
|
|
| filePath, _, err := backend.ModelTTS(input.Input, cfg.Voice, cfg.Language, ml, appConfig, *cfg) |
| if err != nil { |
| return err |
| } |
|
|
| |
| filePath, err = utils.AudioConvert(filePath, input.Format) |
| if err != nil { |
| return err |
| } |
|
|
| return c.Attachment(filePath, filepath.Base(filePath)) |
| } |
| } |
|
|