mirror of
https://github.com/mudler/LocalAI.git
synced 2026-07-30 18:09:05 -04:00
* feat(config): add model artifact source contract Assisted-by: Codex:GPT-5 [Codex] * feat(downloader): add authenticated raw-byte progress Assisted-by: Codex:GPT-5 [Codex] * feat(huggingface): resolve immutable snapshot manifests Assisted-by: Codex:GPT-5 [Codex] * feat(models): add artifact storage primitives Assisted-by: Codex:GPT-5 [Codex] * feat(models): materialize pinned Hugging Face snapshots Assisted-by: Codex:GPT-5 [Codex] * feat(models): bind managed snapshots at runtime Assisted-by: Codex:GPT-5 [Codex] * feat(gallery): materialize model artifacts during install Assisted-by: Codex:GPT-5 [Codex] * feat(gallery): declare managed Hugging Face artifacts Assisted-by: Codex:GPT-5 [Codex] * feat(models): preload managed model artifacts Assisted-by: Codex:GPT-5 [Codex] * fix(gallery): retain shared artifact caches on delete Assisted-by: Codex:GPT-5 [Codex] * feat(models): report artifact acquisition progress Assisted-by: Codex:GPT-5 [Codex] * refactor(backends): load managed models from ModelFile Assisted-by: Codex:GPT-5 [Codex] * refactor(backends): load staged speech model snapshots Assisted-by: Codex:GPT-5 [Codex] * refactor(backends): use staged snapshots in engine backends Assisted-by: Codex:GPT-5 [Codex] * test(distributed): cover staged artifact snapshots Assisted-by: Codex:GPT-5 [Codex] * docs: explain managed model artifacts Assisted-by: Codex:GPT-5 [Codex] * docs: add product design context Assisted-by: Codex:GPT-5 [Codex] * feat(ui): show model artifact download progress Assisted-by: Codex:GPT-5 [Codex] * Eagerly materialize Hugging Face artifacts Materialize HF-backed model references as managed GGUF artifacts during load, with lazy download retained only as fallback. Assisted-by: Codex:GPT-5 [shell] * Refactor HF downloads through a shared executor Assisted-by: Codex:GPT-5 [shell] * drop Signed-off-by: Ettore Di Giacinto <mudler@localai.io> --------- Signed-off-by: Ettore Di Giacinto <mudler@localai.io> Co-authored-by: Ettore Di Giacinto <mudler@localai.io>
218 lines
7.5 KiB
Go
218 lines
7.5 KiB
Go
package galleryop
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
|
|
"github.com/mudler/LocalAI/core/config"
|
|
"github.com/mudler/LocalAI/core/gallery"
|
|
"github.com/mudler/LocalAI/core/services/messaging"
|
|
"github.com/mudler/LocalAI/pkg/model"
|
|
"github.com/mudler/LocalAI/pkg/modelartifacts"
|
|
"github.com/mudler/LocalAI/pkg/system"
|
|
"github.com/mudler/LocalAI/pkg/utils"
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
const (
|
|
processingMessage = "processing file: %s. Total: %s. Current: %s"
|
|
)
|
|
|
|
func (g *GalleryService) modelHandler(op *ManagementOp[gallery.GalleryModel, gallery.ModelConfig], cl *config.ModelConfigLoader, systemState *system.SystemState) error {
|
|
utils.ResetDownloadTimers()
|
|
|
|
// Dedup check in distributed mode — skip if another instance is already processing this element
|
|
if g.galleryStore != nil && op.GalleryElementName != "" && !op.Delete {
|
|
dup, err := g.galleryStore.FindDuplicate(op.GalleryElementName)
|
|
if err == nil && dup != nil && dup.ID != op.ID {
|
|
g.UpdateStatus(op.ID, &OpStatus{
|
|
Processed: true,
|
|
Message: fmt.Sprintf("already being processed by another instance (op %s)", dup.ID),
|
|
})
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// Check if already cancelled
|
|
if op.Context != nil {
|
|
select {
|
|
case <-op.Context.Done():
|
|
g.UpdateStatus(op.ID, &OpStatus{
|
|
Cancelled: true,
|
|
Processed: true,
|
|
Message: "cancelled",
|
|
GalleryElementName: op.GalleryElementName,
|
|
})
|
|
return op.Context.Err()
|
|
default:
|
|
}
|
|
}
|
|
|
|
g.UpdateStatus(op.ID, &OpStatus{Message: fmt.Sprintf("processing model: %s", op.GalleryElementName), Progress: 0, Cancellable: true})
|
|
|
|
bridge := newArtifactProgressBridge(func(status *OpStatus) {
|
|
status.GalleryElementName = op.GalleryElementName
|
|
g.UpdateStatus(op.ID, status)
|
|
})
|
|
operationCtx := op.Context
|
|
if operationCtx == nil {
|
|
operationCtx = context.Background()
|
|
}
|
|
operationCtx = modelartifacts.WithProgressSink(operationCtx, bridge.Sink)
|
|
|
|
// displayDownload displays the download progress
|
|
progressCallback := func(fileName string, current string, total string, percentage float64) {
|
|
// Check for cancellation during progress updates
|
|
if op.Context != nil {
|
|
select {
|
|
case <-op.Context.Done():
|
|
return
|
|
default:
|
|
}
|
|
}
|
|
percentage = bridge.ClampLegacy(percentage)
|
|
g.UpdateStatus(op.ID, &OpStatus{Message: fmt.Sprintf(processingMessage, fileName, total, current), FileName: fileName, Progress: percentage, TotalFileSize: total, DownloadedFileSize: current, Cancellable: true})
|
|
utils.DisplayDownloadFunction(fileName, current, total, percentage)
|
|
}
|
|
|
|
var err error
|
|
if op.Delete {
|
|
err = g.modelManager.DeleteModel(op.GalleryElementName)
|
|
} else {
|
|
err = g.modelManager.InstallModel(operationCtx, op, progressCallback)
|
|
}
|
|
if err != nil {
|
|
// Check if error is due to cancellation
|
|
if op.Context != nil && errors.Is(err, op.Context.Err()) {
|
|
g.UpdateStatus(op.ID, &OpStatus{
|
|
Cancelled: true,
|
|
Processed: true,
|
|
Message: "cancelled",
|
|
GalleryElementName: op.GalleryElementName,
|
|
})
|
|
return err
|
|
}
|
|
return err
|
|
}
|
|
|
|
// Check for cancellation before final steps
|
|
if op.Context != nil {
|
|
select {
|
|
case <-op.Context.Done():
|
|
g.UpdateStatus(op.ID, &OpStatus{
|
|
Cancelled: true,
|
|
Processed: true,
|
|
Message: "cancelled",
|
|
GalleryElementName: op.GalleryElementName,
|
|
})
|
|
return op.Context.Err()
|
|
default:
|
|
}
|
|
}
|
|
|
|
// Reload models
|
|
err = cl.LoadModelConfigsFromPath(systemState.Model.ModelsPath, g.appConfig.ToConfigLoaderOptions()...)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = cl.PreloadWithContext(operationCtx, systemState.Model.ModelsPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Tell peer replicas to refresh their own ModelConfigLoader. The local
|
|
// LoadModelConfigsFromPath above already covered THIS replica; without
|
|
// this broadcast a chat completion routed by the load balancer to a peer
|
|
// would fail to find a model just installed.
|
|
op2 := "install"
|
|
if op.Delete {
|
|
op2 = "delete"
|
|
}
|
|
g.publishCacheInvalidate(messaging.SubjectCacheInvalidateModels, messaging.CacheInvalidateEvent{
|
|
Element: op.GalleryElementName,
|
|
Op: op2,
|
|
})
|
|
|
|
g.UpdateStatus(op.ID,
|
|
&OpStatus{
|
|
Deletion: op.Delete,
|
|
Processed: true,
|
|
GalleryElementName: op.GalleryElementName,
|
|
Message: "completed",
|
|
Progress: 100,
|
|
Cancellable: false})
|
|
|
|
return nil
|
|
}
|
|
|
|
func installModelFromRemoteConfig(ctx context.Context, systemState *system.SystemState, modelLoader *model.ModelLoader, req gallery.GalleryModel, downloadStatus func(string, string, string, float64), enforceScan, automaticallyInstallBackend bool, backendGalleries []config.Gallery, requireBackendIntegrity bool, options ...gallery.InstallOption) error {
|
|
config, err := gallery.GetGalleryConfigFromURLWithContext[gallery.ModelConfig](ctx, req.URL, systemState.Model.ModelsPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
config.Files = append(config.Files, req.AdditionalFiles...)
|
|
|
|
installedModel, err := gallery.InstallModel(ctx, systemState, req.Name, &config, req.Overrides, downloadStatus, enforceScan, options...)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if automaticallyInstallBackend && installedModel.Backend != "" {
|
|
if err := gallery.InstallBackendFromGallery(ctx, backendGalleries, systemState, modelLoader, installedModel.Backend, downloadStatus, false, requireBackendIntegrity); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
type galleryModel struct {
|
|
gallery.GalleryModel `yaml:",inline"` // https://github.com/go-yaml/yaml/issues/63
|
|
ID string `json:"id"`
|
|
}
|
|
|
|
func processRequests(systemState *system.SystemState, modelLoader *model.ModelLoader, enforceScan, automaticallyInstallBackend bool, galleries []config.Gallery, backendGalleries []config.Gallery, requests []galleryModel, requireBackendIntegrity bool, options ...gallery.InstallOption) error {
|
|
ctx := context.Background()
|
|
var err error
|
|
for _, r := range requests {
|
|
utils.ResetDownloadTimers()
|
|
if r.ID == "" {
|
|
err = installModelFromRemoteConfig(ctx, systemState, modelLoader, r.GalleryModel, utils.DisplayDownloadFunction, enforceScan, automaticallyInstallBackend, backendGalleries, requireBackendIntegrity, options...)
|
|
|
|
} else {
|
|
err = gallery.InstallModelFromGallery(
|
|
ctx, galleries, backendGalleries, systemState, modelLoader, r.ID, r.GalleryModel, utils.DisplayDownloadFunction, enforceScan, automaticallyInstallBackend, requireBackendIntegrity, options...)
|
|
}
|
|
}
|
|
return err
|
|
}
|
|
|
|
func ApplyGalleryFromFile(systemState *system.SystemState, modelLoader *model.ModelLoader, enforceScan, automaticallyInstallBackend bool, galleries []config.Gallery, backendGalleries []config.Gallery, s string, requireBackendIntegrity bool, options ...gallery.InstallOption) error {
|
|
dat, err := os.ReadFile(s)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
var requests []galleryModel
|
|
|
|
if err := yaml.Unmarshal(dat, &requests); err != nil {
|
|
return err
|
|
}
|
|
|
|
return processRequests(systemState, modelLoader, enforceScan, automaticallyInstallBackend, galleries, backendGalleries, requests, requireBackendIntegrity, options...)
|
|
}
|
|
|
|
func ApplyGalleryFromString(systemState *system.SystemState, modelLoader *model.ModelLoader, enforceScan, automaticallyInstallBackend bool, galleries []config.Gallery, backendGalleries []config.Gallery, s string, requireBackendIntegrity bool, options ...gallery.InstallOption) error {
|
|
var requests []galleryModel
|
|
err := json.Unmarshal([]byte(s), &requests)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return processRequests(systemState, modelLoader, enforceScan, automaticallyInstallBackend, galleries, backendGalleries, requests, requireBackendIntegrity, options...)
|
|
}
|