Skip to content

Commit

Permalink
feat(preload): prepare models in galleries (#1515)
Browse files Browse the repository at this point in the history
Previously if applying models from the gallery API, we didn't actually
allowed remote URLs as models as nothing was actually downloading the
models referenced in the configuration file. Now we call Preload after
we have all the models loaded in memory.
  • Loading branch information
mudler authored Dec 30, 2023
1 parent a95bb05 commit c1888a8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
24 changes: 12 additions & 12 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,18 @@ func Startup(opts ...options.AppOption) (*options.Option, *config.ConfigLoader,
log.Error().Msgf("error downloading models: %s", err.Error())
}

if options.PreloadJSONModels != "" {
if err := localai.ApplyGalleryFromString(options.Loader.ModelPath, options.PreloadJSONModels, cl, options.Galleries); err != nil {
return nil, nil, err
}
}

if options.PreloadModelsFromPath != "" {
if err := localai.ApplyGalleryFromFile(options.Loader.ModelPath, options.PreloadModelsFromPath, cl, options.Galleries); err != nil {
return nil, nil, err
}
}

if options.Debug {
for _, v := range cl.ListConfigs() {
cfg, _ := cl.GetConfig(v)
Expand All @@ -67,18 +79,6 @@ func Startup(opts ...options.AppOption) (*options.Option, *config.ConfigLoader,
}
}

if options.PreloadJSONModels != "" {
if err := localai.ApplyGalleryFromString(options.Loader.ModelPath, options.PreloadJSONModels, cl, options.Galleries); err != nil {
return nil, nil, err
}
}

if options.PreloadModelsFromPath != "" {
if err := localai.ApplyGalleryFromFile(options.Loader.ModelPath, options.PreloadModelsFromPath, cl, options.Galleries); err != nil {
return nil, nil, err
}
}

// turn off any process that was started by GRPC if the context is canceled
go func() {
<-options.Context.Done()
Expand Down
1 change: 1 addition & 0 deletions api/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ func (cm *ConfigLoader) ListConfigs() []string {
return res
}

// Preload prepare models if they are not local but url or huggingface repositories
func (cm *ConfigLoader) Preload(modelPath string) error {
cm.Lock()
defer cm.Unlock()
Expand Down
6 changes: 6 additions & 0 deletions api/localai/gallery.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@ func (g *galleryApplier) Start(c context.Context, cm *config.ConfigLoader) {
continue
}

err = cm.Preload(g.modelPath)
if err != nil {
updateError(err)
continue
}

g.updateStatus(op.id, &galleryOpStatus{Processed: true, Message: "completed", Progress: 100})
}
}
Expand Down

0 comments on commit c1888a8

Please sign in to comment.