BBrother commited on
Commit
3296d8c
·
1 Parent(s): 8932f35

Update “sd_clsa_webui_colab_ipynb”_修复v2_3.py

Browse files
“sd_clsa_webui_colab_ipynb”_修复v2_3.py CHANGED
@@ -64,6 +64,7 @@ import binascii
64
  import os
65
  import subprocess
66
  import wget
 
67
 
68
  # 初始化变量
69
  a = ""
@@ -118,9 +119,6 @@ subprocess.run(["mkdir", "-p", "/content/ui/models/ESRGAN"])
118
  # 使用subprocess运行wget命令
119
  subprocess.run(["wget", "https://huggingface.co/embed/upscale/resolve/main/4x-UltraSharp.pth", "-O", "/content/ui/models/ESRGAN/4x-UltraSharp.pth"])
120
 
121
- # %cd /content/ui
122
- # pip install -r requirements.txt
123
-
124
 
125
  # 初始化变量
126
  a = ""
@@ -187,26 +185,38 @@ extensions_to_clone = [
187
  for repo_url, destination in extensions_to_clone:
188
  run_git_clone(repo_url, destination)
189
 
190
- # 使用subprocess运行sed命令
191
- # 定义要执行的sed命令
192
- sed_command_1 = '''/from modules import launch_utils/a\\import os'''
193
- sed_command_2 = '''/prepare_environment()/a\\ os.system(f"sed -i -e 's/stable/dict()))/dict())).cuda()/g' /content/ui/repositories/diffusion-stability-stable/ldm/util.py")'''
194
- sed_command_3 = 's/\\["sd_model_checkpoint"\\]/\\["sd_model_checkpoint","sd_vae","CLIP_stop_at_last_layers"\\]/g'
195
 
196
  # 定义要操作的文件路径
197
  launch_py_path = "/content/ui/launch.py"
198
  util_py_path = "/content/ui/repositories/diffusion-stability-stable/ldm/util.py"
199
  shared_py_path = "/content/ui/modules/shared.py"
200
 
201
- # 执行sed命令并捕获异常
202
- try:
203
- subprocess.run(["sed", "-i", "-e", sed_command_1, launch_py_path], check=True)
204
- subprocess.run(["sed", "-i", "-e", sed_command_2, launch_py_path], check=True)
205
- subprocess.run(["sed", "-i", "-e", sed_command_3, shared_py_path], check=True)
206
- except subprocess.CalledProcessError as e:
207
- # 将异常信息写入日志文件
208
- with open("/content/logs.txt", "a") as log_file:
209
- log_file.write(f"Error: {e}\n")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
210
 
211
 
212
 
@@ -265,15 +275,41 @@ if T2I_:
265
  for url, output_path in urls_and_outputs:
266
  run_wget(url, output_path)
267
 
 
 
 
 
 
 
 
 
268
  # 更新相关的sed命令
269
- sed_command_1 = '/from modules import launch_utils/a\\import os'
270
- subprocess.run(["sed", "-i", '-e', sed_command_1, "/content/ui/launch.py"], check=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
271
 
272
- sed_command_2 = ' prepare_environment()/a\\ os.system(f\\"sed -i -e \'s/dict()))/dict())).cuda()/g\' /content/ui/repositories/stable-diffusion-stability-ai/ldm/util.py")\''
273
- subprocess.run(["sed", "-i", '-e', sed_command_2, "/content/ui/launch.py"], check=True)
 
 
274
 
275
- sed_command_3 = 's/\\["sd_model_checkpoint"\\]/\\["sd_model_checkpoint","sd_vae","CLIP_stop_at_last_layers"\\]/g'
276
- subprocess.run(["sed", "-i", '-e', sed_command_3, "/content/ui/modules/shared.py"], check=True)
277
 
278
 
279
 
@@ -682,32 +718,14 @@ d = binascii.unhexlify("646966667573696f6e").decode("ascii")
682
  # 构建变量w
683
  w = binascii.unhexlify("7765627569").decode("ascii")
684
 
685
- # %cd /content/ui/
686
-
687
- #python launch.py --listen --xformers --enable-insecure-extension-access --theme dark --gradio-queu --multiple --no-download-sd-model --share
688
- #python launch.py --listen --xformers --enable-insecure-extension-access --theme dark --gradio-queue --no-download-sd-model --share
689
 
 
 
690
 
 
 
691
 
692
- # 自定义下载
693
- ZDY_Download = False
694
- if ZDY_Download:
695
- url = "https://huggingface.co/stabilityai/sd-vae-ft-mse-original/resolve/main/vae-ft-mse-840000-ema-pruned.safetensors"
696
- # _下载链接._
697
- Download_destination_folder = ""
698
- # _在google云盘中找到想要下载的目标路径右键复制地址._
699
- filename = "vae-ft-mse-840000-ema-pruned.safetensors"
700
- # _保存的文件名._
701
 
702
- target = f'{destination}/{filename}'
703
 
704
- subprocess.run(['wget', '-O', target, url])
705
-
706
-
707
- # 下载生成图片到本地
708
- Picture_Download = False
709
- if Picture_Download:
710
- from google.colab import files
711
-
712
- subprocess.run(['zip', '-r', '/content/outputs.zip', '/content/{a}-d-w/outputs'])
713
- files.download('/content/outputs.zip')
 
64
  import os
65
  import subprocess
66
  import wget
67
+ import fileinput
68
 
69
  # 初始化变量
70
  a = ""
 
119
  # 使用subprocess运行wget命令
120
  subprocess.run(["wget", "https://huggingface.co/embed/upscale/resolve/main/4x-UltraSharp.pth", "-O", "/content/ui/models/ESRGAN/4x-UltraSharp.pth"])
121
 
 
 
 
122
 
123
  # 初始化变量
124
  a = ""
 
185
  for repo_url, destination in extensions_to_clone:
186
  run_git_clone(repo_url, destination)
187
 
 
 
 
 
 
188
 
189
  # 定义要操作的文件路径
190
  launch_py_path = "/content/ui/launch.py"
191
  util_py_path = "/content/ui/repositories/diffusion-stability-stable/ldm/util.py"
192
  shared_py_path = "/content/ui/modules/shared.py"
193
 
194
+ # 使用fileinput模块编辑launch.py文件
195
+ with fileinput.FileInput(launch_py_path, inplace=True, backup=".bak") as file:
196
+ for line in file:
197
+ # 在from modules import launch_utils之后插入import os
198
+ if "from modules import launch_utils" in line:
199
+ print(line, end='')
200
+ print("import os")
201
+ # 在prepare_environment()函数后插入os.system命令
202
+ elif "prepare_environment()" in line:
203
+ print(line, end='')
204
+ print(' os.system(f"sed -i -e \'s/stable/dict()))/dict())).cuda()/g\' /content/ui/repositories/diffusion-stability-stable/ldm/util.py")')
205
+ # 替换指定文本
206
+ else:
207
+ print(line, end='')
208
+
209
+ # 使用fileinput模块编辑shared.py文件
210
+ with fileinput.FileInput(shared_py_path, inplace=True, backup=".bak") as file:
211
+ for line in file:
212
+ # 替换指定文本
213
+ print(line.replace('["sd_model_checkpoint"]', '["sd_model_checkpoint","sd_vae","CLIP_stop_at_last_layers"]'), end='')
214
+
215
+ # 删除备份文件
216
+ for path in [launch_py_path + '.bak', shared_py_path + '.bak']:
217
+ if os.path.exists(path):
218
+ os.remove(path)
219
+
220
 
221
 
222
 
 
275
  for url, output_path in urls_and_outputs:
276
  run_wget(url, output_path)
277
 
278
+ import fileinput
279
+ import os
280
+
281
+ # 定义要操作的文件路径
282
+ launch_py_path = "/content/ui/launch.py"
283
+ util_py_path = "/content/ui/repositories/stable-diffusion-stability-ai/ldm/util.py"
284
+ shared_py_path = "/content/ui/modules/shared.py"
285
+
286
  # 更新相关的sed命令
287
+ sed_command_1 = 'import os'
288
+ sed_command_2 = ' os.system(f"sed -i -e \'s/dict()))/dict())).cuda()/g\' {util_py_path}")'
289
+ sed_command_3 = '["sd_model_checkpoint", "sd_vae", "CLIP_stop_at_last_layers"]'
290
+
291
+ # 使用fileinput模块编辑launch.py文件
292
+ with fileinput.FileInput(launch_py_path, inplace=True, backup=".bak") as file:
293
+ for line in file:
294
+ if "from modules import launch_utils" in line:
295
+ print(line, end='')
296
+ print(sed_command_1)
297
+ elif "prepare_environment()" in line:
298
+ print(line, end='')
299
+ print(sed_command_2)
300
+ else:
301
+ print(line, end='')
302
+
303
+ # 使用fileinput模块编辑shared.py文件
304
+ with fileinput.FileInput(shared_py_path, inplace=True, backup=".bak") as file:
305
+ for line in file:
306
+ print(line.replace('["sd_model_checkpoint"]', sed_command_3), end='')
307
 
308
+ # 删除备份文件
309
+ for path in [launch_py_path + '.bak', shared_py_path + '.bak']:
310
+ if os.path.exists(path):
311
+ os.remove(path)
312
 
 
 
313
 
314
 
315
 
 
718
  # 构建变量w
719
  w = binascii.unhexlify("7765627569").decode("ascii")
720
 
 
 
 
 
721
 
722
+ # 切换到/content/ui目录
723
+ subprocess.run(["cd", "/content/ui"], shell=True)
724
 
725
+ # 安装requirements.txt中的依赖项
726
+ subprocess.run(["pip", "install", "-r", "/content/ui/requirements.txt"])
727
 
 
 
 
 
 
 
 
 
 
728
 
 
729
 
730
+ #python launch.py --listen --xformers --enable-insecure-extension-access --theme dark --gradio-queu --multiple --no-download-sd-model --share
731
+ #python launch.py --listen --xformers --enable-insecure-extension-access --theme dark --gradio-queue --no-download-sd-model --share