2024-04-23 07:22:58 +00:00
|
|
|
package routes
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"html/template"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/go-skynet/LocalAI/core/config"
|
|
|
|
"github.com/go-skynet/LocalAI/core/http/elements"
|
2024-05-02 19:14:10 +00:00
|
|
|
"github.com/go-skynet/LocalAI/core/http/endpoints/localai"
|
2024-04-23 07:22:58 +00:00
|
|
|
"github.com/go-skynet/LocalAI/core/services"
|
2024-05-02 19:14:10 +00:00
|
|
|
"github.com/go-skynet/LocalAI/internal"
|
2024-04-23 07:22:58 +00:00
|
|
|
"github.com/go-skynet/LocalAI/pkg/gallery"
|
|
|
|
"github.com/go-skynet/LocalAI/pkg/model"
|
2024-04-27 07:08:33 +00:00
|
|
|
"github.com/go-skynet/LocalAI/pkg/xsync"
|
|
|
|
|
2024-04-23 07:22:58 +00:00
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
)
|
|
|
|
|
|
|
|
func RegisterUIRoutes(app *fiber.App,
|
|
|
|
cl *config.BackendConfigLoader,
|
|
|
|
ml *model.ModelLoader,
|
|
|
|
appConfig *config.ApplicationConfig,
|
|
|
|
galleryService *services.GalleryService,
|
|
|
|
auth func(*fiber.Ctx) error) {
|
|
|
|
|
2024-05-02 19:14:10 +00:00
|
|
|
app.Get("/", auth, localai.WelcomeEndpoint(appConfig, cl, ml))
|
|
|
|
|
2024-04-27 07:08:33 +00:00
|
|
|
// keeps the state of models that are being installed from the UI
|
|
|
|
var installingModels = xsync.NewSyncedMap[string, string]()
|
|
|
|
|
|
|
|
// Show the Models page (all models)
|
2024-04-23 07:22:58 +00:00
|
|
|
app.Get("/browse", auth, func(c *fiber.Ctx) error {
|
|
|
|
models, _ := gallery.AvailableGalleryModels(appConfig.Galleries, appConfig.ModelPath)
|
|
|
|
|
|
|
|
summary := fiber.Map{
|
2024-04-23 16:43:25 +00:00
|
|
|
"Title": "LocalAI - Models",
|
2024-05-02 19:14:10 +00:00
|
|
|
"Version": internal.PrintableVersion(),
|
2024-04-27 07:08:33 +00:00
|
|
|
"Models": template.HTML(elements.ListModels(models, installingModels)),
|
2024-04-23 16:43:25 +00:00
|
|
|
"Repositories": appConfig.Galleries,
|
2024-04-23 07:22:58 +00:00
|
|
|
// "ApplicationConfig": appConfig,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Render index
|
|
|
|
return c.Render("views/models", summary)
|
|
|
|
})
|
|
|
|
|
2024-04-27 07:08:33 +00:00
|
|
|
// Show the models, filtered from the user input
|
2024-04-23 07:22:58 +00:00
|
|
|
// https://htmx.org/examples/active-search/
|
|
|
|
app.Post("/browse/search/models", auth, func(c *fiber.Ctx) error {
|
|
|
|
form := struct {
|
|
|
|
Search string `form:"search"`
|
|
|
|
}{}
|
|
|
|
if err := c.BodyParser(&form); err != nil {
|
|
|
|
return c.Status(fiber.StatusBadRequest).SendString(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
models, _ := gallery.AvailableGalleryModels(appConfig.Galleries, appConfig.ModelPath)
|
|
|
|
|
|
|
|
filteredModels := []*gallery.GalleryModel{}
|
|
|
|
for _, m := range models {
|
2024-04-23 16:43:25 +00:00
|
|
|
if strings.Contains(m.Name, form.Search) ||
|
|
|
|
strings.Contains(m.Description, form.Search) ||
|
|
|
|
strings.Contains(m.Gallery.Name, form.Search) ||
|
|
|
|
strings.Contains(strings.Join(m.Tags, ","), form.Search) {
|
2024-04-23 07:22:58 +00:00
|
|
|
filteredModels = append(filteredModels, m)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-27 07:08:33 +00:00
|
|
|
return c.SendString(elements.ListModels(filteredModels, installingModels))
|
2024-04-23 07:22:58 +00:00
|
|
|
})
|
|
|
|
|
2024-04-28 21:42:46 +00:00
|
|
|
/*
|
|
|
|
|
|
|
|
Install routes
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2024-04-27 07:08:33 +00:00
|
|
|
// This route is used when the "Install" button is pressed, we submit here a new job to the gallery service
|
2024-04-23 07:22:58 +00:00
|
|
|
// https://htmx.org/examples/progress-bar/
|
|
|
|
app.Post("/browse/install/model/:id", auth, func(c *fiber.Ctx) error {
|
2024-04-27 07:08:33 +00:00
|
|
|
galleryID := strings.Clone(c.Params("id")) // note: strings.Clone is required for multiple requests!
|
2024-04-23 07:22:58 +00:00
|
|
|
|
|
|
|
id, err := uuid.NewUUID()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
uid := id.String()
|
|
|
|
|
2024-04-27 07:08:33 +00:00
|
|
|
installingModels.Set(galleryID, uid)
|
|
|
|
|
2024-04-23 07:22:58 +00:00
|
|
|
op := gallery.GalleryOp{
|
|
|
|
Id: uid,
|
|
|
|
GalleryName: galleryID,
|
|
|
|
Galleries: appConfig.Galleries,
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
galleryService.C <- op
|
|
|
|
}()
|
|
|
|
|
2024-04-28 21:42:46 +00:00
|
|
|
return c.SendString(elements.StartProgressBar(uid, "0", "Installation"))
|
|
|
|
})
|
|
|
|
|
|
|
|
// This route is used when the "Install" button is pressed, we submit here a new job to the gallery service
|
|
|
|
// https://htmx.org/examples/progress-bar/
|
|
|
|
app.Post("/browse/delete/model/:id", auth, func(c *fiber.Ctx) error {
|
|
|
|
galleryID := strings.Clone(c.Params("id")) // note: strings.Clone is required for multiple requests!
|
|
|
|
|
|
|
|
id, err := uuid.NewUUID()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
uid := id.String()
|
|
|
|
|
|
|
|
installingModels.Set(galleryID, uid)
|
|
|
|
|
|
|
|
op := gallery.GalleryOp{
|
|
|
|
Id: uid,
|
|
|
|
Delete: true,
|
|
|
|
GalleryName: galleryID,
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
galleryService.C <- op
|
|
|
|
}()
|
|
|
|
|
|
|
|
return c.SendString(elements.StartProgressBar(uid, "0", "Deletion"))
|
2024-04-23 07:22:58 +00:00
|
|
|
})
|
|
|
|
|
2024-04-27 07:08:33 +00:00
|
|
|
// Display the job current progress status
|
|
|
|
// If the job is done, we trigger the /browse/job/:uid route
|
2024-04-23 07:22:58 +00:00
|
|
|
// https://htmx.org/examples/progress-bar/
|
|
|
|
app.Get("/browse/job/progress/:uid", auth, func(c *fiber.Ctx) error {
|
|
|
|
jobUID := c.Params("uid")
|
|
|
|
|
|
|
|
status := galleryService.GetStatus(jobUID)
|
|
|
|
if status == nil {
|
|
|
|
//fmt.Errorf("could not find any status for ID")
|
|
|
|
return c.SendString(elements.ProgressBar("0"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if status.Progress == 100 {
|
2024-04-27 07:08:33 +00:00
|
|
|
c.Set("HX-Trigger", "done") // this triggers /browse/job/:uid (which is when the job is done)
|
2024-04-23 07:22:58 +00:00
|
|
|
return c.SendString(elements.ProgressBar("100"))
|
|
|
|
}
|
|
|
|
if status.Error != nil {
|
|
|
|
return c.SendString(elements.ErrorProgress(status.Error.Error()))
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.SendString(elements.ProgressBar(fmt.Sprint(status.Progress)))
|
|
|
|
})
|
|
|
|
|
2024-04-27 07:08:33 +00:00
|
|
|
// this route is hit when the job is done, and we display the
|
|
|
|
// final state (for now just displays "Installation completed")
|
2024-04-23 07:22:58 +00:00
|
|
|
app.Get("/browse/job/:uid", auth, func(c *fiber.Ctx) error {
|
2024-04-28 21:42:46 +00:00
|
|
|
|
|
|
|
status := galleryService.GetStatus(c.Params("uid"))
|
|
|
|
|
2024-04-27 07:08:33 +00:00
|
|
|
for _, k := range installingModels.Keys() {
|
|
|
|
if installingModels.Get(k) == c.Params("uid") {
|
|
|
|
installingModels.Delete(k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-28 21:42:46 +00:00
|
|
|
displayText := "Installation completed"
|
|
|
|
if status.Deletion {
|
|
|
|
displayText = "Deletion completed"
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.SendString(elements.DoneProgress(c.Params("uid"), displayText))
|
2024-04-23 07:22:58 +00:00
|
|
|
})
|
2024-05-02 19:14:10 +00:00
|
|
|
|
|
|
|
// Show the Chat page
|
|
|
|
app.Get("/chat/:model", auth, func(c *fiber.Ctx) error {
|
|
|
|
backendConfigs := cl.GetAllBackendConfigs()
|
|
|
|
|
|
|
|
summary := fiber.Map{
|
|
|
|
"Title": "LocalAI - Chat with " + c.Params("model"),
|
|
|
|
"ModelsConfig": backendConfigs,
|
|
|
|
"Model": c.Params("model"),
|
|
|
|
"Version": internal.PrintableVersion(),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Render index
|
|
|
|
return c.Render("views/chat", summary)
|
|
|
|
})
|
|
|
|
app.Get("/chat/", auth, func(c *fiber.Ctx) error {
|
|
|
|
|
|
|
|
backendConfigs := cl.GetAllBackendConfigs()
|
|
|
|
|
|
|
|
if len(backendConfigs) == 0 {
|
|
|
|
return c.SendString("No models available")
|
|
|
|
}
|
|
|
|
|
|
|
|
summary := fiber.Map{
|
|
|
|
"Title": "LocalAI - Chat with " + backendConfigs[0].Name,
|
|
|
|
"ModelsConfig": backendConfigs,
|
|
|
|
"Model": backendConfigs[0].Name,
|
|
|
|
"Version": internal.PrintableVersion(),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Render index
|
|
|
|
return c.Render("views/chat", summary)
|
|
|
|
})
|
|
|
|
|
|
|
|
app.Get("/text2image/:model", auth, func(c *fiber.Ctx) error {
|
|
|
|
backendConfigs := cl.GetAllBackendConfigs()
|
|
|
|
|
|
|
|
summary := fiber.Map{
|
|
|
|
"Title": "LocalAI - Generate images with " + c.Params("model"),
|
|
|
|
"ModelsConfig": backendConfigs,
|
|
|
|
"Model": c.Params("model"),
|
|
|
|
"Version": internal.PrintableVersion(),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Render index
|
|
|
|
return c.Render("views/text2image", summary)
|
|
|
|
})
|
|
|
|
|
|
|
|
app.Get("/text2image/", auth, func(c *fiber.Ctx) error {
|
|
|
|
|
|
|
|
backendConfigs := cl.GetAllBackendConfigs()
|
|
|
|
|
|
|
|
if len(backendConfigs) == 0 {
|
|
|
|
return c.SendString("No models available")
|
|
|
|
}
|
|
|
|
|
|
|
|
summary := fiber.Map{
|
|
|
|
"Title": "LocalAI - Generate images with " + backendConfigs[0].Name,
|
|
|
|
"ModelsConfig": backendConfigs,
|
|
|
|
"Model": backendConfigs[0].Name,
|
|
|
|
"Version": internal.PrintableVersion(),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Render index
|
|
|
|
return c.Render("views/text2image", summary)
|
|
|
|
})
|
|
|
|
|
|
|
|
app.Get("/tts/:model", auth, func(c *fiber.Ctx) error {
|
|
|
|
backendConfigs := cl.GetAllBackendConfigs()
|
|
|
|
|
|
|
|
summary := fiber.Map{
|
|
|
|
"Title": "LocalAI - Generate images with " + c.Params("model"),
|
|
|
|
"ModelsConfig": backendConfigs,
|
|
|
|
"Model": c.Params("model"),
|
|
|
|
"Version": internal.PrintableVersion(),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Render index
|
|
|
|
return c.Render("views/tts", summary)
|
|
|
|
})
|
|
|
|
|
|
|
|
app.Get("/tts/", auth, func(c *fiber.Ctx) error {
|
|
|
|
|
|
|
|
backendConfigs := cl.GetAllBackendConfigs()
|
|
|
|
|
|
|
|
if len(backendConfigs) == 0 {
|
|
|
|
return c.SendString("No models available")
|
|
|
|
}
|
|
|
|
|
|
|
|
summary := fiber.Map{
|
|
|
|
"Title": "LocalAI - Generate audio with " + backendConfigs[0].Name,
|
|
|
|
"ModelsConfig": backendConfigs,
|
|
|
|
"Model": backendConfigs[0].Name,
|
|
|
|
"Version": internal.PrintableVersion(),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Render index
|
|
|
|
return c.Render("views/tts", summary)
|
|
|
|
})
|
2024-04-23 07:22:58 +00:00
|
|
|
}
|