7171    convert_unet_state_dict_to_peft ,
7272    is_wandb_available ,
7373)
74+ from  diffusers .utils .hub_utils  import  load_or_create_model_card , populate_model_card 
7475from  diffusers .utils .import_utils  import  is_xformers_available 
7576from  diffusers .utils .torch_utils  import  is_compiled_module 
7677
@@ -101,7 +102,7 @@ def determine_scheduler_type(pretrained_model_name_or_path, revision):
101102def  save_model_card (
102103    repo_id : str ,
103104    use_dora : bool ,
104-     images = None ,
105+     images :  list   =   None ,
105106    base_model : str  =  None ,
106107    train_text_encoder = False ,
107108    train_text_encoder_ti = False ,
@@ -111,20 +112,17 @@ def save_model_card(
111112    repo_folder = None ,
112113    vae_path = None ,
113114):
114-     img_str  =  "widget:\n " 
115115    lora  =  "lora"  if  not  use_dora  else  "dora" 
116-     for  i , image  in  enumerate (images ):
117-         image .save (os .path .join (repo_folder , f"image_{ i }  ))
118-         img_str  +=  f""" 
119-         - text: '{ validation_prompt  if  validation_prompt  else  ' '  }  
120-           output: 
121-             url: 
122-                 "image_{ i }  
123-         """ 
124-     if  not  images :
125-         img_str  +=  f""" 
126-         - text: '{ instance_prompt }  
127-         """ 
116+ 
117+     widget_dict  =  []
118+     if  images  is  not None :
119+         for  i , image  in  enumerate (images ):
120+             image .save (os .path .join (repo_folder , f"image_{ i }  ))
121+             widget_dict .append (
122+                 {"text" : validation_prompt  if  validation_prompt  else  " " , "output" : {"url" : f"image_{ i }  }}
123+             )
124+     else :
125+         widget_dict .append ({"text" : instance_prompt })
128126    embeddings_filename  =  f"{ repo_folder }  
129127    instance_prompt_webui  =  re .sub (r"<s\d+>" , "" , re .sub (r"<s\d+>" , embeddings_filename , instance_prompt , count = 1 ))
130128    ti_keys  =  ", " .join (f'"{ match }   for  match  in  re .findall (r"<s\d+>" , instance_prompt ))
@@ -169,23 +167,7 @@ def save_model_card(
169167to trigger concept `{ key } { tokens } \n  
170168""" 
171169
172-     yaml  =  f"""--- 
173- tags: 
174- - stable-diffusion-xl 
175- - stable-diffusion-xl-diffusers 
176- - diffusers-training 
177- - text-to-image 
178- - diffusers 
179- - { lora }  
180- - template:sd-lora 
181- { img_str } 
182- base_model: { base_model }  
183- instance_prompt: { instance_prompt }  
184- license: openrail++ 
185- --- 
186- """ 
187- 
188-     model_card  =  f""" 
170+     model_description  =  f""" 
189171# SDXL LoRA DreamBooth - { repo_id }  
190172
191173<Gallery /> 
@@ -234,8 +216,25 @@ def save_model_card(
234216
235217{ license } 
236218""" 
237-     with  open (os .path .join (repo_folder , "README.md" ), "w" ) as  f :
238-         f .write (yaml  +  model_card )
219+     model_card  =  load_or_create_model_card (
220+         repo_id_or_path = repo_id ,
221+         from_training = True ,
222+         license = "openrail++" ,
223+         base_model = base_model ,
224+         prompt = instance_prompt ,
225+         model_description = model_description ,
226+         widget = widget_dict ,
227+     )
228+     tags  =  [
229+         "text-to-image" ,
230+         "stable-diffusion-xl" ,
231+         "stable-diffusion-xl-diffusers" ,
232+         "text-to-image" ,
233+         "diffusers" ,
234+         lora ,
235+         "template:sd-lora" ,
236+     ]
237+     model_card  =  populate_model_card (model_card , tags = tags )
239238
240239
241240def  log_validation (
0 commit comments