Default38693 commited on
Commit
65c703b
1 Parent(s): 815b616

Upload 4 files

Browse files
LyCORIS_mover/batch_lora_json_extractor.py ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import argparse
2
+ import os
3
+ import subprocess
4
+
5
+ def batch_lora_json_extraction(src_dir, lora_json_extractor_path):
6
+ for file in os.listdir(src_dir):
7
+ if file.endswith(".safetensors"):
8
+ input_file = os.path.join(src_dir, file)
9
+ output_file = os.path.join(src_dir, file.replace(".safetensors", ".json"))
10
+ subprocess.run(["python", lora_json_extractor_path, input_file, output_file])
11
+
12
+ if __name__ == "__main__":
13
+ parser = argparse.ArgumentParser(description="Batch extraction of .safetensors files using lora_json_extractor.py.")
14
+ parser.add_argument("src_dir", help="Source directory containing .safetensors files.")
15
+ parser.add_argument("lora_json_extractor_path", help="Path to lora_json_extractor.py script.")
16
+ args = parser.parse_args()
17
+
18
+ batch_lora_json_extraction(args.src_dir, args.lora_json_extractor_path)
LyCORIS_mover/lora_json_extractor.py ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import sys
3
+
4
+
5
+ # 既存の関数 (extract_metadata と save_metadata_to_json) は変更せずに再利用できます。
6
+
7
+ def extract_metadata(_file_path, _start_marker):
8
+ local_metadata = ""
9
+
10
+ with open(_file_path, "rb") as f:
11
+ content = f.read()
12
+ start_pos = content.find(_start_marker)
13
+
14
+ if start_pos != -1:
15
+ end_pos = start_pos
16
+ brace_count = 0
17
+ for idx, b in enumerate(content[start_pos:]):
18
+ if b == ord('{'):
19
+ brace_count += 1
20
+ elif b == ord('}'):
21
+ brace_count -= 1
22
+
23
+ if brace_count == 0:
24
+ end_pos += idx
25
+ break
26
+
27
+ json_bytes = content[start_pos:end_pos + 1]
28
+ content_str = json_bytes.decode("utf-8")
29
+ try:
30
+ local_metadata = json.loads(content_str)
31
+ except json.JSONDecodeError as e:
32
+ print(f"Error decoding JSON: {e}")
33
+
34
+ return local_metadata
35
+
36
+
37
+ def save_metadata_to_json(_metadata, _output_file):
38
+ with open(_output_file, "w", encoding="utf-8") as f:
39
+ json.dump(_metadata, f, ensure_ascii=False, indent=2)
40
+
41
+
42
+ def extract_metadata_layer(json_data):
43
+ local_metadata_layer = {}
44
+
45
+ for key, value in json_data.items():
46
+ if key == "__metadata__":
47
+ local_metadata_layer[key] = value
48
+ elif isinstance(value, dict):
49
+ extracted = extract_metadata_layer(value)
50
+ if extracted:
51
+ local_metadata_layer[key] = extracted
52
+
53
+ return local_metadata_layer
54
+
55
+
56
+ def load_json_file(_file_path):
57
+ with open(_file_path, "r", encoding="utf-8") as f:
58
+ return json.load(f)
59
+
60
+
61
+ if __name__ == "__main__":
62
+ if len(sys.argv) != 3:
63
+ print("Usage: python module.py <input_file> <output_file>")
64
+ sys.exit(1)
65
+
66
+ file_path = sys.argv[1]
67
+ output_file = sys.argv[2]
68
+
69
+ # メタ情報の開始マーカー
70
+ start_marker = b'{"__metadata__":'
71
+
72
+ # メタ情報の抽出
73
+ metadata = extract_metadata(file_path, start_marker)
74
+
75
+ # メタ情報を整形して JSON ファイルに保存
76
+ save_metadata_to_json(metadata, output_file)
77
+
78
+ # JSON ファイルを読み込む
79
+ data = load_json_file(output_file)
80
+
81
+ # "__metadata__" 階層のみを抽出
82
+ metadata_layer = extract_metadata_layer(data)
83
+
84
+ # "__metadata__" 階層のみを含む JSON ファイルに保存
85
+ save_metadata_to_json(metadata_layer, output_file)
LyCORIS_mover/move_non_lora_safetensors.py ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import argparse
2
+ import json
3
+ import os
4
+ import shutil
5
+
6
+ def move_non_lora_safetensors(src_dir, dest_dir):
7
+ for file in os.listdir(src_dir):
8
+ if file.endswith(".json"):
9
+ try:
10
+ with open(os.path.join(src_dir, file), 'r', encoding='utf-8') as f:
11
+ data = json.load(f)
12
+ if data.get("__metadata__", {}).get("ss_network_module") != "networks.lora":
13
+ safetensors_file = file.replace(".json", ".safetensors")
14
+ src_path = os.path.join(src_dir, safetensors_file)
15
+ dest_path = os.path.join(dest_dir, safetensors_file)
16
+ if os.path.isfile(src_path):
17
+ shutil.move(src_path, dest_path)
18
+ except json.JSONDecodeError:
19
+ continue
20
+
21
+ if __name__ == "__main__":
22
+ parser = argparse.ArgumentParser(description="Move .safetensors files of non-networks.lora JSON content.")
23
+ parser.add_argument("src_dir", help="Source directory containing .json and .safetensors files.")
24
+ parser.add_argument("dest_dir", help="Destination directory for non-networks.lora .safetensors files.")
25
+ args = parser.parse_args()
26
+
27
+ move_non_lora_safetensors(args.src_dir, args.dest_dir)
LyCORIS_mover/mover.bat ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ chcp 65001
2
+ @echo off
3
+ echo "nannka mojibakesuru baainiha bat no senntouniaru chcp 65001 wo gyougoto sakujosuru"
4
+
5
+ echo "loraフォルダのバックアップ取った?取ってないならそのままバツで閉じる。取ったならEnter"
6
+ pause
7
+
8
+ cd /D %~dp0
9
+ echo "lora_dir_path にはloraディレクトリを指定"
10
+ set lora_dir_path="H:\WEBUI_Last\models\Lora"
11
+
12
+ echo "extractorはlora_json_extractorの場所を"
13
+ set extractor=%~dp0\lora_json_extractor.py
14
+
15
+ echo "lycoris_dir_pathには移動先となるリコリスのパスを指定"
16
+ set lycoris_dir_path="H:\WEBUI_Last\models\LyCORIS"
17
+ python batch_lora_json_extractor.py %lora_dir_path% %extractor%
18
+ python move_non_lora_safetensors.py %lora_dir_path% %lycoris_dir_path%
19
+
20
+ echo "移動完了!残ったjsonファイルは自力で削除してね(検索ボックスに.jsonで引っ掛けてCtrl+Aで選択してゴミ箱にでも移動させればOK)"
21
+ echo "このウィンドウはバツ閉じすればOK"
22
+ pause