Upload 2 files
Browse files- sdxl_keys.txt +0 -0
- stkey.py +121 -0
sdxl_keys.txt
ADDED
The diff for this file is too large to render.
See raw diff
|
|
stkey.py
ADDED
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import argparse
|
2 |
+
from pathlib import Path
|
3 |
+
import json
|
4 |
+
import re
|
5 |
+
import gc
|
6 |
+
from safetensors.torch import load_file, save_file
|
7 |
+
import torch
|
8 |
+
|
9 |
+
|
10 |
+
SDXL_KEYS_FILE = "sdxl_keys.txt"
|
11 |
+
|
12 |
+
|
13 |
+
def list_uniq(l):
|
14 |
+
return sorted(set(l), key=l.index)
|
15 |
+
|
16 |
+
|
17 |
+
def read_safetensors_metadata(path: str):
|
18 |
+
with open(path, 'rb') as f:
|
19 |
+
header_size = int.from_bytes(f.read(8), 'little')
|
20 |
+
header_json = f.read(header_size).decode('utf-8')
|
21 |
+
header = json.loads(header_json)
|
22 |
+
metadata = header.get('__metadata__', {})
|
23 |
+
return metadata
|
24 |
+
|
25 |
+
|
26 |
+
def keys_from_file(path: str):
|
27 |
+
keys = []
|
28 |
+
try:
|
29 |
+
with open(str(Path(path)), encoding='utf-8', mode='r') as f:
|
30 |
+
lines = f.readlines()
|
31 |
+
for line in lines:
|
32 |
+
keys.append(line.strip())
|
33 |
+
except Exception as e:
|
34 |
+
print(e)
|
35 |
+
finally:
|
36 |
+
return keys
|
37 |
+
|
38 |
+
|
39 |
+
def validate_keys(keys: list[str], rfile: str=SDXL_KEYS_FILE):
|
40 |
+
missing = []
|
41 |
+
added = []
|
42 |
+
try:
|
43 |
+
rkeys = keys_from_file(rfile)
|
44 |
+
all_keys = list_uniq(keys + rkeys)
|
45 |
+
for key in all_keys:
|
46 |
+
if key in set(rkeys) and key not in set(keys): missing.append(key)
|
47 |
+
if key in set(keys) and key not in set(rkeys): added.append(key)
|
48 |
+
except Exception as e:
|
49 |
+
print(e)
|
50 |
+
finally:
|
51 |
+
return missing, added
|
52 |
+
|
53 |
+
|
54 |
+
def read_safetensors_key(path: str):
|
55 |
+
try:
|
56 |
+
keys = []
|
57 |
+
state_dict = load_file(str(Path(path)))
|
58 |
+
for k, v in state_dict.items():
|
59 |
+
keys.append(k)
|
60 |
+
except Exception as e:
|
61 |
+
print(e)
|
62 |
+
finally:
|
63 |
+
del state_dict
|
64 |
+
torch.cuda.empty_cache()
|
65 |
+
gc.collect()
|
66 |
+
return keys
|
67 |
+
|
68 |
+
|
69 |
+
def write_safetensors_key(keys: list[str], path: str, is_validate: bool=True, rpath: str=SDXL_KEYS_FILE):
|
70 |
+
if len(keys) == 0: return False
|
71 |
+
try:
|
72 |
+
with open(str(Path(path)), encoding='utf-8', mode='w') as f:
|
73 |
+
f.write("\n".join(keys))
|
74 |
+
if is_validate:
|
75 |
+
missing, added = validate_keys(keys, rpath)
|
76 |
+
with open(str(Path(path).stem + "_missing.txt"), encoding='utf-8', mode='w') as f:
|
77 |
+
f.write("\n".join(missing))
|
78 |
+
with open(str(Path(path).stem + "_added.txt"), encoding='utf-8', mode='w') as f:
|
79 |
+
f.write("\n".join(added))
|
80 |
+
return True
|
81 |
+
except Exception as e:
|
82 |
+
print(e)
|
83 |
+
return False
|
84 |
+
|
85 |
+
|
86 |
+
def stkey(input: str, out_filename: str="", is_validate: bool=True, rfile: str=SDXL_KEYS_FILE):
|
87 |
+
keys = read_safetensors_key(input)
|
88 |
+
if len(keys) != 0 and out_filename: write_safetensors_key(keys, out_filename, is_validate, rfile)
|
89 |
+
if len(keys) != 0:
|
90 |
+
print("Metadata:")
|
91 |
+
print(read_safetensors_metadata(input))
|
92 |
+
print("\nKeys:")
|
93 |
+
print("\n".join(keys))
|
94 |
+
if is_validate:
|
95 |
+
missing, added = validate_keys(keys, rfile)
|
96 |
+
print("\nMissing Keys:")
|
97 |
+
print("\n".join(missing))
|
98 |
+
print("\nAdded Keys:")
|
99 |
+
print("\n".join(added))
|
100 |
+
|
101 |
+
|
102 |
+
if __name__ == "__main__":
|
103 |
+
parser = argparse.ArgumentParser()
|
104 |
+
parser.add_argument("input", type=str, help="Input safetensors file.")
|
105 |
+
parser.add_argument("-s", "--save", action="store_true", default=False, help="Output to text file.")
|
106 |
+
parser.add_argument("-o", "--output", default="", type=str, help="Output to specific text file.")
|
107 |
+
parser.add_argument("-v", "--val", action="store_false", default=True, help="Disable key validation.")
|
108 |
+
parser.add_argument("-r", "--rfile", default=SDXL_KEYS_FILE, type=str, help="Specify reference file to validate keys.")
|
109 |
+
|
110 |
+
args = parser.parse_args()
|
111 |
+
|
112 |
+
if args.save: out_filename = Path(args.input).stem + ".txt"
|
113 |
+
out_filename = args.output if args.output else out_filename
|
114 |
+
|
115 |
+
stkey(args.input, out_filename, args.val, args.rfile)
|
116 |
+
|
117 |
+
|
118 |
+
# Usage:
|
119 |
+
# python stkey.py sd_xl_base_1.0_0.9vae.safetensors
|
120 |
+
# python stkey.py sd_xl_base_1.0_0.9vae.safetensors -s
|
121 |
+
# python stkey.py sd_xl_base_1.0_0.9vae.safetensors -o key.txt
|