@@ -43,10 +43,10 @@ prompt_templates:
4343 content: ""
4444
4545*/
46- // Config is the model configuration which contains all the model details
46+ // ModelConfig is the model configuration which contains all the model details
4747// This configuration is read from the gallery endpoint and is used to download and install the model
4848// It is the internal structure, separated from the request
49- type Config struct {
49+ type ModelConfig struct {
5050 Description string `yaml:"description"`
5151 Icon string `yaml:"icon"`
5252 License string `yaml:"license"`
@@ -68,37 +68,78 @@ type PromptTemplate struct {
6868 Content string `yaml:"content"`
6969}
7070
71- func GetGalleryConfigFromURL (url string , basePath string ) (Config , error ) {
72- var config Config
73- uri := downloader .URI (url )
74- err := uri .DownloadWithCallback (basePath , func (url string , d []byte ) error {
75- return yaml .Unmarshal (d , & config )
76- })
77- if err != nil {
78- log .Error ().Err (err ).Str ("url" , url ).Msg ("failed to get gallery config for url" )
79- return config , err
71+ // Installs a model from the gallery
72+ func InstallModelFromGallery (galleries []config.Gallery , name string , basePath string , req GalleryModel , downloadStatus func (string , string , string , float64 ), enforceScan bool ) error {
73+
74+ applyModel := func (model * GalleryModel ) error {
75+ name = strings .ReplaceAll (name , string (os .PathSeparator ), "__" )
76+
77+ var config ModelConfig
78+
79+ if len (model .URL ) > 0 {
80+ var err error
81+ config , err = GetGalleryConfigFromURL [ModelConfig ](model .URL , basePath )
82+ if err != nil {
83+ return err
84+ }
85+ config .Description = model .Description
86+ config .License = model .License
87+ } else if len (model .ConfigFile ) > 0 {
88+ // TODO: is this worse than using the override method with a blank cfg yaml?
89+ reYamlConfig , err := yaml .Marshal (model .ConfigFile )
90+ if err != nil {
91+ return err
92+ }
93+ config = ModelConfig {
94+ ConfigFile : string (reYamlConfig ),
95+ Description : model .Description ,
96+ License : model .License ,
97+ URLs : model .URLs ,
98+ Name : model .Name ,
99+ Files : make ([]File , 0 ), // Real values get added below, must be blank
100+ // Prompt Template Skipped for now - I expect in this mode that they will be delivered as files.
101+ }
102+ } else {
103+ return fmt .Errorf ("invalid gallery model %+v" , model )
104+ }
105+
106+ installName := model .Name
107+ if req .Name != "" {
108+ installName = req .Name
109+ }
110+
111+ // Copy the model configuration from the request schema
112+ config .URLs = append (config .URLs , model .URLs ... )
113+ config .Icon = model .Icon
114+ config .Files = append (config .Files , req .AdditionalFiles ... )
115+ config .Files = append (config .Files , model .AdditionalFiles ... )
116+
117+ // TODO model.Overrides could be merged with user overrides (not defined yet)
118+ if err := mergo .Merge (& model .Overrides , req .Overrides , mergo .WithOverride ); err != nil {
119+ return err
120+ }
121+
122+ if err := InstallModel (basePath , installName , & config , model .Overrides , downloadStatus , enforceScan ); err != nil {
123+ return err
124+ }
125+
126+ return nil
80127 }
81- return config , nil
82- }
83128
84- func ReadConfigFile (filePath string ) (* Config , error ) {
85- // Read the YAML file
86- yamlFile , err := os .ReadFile (filePath )
129+ models , err := AvailableGalleryModels (galleries , basePath )
87130 if err != nil {
88- return nil , fmt . Errorf ( "failed to read YAML file: %v" , err )
131+ return err
89132 }
90133
91- // Unmarshal YAML data into a Config struct
92- var config Config
93- err = yaml .Unmarshal (yamlFile , & config )
94- if err != nil {
95- return nil , fmt .Errorf ("failed to unmarshal YAML: %v" , err )
134+ model := FindGalleryElement (models , name , basePath )
135+ if model == nil {
136+ return fmt .Errorf ("no model found with name %q" , name )
96137 }
97138
98- return & config , nil
139+ return applyModel ( model )
99140}
100141
101- func InstallModel (basePath , nameOverride string , config * Config , configOverrides map [string ]interface {}, downloadStatus func (string , string , string , float64 ), enforceScan bool ) error {
142+ func InstallModel (basePath , nameOverride string , config * ModelConfig , configOverrides map [string ]interface {}, downloadStatus func (string , string , string , float64 ), enforceScan bool ) error {
102143 // Create base path if it doesn't exist
103144 err := os .MkdirAll (basePath , 0750 )
104145 if err != nil {
@@ -222,10 +263,10 @@ func galleryFileName(name string) string {
222263 return "._gallery_" + name + ".yaml"
223264}
224265
225- func GetLocalModelConfiguration (basePath string , name string ) (* Config , error ) {
266+ func GetLocalModelConfiguration (basePath string , name string ) (* ModelConfig , error ) {
226267 name = strings .ReplaceAll (name , string (os .PathSeparator ), "__" )
227268 galleryFile := filepath .Join (basePath , galleryFileName (name ))
228- return ReadConfigFile (galleryFile )
269+ return ReadConfigFile [ ModelConfig ] (galleryFile )
229270}
230271
231272func DeleteModelFromSystem (basePath string , name string , additionalFiles []string ) error {
@@ -245,7 +286,7 @@ func DeleteModelFromSystem(basePath string, name string, additionalFiles []strin
245286 var err error
246287 // Delete all the files associated to the model
247288 // read the model config
248- galleryconfig , err := ReadConfigFile (galleryFile )
289+ galleryconfig , err := ReadConfigFile [ ModelConfig ] (galleryFile )
249290 if err != nil {
250291 log .Error ().Err (err ).Msgf ("failed to read gallery file %s" , configFile )
251292 }
0 commit comments