-
Notifications
You must be signed in to change notification settings - Fork 4
/
webui.py
118 lines (105 loc) · 4.96 KB
/
webui.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
"""EMA-VFI-WebUI Application"""
import os
os.environ['FOR_DISABLE_CONSOLE_CTRL_HANDLER'] = "1"
import shutil
import time
import signal
import argparse
from typing import Callable
from interpolate_engine import InterpolateEngine
from webui_utils.simple_log import SimpleLog
from webui_utils.simple_config import SimpleConfig
from webui_utils.file_utils import create_directories, is_safe_path
from webui_utils.color_out import ColorOut
from webui_utils.mtqdm import Mtqdm
from create_ui import create_ui
from webui_tips import WebuiTips
global log
def main():
"""Run the application"""
global log
parser = argparse.ArgumentParser(description='EMA-VFI Web UI')
parser.add_argument("--config_path", type=str, default="config.yaml",
help="path to config YAML file")
parser.add_argument("--verbose", dest="verbose", default=False, action="store_true",
help="Show extra details")
args = parser.parse_args()
log = SimpleLog(args.verbose)
config = SimpleConfig(args.config_path).config_obj()
clean_working_directory(config.directories["working"])
create_directories(config.directories)
WebUI(config, log).start()
class WebUI:
"""Top-Level application logic"""
def __init__(self,
config : SimpleConfig,
log : SimpleLog):
self.config = config
self.log = log
self.restart = False
self.prevent_inbrowser = False
def start(self):
"""Create the UI and start the event loop"""
Mtqdm().set_use_color(self.config.user_interface["mtqdm_use_color"])
Mtqdm().set_palette(self.config.user_interface["mtqdm_palette"])
WebuiTips.set_tips_path(self.config.user_interface["tips_path"])
model = self.config.engine_settings["model"]
gpu_ids = self.config.engine_settings["gpu_ids"]
use_time_step = self.config.engine_settings["use_time_step"]
try:
engine = InterpolateEngine(model, gpu_ids, use_time_step=use_time_step)
except RuntimeError as error:
print(f"Error loading interpolation engine: {error}")
engine = None
while True:
print()
if self.config.user_interface["mtqdm_use_color"]:
print("\x1b[40m\x1b[38;2;0;0;0m▐\x1b[38;2;255;255;255m▄▄▄\x1b[38;2;255;255;0m▄▄▄\x1b[38;2;0;255;255m▄▄▄\x1b[38;2;0;255;0m▄▄▄\x1b[38;2;255;0;255m▄▄▄\x1b[38;2;255;0;0m▄▄▄\x1b[38;2;0;0;255m▄▄▄\x1b[38;2;0;0;0m▌\x1b[0m")
print("\x1b[40m\x1b[38;2;0;0;0m▐\x1b[38;2;255;255;255m███\x1b[97m EMA-VFI WEBUI \x1b[38;2;0;0;255m███\x1b[38;2;0;0;0m▌\x1b[0m")
print("\x1b[40m\x1b[38;2;0;0;0m▐\x1b[38;2;255;255;255m▀▀▀\x1b[38;2;255;255;0m▀▀▀\x1b[38;2;0;255;255m▀▀▀\x1b[38;2;0;255;0m▀▀▀\x1b[38;2;255;0;255m▀▀▀\x1b[38;2;255;0;0m▀▀▀\x1b[38;2;0;0;255m▀▀▀\x1b[38;2;0;0;0m▌\x1b[0m")
else:
print(" ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄")
print(" ███ EMA-VFI WEBUI ███")
print(" ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀")
print()
app = create_ui(self.config, engine, self.log, self.restart_app)
app.launch(inbrowser = self.config.app_settings["auto_launch_browser"] and not self.prevent_inbrowser,
server_name = self.config.app_settings["server_name"],
server_port = self.config.app_settings["server_port"],
prevent_thread_lock=True, quiet=True)
# after initial launch, disable inbrowser for subsequent restarts
self.prevent_inbrowser = True
self.wait_on_server(app)
print()
ColorOut("Restarting ...", "yellow")
def restart_app(self):
"""Signal to the event loop to restart the application"""
self.restart = True
def wait_on_server(self, app):
"""Restart application if signal is set"""
while True:
time.sleep(0.5)
if self.restart:
self.restart = False
time.sleep(0.5)
app.close()
time.sleep(0.5)
break
def clean_working_directory(working_directory):
if os.path.exists(working_directory) and is_safe_path(working_directory):
shutil.rmtree(working_directory, ignore_errors=True)
def sigint_handler(sig, frame):
"""Make the program just exit at ctrl+c without waiting for anything"""
ColorOut(f'Interrupted with signal {sig} in {frame}', "red")
global log
if log.messages:
log.messages.reverse()
recent = log.messages[:16]
recent.reverse()
ColorOut("Most recent log entries", "yellow")
for entry in recent:
ColorOut(entry, "yellow", "none")
os._exit(0) #pylint: disable=protected-access
signal.signal(signal.SIGINT, sigint_handler)
if __name__ == '__main__':
main()