Update SW_run.py
Browse files
SW_run.py
CHANGED
@@ -1,59 +1,57 @@
|
|
1 |
-
# 导入所需的模块
|
2 |
-
from modules import launch_utils
|
3 |
-
import os
|
4 |
import subprocess
|
5 |
-
import
|
6 |
-
|
7 |
-
|
8 |
-
args = launch_utils.args
|
9 |
-
args.listen = True
|
10 |
-
args.xformers = True
|
11 |
-
args.enable_insecure_extension_access = True
|
12 |
-
args.theme = "dark"
|
13 |
-
args.gradio_queue = True
|
14 |
-
args.multiple = True
|
15 |
-
args.no_download_sd_model = True
|
16 |
-
args.share = True
|
17 |
-
|
18 |
-
python = launch_utils.python
|
19 |
-
git = launch_utils.git
|
20 |
-
index_url = launch_utils.index_url
|
21 |
-
dir_repos = launch_utils.dir_repos
|
22 |
-
|
23 |
-
commit_hash = launch_utils.commit_hash
|
24 |
-
git_tag = launch_utils.git_tag
|
25 |
-
|
26 |
-
run = launch_utils.run
|
27 |
-
is_installed = launch_utils.is_installed
|
28 |
-
repo_dir = launch_utils.repo_dir
|
29 |
-
|
30 |
-
run_pip = launch_utils.run_pip
|
31 |
-
check_run_python = launch_utils.check_run_python
|
32 |
-
git_clone = launch_utils.git_clone
|
33 |
-
git_pull_recursive = launch_utils.git_pull_recursive
|
34 |
-
list_extensions = launch_utils.list_extensions
|
35 |
-
run_extension_installer = launch_utils.run_extension_installer
|
36 |
-
prepare_environment = launch_utils.prepare_environment
|
37 |
-
configure_for_tests = launch_utils.configure_for_tests
|
38 |
-
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 __name__ == "__main__": 中的代码
|
51 |
-
|
52 |
-
# 设置标志变量来确定是否执行主程序
|
53 |
-
run_main = True
|
54 |
-
|
55 |
-
if __name__ == "__main__":
|
56 |
-
run_main = True # 如果直接运行 launch.py,则设置为 True
|
57 |
-
main()
|
58 |
|
59 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import subprocess
|
2 |
+
import sys
|
3 |
+
import logging
|
4 |
+
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
+
# 检查是否存在 logs_run.txt 文件,如果存在则删除它
|
7 |
+
log_file = '/bushu/logs_run.txt'
|
8 |
+
if os.path.exists(log_file):
|
9 |
+
os.remove(log_file)
|
10 |
+
|
11 |
+
# 检查是否存在 url.txt 文件,如果存在则删除它
|
12 |
+
url_file = '/bushu/url.txt'
|
13 |
+
if os.path.exists(url_file):
|
14 |
+
os.remove(url_file)
|
15 |
+
|
16 |
+
# 配置日志
|
17 |
+
logging.basicConfig(filename=log_file, level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
18 |
+
|
19 |
+
# 创建一个将输出同时发送到控制台和日志文件的处理程序
|
20 |
+
console_handler = logging.StreamHandler(sys.stdout)
|
21 |
+
console_handler.setLevel(logging.INFO)
|
22 |
+
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
|
23 |
+
console_handler.setFormatter(formatter)
|
24 |
+
logging.getLogger().addHandler(console_handler)
|
25 |
+
|
26 |
+
# 重定向标准输出和标准错误到日志文件和控制台
|
27 |
+
sys.stdout = logging.getLogger().handlers[0].stream
|
28 |
+
sys.stderr = logging.getLogger().handlers[0].stream
|
29 |
+
|
30 |
+
# 切换到/bushu/ui目录
|
31 |
+
subprocess.run(["cd", "/bushu/ui"], shell=True)
|
32 |
+
|
33 |
+
try:
|
34 |
+
# 在 try 块中运行 launch.py,并将输出重定向到日志文件
|
35 |
+
with open(log_file, 'a') as log_file_handle:
|
36 |
+
process = subprocess.Popen(['python', 'launch.py'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True, bufsize=1)
|
37 |
+
for line in iter(process.stdout.readline, ''):
|
38 |
+
if "Running on" in line:
|
39 |
+
# 如果日志行包含 "Running on",将它写入 url.txt 文件
|
40 |
+
with open(url_file, 'a') as url_file_handle:
|
41 |
+
url_file_handle.write(line)
|
42 |
+
else:
|
43 |
+
# 否则,将日志行写入 logs_run.txt 文件
|
44 |
+
log_file_handle.write(line)
|
45 |
+
log_file_handle.flush()
|
46 |
+
process.stdout.close()
|
47 |
+
process.wait()
|
48 |
+
except subprocess.CalledProcessError as e:
|
49 |
+
# 捕获异常并记录到日志文件
|
50 |
+
logging.exception(f"程序发生异常: {e}")
|
51 |
+
except Exception as e:
|
52 |
+
# 捕获其他异常并记录到日志文件
|
53 |
+
logging.exception(f"程序发生异常: {e}")
|
54 |
+
finally:
|
55 |
+
# 最后,关闭日志处理程序,以确保所有日志都被写入到日志文件
|
56 |
+
logging.getLogger().removeHandler(console_handler)
|
57 |
+
console_handler.close()
|