2024-03-14 22:08:34 +00:00
|
|
|
package elevenlabs
|
|
|
|
|
|
|
|
import (
|
2024-06-23 08:24:36 +00:00
|
|
|
"github.com/mudler/LocalAI/core/backend"
|
|
|
|
"github.com/mudler/LocalAI/core/config"
|
|
|
|
fiberContext "github.com/mudler/LocalAI/core/http/ctx"
|
|
|
|
"github.com/mudler/LocalAI/pkg/model"
|
2024-03-14 22:08:34 +00:00
|
|
|
|
|
|
|
"github.com/gofiber/fiber/v2"
|
2024-06-23 08:24:36 +00:00
|
|
|
"github.com/mudler/LocalAI/core/schema"
|
2024-03-14 22:08:34 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
)
|
|
|
|
|
2024-03-29 21:29:33 +00:00
|
|
|
// TTSEndpoint is the OpenAI Speech API endpoint https://platform.openai.com/docs/api-reference/audio/createSpeech
|
|
|
|
// @Summary Generates audio from the input text.
|
|
|
|
// @Param voice-id path string true "Account ID"
|
|
|
|
// @Param request body schema.TTSRequest true "query params"
|
|
|
|
// @Success 200 {string} binary "Response"
|
|
|
|
// @Router /v1/text-to-speech/{voice-id} [post]
|
2024-04-17 21:33:49 +00:00
|
|
|
func TTSEndpoint(cl *config.BackendConfigLoader, ml *model.ModelLoader, appConfig *config.ApplicationConfig) func(c *fiber.Ctx) error {
|
2024-03-14 22:08:34 +00:00
|
|
|
return func(c *fiber.Ctx) error {
|
|
|
|
|
|
|
|
input := new(schema.ElevenLabsTTSRequest)
|
|
|
|
voiceID := c.Params("voice-id")
|
|
|
|
|
|
|
|
// Get input data from the request body
|
|
|
|
if err := c.BodyParser(input); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-07-10 13:28:39 +00:00
|
|
|
modelFile, err := fiberContext.ModelFromContext(c, cl, ml, input.ModelID, false)
|
2024-03-14 22:08:34 +00:00
|
|
|
if err != nil {
|
2024-04-17 21:33:49 +00:00
|
|
|
modelFile = input.ModelID
|
2024-03-14 22:08:34 +00:00
|
|
|
log.Warn().Msgf("Model not found in context: %s", input.ModelID)
|
|
|
|
}
|
|
|
|
|
2024-04-17 21:33:49 +00:00
|
|
|
cfg, err := cl.LoadBackendConfigFileByName(modelFile, appConfig.ModelPath,
|
|
|
|
config.LoadOptionDebug(appConfig.Debug),
|
|
|
|
config.LoadOptionThreads(appConfig.Threads),
|
|
|
|
config.LoadOptionContextSize(appConfig.ContextSize),
|
|
|
|
config.LoadOptionF16(appConfig.F16),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
modelFile = input.ModelID
|
|
|
|
log.Warn().Msgf("Model not found in context: %s", input.ModelID)
|
|
|
|
} else {
|
|
|
|
if input.ModelID != "" {
|
|
|
|
modelFile = input.ModelID
|
|
|
|
} else {
|
|
|
|
modelFile = cfg.Model
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.Debug().Msgf("Request for model: %s", modelFile)
|
|
|
|
|
2024-06-01 18:26:27 +00:00
|
|
|
filePath, _, err := backend.ModelTTS(cfg.Backend, input.Text, modelFile, "", voiceID, ml, appConfig, *cfg)
|
2024-04-17 21:33:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2024-03-14 22:08:34 +00:00
|
|
|
}
|
2024-04-17 21:33:49 +00:00
|
|
|
return c.Download(filePath)
|
2024-03-14 22:08:34 +00:00
|
|
|
}
|
|
|
|
}
|