Update SW_run.py
Browse files
SW_run.py
CHANGED
@@ -1,11 +1,14 @@
|
|
1 |
-
import
|
2 |
-
|
3 |
-
import logging
|
4 |
import os
|
5 |
-
import
|
6 |
-
|
|
|
|
|
|
|
|
|
7 |
|
8 |
-
|
9 |
|
10 |
# 打开并执行txt文件
|
11 |
with open('/etc/main/StartBianLiang.txt', 'r') as file:
|
@@ -17,69 +20,189 @@ cleaned_code = '\n'.join(line.strip() for line in code.splitlines() if line.stri
|
|
17 |
# 执行干净的代码
|
18 |
exec(cleaned_code)
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from __future__ import annotations
|
2 |
+
|
|
|
3 |
import os
|
4 |
+
import time
|
5 |
+
|
6 |
+
from modules import timer
|
7 |
+
from modules import initialize_util
|
8 |
+
from modules import initialize
|
9 |
+
from pathlib import Path
|
10 |
|
11 |
+
ngrok_use = False
|
12 |
|
13 |
# 打开并执行txt文件
|
14 |
with open('/etc/main/StartBianLiang.txt', 'r') as file:
|
|
|
20 |
# 执行干净的代码
|
21 |
exec(cleaned_code)
|
22 |
|
23 |
+
#ngrok穿透
|
24 |
+
ngrokTokenFile='/etc/main/Authtoken.txt' # 非必填 存放ngrokToken的文件的路径
|
25 |
+
|
26 |
+
def ngrok_start(ngrokTokenFile: str, port: int, address_name: str, should_run: bool):
|
27 |
+
if not should_run:
|
28 |
+
print('Skipping ngrok start')
|
29 |
+
return
|
30 |
+
if Path(ngrokTokenFile).exists():
|
31 |
+
with open(ngrokTokenFile, encoding="utf-8") as nkfile:
|
32 |
+
ngrokToken = nkfile.readline()
|
33 |
+
print('use nrgok')
|
34 |
+
from pyngrok import conf, ngrok
|
35 |
+
conf.get_default().auth_token = ngrokToken
|
36 |
+
conf.get_default().monitor_thread = False
|
37 |
+
ssh_tunnels = ngrok.get_tunnels(conf.get_default())
|
38 |
+
if len(ssh_tunnels) == 0:
|
39 |
+
ssh_tunnel = ngrok.connect(port, bind_tls=True)
|
40 |
+
print(f'{address_name}:' + ssh_tunnel.public_url)
|
41 |
+
else:
|
42 |
+
print(f'{address_name}:' + ssh_tunnels[0].public_url)
|
43 |
+
else:
|
44 |
+
print('skip start ngrok')
|
45 |
+
|
46 |
+
if ngrok_use :
|
47 |
+
ngrok_start(ngrokTokenFile,7860,'ngrok_url:',ngrok_use)
|
48 |
+
|
49 |
+
startup_timer = timer.startup_timer
|
50 |
+
startup_timer.record("launcher")
|
51 |
+
|
52 |
+
initialize.imports()
|
53 |
+
|
54 |
+
initialize.check_versions()
|
55 |
+
|
56 |
+
|
57 |
+
def create_api(app):
|
58 |
+
from modules.api.api import Api
|
59 |
+
from modules.call_queue import queue_lock
|
60 |
+
|
61 |
+
api = Api(app, queue_lock)
|
62 |
+
return api
|
63 |
+
|
64 |
+
|
65 |
+
def api_only():
|
66 |
+
from fastapi import FastAPI
|
67 |
+
from modules.shared_cmd_options import cmd_opts
|
68 |
+
|
69 |
+
initialize.initialize()
|
70 |
+
|
71 |
+
app = FastAPI()
|
72 |
+
initialize_util.setup_middleware(app)
|
73 |
+
api = create_api(app)
|
74 |
+
|
75 |
+
from modules import script_callbacks
|
76 |
+
script_callbacks.before_ui_callback()
|
77 |
+
script_callbacks.app_started_callback(None, app)
|
78 |
+
|
79 |
+
print(f"Startup time: {startup_timer.summary()}.")
|
80 |
+
api.launch(
|
81 |
+
server_name="0.0.0.0" if cmd_opts.listen else "127.0.0.1",
|
82 |
+
port=cmd_opts.port if cmd_opts.port else 7861,
|
83 |
+
root_path=f"/{cmd_opts.subpath}" if cmd_opts.subpath else ""
|
84 |
+
)
|
85 |
+
|
86 |
+
|
87 |
+
def webui():
|
88 |
+
from modules.shared_cmd_options import cmd_opts
|
89 |
+
|
90 |
+
launch_api = cmd_opts.api
|
91 |
+
initialize.initialize()
|
92 |
+
|
93 |
+
from modules import shared, ui_tempdir, script_callbacks, ui, progress, ui_extra_networks
|
94 |
+
|
95 |
+
while 1:
|
96 |
+
if shared.opts.clean_temp_dir_at_start:
|
97 |
+
ui_tempdir.cleanup_tmpdr()
|
98 |
+
startup_timer.record("cleanup temp dir")
|
99 |
+
|
100 |
+
script_callbacks.before_ui_callback()
|
101 |
+
startup_timer.record("scripts before_ui_callback")
|
102 |
+
|
103 |
+
shared.demo = ui.create_ui()
|
104 |
+
startup_timer.record("create ui")
|
105 |
+
|
106 |
+
if not cmd_opts.no_gradio_queue:
|
107 |
+
shared.demo.queue(64)
|
108 |
+
|
109 |
+
gradio_auth_creds = list(initialize_util.get_gradio_auth_creds()) or None
|
110 |
+
|
111 |
+
auto_launch_browser = False
|
112 |
+
if os.getenv('SD_WEBUI_RESTARTING') != '1':
|
113 |
+
if shared.opts.auto_launch_browser == "Remote" or cmd_opts.autolaunch:
|
114 |
+
auto_launch_browser = True
|
115 |
+
elif shared.opts.auto_launch_browser == "Local":
|
116 |
+
auto_launch_browser = not any([cmd_opts.listen, cmd_opts.share, cmd_opts.ngrok, cmd_opts.server_name])
|
117 |
+
|
118 |
+
app, local_url, share_url = shared.demo.launch(
|
119 |
+
share=cmd_opts.share,
|
120 |
+
server_name=initialize_util.gradio_server_name(),
|
121 |
+
server_port=cmd_opts.port,
|
122 |
+
ssl_keyfile=cmd_opts.tls_keyfile,
|
123 |
+
ssl_certfile=cmd_opts.tls_certfile,
|
124 |
+
ssl_verify=cmd_opts.disable_tls_verify,
|
125 |
+
debug=cmd_opts.gradio_debug,
|
126 |
+
auth=gradio_auth_creds,
|
127 |
+
inbrowser=auto_launch_browser,
|
128 |
+
prevent_thread_lock=True,
|
129 |
+
allowed_paths=cmd_opts.gradio_allowed_path,
|
130 |
+
app_kwargs={
|
131 |
+
"docs_url": "/docs",
|
132 |
+
"redoc_url": "/redoc",
|
133 |
+
},
|
134 |
+
root_path=f"/{cmd_opts.subpath}" if cmd_opts.subpath else "",
|
135 |
+
)
|
136 |
+
|
137 |
+
startup_timer.record("gradio launch")
|
138 |
+
|
139 |
+
# gradio uses a very open CORS policy via app.user_middleware, which makes it possible for
|
140 |
+
# an attacker to trick the user into opening a malicious HTML page, which makes a request to the
|
141 |
+
# running web ui and do whatever the attacker wants, including installing an extension and
|
142 |
+
# running its code. We disable this here. Suggested by RyotaK.
|
143 |
+
app.user_middleware = [x for x in app.user_middleware if x.cls.__name__ != 'CORSMiddleware']
|
144 |
+
|
145 |
+
initialize_util.setup_middleware(app)
|
146 |
+
|
147 |
+
progress.setup_progress_api(app)
|
148 |
+
ui.setup_ui_api(app)
|
149 |
+
|
150 |
+
if launch_api:
|
151 |
+
create_api(app)
|
152 |
+
|
153 |
+
ui_extra_networks.add_pages_to_demo(app)
|
154 |
+
|
155 |
+
startup_timer.record("add APIs")
|
156 |
+
|
157 |
+
with startup_timer.subcategory("app_started_callback"):
|
158 |
+
script_callbacks.app_started_callback(shared.demo, app)
|
159 |
+
|
160 |
+
timer.startup_record = startup_timer.dump()
|
161 |
+
print(f"Startup time: {startup_timer.summary()}.")
|
162 |
+
|
163 |
+
try:
|
164 |
+
while True:
|
165 |
+
server_command = shared.state.wait_for_server_command(timeout=5)
|
166 |
+
if server_command:
|
167 |
+
if server_command in ("stop", "restart"):
|
168 |
+
break
|
169 |
+
else:
|
170 |
+
print(f"Unknown server command: {server_command}")
|
171 |
+
except KeyboardInterrupt:
|
172 |
+
print('Caught KeyboardInterrupt, stopping...')
|
173 |
+
server_command = "stop"
|
174 |
+
|
175 |
+
if server_command == "stop":
|
176 |
+
print("Stopping server...")
|
177 |
+
# If we catch a keyboard interrupt, we want to stop the server and exit.
|
178 |
+
shared.demo.close()
|
179 |
+
break
|
180 |
+
|
181 |
+
# disable auto launch webui in browser for subsequent UI Reload
|
182 |
+
os.environ.setdefault('SD_WEBUI_RESTARTING', '1')
|
183 |
+
|
184 |
+
print('Restarting UI...')
|
185 |
+
shared.demo.close()
|
186 |
+
time.sleep(0.5)
|
187 |
+
startup_timer.reset()
|
188 |
+
script_callbacks.app_reload_callback()
|
189 |
+
startup_timer.record("app reload callback")
|
190 |
+
script_callbacks.script_unloaded_callback()
|
191 |
+
startup_timer.record("scripts unloaded callback")
|
192 |
+
initialize.initialize_rest(reload_script_modules=True)
|
193 |
+
|
194 |
+
|
195 |
+
from modules.shared_cmd_options import cmd_opts
|
196 |
+
|
197 |
+
if cmd_opts.nowebui:
|
198 |
+
api_only()
|
199 |
+
else:
|
200 |
+
webui()
|
201 |
+
|
202 |
+
#if __name__ == "__main__":
|
203 |
+
# from modules.shared_cmd_options import cmd_opts#
|
204 |
+
|
205 |
+
# if cmd_opts.nowebui:
|
206 |
+
# api_only()
|
207 |
+
# else:
|
208 |
+
# webui()
|