BBrother commited on
Commit
b34fbac
1 Parent(s): f839e16

Update launch.py

Browse files
Files changed (1) hide show
  1. launch.py +57 -60
launch.py CHANGED
@@ -1,73 +1,70 @@
1
- import os
2
  import subprocess
3
- from modules import launch_utils
4
- import pyngrok
5
- from pathlib import Path
6
-
 
7
 
8
- #ngrok穿透
9
- ngrok_use = True
10
- ngrokTokenFile='/bushu/Authtoken.txt' # 非必填 存放ngrokToken的文件的路径
 
11
 
12
- #ngrok
13
- def ngrok_start(ngrokTokenFile: str, port: int, address_name: str, should_run: bool):
14
- if not should_run:
15
- print('Skipping ngrok start')
16
- return
17
- if Path(ngrokTokenFile).exists():
18
- with open(ngrokTokenFile, encoding="utf-8") as nkfile:
19
- ngrokToken = nkfile.readline()
20
- print('use nrgok')
21
- from pyngrok import conf, ngrok
22
- conf.get_default().auth_token = ngrokToken
23
- conf.get_default().monitor_thread = False
24
- ssh_tunnels = ngrok.get_tunnels(conf.get_default())
25
- if len(ssh_tunnels) == 0:
26
- ssh_tunnel = ngrok.connect(port, bind_tls=True)
27
- print(f'{address_name}:' + ssh_tunnel.public_url)
28
- else:
29
- print(f'{address_name}:' + ssh_tunnels[0].public_url)
30
- else:
31
- print('skip start ngrok')
32
 
 
 
33
 
34
- args = launch_utils.args
35
- python = launch_utils.python
36
- git = launch_utils.git
37
- index_url = launch_utils.index_url
38
- dir_repos = launch_utils.dir_repos
 
39
 
40
- commit_hash = launch_utils.commit_hash
41
- git_tag = launch_utils.git_tag
 
42
 
43
- run = launch_utils.run
44
- is_installed = launch_utils.is_installed
45
- repo_dir = launch_utils.repo_dir
46
 
47
- run_pip = launch_utils.run_pip
48
- check_run_python = launch_utils.check_run_python
49
- git_clone = launch_utils.git_clone
50
- git_pull_recursive = launch_utils.git_pull_recursive
51
- list_extensions = launch_utils.list_extensions
52
- run_extension_installer = launch_utils.run_extension_installer
53
- prepare_environment = launch_utils.prepare_environment
54
- configure_for_tests = launch_utils.configure_for_tests
55
- start = launch_utils.start
56
 
57
- def run_command(command):
58
- subprocess.run(command, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
59
 
60
- def main():
61
- ngrok_start(ngrokTokenFile,7860,'use ngrok',ngrok_use)
62
- if not args.skip_prepare_environment:
63
- launch_utils.prepare_environment()
64
- run_command(f"""sed -i -e ''"s/dict()))/dict())).cuda()/g"'' /bushu/ui/repositories/stable-diffusion-stability-ai/ldm/util.py""")
65
- run_command(f"""sed -i -e ''"s/stable/dict()))/dict())).cuda()/g"'' /bushu/ui/repositories/diffusion-stability-stable/ldm/util.py""")
 
 
 
 
 
 
66
 
67
- if args.test_server:
68
- launch_utils.configure_for_tests()
 
69
 
70
- launch_utils.start()
71
 
72
- if __name__ == "__main__":
73
- main()
 
 
 
 
 
 
 
 
 
 
1
  import subprocess
2
+ import sys
3
+ import logging
4
+ import os
5
+ import binascii # 用于将文本转换为十六进制
6
+ import re # 用于正则表达式
7
 
8
+ # 检查是否存在 logs_run.txt 文件,如果存在则删除它
9
+ log_file = '/bushu/logs_run.txt'
10
+ if os.path.exists(log_file):
11
+ os.remove(log_file)
12
 
13
+ # 检查是否存在 url.txt 文件,如果存在则删除它
14
+ url_file = '/bushu/url.txt'
15
+ if os.path.exists(url_file):
16
+ os.remove(url_file)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
+ # 配置日志
19
+ logging.basicConfig(filename=log_file, level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
20
 
21
+ # 创建一个将输出同时发送到控制台和日志文件的处理程序
22
+ console_handler = logging.StreamHandler(sys.stdout)
23
+ console_handler.setLevel(logging.INFO)
24
+ formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
25
+ console_handler.setFormatter(formatter)
26
+ logging.getLogger().addHandler(console_handler)
27
 
28
+ # 重定向标准输出和标准错误到日志文件和控制台
29
+ sys.stdout = logging.getLogger().handlers[0].stream
30
+ sys.stderr = logging.getLogger().handlers[0].stream
31
 
32
+ # 更改当前工作目录为 /bushu/ui
33
+ os.chdir('/bushu/ui')
 
34
 
35
+ try:
36
+ # try 块中运行 /bushu/ui/launch.py,并将输出重定向到日志文件
37
+ with open(log_file, 'a') as log_file_handle, open(url_file, 'a') as url_file_handle:
38
+ process = subprocess.Popen(['python', '/bushu/ui/launch.py', '--port 8081'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True)
 
 
 
 
 
39
 
40
+ url_pattern = re.compile(r'https?://\S+')
 
41
 
42
+ for line in process.stdout:
43
+ if "Public" in line or "Application" in line:
44
+ # 将包含 "Public" 或 "Application" 的行转换为十六进制并写入 url.txt
45
+ hex_line = binascii.hexlify(line.encode()).decode()
46
+ url_file_handle.write(hex_line + '\n')
47
+ else:
48
+ # 查找行中的 URL 地址并将匹配的行转换为十六进制后写入 url.txt
49
+ urls = url_pattern.findall(line)
50
+ if urls:
51
+ for url in urls:
52
+ hex_url = binascii.hexlify(url.encode()).decode()
53
+ url_file_handle.write(hex_url + '\n')
54
 
55
+ # 将其他行写入日志文件
56
+ log_file_handle.write(line)
57
+ log_file_handle.flush()
58
 
59
+ process.wait()
60
 
61
+ except subprocess.CalledProcessError as e:
62
+ # 捕获异常并记录到日志文件
63
+ logging.exception(f"程序发生异常: {e}")
64
+ except Exception as e:
65
+ # 捕获其他异常并记录到日志文件
66
+ logging.exception(f"程序发生异常: {e}")
67
+ finally:
68
+ # 最后,关闭日志处理程序,以确保所有日志都被写入到日志文件
69
+ logging.getLogger().removeHandler(console_handler)
70
+ console_handler.close()