diff options
author | AUTOMATIC <16777216c@gmail.com> | 2023-03-14 06:10:26 +0000 |
---|---|---|
committer | AUTOMATIC <16777216c@gmail.com> | 2023-03-14 06:10:26 +0000 |
commit | c19530f1a590d758463f84523dd4c48c34d723e6 (patch) | |
tree | 096f701ce58e01e2e0a27f2bfcbd79cc824de6e9 /modules/sd_models.py | |
parent | dfeee786f903e392dbef1519c7c246b9856ebab3 (diff) | |
download | stable-diffusion-webui-gfx803-c19530f1a590d758463f84523dd4c48c34d723e6.tar.gz stable-diffusion-webui-gfx803-c19530f1a590d758463f84523dd4c48c34d723e6.tar.bz2 stable-diffusion-webui-gfx803-c19530f1a590d758463f84523dd4c48c34d723e6.zip |
Add view metadata button for Lora cards.
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..5f57ec0c 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] == '{':
+ 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":
|