diff options
author | ParityError <36368048+ParityError@users.noreply.github.com> | 2023-03-17 07:36:17 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-03-17 07:36:17 +0000 |
commit | 34c0f499c540f69a6de11caa940483e8f581074d (patch) | |
tree | a3d77fc1a626b372305b8f7220617f1ed97afc21 /modules/sd_models.py | |
parent | 5c051c0618bec1417827910b601ba915d0ca6c4e (diff) | |
parent | a9fed7c364061ae6efb37f797b6b522cb3cf7aa2 (diff) | |
download | stable-diffusion-webui-gfx803-34c0f499c540f69a6de11caa940483e8f581074d.tar.gz stable-diffusion-webui-gfx803-34c0f499c540f69a6de11caa940483e8f581074d.tar.bz2 stable-diffusion-webui-gfx803-34c0f499c540f69a6de11caa940483e8f581074d.zip |
Merge branch 'AUTOMATIC1111:master' into master
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":
|