File size: 4,912 Bytes
88695a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
from modules import launch_utils
import os
import subprocess
import threading
import logging
import sys


# 检查是否存在 logs_run.txt 文件,如果存在则删除它
log_file = '/bushu/logs_run.txt'
if os.path.exists(log_file):
    os.remove(log_file)

# 配置日志
logging.basicConfig(filename=log_file, level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')

# 创建一个将输出同时发送到控制台和日志文件的处理程序
console_handler = logging.StreamHandler(sys.stdout)
console_handler.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
console_handler.setFormatter(formatter)
logging.getLogger().addHandler(console_handler)

# 重定向标准输出和标准错误到日志文件和控制台
sys.stdout = logging.getLogger().handlers[0].stream
sys.stderr = logging.getLogger().handlers[0].stream

# 切换到/bushu/ui目录
subprocess.run(["cd", "/bushu/ui"], shell=True)

try:
    # 在try块中运行v2_3.py,并将输出重定向到日志文件
    with open(log_file, 'a') as log_file_handle:
        subprocess.run(['python', '/bushu/ui/launch.py'], check=True, stdout=log_file_handle, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
    # 捕获异常并记录到日志文件
    logging.exception(f"程序发生异常: {e}")
except Exception as e:
    # 捕获其他异常并记录到日志文件
    logging.exception(f"程序发生异常: {e}")
finally:
    # 最后,关闭日志处理程序,以确保所有日志都被写入到日志文件
    logging.getLogger().removeHandler(console_handler)
    console_handler.close()

# 设置启动参数
args = launch_utils.args
args.listen = True
args.xformers = True
args.enable_insecure_extension_access = True
args.theme = "dark"
args.gradio_queue = True
args.multiple = True
args.no_download_sd_model = True
args.share = True

python = launch_utils.python
git = launch_utils.git
index_url = launch_utils.index_url
dir_repos = launch_utils.dir_repos

commit_hash = launch_utils.commit_hash
git_tag = launch_utils.git_tag

run = launch_utils.run
is_installed = launch_utils.is_installed
repo_dir = launch_utils.repo_dir

run_pip = launch_utils.run_pip
check_run_python = launch_utils.check_run_python
git_clone = launch_utils.git_clone
git_pull_recursive = launch_utils.git_pull_recursive
list_extensions = launch_utils.list_extensions
run_extension_installer = launch_utils.run_extension_installer
prepare_environment = launch_utils.prepare_environment
configure_for_tests = launch_utils.configure_for_tests
start = launch_utils.start

util_py_path = '/bushu/ui/repositories/diffusion-stability-stable/ldm/util.py'

# 打开日志文件以写入程序的所有输出
log_file_path = '/bushu/logs_run_launch.txt'
log_file = open(log_file_path, 'w')

# 创建一个线程,用于实时查找并打印链接
def find_and_print_links():
    while True:
        line = log_process.stdout.readline()
        if "Public WebUI Colab URL" in line:
            url = line.split("Public WebUI Colab URL: ")[1].strip()
            print(url)  # 在控制台中显示链接
        else:
            # 将其他消息记录到日志文件
            log_file.write(line)
            log_file.flush()

# 使用tee命令将程序的输出同时写入日志文件和进程管道
tee_command = f"{python} -u -m tee -a {log_file_path}"
log_process = subprocess.Popen(tee_command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, bufsize=1, universal_newlines=True)

def main():
    if not args.skip_prepare_environment:
        prepare_environment()
        os.system(f"sed -i -e 's/dict()))/dict())).cuda()/g' {util_py_path}")
        os.system(f"sed -i -e 's/stable/dict()))/dict())).cuda()/g' /content/ui/repositories/diffusion-stability-stable/ldm/util.py")
        os.system(f"sed -i -e 's/dict()))/dict())).cuda()/g' {util_py_path}")
        os.system(f"sed -i -e 's/stable/dict()))/dict())).cuda()/g' /content/ui/repositories/diffusion-stability-stable/ldm/util.py")
        os.system(f"sed -i -e 's/dict()))/dict())).cuda()/g' {util_py_path}")
        os.system(f"sed -i -e 's/stable/dict()))/dict())).cuda()/g' /content/ui/repositories/diffusion-stability-stable/ldm/util.py")

    if args.test_server:
        configure_for_tests()

    start()

# 创建一个线程,用于实时查找并打印链接
def find_and_print_links():
    while True:
        line = log_process.stdout.readline()
        if "Public WebUI Colab URL" in line:
            url = line.split("Public WebUI Colab URL: ")[1].strip()
            print(url)  # 在控制台中显示链接
        else:
            # 将其他消息记录到日志文件
            log_file.write(line)
            log_file.flush()

# 调用 main() 函数,不再需要条件判断
main()

# 等待程序执行完毕
log_process.wait()

# 关闭日志文件
log_file.close()