caocaocoa commited on
Commit
455312f
·
1 Parent(s): 322b159

Upload main5.py

Browse files
Files changed (1) hide show
  1. main5.py +88 -0
main5.py ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import zipfile
3
+ import ipywidgets as widgets
4
+ from IPython.display import display
5
+ from tqdm import tqdm
6
+
7
+ class ModelExtractor:
8
+ def __init__(self, base_path):
9
+ self.base_path = base_path
10
+ self.vae_zip_file_path = os.path.join(base_path, "models/Stable-diffusion/\u805A\u661Fvae.zip")
11
+ self.vae_extract_path = os.path.join(base_path, "models/VAE/\u805A\u661FVAE")
12
+ self.embeddings_zip_file_path = os.path.join(base_path, "models/Lora/\u805A\u661Fembeddings.zip")
13
+ self.embeddings_extract_path = os.path.join(base_path, "embeddings/\u805A\u661Fembeddings")
14
+ self.lora_zip_file_path = os.path.join(base_path, "models/Stable-diffusion/\u805A\u661Flora.zip")
15
+ self.lora_extract_path = os.path.join(base_path, "models/Lora/\u805A\u661FLora")
16
+
17
+ # 设置文件系统编码环境
18
+ os.environ['LANG'] = 'en_US.UTF-8'
19
+
20
+ # 创建输入框和按钮
21
+ self.old_char_input = widgets.Text(value='/mnt/8BxdTF', description='替换前的字符:')
22
+ self.new_char_input = widgets.Text(value='', description='替换后的字符:')
23
+ self.replace_button = widgets.Button(description='替换')
24
+
25
+ # 显示输入框和按钮
26
+ display(self.old_char_input, self.new_char_input, self.replace_button)
27
+
28
+ # 将按钮与回调函数连接
29
+ self.replace_button.on_click(self.replace_callback)
30
+
31
+ def find_and_replace_path(self, file_path, old_str, new_str):
32
+ return file_path.replace(old_str, new_str)
33
+
34
+ def unzip_and_fix_encoding(self, zip_file, extract_path, old_char, new_char):
35
+ # 解压文件
36
+ with zipfile.ZipFile(zip_file, 'r') as zip_ref:
37
+ zip_ref.extractall(extract_path)
38
+
39
+ # 删除原始压缩文件
40
+ os.remove(zip_file)
41
+
42
+ def replace_callback(self, button):
43
+ # 获取输入框中的值
44
+ old_char = self.old_char_input.value
45
+ new_char = self.new_char_input.value
46
+
47
+ # 替换路径
48
+ self.base_path = self.find_and_replace_path(self.base_path, old_char, new_char)
49
+ self.vae_zip_file_path = self.find_and_replace_path(self.vae_zip_file_path, old_char, new_char)
50
+ self.vae_extract_path = self.find_and_replace_path(self.vae_extract_path, old_char, new_char)
51
+
52
+ self.embeddings_zip_file_path = self.find_and_replace_path(self.embeddings_zip_file_path, old_char, new_char)
53
+ self.embeddings_extract_path = self.find_and_replace_path(self.embeddings_extract_path, old_char, new_char)
54
+
55
+ self.lora_zip_file_path = self.find_and_replace_path(self.lora_zip_file_path, old_char, new_char)
56
+ self.lora_extract_path = self.find_and_replace_path(self.lora_extract_path, old_char, new_char)
57
+
58
+ # 替换文件夹路径
59
+ self.vae_extract_path = self.find_and_replace_path(self.vae_extract_path, old_char, new_char)
60
+ self.embeddings_extract_path = self.find_and_replace_path(self.embeddings_extract_path, old_char, new_char)
61
+ self.lora_extract_path = self.find_and_replace_path(self.lora_extract_path, old_char, new_char)
62
+
63
+ # 解压 VAE 并修复文件名编码
64
+ self.unzip_and_fix_encoding(self.vae_zip_file_path, self.vae_extract_path, old_char, new_char)
65
+
66
+ # 解压 embeddings 并修复文件名编码
67
+ self.unzip_and_fix_encoding(self.embeddings_zip_file_path, self.embeddings_extract_path, old_char, new_char)
68
+
69
+ # 解压 Lora 并修复文件名编码
70
+ self.unzip_and_fix_encoding(self.lora_zip_file_path, self.lora_extract_path, old_char, new_char)
71
+
72
+ # 遍历并修复两个路径的文件名编码
73
+ for current_base_path in [self.vae_extract_path, self.lora_extract_path]:
74
+ for root, dirs, files in os.walk(current_base_path):
75
+ for file in files:
76
+ # 拼接文件的完整路径
77
+ file_path = os.path.join(root, file)
78
+ # 从乱码中解析文件名
79
+ decoded_name = file.encode('cp437').decode('gbk')
80
+ # 更改文件名
81
+ os.rename(file_path, os.path.join(root, decoded_name))
82
+
83
+ print(f"{current_base_path}路径的文件名编码修复完成。")
84
+
85
+ print("VAE、embeddings 和 Lora 解压完成,并进行字符替换和文件名编码修复,原始压缩文件已删除.")
86
+
87
+ # 实例化类并执行替换操作
88
+ model_extractor = ModelExtractor("/mnt/8BxdTF")