Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tts): Implement naive response_format for tts endpoint #4035

Merged
merged 2 commits into from
Nov 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions core/http/endpoints/localai/tts.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,19 @@ import (
"github.com/gofiber/fiber/v2"
"github.com/mudler/LocalAI/core/schema"
"github.com/rs/zerolog/log"

"github.com/mudler/LocalAI/pkg/utils"
)

// TTSEndpoint is the OpenAI Speech API endpoint https://platform.openai.com/docs/api-reference/audio/createSpeech
// @Summary Generates audio from the input text.
// @Accept json
// @Produce audio/x-wav
// @Param request body schema.TTSRequest true "query params"
// @Success 200 {string} binary "generated audio/wav file"
// @Router /v1/audio/speech [post]
// @Router /tts [post]
//
// @Summary Generates audio from the input text.
// @Accept json
// @Produce audio/x-wav
// @Param request body schema.TTSRequest true "query params"
// @Success 200 {string} binary "generated audio/wav file"
// @Router /v1/audio/speech [post]
// @Router /tts [post]
func TTSEndpoint(cl *config.BackendConfigLoader, ml *model.ModelLoader, appConfig *config.ApplicationConfig) func(c *fiber.Ctx) error {
return func(c *fiber.Ctx) error {

Expand Down Expand Up @@ -67,6 +70,13 @@ func TTSEndpoint(cl *config.BackendConfigLoader, ml *model.ModelLoader, appConfi
if err != nil {
return err
}

// Convert generated file to target format
filePath, err = utils.AudioConvert(filePath, input.Format)
if err != nil {
return err
}

return c.Download(filePath)
}
}
1 change: 1 addition & 0 deletions core/schema/localai.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type TTSRequest struct {
Voice string `json:"voice" yaml:"voice"` // voice audio file or speaker id
Backend string `json:"backend" yaml:"backend"`
Language string `json:"language,omitempty" yaml:"language,omitempty"` // (optional) language to use with TTS model
Format string `json:"response_format,omitempty" yaml:"response_format,omitempty"` // (optional) output format
}

type StoresSet struct {
Expand Down
30 changes: 30 additions & 0 deletions pkg/utils/ffmpeg.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"os/exec"
"strings"
)

func ffmpegCommand(args []string) (string, error) {
Expand All @@ -23,3 +24,32 @@ func AudioToWav(src, dst string) error {
}
return nil
}

// AudioConvert converts generated wav file from tts to other output formats.
// TODO: handle pcm to have 100% parity of supported format from OpenAI
func AudioConvert(src string, format string) (string, error) {
extension := ""
// compute file extension from format, default to wav
switch format {
case "opus":
extension = ".ogg"
case "mp3", "aac", "flac":
extension = fmt.Sprintf(".%s", format)
default:
extension = ".wav"
}

// if .wav, do nothing
if extension == ".wav" {
return src, nil
}

// naive conversion based on default values and target extension of file
dst := strings.Replace(src, ".wav", extension, -1)
commandArgs := []string{"-y", "-i", src, "-vn", dst}
out, err := ffmpegCommand(commandArgs)
if err != nil {
return "", fmt.Errorf("error: %w out: %s", err, out)
}
return dst, nil
}
6 changes: 5 additions & 1 deletion swagger/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1721,7 +1721,11 @@ const docTemplate = `{
"voice": {
"description": "voice audio file or speaker id",
"type": "string"
}
},
"response_format": {
"description": "(optional) output format of generated audio file, defaults to wav, accept wav, mp3, flac, aac, opus",
"type": "string"
},
}
},
"schema.ToolCall": {
Expand Down
6 changes: 5 additions & 1 deletion swagger/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -1714,6 +1714,10 @@
"voice": {
"description": "voice audio file or speaker id",
"type": "string"
},
"response_format": {
"description": "(optional) output format of generated audio file, defaults to wav, accept wav, mp3, flac, aac, opus",
"type": "string"
}
}
},
Expand Down Expand Up @@ -1742,4 +1746,4 @@
"in": "header"
}
}
}
}
3 changes: 3 additions & 0 deletions swagger/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,9 @@ definitions:
voice:
description: voice audio file or speaker id
type: string
response_format:
description: (optional) output format of generated audio file, defaults to wav, accept wav, mp3, flac, aac, opus
type: string
type: object
schema.ToolCall:
properties:
Expand Down