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 # 设置启动参数 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 # 其他设置... util_py_path = '/bushu/ui/repositories/diffusion-stability-stable/ldm/util.py' def main(): if not args.skip_prepare_environment: prepare_environment() for _ in range(3): 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() # 打开日志文件以写入程序的所有输出 log_file_path = '/bushu/logs_run_launch.txt' log_file = open(log_file_path, 'w') # 使用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) __name__ = "__main__" main() # 等待程序执行完毕 log_process.wait() # 关闭日志文件 log_file.close()