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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
|
3 |
+
from modules import ui_common, shared, script_callbacks, scripts, sd_models, sysinfo, timer
|
4 |
+
from modules.call_queue import wrap_gradio_call
|
5 |
+
from modules.shared import opts
|
6 |
+
from modules.ui_components import FormRow
|
7 |
+
from modules.ui_gradio_extensions import reload_javascript
|
8 |
+
from concurrent.futures import ThreadPoolExecutor, as_completed
|
9 |
+
|
10 |
+
|
11 |
+
def get_value_for_setting(key):
|
12 |
+
value = getattr(opts, key)
|
13 |
+
|
14 |
+
info = opts.data_labels[key]
|
15 |
+
args = info.component_args() if callable(info.component_args) else info.component_args or {}
|
16 |
+
args = {k: v for k, v in args.items() if k not in {'precision'}}
|
17 |
+
|
18 |
+
return gr.update(value=value, **args)
|
19 |
+
|
20 |
+
|
21 |
+
def create_setting_component(key, is_quicksettings=False):
|
22 |
+
def fun():
|
23 |
+
return opts.data[key] if key in opts.data else opts.data_labels[key].default
|
24 |
+
|
25 |
+
info = opts.data_labels[key]
|
26 |
+
t = type(info.default)
|
27 |
+
|
28 |
+
args = info.component_args() if callable(info.component_args) else info.component_args
|
29 |
+
|
30 |
+
if info.component is not None:
|
31 |
+
comp = info.component
|
32 |
+
elif t == str:
|
33 |
+
comp = gr.Textbox
|
34 |
+
elif t == int:
|
35 |
+
comp = gr.Number
|
36 |
+
elif t == bool:
|
37 |
+
comp = gr.Checkbox
|
38 |
+
else:
|
39 |
+
raise Exception(f'bad options item type: {t} for key {key}')
|
40 |
+
|
41 |
+
elem_id = f"setting_{key}"
|
42 |
+
|
43 |
+
if info.refresh is not None:
|
44 |
+
if is_quicksettings:
|
45 |
+
res = comp(label=info.label, value=fun(), elem_id=elem_id, **(args or {}))
|
46 |
+
ui_common.create_refresh_button(res, info.refresh, info.component_args, f"refresh_{key}")
|
47 |
+
else:
|
48 |
+
with FormRow():
|
49 |
+
res = comp(label=info.label, value=fun(), elem_id=elem_id, **(args or {}))
|
50 |
+
ui_common.create_refresh_button(res, info.refresh, info.component_args, f"refresh_{key}")
|
51 |
+
else:
|
52 |
+
res = comp(label=info.label, value=fun(), elem_id=elem_id, **(args or {}))
|
53 |
+
|
54 |
+
return res
|
55 |
+
|
56 |
+
|
57 |
+
class UiSettings:
|
58 |
+
submit = None
|
59 |
+
result = None
|
60 |
+
interface = None
|
61 |
+
components = None
|
62 |
+
component_dict = None
|
63 |
+
dummy_component = None
|
64 |
+
quicksettings_list = None
|
65 |
+
quicksettings_names = None
|
66 |
+
text_settings = None
|
67 |
+
show_all_pages = None
|
68 |
+
show_one_page = None
|
69 |
+
search_input = None
|
70 |
+
|
71 |
+
def run_settings(self, *args):
|
72 |
+
changed = []
|
73 |
+
|
74 |
+
for key, value, comp in zip(opts.data_labels.keys(), args, self.components):
|
75 |
+
assert comp == self.dummy_component or opts.same_type(value, opts.data_labels[key].default), f"Bad value for setting {key}: {value}; expecting {type(opts.data_labels[key].default).__name__}"
|
76 |
+
|
77 |
+
for key, value, comp in zip(opts.data_labels.keys(), args, self.components):
|
78 |
+
if comp == self.dummy_component:
|
79 |
+
continue
|
80 |
+
|
81 |
+
if opts.set(key, value):
|
82 |
+
changed.append(key)
|
83 |
+
|
84 |
+
try:
|
85 |
+
opts.save(shared.config_filename)
|
86 |
+
except RuntimeError:
|
87 |
+
return opts.dumpjson(), f'{len(changed)} settings changed without save: {", ".join(changed)}.'
|
88 |
+
return opts.dumpjson(), f'{len(changed)} settings changed{": " if changed else ""}{", ".join(changed)}.'
|
89 |
+
|
90 |
+
def run_settings_single(self, value, key):
|
91 |
+
if not opts.same_type(value, opts.data_labels[key].default):
|
92 |
+
return gr.update(visible=True), opts.dumpjson()
|
93 |
+
|
94 |
+
if value is None or not opts.set(key, value):
|
95 |
+
return gr.update(value=getattr(opts, key)), opts.dumpjson()
|
96 |
+
|
97 |
+
opts.save(shared.config_filename)
|
98 |
+
|
99 |
+
return get_value_for_setting(key), opts.dumpjson()
|
100 |
+
|
101 |
+
def create_ui(self, loadsave, dummy_component):
|
102 |
+
self.components = []
|
103 |
+
self.component_dict = {}
|
104 |
+
self.dummy_component = dummy_component
|
105 |
+
|
106 |
+
shared.settings_components = self.component_dict
|
107 |
+
|
108 |
+
script_callbacks.ui_settings_callback()
|
109 |
+
opts.reorder()
|
110 |
+
|
111 |
+
with gr.Blocks(analytics_enabled=False) as settings_interface:
|
112 |
+
with gr.Row():
|
113 |
+
with gr.Column(scale=6):
|
114 |
+
self.submit = gr.Button(value="Apply settings", variant='primary', elem_id="settings_submit")
|
115 |
+
|
116 |
+
self.result = gr.HTML(elem_id="settings_result")
|
117 |
+
|
118 |
+
self.quicksettings_names = opts.quicksettings_list
|
119 |
+
self.quicksettings_names = {x: i for i, x in enumerate(self.quicksettings_names) if x != 'quicksettings'}
|
120 |
+
|
121 |
+
self.quicksettings_list = []
|
122 |
+
|
123 |
+
previous_section = None
|
124 |
+
current_tab = None
|
125 |
+
current_row = None
|
126 |
+
with gr.Tabs(elem_id="settings"):
|
127 |
+
for i, (k, item) in enumerate(opts.data_labels.items()):
|
128 |
+
section_must_be_skipped = item.section[0] is None
|
129 |
+
|
130 |
+
if previous_section != item.section and not section_must_be_skipped:
|
131 |
+
elem_id, text = item.section
|
132 |
+
|
133 |
+
if current_tab is not None:
|
134 |
+
current_row.__exit__()
|
135 |
+
current_tab.__exit__()
|
136 |
+
|
137 |
+
gr.Group()
|
138 |
+
current_tab = gr.TabItem(elem_id=f"settings_{elem_id}", label=text)
|
139 |
+
current_tab.__enter__()
|
140 |
+
current_row = gr.Column(elem_id=f"column_settings_{elem_id}", variant='compact')
|
141 |
+
current_row.__enter__()
|
142 |
+
|
143 |
+
previous_section = item.section
|
144 |
+
|
145 |
+
if k in self.quicksettings_names and not shared.cmd_opts.freeze_settings:
|
146 |
+
self.quicksettings_list.append((i, k, item))
|
147 |
+
self.components.append(dummy_component)
|
148 |
+
elif section_must_be_skipped:
|
149 |
+
self.components.append(dummy_component)
|
150 |
+
else:
|
151 |
+
component = create_setting_component(k)
|
152 |
+
self.component_dict[k] = component
|
153 |
+
self.components.append(component)
|
154 |
+
|
155 |
+
if current_tab is not None:
|
156 |
+
current_row.__exit__()
|
157 |
+
current_tab.__exit__()
|
158 |
+
|
159 |
+
with gr.TabItem("Defaults", id="defaults", elem_id="settings_tab_defaults"):
|
160 |
+
loadsave.create_ui()
|
161 |
+
|
162 |
+
with gr.TabItem("Sysinfo", id="sysinfo", elem_id="settings_tab_sysinfo"):
|
163 |
+
gr.HTML('<a href="./internal/sysinfo-download" class="sysinfo_big_link" download>Download system info</a><br /><a href="./internal/sysinfo" target="_blank">(or open as text in a new page)</a>', elem_id="sysinfo_download")
|
164 |
+
|
165 |
+
with gr.Row():
|
166 |
+
with gr.Column(scale=1):
|
167 |
+
sysinfo_check_file = gr.File(label="Check system info for validity", type='binary')
|
168 |
+
with gr.Column(scale=1):
|
169 |
+
sysinfo_check_output = gr.HTML("", elem_id="sysinfo_validity")
|
170 |
+
with gr.Column(scale=100):
|
171 |
+
pass
|
172 |
+
|
173 |
+
with gr.TabItem("Actions", id="actions", elem_id="settings_tab_actions"):
|
174 |
+
request_notifications = gr.Button(value='Request browser notifications', elem_id="request_notifications")
|
175 |
+
download_localization = gr.Button(value='Download localization template', elem_id="download_localization")
|
176 |
+
reload_script_bodies = gr.Button(value='Reload custom script bodies (No ui updates, No restart)', variant='secondary', elem_id="settings_reload_script_bodies")
|
177 |
+
with gr.Row():
|
178 |
+
unload_sd_model = gr.Button(value='Unload SD checkpoint to RAM', elem_id="sett_unload_sd_model")
|
179 |
+
reload_sd_model = gr.Button(value='Load SD checkpoint to VRAM from RAM', elem_id="sett_reload_sd_model")
|
180 |
+
with gr.Row():
|
181 |
+
calculate_all_checkpoint_hash = gr.Button(value='Calculate hash for all checkpoint', elem_id="calculate_all_checkpoint_hash")
|
182 |
+
calculate_all_checkpoint_hash_threads = gr.Number(value=1, label="Number of parallel calculations", elem_id="calculate_all_checkpoint_hash_threads", precision=0, minimum=1)
|
183 |
+
|
184 |
+
with gr.TabItem("Licenses", id="licenses", elem_id="settings_tab_licenses"):
|
185 |
+
gr.HTML(shared.html("licenses.html"), elem_id="licenses")
|
186 |
+
|
187 |
+
self.show_all_pages = gr.Button(value="Show all pages", elem_id="settings_show_all_pages")
|
188 |
+
self.show_one_page = gr.Button(value="Show only one page", elem_id="settings_show_one_page", visible=False)
|
189 |
+
self.show_one_page.click(lambda: None)
|
190 |
+
|
191 |
+
self.search_input = gr.Textbox(value="", elem_id="settings_search", max_lines=1, placeholder="Search...", show_label=False)
|
192 |
+
|
193 |
+
self.text_settings = gr.Textbox(elem_id="settings_json", value=lambda: opts.dumpjson(), visible=False)
|
194 |
+
|
195 |
+
def call_func_and_return_text(func, text):
|
196 |
+
def handler():
|
197 |
+
t = timer.Timer()
|
198 |
+
func()
|
199 |
+
t.record(text)
|
200 |
+
|
201 |
+
return f'{text} in {t.total:.1f}s'
|
202 |
+
|
203 |
+
return handler
|
204 |
+
|
205 |
+
unload_sd_model.click(
|
206 |
+
fn=call_func_and_return_text(sd_models.unload_model_weights, 'Unloaded the checkpoint'),
|
207 |
+
inputs=[],
|
208 |
+
outputs=[self.result]
|
209 |
+
)
|
210 |
+
|
211 |
+
reload_sd_model.click(
|
212 |
+
fn=call_func_and_return_text(lambda: sd_models.send_model_to_device(shared.sd_model), 'Loaded the checkpoint'),
|
213 |
+
inputs=[],
|
214 |
+
outputs=[self.result]
|
215 |
+
)
|
216 |
+
|
217 |
+
request_notifications.click(
|
218 |
+
fn=lambda: None,
|
219 |
+
inputs=[],
|
220 |
+
outputs=[],
|
221 |
+
_js='function(){}'
|
222 |
+
)
|
223 |
+
|
224 |
+
download_localization.click(
|
225 |
+
fn=lambda: None,
|
226 |
+
inputs=[],
|
227 |
+
outputs=[],
|
228 |
+
_js='download_localization'
|
229 |
+
)
|
230 |
+
|
231 |
+
def reload_scripts():
|
232 |
+
scripts.reload_script_body_only()
|
233 |
+
reload_javascript() # need to refresh the html page
|
234 |
+
|
235 |
+
reload_script_bodies.click(
|
236 |
+
fn=reload_scripts,
|
237 |
+
inputs=[],
|
238 |
+
outputs=[]
|
239 |
+
)
|
240 |
+
|
241 |
+
|
242 |
+
def check_file(x):
|
243 |
+
if x is None:
|
244 |
+
return ''
|
245 |
+
|
246 |
+
if sysinfo.check(x.decode('utf8', errors='ignore')):
|
247 |
+
return 'Valid'
|
248 |
+
|
249 |
+
return 'Invalid'
|
250 |
+
|
251 |
+
sysinfo_check_file.change(
|
252 |
+
fn=check_file,
|
253 |
+
inputs=[sysinfo_check_file],
|
254 |
+
outputs=[sysinfo_check_output],
|
255 |
+
)
|
256 |
+
|
257 |
+
def calculate_all_checkpoint_hash_fn(max_thread):
|
258 |
+
checkpoints_list = sd_models.checkpoints_list.values()
|
259 |
+
with ThreadPoolExecutor(max_workers=max_thread) as executor:
|
260 |
+
futures = [executor.submit(checkpoint.calculate_shorthash) for checkpoint in checkpoints_list]
|
261 |
+
completed = 0
|
262 |
+
for _ in as_completed(futures):
|
263 |
+
completed += 1
|
264 |
+
print(f"{completed} / {len(checkpoints_list)} ")
|
265 |
+
print("Finish calculating hash for all checkpoints")
|
266 |
+
|
267 |
+
calculate_all_checkpoint_hash.click(
|
268 |
+
fn=calculate_all_checkpoint_hash_fn,
|
269 |
+
inputs=[calculate_all_checkpoint_hash_threads],
|
270 |
+
)
|
271 |
+
|
272 |
+
self.interface = settings_interface
|
273 |
+
|
274 |
+
def add_quicksettings(self):
|
275 |
+
with gr.Row(elem_id="quicksettings", variant="compact"):
|
276 |
+
for _i, k, _item in sorted(self.quicksettings_list, key=lambda x: self.quicksettings_names.get(x[1], x[0])):
|
277 |
+
component = create_setting_component(k, is_quicksettings=True)
|
278 |
+
self.component_dict[k] = component
|
279 |
+
|
280 |
+
def add_functionality(self, demo):
|
281 |
+
self.submit.click(
|
282 |
+
fn=wrap_gradio_call(lambda *args: self.run_settings(*args), extra_outputs=[gr.update()]),
|
283 |
+
inputs=self.components,
|
284 |
+
outputs=[self.text_settings, self.result],
|
285 |
+
)
|
286 |
+
|
287 |
+
for _i, k, _item in self.quicksettings_list:
|
288 |
+
component = self.component_dict[k]
|
289 |
+
info = opts.data_labels[k]
|
290 |
+
|
291 |
+
if isinstance(component, gr.Textbox):
|
292 |
+
methods = [component.submit, component.blur]
|
293 |
+
elif hasattr(component, 'release'):
|
294 |
+
methods = [component.release]
|
295 |
+
else:
|
296 |
+
methods = [component.change]
|
297 |
+
|
298 |
+
for method in methods:
|
299 |
+
method(
|
300 |
+
fn=lambda value, k=k: self.run_settings_single(value, key=k),
|
301 |
+
inputs=[component],
|
302 |
+
outputs=[component, self.text_settings],
|
303 |
+
show_progress=info.refresh is not None,
|
304 |
+
)
|
305 |
+
|
306 |
+
button_set_checkpoint = gr.Button('Change checkpoint', elem_id='change_checkpoint', visible=False)
|
307 |
+
button_set_checkpoint.click(
|
308 |
+
fn=lambda value, _: self.run_settings_single(value, key='sd_model_checkpoint'),
|
309 |
+
_js="function(v){ var res = desiredCheckpointName; desiredCheckpointName = ''; return [res || v, null]; }",
|
310 |
+
inputs=[self.component_dict['sd_model_checkpoint'], self.dummy_component],
|
311 |
+
outputs=[self.component_dict['sd_model_checkpoint'], self.text_settings],
|
312 |
+
)
|
313 |
+
|
314 |
+
component_keys = [k for k in opts.data_labels.keys() if k in self.component_dict]
|
315 |
+
|
316 |
+
def get_settings_values():
|
317 |
+
return [get_value_for_setting(key) for key in component_keys]
|
318 |
+
|
319 |
+
demo.load(
|
320 |
+
fn=get_settings_values,
|
321 |
+
inputs=[],
|
322 |
+
outputs=[self.component_dict[k] for k in component_keys],
|
323 |
+
queue=False,
|
324 |
+
)
|
325 |
+
|
326 |
+
def search(self, text):
|
327 |
+
print(text)
|
328 |
+
|
329 |
+
return [gr.update(visible=text in (comp.label or "")) for comp in self.components]
|