2023-05-20 15:26:16 -06:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2022-08-27 12:32:28 -06:00
|
|
|
import os
|
2022-10-01 14:47:42 -06:00
|
|
|
import time
|
2023-01-23 11:28:59 -07:00
|
|
|
|
2023-07-17 11:10:24 -06:00
|
|
|
from modules import timer
|
2023-08-09 09:11:13 -06:00
|
|
|
from modules import initialize_util
|
|
|
|
from modules import initialize
|
|
|
|
|
2023-05-20 15:41:41 -06:00
|
|
|
startup_timer = timer.startup_timer
|
2023-07-17 11:10:24 -06:00
|
|
|
startup_timer.record("launcher")
|
2022-10-06 04:21:32 -06:00
|
|
|
|
2023-08-09 09:11:13 -06:00
|
|
|
initialize.imports()
|
2022-08-25 12:52:05 -06:00
|
|
|
|
2023-08-09 09:11:13 -06:00
|
|
|
initialize.check_versions()
|
2022-11-04 12:36:47 -06:00
|
|
|
|
|
|
|
|
2022-10-17 10:58:34 -06:00
|
|
|
def create_api(app):
|
2022-10-17 02:38:32 -06:00
|
|
|
from modules.api.api import Api
|
2023-08-09 09:11:13 -06:00
|
|
|
from modules.call_queue import queue_lock
|
|
|
|
|
2022-10-18 00:51:53 -06:00
|
|
|
api = Api(app, queue_lock)
|
2022-10-17 10:58:34 -06:00
|
|
|
return api
|
|
|
|
|
2022-10-31 08:36:45 -06:00
|
|
|
|
2022-10-17 10:58:34 -06:00
|
|
|
def api_only():
|
2023-08-09 09:11:13 -06:00
|
|
|
from fastapi import FastAPI
|
|
|
|
from modules.shared_cmd_options import cmd_opts
|
|
|
|
|
|
|
|
initialize.initialize()
|
2022-10-01 11:31:58 -06:00
|
|
|
|
2022-10-17 10:58:34 -06:00
|
|
|
app = FastAPI()
|
2023-08-09 09:11:13 -06:00
|
|
|
initialize_util.setup_middleware(app)
|
2022-10-17 10:58:34 -06:00
|
|
|
api = create_api(app)
|
|
|
|
|
2023-08-09 09:11:13 -06:00
|
|
|
from modules import script_callbacks
|
|
|
|
script_callbacks.before_ui_callback()
|
|
|
|
script_callbacks.app_started_callback(None, app)
|
2022-11-02 01:04:35 -06:00
|
|
|
|
2023-03-11 06:27:58 -07:00
|
|
|
print(f"Startup time: {startup_timer.summary()}.")
|
2023-06-28 04:15:01 -06:00
|
|
|
api.launch(
|
2023-12-30 05:12:48 -07:00
|
|
|
server_name=initialize_util.gradio_server_name(),
|
2023-06-28 04:15:01 -06:00
|
|
|
port=cmd_opts.port if cmd_opts.port else 7861,
|
2023-07-25 05:36:39 -06:00
|
|
|
root_path=f"/{cmd_opts.subpath}" if cmd_opts.subpath else ""
|
2023-06-28 04:15:01 -06:00
|
|
|
)
|
2022-10-06 03:08:48 -06:00
|
|
|
|
2023-05-16 11:58:35 -06:00
|
|
|
|
2022-10-20 09:32:17 -06:00
|
|
|
def webui():
|
2023-08-09 09:11:13 -06:00
|
|
|
from modules.shared_cmd_options import cmd_opts
|
|
|
|
|
2022-10-20 09:32:17 -06:00
|
|
|
launch_api = cmd_opts.api
|
2023-08-09 09:11:13 -06:00
|
|
|
initialize.initialize()
|
|
|
|
|
|
|
|
from modules import shared, ui_tempdir, script_callbacks, ui, progress, ui_extra_networks
|
2022-10-17 00:58:42 -06:00
|
|
|
|
2022-10-17 02:38:32 -06:00
|
|
|
while 1:
|
2022-11-27 01:52:53 -07:00
|
|
|
if shared.opts.clean_temp_dir_at_start:
|
|
|
|
ui_tempdir.cleanup_tmpdr()
|
2023-03-11 06:27:58 -07:00
|
|
|
startup_timer.record("cleanup temp dir")
|
2022-11-27 01:52:53 -07:00
|
|
|
|
2023-08-09 09:11:13 -06:00
|
|
|
script_callbacks.before_ui_callback()
|
2023-03-11 06:27:58 -07:00
|
|
|
startup_timer.record("scripts before_ui_callback")
|
2023-01-21 06:15:53 -07:00
|
|
|
|
2023-08-09 09:11:13 -06:00
|
|
|
shared.demo = ui.create_ui()
|
2023-03-11 06:27:58 -07:00
|
|
|
startup_timer.record("create ui")
|
2022-10-18 09:51:36 -06:00
|
|
|
|
2023-03-20 23:49:08 -06:00
|
|
|
if not cmd_opts.no_gradio_queue:
|
2023-01-21 08:47:54 -07:00
|
|
|
shared.demo.queue(64)
|
|
|
|
|
2023-08-09 09:11:13 -06:00
|
|
|
gradio_auth_creds = list(initialize_util.get_gradio_auth_creds()) or None
|
2023-02-19 03:11:48 -07:00
|
|
|
|
2023-08-07 20:22:35 -06:00
|
|
|
auto_launch_browser = False
|
|
|
|
if os.getenv('SD_WEBUI_RESTARTING') != '1':
|
|
|
|
if shared.opts.auto_launch_browser == "Remote" or cmd_opts.autolaunch:
|
|
|
|
auto_launch_browser = True
|
|
|
|
elif shared.opts.auto_launch_browser == "Local":
|
2023-08-27 12:42:02 -06:00
|
|
|
auto_launch_browser = not cmd_opts.webui_is_non_local
|
2023-08-07 20:22:35 -06:00
|
|
|
|
2023-01-14 03:38:10 -07:00
|
|
|
app, local_url, share_url = shared.demo.launch(
|
2022-10-02 12:26:38 -06:00
|
|
|
share=cmd_opts.share,
|
2023-08-09 09:11:13 -06:00
|
|
|
server_name=initialize_util.gradio_server_name(),
|
2022-10-02 12:26:38 -06:00
|
|
|
server_port=cmd_opts.port,
|
2022-11-05 03:06:51 -06:00
|
|
|
ssl_keyfile=cmd_opts.tls_keyfile,
|
|
|
|
ssl_certfile=cmd_opts.tls_certfile,
|
2023-04-27 19:30:19 -06:00
|
|
|
ssl_verify=cmd_opts.disable_tls_verify,
|
2022-10-02 12:26:38 -06:00
|
|
|
debug=cmd_opts.gradio_debug,
|
2023-05-19 08:28:41 -06:00
|
|
|
auth=gradio_auth_creds,
|
2023-08-07 20:22:35 -06:00
|
|
|
inbrowser=auto_launch_browser,
|
2023-05-18 01:36:11 -06:00
|
|
|
prevent_thread_lock=True,
|
|
|
|
allowed_paths=cmd_opts.gradio_allowed_path,
|
2023-05-22 00:53:24 -06:00
|
|
|
app_kwargs={
|
|
|
|
"docs_url": "/docs",
|
|
|
|
"redoc_url": "/redoc",
|
|
|
|
},
|
2023-07-08 05:58:33 -06:00
|
|
|
root_path=f"/{cmd_opts.subpath}" if cmd_opts.subpath else "",
|
2022-10-02 12:26:38 -06:00
|
|
|
)
|
2023-05-11 14:46:45 -06:00
|
|
|
|
2023-03-11 06:27:58 -07:00
|
|
|
startup_timer.record("gradio launch")
|
|
|
|
|
2022-11-04 01:07:29 -06:00
|
|
|
# gradio uses a very open CORS policy via app.user_middleware, which makes it possible for
|
|
|
|
# an attacker to trick the user into opening a malicious HTML page, which makes a request to the
|
2022-12-14 19:01:32 -07:00
|
|
|
# running web ui and do whatever the attacker wants, including installing an extension and
|
|
|
|
# running its code. We disable this here. Suggested by RyotaK.
|
2022-11-04 01:07:29 -06:00
|
|
|
app.user_middleware = [x for x in app.user_middleware if x.cls.__name__ != 'CORSMiddleware']
|
|
|
|
|
2023-08-09 09:11:13 -06:00
|
|
|
initialize_util.setup_middleware(app)
|
2022-10-02 12:26:38 -06:00
|
|
|
|
2023-08-09 09:11:13 -06:00
|
|
|
progress.setup_progress_api(app)
|
|
|
|
ui.setup_ui_api(app)
|
2023-01-15 08:50:56 -07:00
|
|
|
|
2022-10-31 08:36:45 -06:00
|
|
|
if launch_api:
|
2022-10-17 10:58:34 -06:00
|
|
|
create_api(app)
|
2022-10-02 12:26:38 -06:00
|
|
|
|
2023-01-28 12:52:27 -07:00
|
|
|
ui_extra_networks.add_pages_to_demo(app)
|
|
|
|
|
2023-05-20 15:41:41 -06:00
|
|
|
startup_timer.record("add APIs")
|
|
|
|
|
|
|
|
with startup_timer.subcategory("app_started_callback"):
|
2023-08-09 09:11:13 -06:00
|
|
|
script_callbacks.app_started_callback(shared.demo, app)
|
2023-03-11 06:27:58 -07:00
|
|
|
|
2023-05-20 15:41:41 -06:00
|
|
|
timer.startup_record = startup_timer.dump()
|
2023-03-11 06:27:58 -07:00
|
|
|
print(f"Startup time: {startup_timer.summary()}.")
|
2022-10-30 08:46:43 -06:00
|
|
|
|
2023-05-11 14:46:45 -06:00
|
|
|
try:
|
|
|
|
while True:
|
|
|
|
server_command = shared.state.wait_for_server_command(timeout=5)
|
|
|
|
if server_command:
|
|
|
|
if server_command in ("stop", "restart"):
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
print(f"Unknown server command: {server_command}")
|
|
|
|
except KeyboardInterrupt:
|
2023-05-16 11:58:35 -06:00
|
|
|
print('Caught KeyboardInterrupt, stopping...')
|
2023-05-11 14:46:45 -06:00
|
|
|
server_command = "stop"
|
|
|
|
|
|
|
|
if server_command == "stop":
|
2023-05-16 11:58:35 -06:00
|
|
|
print("Stopping server...")
|
2023-05-11 14:46:45 -06:00
|
|
|
# If we catch a keyboard interrupt, we want to stop the server and exit.
|
|
|
|
shared.demo.close()
|
|
|
|
break
|
2023-05-20 15:41:41 -06:00
|
|
|
|
2023-08-07 20:22:35 -06:00
|
|
|
# disable auto launch webui in browser for subsequent UI Reload
|
|
|
|
os.environ.setdefault('SD_WEBUI_RESTARTING', '1')
|
|
|
|
|
2023-01-03 08:38:21 -07:00
|
|
|
print('Restarting UI...')
|
2023-05-11 14:46:45 -06:00
|
|
|
shared.demo.close()
|
|
|
|
time.sleep(0.5)
|
2023-03-12 12:25:22 -06:00
|
|
|
startup_timer.reset()
|
2023-08-09 09:11:13 -06:00
|
|
|
script_callbacks.app_reload_callback()
|
2023-05-19 07:08:40 -06:00
|
|
|
startup_timer.record("app reload callback")
|
2023-08-09 09:11:13 -06:00
|
|
|
script_callbacks.script_unloaded_callback()
|
2023-05-19 07:08:40 -06:00
|
|
|
startup_timer.record("scripts unloaded callback")
|
2023-08-09 09:11:13 -06:00
|
|
|
initialize.initialize_rest(reload_script_modules=True)
|
2023-01-20 22:36:07 -07:00
|
|
|
|
2022-09-11 09:48:36 -06:00
|
|
|
|
2022-09-08 03:17:26 -06:00
|
|
|
if __name__ == "__main__":
|
2023-08-09 09:11:13 -06:00
|
|
|
from modules.shared_cmd_options import cmd_opts
|
|
|
|
|
2022-10-18 00:51:53 -06:00
|
|
|
if cmd_opts.nowebui:
|
2022-10-17 10:58:34 -06:00
|
|
|
api_only()
|
2022-10-17 02:38:32 -06:00
|
|
|
else:
|
2022-10-20 09:32:17 -06:00
|
|
|
webui()
|