2024-03-01 15:19:53 +00:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2024-06-05 06:45:24 +00:00
|
|
|
"fmt"
|
2024-03-01 15:19:53 +00:00
|
|
|
"os"
|
2024-04-28 21:42:46 +00:00
|
|
|
"path/filepath"
|
2024-03-01 15:19:53 +00:00
|
|
|
"sync"
|
|
|
|
|
2024-06-23 08:24:36 +00:00
|
|
|
"github.com/mudler/LocalAI/core/config"
|
2024-06-24 15:32:12 +00:00
|
|
|
"github.com/mudler/LocalAI/core/gallery"
|
2024-06-23 08:24:36 +00:00
|
|
|
"github.com/mudler/LocalAI/pkg/startup"
|
|
|
|
"github.com/mudler/LocalAI/pkg/utils"
|
2024-03-01 15:19:53 +00:00
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
type GalleryService struct {
|
2024-06-05 06:45:24 +00:00
|
|
|
appConfig *config.ApplicationConfig
|
2024-03-01 15:19:53 +00:00
|
|
|
sync.Mutex
|
|
|
|
C chan gallery.GalleryOp
|
|
|
|
statuses map[string]*gallery.GalleryOpStatus
|
|
|
|
}
|
|
|
|
|
2024-06-05 06:45:24 +00:00
|
|
|
func NewGalleryService(appConfig *config.ApplicationConfig) *GalleryService {
|
2024-03-01 15:19:53 +00:00
|
|
|
return &GalleryService{
|
2024-06-05 06:45:24 +00:00
|
|
|
appConfig: appConfig,
|
2024-03-01 15:19:53 +00:00
|
|
|
C: make(chan gallery.GalleryOp),
|
|
|
|
statuses: make(map[string]*gallery.GalleryOpStatus),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-10 11:18:32 +00:00
|
|
|
func prepareModel(modelPath string, req gallery.GalleryModel, downloadStatus func(string, string, string, float64), enforceScan bool) error {
|
2024-04-17 21:33:49 +00:00
|
|
|
|
2024-06-04 14:32:47 +00:00
|
|
|
config, err := gallery.GetGalleryConfigFromURL(req.URL, modelPath)
|
2024-04-17 21:33:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
config.Files = append(config.Files, req.AdditionalFiles...)
|
|
|
|
|
2024-07-10 11:18:32 +00:00
|
|
|
return gallery.InstallModel(modelPath, req.Name, &config, req.Overrides, downloadStatus, enforceScan)
|
2024-04-17 21:33:49 +00:00
|
|
|
}
|
|
|
|
|
2024-03-01 15:19:53 +00:00
|
|
|
func (g *GalleryService) UpdateStatus(s string, op *gallery.GalleryOpStatus) {
|
|
|
|
g.Lock()
|
|
|
|
defer g.Unlock()
|
|
|
|
g.statuses[s] = op
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *GalleryService) GetStatus(s string) *gallery.GalleryOpStatus {
|
|
|
|
g.Lock()
|
|
|
|
defer g.Unlock()
|
|
|
|
|
|
|
|
return g.statuses[s]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *GalleryService) GetAllStatus() map[string]*gallery.GalleryOpStatus {
|
|
|
|
g.Lock()
|
|
|
|
defer g.Unlock()
|
|
|
|
|
|
|
|
return g.statuses
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *GalleryService) Start(c context.Context, cl *config.BackendConfigLoader) {
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-c.Done():
|
|
|
|
return
|
|
|
|
case op := <-g.C:
|
|
|
|
utils.ResetDownloadTimers()
|
|
|
|
|
|
|
|
g.UpdateStatus(op.Id, &gallery.GalleryOpStatus{Message: "processing", Progress: 0})
|
|
|
|
|
|
|
|
// updates the status with an error
|
2024-06-05 06:45:24 +00:00
|
|
|
var updateError func(e error)
|
|
|
|
if !g.appConfig.OpaqueErrors {
|
|
|
|
updateError = func(e error) {
|
|
|
|
g.UpdateStatus(op.Id, &gallery.GalleryOpStatus{Error: e, Processed: true, Message: "error: " + e.Error()})
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
updateError = func(_ error) {
|
|
|
|
g.UpdateStatus(op.Id, &gallery.GalleryOpStatus{Error: fmt.Errorf("an error occurred"), Processed: true})
|
|
|
|
}
|
2024-03-01 15:19:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// displayDownload displays the download progress
|
|
|
|
progressCallback := func(fileName string, current string, total string, percentage float64) {
|
|
|
|
g.UpdateStatus(op.Id, &gallery.GalleryOpStatus{Message: "processing", FileName: fileName, Progress: percentage, TotalFileSize: total, DownloadedFileSize: current})
|
|
|
|
utils.DisplayDownloadFunction(fileName, current, total, percentage)
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
2024-04-28 21:42:46 +00:00
|
|
|
|
|
|
|
// delete a model
|
|
|
|
if op.Delete {
|
|
|
|
modelConfig := &config.BackendConfig{}
|
2024-06-24 15:32:12 +00:00
|
|
|
|
2024-04-28 21:42:46 +00:00
|
|
|
// Galleryname is the name of the model in this case
|
2024-06-05 06:45:24 +00:00
|
|
|
dat, err := os.ReadFile(filepath.Join(g.appConfig.ModelPath, op.GalleryModelName+".yaml"))
|
2024-04-28 21:42:46 +00:00
|
|
|
if err != nil {
|
|
|
|
updateError(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
err = yaml.Unmarshal(dat, modelConfig)
|
|
|
|
if err != nil {
|
|
|
|
updateError(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
files := []string{}
|
|
|
|
// Remove the model from the config
|
|
|
|
if modelConfig.Model != "" {
|
|
|
|
files = append(files, modelConfig.ModelFileName())
|
2024-03-01 15:19:53 +00:00
|
|
|
}
|
2024-04-28 21:42:46 +00:00
|
|
|
|
|
|
|
if modelConfig.MMProj != "" {
|
|
|
|
files = append(files, modelConfig.MMProjFileName())
|
|
|
|
}
|
|
|
|
|
2024-06-05 06:45:24 +00:00
|
|
|
err = gallery.DeleteModelFromSystem(g.appConfig.ModelPath, op.GalleryModelName, files)
|
2024-06-13 14:12:46 +00:00
|
|
|
if err != nil {
|
|
|
|
updateError(err)
|
|
|
|
continue
|
|
|
|
}
|
2024-03-01 15:19:53 +00:00
|
|
|
} else {
|
2024-04-28 21:42:46 +00:00
|
|
|
// if the request contains a gallery name, we apply the gallery from the gallery list
|
2024-05-06 23:17:07 +00:00
|
|
|
if op.GalleryModelName != "" {
|
2024-07-10 11:18:32 +00:00
|
|
|
err = gallery.InstallModelFromGallery(op.Galleries, op.GalleryModelName, g.appConfig.ModelPath, op.Req, progressCallback, g.appConfig.EnforcePredownloadScans)
|
2024-04-28 21:42:46 +00:00
|
|
|
} else if op.ConfigURL != "" {
|
2024-07-10 11:18:32 +00:00
|
|
|
err = startup.InstallModels(op.Galleries, op.ConfigURL, g.appConfig.ModelPath, g.appConfig.EnforcePredownloadScans, progressCallback, op.ConfigURL)
|
2024-06-13 14:12:46 +00:00
|
|
|
if err != nil {
|
|
|
|
updateError(err)
|
|
|
|
continue
|
|
|
|
}
|
2024-06-05 06:45:24 +00:00
|
|
|
err = cl.Preload(g.appConfig.ModelPath)
|
2024-04-28 21:42:46 +00:00
|
|
|
} else {
|
2024-07-10 11:18:32 +00:00
|
|
|
err = prepareModel(g.appConfig.ModelPath, op.Req, progressCallback, g.appConfig.EnforcePredownloadScans)
|
2024-04-28 21:42:46 +00:00
|
|
|
}
|
2024-03-01 15:19:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
updateError(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reload models
|
2024-06-05 06:45:24 +00:00
|
|
|
err = cl.LoadBackendConfigsFromPath(g.appConfig.ModelPath)
|
2024-03-01 15:19:53 +00:00
|
|
|
if err != nil {
|
|
|
|
updateError(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-06-05 06:45:24 +00:00
|
|
|
err = cl.Preload(g.appConfig.ModelPath)
|
2024-03-01 15:19:53 +00:00
|
|
|
if err != nil {
|
|
|
|
updateError(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-04-28 21:42:46 +00:00
|
|
|
g.UpdateStatus(op.Id,
|
|
|
|
&gallery.GalleryOpStatus{
|
2024-05-06 23:17:07 +00:00
|
|
|
Deletion: op.Delete,
|
|
|
|
Processed: true,
|
|
|
|
GalleryModelName: op.GalleryModelName,
|
|
|
|
Message: "completed",
|
|
|
|
Progress: 100})
|
2024-03-01 15:19:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
type galleryModel struct {
|
|
|
|
gallery.GalleryModel `yaml:",inline"` // https://github.com/go-yaml/yaml/issues/63
|
|
|
|
ID string `json:"id"`
|
|
|
|
}
|
|
|
|
|
2024-07-10 11:18:32 +00:00
|
|
|
func processRequests(modelPath string, enforceScan bool, galleries []config.Gallery, requests []galleryModel) error {
|
2024-03-01 15:19:53 +00:00
|
|
|
var err error
|
|
|
|
for _, r := range requests {
|
|
|
|
utils.ResetDownloadTimers()
|
|
|
|
if r.ID == "" {
|
2024-07-10 11:18:32 +00:00
|
|
|
err = prepareModel(modelPath, r.GalleryModel, utils.DisplayDownloadFunction, enforceScan)
|
2024-04-17 21:33:49 +00:00
|
|
|
|
2024-03-01 15:19:53 +00:00
|
|
|
} else {
|
2024-06-13 14:12:46 +00:00
|
|
|
err = gallery.InstallModelFromGallery(
|
2024-07-10 11:18:32 +00:00
|
|
|
galleries, r.ID, modelPath, r.GalleryModel, utils.DisplayDownloadFunction, enforceScan)
|
2024-03-01 15:19:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-07-10 11:18:32 +00:00
|
|
|
func ApplyGalleryFromFile(modelPath, s string, enforceScan bool, galleries []config.Gallery) error {
|
2024-03-01 15:19:53 +00:00
|
|
|
dat, err := os.ReadFile(s)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var requests []galleryModel
|
|
|
|
|
|
|
|
if err := yaml.Unmarshal(dat, &requests); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-07-10 11:18:32 +00:00
|
|
|
return processRequests(modelPath, enforceScan, galleries, requests)
|
2024-03-01 15:19:53 +00:00
|
|
|
}
|
|
|
|
|
2024-07-10 11:18:32 +00:00
|
|
|
func ApplyGalleryFromString(modelPath, s string, enforceScan bool, galleries []config.Gallery) error {
|
2024-03-01 15:19:53 +00:00
|
|
|
var requests []galleryModel
|
|
|
|
err := json.Unmarshal([]byte(s), &requests)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-07-10 11:18:32 +00:00
|
|
|
return processRequests(modelPath, enforceScan, galleries, requests)
|
2024-03-01 15:19:53 +00:00
|
|
|
}
|