BBrother commited on
Commit
e32aff8
1 Parent(s): 81457ee

Update launch.py

Browse files
Files changed (1) hide show
  1. launch.py +88 -20
launch.py CHANGED
@@ -1,8 +1,47 @@
1
- # 导入所需的模块
2
  from modules import launch_utils
3
  import os
4
  import subprocess
5
  import threading
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
  # 设置启动参数
8
  args = launch_utils.args
@@ -39,29 +78,58 @@ start = launch_utils.start
39
 
40
  util_py_path = '/bushu/ui/repositories/diffusion-stability-stable/ldm/util.py'
41
 
42
- # 创建主程序函数
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
  def main():
44
- # 创建线程用于实时查找并打印链接
45
- link_thread = threading.Thread(target=find_and_print_links)
46
- link_thread.daemon = True # 设置为守护线程,程序退出时自动结束
47
- link_thread.start()
48
-
49
- # 主程序代码块
50
- if args.listen:
51
- if not is_installed('grpcio'):
52
- run_pip(f"grpcio=={args.grpcio_version}")
53
- if not is_installed('grpcio-tools'):
54
- run_pip(f"grpcio-tools=={args.grpcio_version}")
55
- elif args.install_vnck:
56
  configure_for_tests()
57
 
58
  start()
59
 
60
- # 设置标志变量来确定是否执行主程序
61
- run_main = True
 
 
 
 
 
 
 
 
 
 
 
 
62
 
63
- if __name__ == "__main__":
64
- run_main = True # 如果直接运行 launch.py,则设置为 True
65
- main()
66
 
67
- # 其他模块导入语句
 
 
 
1
  from modules import launch_utils
2
  import os
3
  import subprocess
4
  import threading
5
+ import logging
6
+ import sys
7
+
8
+
9
+ # 检查是否存在 logs_run.txt 文件,如果存在则删除它
10
+ log_file = '/bushu/logs_run.txt'
11
+ if os.path.exists(log_file):
12
+ os.remove(log_file)
13
+
14
+ # 配置日志
15
+ logging.basicConfig(filename=log_file, level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
16
+
17
+ # 创建一个将输出同时发送到控制台和日志文件的处理程序
18
+ console_handler = logging.StreamHandler(sys.stdout)
19
+ console_handler.setLevel(logging.INFO)
20
+ formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
21
+ console_handler.setFormatter(formatter)
22
+ logging.getLogger().addHandler(console_handler)
23
+
24
+ # 重定向标准输出和标准错误到日志文件和控制台
25
+ sys.stdout = logging.getLogger().handlers[0].stream
26
+ sys.stderr = logging.getLogger().handlers[0].stream
27
+
28
+ # 切换到/bushu/ui目录
29
+ subprocess.run(["cd", "/bushu/ui"], shell=True)
30
+
31
+ try:
32
+ # 在try块中运行v2_3.py,并将输出重定向到日志文件
33
+ with open(log_file, 'a') as log_file_handle:
34
+ subprocess.run(['python', 'launch.py'], check=True, stdout=log_file_handle, stderr=subprocess.STDOUT)
35
+ except subprocess.CalledProcessError as e:
36
+ # 捕获异常并记录到日志文件
37
+ logging.exception(f"程序发生异常: {e}")
38
+ except Exception as e:
39
+ # 捕获其他异常并记录到日志文件
40
+ logging.exception(f"程序发生异常: {e}")
41
+ finally:
42
+ # 最后,关闭日志处理程序,以确保所有日志都被写入到日志文件
43
+ logging.getLogger().removeHandler(console_handler)
44
+ console_handler.close()
45
 
46
  # 设置启动参数
47
  args = launch_utils.args
 
78
 
79
  util_py_path = '/bushu/ui/repositories/diffusion-stability-stable/ldm/util.py'
80
 
81
+ # 打开日志文件以写入程序的所有输出
82
+ log_file_path = '/bushu/logs_run_launch.txt'
83
+ log_file = open(log_file_path, 'w')
84
+
85
+ # 创建一个线程,用于实时查找并打印链接
86
+ def find_and_print_links():
87
+ while True:
88
+ line = log_process.stdout.readline()
89
+ if "Public WebUI Colab URL" in line:
90
+ url = line.split("Public WebUI Colab URL: ")[1].strip()
91
+ print(url) # 在控制台中显示链接
92
+ else:
93
+ # 将其他消息记录到日志文件
94
+ log_file.write(line)
95
+ log_file.flush()
96
+
97
+ # 使用tee命令将程序的输出同时写入日志文件和进程管道
98
+ tee_command = f"{python} -u -m tee -a {log_file_path}"
99
+ log_process = subprocess.Popen(tee_command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, bufsize=1, universal_newlines=True)
100
+
101
  def main():
102
+ if not args.skip_prepare_environment:
103
+ prepare_environment()
104
+ os.system(f"sed -i -e 's/dict()))/dict())).cuda()/g' {util_py_path}")
105
+ os.system(f"sed -i -e 's/stable/dict()))/dict())).cuda()/g' /content/ui/repositories/diffusion-stability-stable/ldm/util.py")
106
+ os.system(f"sed -i -e 's/dict()))/dict())).cuda()/g' {util_py_path}")
107
+ os.system(f"sed -i -e 's/stable/dict()))/dict())).cuda()/g' /content/ui/repositories/diffusion-stability-stable/ldm/util.py")
108
+ os.system(f"sed -i -e 's/dict()))/dict())).cuda()/g' {util_py_path}")
109
+ os.system(f"sed -i -e 's/stable/dict()))/dict())).cuda()/g' /content/ui/repositories/diffusion-stability-stable/ldm/util.py")
110
+
111
+ if args.test_server:
 
 
112
  configure_for_tests()
113
 
114
  start()
115
 
116
+ # 创建一个线程,用于实时查找并打印链接
117
+ def find_and_print_links():
118
+ while True:
119
+ line = log_process.stdout.readline()
120
+ if "Public WebUI Colab URL" in line:
121
+ url = line.split("Public WebUI Colab URL: ")[1].strip()
122
+ print(url) # 在控制台中显示链接
123
+ else:
124
+ # 将其他消息记录到日志文件
125
+ log_file.write(line)
126
+ log_file.flush()
127
+
128
+ # 调用 main() 函数,不再需要条件判断
129
+ main()
130
 
131
+ # 等待程序执行完毕
132
+ log_process.wait()
 
133
 
134
+ # 关闭日志文件
135
+ log_file.close()