diff options
author | InvincibleDude <81354513+InvincibleDude@users.noreply.github.com> | 2023-03-14 16:55:59 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-03-14 16:55:59 +0300 |
commit | f5e44364535ccc7efef445dacb6395c3942f2f17 (patch) | |
tree | ca7384e6225fccdae1e89db1e0ccc44dd60c7311 /modules/sd_models.py | |
parent | f6e27378404631d951656388fc178b784fe1495b (diff) | |
parent | a9fed7c364061ae6efb37f797b6b522cb3cf7aa2 (diff) |
Merge branch 'master' into improved-hr-conflict-test
Diffstat (limited to 'modules/sd_models.py')
-rw-r--r-- | modules/sd_models.py | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/modules/sd_models.py b/modules/sd_models.py index 93959f55..f0cb1240 100644 --- a/modules/sd_models.py +++ b/modules/sd_models.py @@ -210,6 +210,30 @@ def get_state_dict_from_checkpoint(pl_sd): return pl_sd
+def read_metadata_from_safetensors(filename):
+ import json
+
+ with open(filename, mode="rb") as file:
+ metadata_len = file.read(8)
+ metadata_len = int.from_bytes(metadata_len, "little")
+ json_start = file.read(2)
+
+ assert metadata_len > 2 and json_start in (b'{"', b"{'"), f"{filename} is not a safetensors file"
+ json_data = json_start + file.read(metadata_len-2)
+ json_obj = json.loads(json_data)
+
+ res = {}
+ for k, v in json_obj.get("__metadata__", {}).items():
+ res[k] = v
+ if isinstance(v, str) and v[0:1] == '{':
+ try:
+ res[k] = json.loads(v)
+ except Exception as e:
+ pass
+
+ return res
+
+
def read_state_dict(checkpoint_file, print_global_state=False, map_location=None):
_, extension = os.path.splitext(checkpoint_file)
if extension.lower() == ".safetensors":
|