-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: mudler <mudler@localai.io>
- Loading branch information
Showing
12 changed files
with
202 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package api | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
model "github.com/go-skynet/LocalAI/pkg/model" | ||
"github.com/go-skynet/LocalAI/pkg/tts" | ||
llama "github.com/go-skynet/go-llama.cpp" | ||
"github.com/gofiber/fiber/v2" | ||
) | ||
|
||
type TTSRequest struct { | ||
Model string `json:"model" yaml:"model"` | ||
Input string `json:"input" yaml:"input"` | ||
} | ||
|
||
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 ttsEndpoint(cm *ConfigMerger, o *Option) func(c *fiber.Ctx) error { | ||
return func(c *fiber.Ctx) error { | ||
|
||
input := new(TTSRequest) | ||
// Get input data from the request body | ||
if err := c.BodyParser(input); err != nil { | ||
return err | ||
} | ||
|
||
piperModel, err := o.loader.BackendLoader(model.PiperBackend, input.Model, []llama.ModelOption{}, uint32(0), o.assetsDestination) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if piperModel == nil { | ||
return fmt.Errorf("could not load piper model") | ||
} | ||
|
||
w, ok := piperModel.(*tts.Piper) | ||
if !ok { | ||
return fmt.Errorf("loader returned non-piper object %+v", w) | ||
} | ||
|
||
if err := os.MkdirAll(o.audioDir, 0755); err != nil { | ||
return err | ||
} | ||
|
||
fileName := generateUniqueFileName(o.audioDir, "piper", ".wav") | ||
filePath := filepath.Join(o.audioDir, fileName) | ||
|
||
w.TTS(input.Input, input.Model, filePath) | ||
|
||
return c.Download(filePath) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
//go:build tts | ||
// +build tts | ||
|
||
package tts | ||
|
||
import ( | ||
piper "github.com/mudler/go-piper" | ||
) | ||
|
||
func tts(text, model, assetDir, arLib, dst string) error { | ||
return piper.TextToWav(text, model, assetDir, arLib, dst) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
//go:build !tts | ||
// +build !tts | ||
|
||
package tts | ||
|
||
import "fmt" | ||
|
||
func tts(text, model, assetDir, arLib, dst string) error { | ||
return fmt.Errorf("this version of LocalAI was built without the tts tag") | ||
} |
Oops, something went wrong.