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()