Update extras.py
Browse files
extras.py
CHANGED
|
@@ -7,7 +7,7 @@ import json
|
|
| 7 |
import torch
|
| 8 |
import tqdm
|
| 9 |
|
| 10 |
-
from modules import shared, images, sd_models, sd_vae, sd_models_config
|
| 11 |
from modules.ui_common import plaintext_to_html
|
| 12 |
import gradio as gr
|
| 13 |
import safetensors.torch
|
|
@@ -72,21 +72,9 @@ def to_half(tensor, enable):
|
|
| 72 |
return tensor
|
| 73 |
|
| 74 |
|
| 75 |
-
def
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
for checkpoint_name in [primary_model_name, secondary_model_name, tertiary_model_name]:
|
| 79 |
-
checkpoint_info = sd_models.checkpoints_list.get(checkpoint_name, None)
|
| 80 |
-
if checkpoint_info is None:
|
| 81 |
-
continue
|
| 82 |
-
|
| 83 |
-
metadata.update(checkpoint_info.metadata)
|
| 84 |
-
|
| 85 |
-
return json.dumps(metadata, indent=4, ensure_ascii=False)
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
def run_modelmerger(id_task, primary_model_name, secondary_model_name, tertiary_model_name, interp_method, multiplier, save_as_half, custom_name, checkpoint_format, config_source, bake_in_vae, discard_weights, save_metadata, add_merge_recipe, copy_metadata_fields, metadata_json):
|
| 89 |
-
shared.state.begin(job="model-merge")
|
| 90 |
|
| 91 |
def fail(message):
|
| 92 |
shared.state.textinfo = message
|
|
@@ -254,25 +242,11 @@ def run_modelmerger(id_task, primary_model_name, secondary_model_name, tertiary_
|
|
| 254 |
shared.state.textinfo = "Saving"
|
| 255 |
print(f"Saving to {output_modelname}...")
|
| 256 |
|
| 257 |
-
metadata =
|
| 258 |
-
|
| 259 |
-
if save_metadata and copy_metadata_fields:
|
| 260 |
-
if primary_model_info:
|
| 261 |
-
metadata.update(primary_model_info.metadata)
|
| 262 |
-
if secondary_model_info:
|
| 263 |
-
metadata.update(secondary_model_info.metadata)
|
| 264 |
-
if tertiary_model_info:
|
| 265 |
-
metadata.update(tertiary_model_info.metadata)
|
| 266 |
|
| 267 |
if save_metadata:
|
| 268 |
-
|
| 269 |
-
metadata.update(json.loads(metadata_json))
|
| 270 |
-
except Exception as e:
|
| 271 |
-
errors.display(e, "readin metadata from json")
|
| 272 |
-
|
| 273 |
-
metadata["format"] = "pt"
|
| 274 |
|
| 275 |
-
if save_metadata and add_merge_recipe:
|
| 276 |
merge_recipe = {
|
| 277 |
"type": "webui", # indicate this model was merged with webui's built-in merger
|
| 278 |
"primary_model_hash": primary_model_info.sha256,
|
|
@@ -288,6 +262,7 @@ def run_modelmerger(id_task, primary_model_name, secondary_model_name, tertiary_
|
|
| 288 |
"is_inpainting": result_is_inpainting_model,
|
| 289 |
"is_instruct_pix2pix": result_is_instruct_pix2pix_model
|
| 290 |
}
|
|
|
|
| 291 |
|
| 292 |
sd_merge_models = {}
|
| 293 |
|
|
@@ -307,12 +282,11 @@ def run_modelmerger(id_task, primary_model_name, secondary_model_name, tertiary_
|
|
| 307 |
if tertiary_model_info:
|
| 308 |
add_model_metadata(tertiary_model_info)
|
| 309 |
|
| 310 |
-
metadata["sd_merge_recipe"] = json.dumps(merge_recipe)
|
| 311 |
metadata["sd_merge_models"] = json.dumps(sd_merge_models)
|
| 312 |
|
| 313 |
_, extension = os.path.splitext(output_modelname)
|
| 314 |
if extension.lower() == ".safetensors":
|
| 315 |
-
safetensors.torch.save_file(theta_0, output_modelname, metadata=metadata
|
| 316 |
else:
|
| 317 |
torch.save(theta_0, output_modelname)
|
| 318 |
|
|
|
|
| 7 |
import torch
|
| 8 |
import tqdm
|
| 9 |
|
| 10 |
+
from modules import shared, images, sd_models, sd_vae, sd_models_config
|
| 11 |
from modules.ui_common import plaintext_to_html
|
| 12 |
import gradio as gr
|
| 13 |
import safetensors.torch
|
|
|
|
| 72 |
return tensor
|
| 73 |
|
| 74 |
|
| 75 |
+
def run_modelmerger(id_task, primary_model_name, secondary_model_name, tertiary_model_name, interp_method, multiplier, save_as_half, custom_name, checkpoint_format, config_source, bake_in_vae, discard_weights, save_metadata):
|
| 76 |
+
shared.state.begin()
|
| 77 |
+
shared.state.job = 'model-merge'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
def fail(message):
|
| 80 |
shared.state.textinfo = message
|
|
|
|
| 242 |
shared.state.textinfo = "Saving"
|
| 243 |
print(f"Saving to {output_modelname}...")
|
| 244 |
|
| 245 |
+
metadata = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 246 |
|
| 247 |
if save_metadata:
|
| 248 |
+
metadata = {"format": "pt"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 249 |
|
|
|
|
| 250 |
merge_recipe = {
|
| 251 |
"type": "webui", # indicate this model was merged with webui's built-in merger
|
| 252 |
"primary_model_hash": primary_model_info.sha256,
|
|
|
|
| 262 |
"is_inpainting": result_is_inpainting_model,
|
| 263 |
"is_instruct_pix2pix": result_is_instruct_pix2pix_model
|
| 264 |
}
|
| 265 |
+
metadata["sd_merge_recipe"] = json.dumps(merge_recipe)
|
| 266 |
|
| 267 |
sd_merge_models = {}
|
| 268 |
|
|
|
|
| 282 |
if tertiary_model_info:
|
| 283 |
add_model_metadata(tertiary_model_info)
|
| 284 |
|
|
|
|
| 285 |
metadata["sd_merge_models"] = json.dumps(sd_merge_models)
|
| 286 |
|
| 287 |
_, extension = os.path.splitext(output_modelname)
|
| 288 |
if extension.lower() == ".safetensors":
|
| 289 |
+
safetensors.torch.save_file(theta_0, output_modelname, metadata=metadata)
|
| 290 |
else:
|
| 291 |
torch.save(theta_0, output_modelname)
|
| 292 |
|