forked from ddPn08/Radiata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui.py
148 lines (106 loc) · 3.79 KB
/
ui.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import importlib
import os
from typing import *
import gradio as gr
import gradio.routes
from modules import model_manager, shared
from .components import header
from .shared import ROOT_DIR
class Tab:
TABS_DIR = os.path.join(ROOT_DIR, "modules", "tabs")
def __init__(self, filepath: str) -> None:
self.filepath = filepath
def sort(self):
return 1
def title(self):
return ""
def ui(self, outlet: Callable):
pass
def visible(self):
return True
def __call__(self):
children_dir = self.filepath[:-3]
children = []
if os.path.isdir(children_dir):
for file in os.listdir(children_dir):
if not file.endswith(".py"):
continue
module_name = file[:-3]
parent = os.path.relpath(Tab.TABS_DIR, Tab.TABS_DIR).replace("/", ".")
if parent.startswith("."):
parent = parent[1:]
if parent.endswith("."):
parent = parent[:-1]
children.append(
importlib.import_module(f"modules.tabs.{parent}.{module_name}")
)
children = sorted(children, key=lambda x: x.sort())
tabs = []
for child in children:
attrs = child.__dict__
tab = [x for x in attrs.values() if issubclass(x, Tab)]
if len(tab) > 0:
tabs.append(tab[0])
def outlet():
with gr.Tabs():
for tab in tabs:
if not tab.visible():
continue
with gr.Tab(tab.title()):
tab()
return self.ui(outlet)
def load_tabs() -> List[Tab]:
tabs = []
files = os.listdir(os.path.join(ROOT_DIR, "modules", "tabs"))
for file in files:
if not file.endswith(".py"):
continue
module_name = file[:-3]
module = importlib.import_module(f"modules.tabs.{module_name}")
attrs = module.__dict__
TabClass = [
x
for x in attrs.values()
if type(x) == type and issubclass(x, Tab) and not x == Tab
]
if len(TabClass) > 0:
tabs.append((file, TabClass[0]))
tabs = sorted([TabClass(file) for file, TabClass in tabs], key=lambda x: x.sort())
return tabs
def webpath(fn):
if fn.startswith(ROOT_DIR):
web_path = os.path.relpath(fn, ROOT_DIR).replace("\\", "/")
else:
web_path = os.path.abspath(fn)
return f"file={web_path}?{os.path.getmtime(fn)}"
def javascript_html():
script_js = os.path.join(ROOT_DIR, "script.js")
head = f'<script type="text/javascript" src="{webpath(script_js)}"></script>\n'
return head
def css_html():
return f'<link rel="stylesheet" property="stylesheet" href="{webpath(os.path.join(ROOT_DIR, "styles.css"))}">'
def create_head():
head = ""
head += css_html()
head += javascript_html()
def template_response(*args, **kwargs):
res = shared.gradio_template_response_original(*args, **kwargs)
res.body = res.body.replace(b"</head>", f"{head}</head>".encode("utf8"))
res.init_headers()
return res
gradio.routes.templates.TemplateResponse = template_response
def create_ui():
block = gr.Blocks()
with block:
if model_manager.mode == "stable-diffusion":
header.ui()
with gr.Tabs(elem_id="radiata-root"):
for tab in load_tabs():
if not tab.visible():
continue
with gr.Tab(tab.title()):
tab()
create_head()
return block
if not hasattr(shared, "gradio_template_response_original"):
shared.gradio_template_response_original = gradio.routes.templates.TemplateResponse