You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
241 lines
6.1 KiB
241 lines
6.1 KiB
1 year ago
|
package localai
|
||
2 years ago
|
|
||
|
import (
|
||
|
"context"
|
||
|
"fmt"
|
||
2 years ago
|
"os"
|
||
2 years ago
|
"sync"
|
||
2 years ago
|
"time"
|
||
2 years ago
|
|
||
2 years ago
|
json "github.com/json-iterator/go"
|
||
|
|
||
1 year ago
|
config "github.com/go-skynet/LocalAI/api/config"
|
||
2 years ago
|
"github.com/go-skynet/LocalAI/pkg/gallery"
|
||
|
"github.com/gofiber/fiber/v2"
|
||
|
"github.com/google/uuid"
|
||
2 years ago
|
"github.com/rs/zerolog/log"
|
||
2 years ago
|
)
|
||
|
|
||
|
type galleryOp struct {
|
||
2 years ago
|
req gallery.GalleryModel
|
||
|
id string
|
||
|
galleries []gallery.Gallery
|
||
|
galleryName string
|
||
2 years ago
|
}
|
||
|
|
||
|
type galleryOpStatus struct {
|
||
2 years ago
|
Error error `json:"error"`
|
||
|
Processed bool `json:"processed"`
|
||
|
Message string `json:"message"`
|
||
|
Progress float64 `json:"progress"`
|
||
|
TotalFileSize string `json:"file_size"`
|
||
|
DownloadedFileSize string `json:"downloaded_size"`
|
||
2 years ago
|
}
|
||
|
|
||
|
type galleryApplier struct {
|
||
|
modelPath string
|
||
|
sync.Mutex
|
||
|
C chan galleryOp
|
||
|
statuses map[string]*galleryOpStatus
|
||
|
}
|
||
|
|
||
1 year ago
|
func NewGalleryService(modelPath string) *galleryApplier {
|
||
2 years ago
|
return &galleryApplier{
|
||
|
modelPath: modelPath,
|
||
|
C: make(chan galleryOp),
|
||
|
statuses: make(map[string]*galleryOpStatus),
|
||
|
}
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
// prepareModel applies a
|
||
1 year ago
|
func prepareModel(modelPath string, req gallery.GalleryModel, cm *config.ConfigLoader, downloadStatus func(string, string, string, float64)) error {
|
||
2 years ago
|
|
||
2 years ago
|
config, err := gallery.GetGalleryConfigFromURL(req.URL)
|
||
2 years ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
config.Files = append(config.Files, req.AdditionalFiles...)
|
||
|
|
||
2 years ago
|
return gallery.InstallModel(modelPath, req.Name, &config, req.Overrides, downloadStatus)
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
func (g *galleryApplier) updateStatus(s string, op *galleryOpStatus) {
|
||
2 years ago
|
g.Lock()
|
||
|
defer g.Unlock()
|
||
|
g.statuses[s] = op
|
||
|
}
|
||
|
|
||
2 years ago
|
func (g *galleryApplier) getStatus(s string) *galleryOpStatus {
|
||
2 years ago
|
g.Lock()
|
||
|
defer g.Unlock()
|
||
|
|
||
|
return g.statuses[s]
|
||
|
}
|
||
|
|
||
1 year ago
|
func (g *galleryApplier) Start(c context.Context, cm *config.ConfigLoader) {
|
||
2 years ago
|
go func() {
|
||
|
for {
|
||
|
select {
|
||
|
case <-c.Done():
|
||
|
return
|
||
|
case op := <-g.C:
|
||
2 years ago
|
g.updateStatus(op.id, &galleryOpStatus{Message: "processing", Progress: 0})
|
||
2 years ago
|
|
||
2 years ago
|
// updates the status with an error
|
||
2 years ago
|
updateError := func(e error) {
|
||
2 years ago
|
g.updateStatus(op.id, &galleryOpStatus{Error: e, Processed: true, Message: "error: " + e.Error()})
|
||
2 years ago
|
}
|
||
2 years ago
|
|
||
2 years ago
|
// displayDownload displays the download progress
|
||
|
progressCallback := func(fileName string, current string, total string, percentage float64) {
|
||
|
g.updateStatus(op.id, &galleryOpStatus{Message: "processing", Progress: percentage, TotalFileSize: total, DownloadedFileSize: current})
|
||
2 years ago
|
displayDownload(fileName, current, total, percentage)
|
||
2 years ago
|
}
|
||
|
|
||
|
var err error
|
||
|
// if the request contains a gallery name, we apply the gallery from the gallery list
|
||
|
if op.galleryName != "" {
|
||
|
err = gallery.InstallModelFromGallery(op.galleries, op.galleryName, g.modelPath, op.req, progressCallback)
|
||
|
} else {
|
||
|
err = prepareModel(g.modelPath, op.req, cm, progressCallback)
|
||
|
}
|
||
|
|
||
|
if err != nil {
|
||
2 years ago
|
updateError(err)
|
||
|
continue
|
||
|
}
|
||
|
|
||
2 years ago
|
// Reload models
|
||
|
err = cm.LoadConfigs(g.modelPath)
|
||
|
if err != nil {
|
||
|
updateError(err)
|
||
|
continue
|
||
|
}
|
||
|
|
||
|
g.updateStatus(op.id, &galleryOpStatus{Processed: true, Message: "completed", Progress: 100})
|
||
2 years ago
|
}
|
||
|
}
|
||
|
}()
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
var lastProgress time.Time = time.Now()
|
||
|
var startTime time.Time = time.Now()
|
||
|
|
||
|
func displayDownload(fileName string, current string, total string, percentage float64) {
|
||
|
currentTime := time.Now()
|
||
|
|
||
|
if currentTime.Sub(lastProgress) >= 5*time.Second {
|
||
|
|
||
|
lastProgress = currentTime
|
||
|
|
||
|
// calculate ETA based on percentage and elapsed time
|
||
|
var eta time.Duration
|
||
|
if percentage > 0 {
|
||
|
elapsed := currentTime.Sub(startTime)
|
||
|
eta = time.Duration(float64(elapsed)*(100/percentage) - float64(elapsed))
|
||
|
}
|
||
|
|
||
|
if total != "" {
|
||
|
log.Debug().Msgf("Downloading %s: %s/%s (%.2f%%) ETA: %s", fileName, current, total, percentage, eta)
|
||
|
} else {
|
||
|
log.Debug().Msgf("Downloading: %s", current)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
2 years ago
|
type galleryModel struct {
|
||
|
gallery.GalleryModel
|
||
|
ID string `json:"id"`
|
||
|
}
|
||
|
|
||
1 year ago
|
func ApplyGalleryFromFile(modelPath, s string, cm *config.ConfigLoader, galleries []gallery.Gallery) error {
|
||
2 years ago
|
dat, err := os.ReadFile(s)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
2 years ago
|
return ApplyGalleryFromString(modelPath, string(dat), cm, galleries)
|
||
2 years ago
|
}
|
||
2 years ago
|
|
||
1 year ago
|
func ApplyGalleryFromString(modelPath, s string, cm *config.ConfigLoader, galleries []gallery.Gallery) error {
|
||
2 years ago
|
var requests []galleryModel
|
||
2 years ago
|
err := json.Unmarshal([]byte(s), &requests)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
for _, r := range requests {
|
||
2 years ago
|
if r.ID == "" {
|
||
|
err = prepareModel(modelPath, r.GalleryModel, cm, displayDownload)
|
||
|
} else {
|
||
|
err = gallery.InstallModelFromGallery(galleries, r.ID, modelPath, r.GalleryModel, displayDownload)
|
||
2 years ago
|
}
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
return err
|
||
2 years ago
|
}
|
||
|
|
||
1 year ago
|
/// Endpoints
|
||
|
|
||
|
func GetOpStatusEndpoint(g *galleryApplier) func(c *fiber.Ctx) error {
|
||
2 years ago
|
return func(c *fiber.Ctx) error {
|
||
|
|
||
2 years ago
|
status := g.getStatus(c.Params("uuid"))
|
||
2 years ago
|
if status == nil {
|
||
|
return fmt.Errorf("could not find any status for ID")
|
||
|
}
|
||
|
|
||
|
return c.JSON(status)
|
||
|
}
|
||
|
}
|
||
|
|
||
2 years ago
|
type GalleryModel struct {
|
||
|
ID string `json:"id"`
|
||
|
gallery.GalleryModel
|
||
|
}
|
||
|
|
||
1 year ago
|
func ApplyModelGalleryEndpoint(modelPath string, cm *config.ConfigLoader, g chan galleryOp, galleries []gallery.Gallery) func(c *fiber.Ctx) error {
|
||
2 years ago
|
return func(c *fiber.Ctx) error {
|
||
2 years ago
|
input := new(GalleryModel)
|
||
2 years ago
|
// Get input data from the request body
|
||
|
if err := c.BodyParser(input); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
uuid, err := uuid.NewUUID()
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
g <- galleryOp{
|
||
2 years ago
|
req: input.GalleryModel,
|
||
|
id: uuid.String(),
|
||
|
galleryName: input.ID,
|
||
|
galleries: galleries,
|
||
2 years ago
|
}
|
||
|
return c.JSON(struct {
|
||
2 years ago
|
ID string `json:"uuid"`
|
||
2 years ago
|
StatusURL string `json:"status"`
|
||
|
}{ID: uuid.String(), StatusURL: c.BaseURL() + "/models/jobs/" + uuid.String()})
|
||
|
}
|
||
|
}
|
||
2 years ago
|
|
||
1 year ago
|
func ListModelFromGalleryEndpoint(galleries []gallery.Gallery, basePath string) func(c *fiber.Ctx) error {
|
||
2 years ago
|
return func(c *fiber.Ctx) error {
|
||
|
log.Debug().Msgf("Listing models from galleries: %+v", galleries)
|
||
|
|
||
|
models, err := gallery.AvailableGalleryModels(galleries, basePath)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
log.Debug().Msgf("Models found from galleries: %+v", models)
|
||
|
for _, m := range models {
|
||
|
log.Debug().Msgf("Model found from galleries: %+v", m)
|
||
|
}
|
||
|
dat, err := json.Marshal(models)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
return c.Send(dat)
|
||
|
}
|
||
|
}
|