Skip to content

Commit 71eaa5c

Browse files
authored
rework UI so that the toolbar is managed by Forge
1 parent b787805 commit 71eaa5c

File tree

9 files changed

+105
-28
lines changed

9 files changed

+105
-28
lines changed

modules/initialize_util.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,14 +181,14 @@ def configure_opts_onchange():
181181
from modules.call_queue import wrap_queued_call
182182
from modules_forge import main_thread
183183

184-
shared.opts.onchange("sd_model_checkpoint", wrap_queued_call(lambda: main_thread.run_and_wait_result(sd_models.reload_model_weights)), call=False)
185-
shared.opts.onchange("sd_vae", wrap_queued_call(lambda: main_thread.run_and_wait_result(sd_vae.reload_vae_weights)), call=False)
184+
# shared.opts.onchange("sd_model_checkpoint", wrap_queued_call(lambda: main_thread.run_and_wait_result(sd_models.reload_model_weights)), call=False)
185+
# shared.opts.onchange("sd_vae", wrap_queued_call(lambda: main_thread.run_and_wait_result(sd_vae.reload_vae_weights)), call=False)
186186
shared.opts.onchange("sd_vae_overrides_per_model_preferences", wrap_queued_call(lambda: main_thread.run_and_wait_result(sd_vae.reload_vae_weights)), call=False)
187187
shared.opts.onchange("temp_dir", ui_tempdir.on_tmpdir_changed)
188188
shared.opts.onchange("gradio_theme", shared.reload_gradio_theme)
189189
# shared.opts.onchange("cross_attention_optimization", wrap_queued_call(lambda: sd_hijack.model_hijack.redo_hijack(shared.sd_model)), call=False)
190-
shared.opts.onchange("fp8_storage", wrap_queued_call(lambda: sd_models.reload_model_weights()), call=False)
191-
shared.opts.onchange("cache_fp16_weight", wrap_queued_call(lambda: sd_models.reload_model_weights(forced_reload=True)), call=False)
190+
# shared.opts.onchange("fp8_storage", wrap_queued_call(lambda: sd_models.reload_model_weights()), call=False)
191+
# shared.opts.onchange("cache_fp16_weight", wrap_queued_call(lambda: sd_models.reload_model_weights(forced_reload=True)), call=False)
192192
startup_timer.record("opts onchange")
193193

194194

modules/processing.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1502,8 +1502,7 @@ def setup_conds(self):
15021502
if self.enable_hr and self.hr_checkpoint_info is None:
15031503
if shared.opts.hires_fix_use_firstpass_conds:
15041504
self.calculate_hr_conds()
1505-
1506-
elif shared.sd_model.sd_checkpoint_info == sd_models.select_checkpoint(): # if in lowvram mode, we need to calculate conds right away, before the cond NN is unloaded
1505+
else:
15071506
with devices.autocast():
15081507
extra_networks.activate(self, self.hr_extra_network_data)
15091508

modules/processing_scripts/refiner.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ def ui(self, is_img2img):
2424
gr.Markdown('Refiner is currently under maintenance and unavailable. Sorry for the inconvenience.')
2525

2626
with gr.Row():
27-
refiner_checkpoint = gr.Dropdown(label='Checkpoint', elem_id=self.elem_id("checkpoint"), choices=["", *sd_models.checkpoint_tiles()], value='', tooltip="switch to another model in the middle of generation", interactive=False)
28-
create_refresh_button(refiner_checkpoint, sd_models.list_models, lambda: {"choices": sd_models.checkpoint_tiles()}, self.elem_id("checkpoint_refresh"))
27+
refiner_checkpoint = gr.Dropdown(label='Checkpoint', elem_id=self.elem_id("checkpoint"), choices=["", *sd_models.checkpoint_tiles()], value='', tooltip="switch to another model in the middle of generation", interactive=False, visible=False)
28+
# create_refresh_button(refiner_checkpoint, sd_models.list_models, lambda: {"choices": sd_models.checkpoint_tiles()}, self.elem_id("checkpoint_refresh"))
2929

30-
refiner_switch_at = gr.Slider(value=0.8, label="Switch at", minimum=0.01, maximum=1.0, step=0.01, elem_id=self.elem_id("switch_at"), tooltip="fraction of sampling steps when the switch to refiner model should happen; 1=never, 0.5=switch in the middle of generation", interactive=False)
30+
refiner_switch_at = gr.Slider(value=0.8, label="Switch at", minimum=0.01, maximum=1.0, step=0.01, elem_id=self.elem_id("switch_at"), tooltip="fraction of sampling steps when the switch to refiner model should happen; 1=never, 0.5=switch in the middle of generation", interactive=False, visible=False)
3131

3232
def lookup_checkpoint(title):
3333
info = sd_models.get_closet_checkpoint_match(title)

modules/sd_models.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,6 @@ def get_obj_from_str(string, reload=False):
531531

532532
@torch.no_grad()
533533
def load_model(checkpoint_info=None, already_loaded_state_dict=None):
534-
from modules import sd_hijack
535534
checkpoint_info = checkpoint_info or select_checkpoint()
536535

537536
timer = Timer()
@@ -603,11 +602,11 @@ def reuse_model_from_already_loaded(sd_model, checkpoint_info, timer):
603602

604603

605604
def reload_model_weights(sd_model=None, info=None, forced_reload=False):
606-
return load_model(info)
605+
pass
607606

608607

609608
def unload_model_weights(sd_model=None, info=None):
610-
return sd_model
609+
pass
611610

612611

613612
def apply_token_merging(sd_model, token_merging_ratio):

modules/shared_options.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@
168168
}))
169169

170170
options_templates.update(options_section(('sd', "Stable Diffusion", "sd"), {
171-
"sd_model_checkpoint": OptionInfo(None, "Stable Diffusion checkpoint", gr.Dropdown, lambda: {"choices": shared_items.list_checkpoint_tiles(shared.opts.sd_checkpoint_dropdown_use_short)}, refresh=shared_items.refresh_checkpoints, infotext='Model hash'),
171+
"sd_model_checkpoint": OptionInfo(None, "(Managed by Forge)", gr.State),
172172
"sd_checkpoints_limit": OptionInfo(1, "Maximum number of checkpoints loaded at the same time", gr.Slider, {"minimum": 1, "maximum": 10, "step": 1}),
173173
"sd_checkpoints_keep_in_cpu": OptionInfo(True, "Only keep one model on device").info("will keep models other than the currently used one in RAM rather than VRAM"),
174174
"sd_checkpoint_cache": OptionInfo(0, "Checkpoints to cache in RAM", gr.Slider, {"minimum": 0, "maximum": 10, "step": 1}).info("obsolete; set to 0 and use the two settings above instead"),
@@ -178,7 +178,7 @@
178178
"enable_batch_seeds": OptionInfo(True, "Make K-diffusion samplers produce same images in a batch as when making a single image"),
179179
"comma_padding_backtrack": OptionInfo(20, "Prompt word wrap length limit", gr.Slider, {"minimum": 0, "maximum": 74, "step": 1}).info("in tokens - for texts shorter than specified, if they don't fit into 75 token limit, move them to the next 75 token chunk"),
180180
"sdxl_clip_l_skip": OptionInfo(False, "Clip skip SDXL", gr.Checkbox).info("Enable Clip skip for the secondary clip model in sdxl. Has no effect on SD 1.5 or SD 2.0/2.1."),
181-
"CLIP_stop_at_last_layers": OptionInfo(1, "Clip skip", gr.Slider, {"minimum": 1, "maximum": 12, "step": 1}, infotext="Clip skip").link("wiki", "https://github.com/AUTOMATIC1111/stable-diffusion-webui/wiki/Features#clip-skip").info("ignore last layers of CLIP network; 1 ignores none, 2 ignores one layer"),
181+
"CLIP_stop_at_last_layers": OptionInfo(1, "(Managed by Forge)", gr.State),
182182
"upcast_attn": OptionInfo(False, "Upcast cross attention layer to float32"),
183183
"randn_source": OptionInfo("GPU", "Random number generator source.", gr.Radio, {"choices": ["GPU", "CPU", "NV"]}, infotext="RNG").info("changes seeds drastically; use CPU to produce the same picture across different videocard vendors; use NV to produce same picture as on NVidia videocards"),
184184
"tiling": OptionInfo(False, "Tiling", infotext='Tiling').info("produce a tileable picture"),
@@ -204,7 +204,7 @@
204204
For img2img, VAE is used to process user's input image before the sampling, and to create an image after sampling.
205205
"""),
206206
"sd_vae_checkpoint_cache": OptionInfo(0, "VAE Checkpoints to cache in RAM", gr.Slider, {"minimum": 0, "maximum": 10, "step": 1}),
207-
"sd_vae": OptionInfo("Automatic", "SD VAE", gr.Dropdown, lambda: {"choices": shared_items.sd_vae_items()}, refresh=shared_items.refresh_vae_list, infotext='VAE').info("choose VAE model: Automatic = use one with same filename as checkpoint; None = use VAE from checkpoint"),
207+
"sd_vae": OptionInfo("Automatic", "(Managed by Forge)", gr.State),
208208
"sd_vae_overrides_per_model_preferences": OptionInfo(True, "Selected VAE overrides per-model preferences").info("you can set per-model VAE either by editing user metadata for checkpoints, or by making the VAE have same name as checkpoint"),
209209
"auto_vae_precision_bfloat16": OptionInfo(False, "Automatically convert VAE to bfloat16").info("triggers when a tensor with NaNs is produced in VAE; disabling the option in this case will result in a black square image; if enabled, overrides the option below"),
210210
"auto_vae_precision": OptionInfo(True, "Automatically revert VAE to 32-bit floats").info("triggers when a tensor with NaNs is produced in VAE; disabling the option in this case will result in a black square image"),
@@ -329,7 +329,7 @@
329329

330330
options_templates.update(options_section(('ui', "User interface", "ui"), {
331331
"localization": OptionInfo("None", "Localization", gr.Dropdown, lambda: {"choices": ["None"] + list(localization.localizations.keys())}, refresh=lambda: localization.list_localizations(cmd_opts.localizations_dir)).needs_reload_ui(),
332-
"quicksettings_list": OptionInfo(["sd_model_checkpoint", "sd_vae", "CLIP_stop_at_last_layers"], "Quicksettings list", ui_components.DropdownMulti, lambda: {"choices": list(shared.opts.data_labels.keys())}).js("info", "settingsHintsShowQuicksettings").info("setting entries that appear at the top of page rather than in settings tab").needs_reload_ui(),
332+
"quick_setting_list": OptionInfo([], "Quicksettings list", ui_components.DropdownMulti, lambda: {"choices": list(shared.opts.data_labels.keys())}).js("info", "settingsHintsShowQuicksettings").info("setting entries that appear at the top of page rather than in settings tab").needs_reload_ui(),
333333
"ui_tab_order": OptionInfo([], "UI tab order", ui_components.DropdownMulti, lambda: {"choices": list(shared.tab_names)}).needs_reload_ui(),
334334
"hidden_tabs": OptionInfo([], "Hidden UI tabs", ui_components.DropdownMulti, lambda: {"choices": list(shared.tab_names)}).needs_reload_ui(),
335335
"ui_reorder_list": OptionInfo([], "UI item order for txt2img/img2img tabs", ui_components.DropdownMulti, lambda: {"choices": list(shared_items.ui_reorder_categories())}).info("selected items appear first").needs_reload_ui(),

modules/ui.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from modules import prompt_parser
2727
from modules.infotext_utils import image_from_url_text, PasteField
2828
from modules_forge.forge_canvas.canvas import ForgeCanvas, canvas_head
29+
from modules_forge import main_entry
2930

3031

3132
create_setting_component = ui_settings.create_setting_component
@@ -328,15 +329,15 @@ def create_ui():
328329
hr_resize_x = gr.Slider(minimum=0, maximum=2048, step=8, label="Resize width to", value=0, elem_id="txt2img_hr_resize_x")
329330
hr_resize_y = gr.Slider(minimum=0, maximum=2048, step=8, label="Resize height to", value=0, elem_id="txt2img_hr_resize_y")
330331

331-
with FormRow(elem_id="txt2img_hires_fix_row3", variant="compact", visible=opts.hires_fix_show_sampler) as hr_sampler_container:
332+
with FormRow(elem_id="txt2img_hires_fix_row3", variant="compact") as hr_sampler_container:
332333

333-
hr_checkpoint_name = gr.Dropdown(label='Checkpoint', elem_id="hr_checkpoint", choices=["Use same checkpoint"] + modules.sd_models.checkpoint_tiles(use_short=True), value="Use same checkpoint")
334-
create_refresh_button(hr_checkpoint_name, modules.sd_models.list_models, lambda: {"choices": ["Use same checkpoint"] + modules.sd_models.checkpoint_tiles(use_short=True)}, "hr_checkpoint_refresh")
334+
hr_checkpoint_name = gr.Dropdown(label='Checkpoint', elem_id="hr_checkpoint", choices=["Use same checkpoint"], value="Use same checkpoint", visible=False, interactive=False)
335+
# create_refresh_button(hr_checkpoint_name, modules.sd_models.list_models, lambda: {"choices": ["Use same checkpoint"] + modules.sd_models.checkpoint_tiles(use_short=True)}, "hr_checkpoint_refresh")
335336

336337
hr_sampler_name = gr.Dropdown(label='Hires sampling method', elem_id="hr_sampler", choices=["Use same sampler"] + sd_samplers.visible_sampler_names(), value="Use same sampler")
337338
hr_scheduler = gr.Dropdown(label='Hires schedule type', elem_id="hr_scheduler", choices=["Use same scheduler"] + [x.label for x in sd_schedulers.schedulers], value="Use same scheduler")
338339

339-
with FormRow(elem_id="txt2img_hires_fix_row4", variant="compact", visible=opts.hires_fix_show_prompts) as hr_prompts_container:
340+
with FormRow(elem_id="txt2img_hires_fix_row4", variant="compact") as hr_prompts_container:
340341
with gr.Column(scale=80):
341342
with gr.Row():
342343
hr_prompt = gr.Textbox(label="Hires prompt", elem_id="hires_prompt", show_label=False, lines=3, placeholder="Prompt for hires fix pass.\nLeave empty to use the same prompt as in first pass.", elem_classes=["prompt"])
@@ -889,8 +890,8 @@ def select_img2img_tab(tab):
889890
settings.create_ui(loadsave, dummy_component)
890891

891892
interfaces = [
892-
(txt2img_interface, "txt2img", "txt2img"),
893-
(img2img_interface, "img2img", "img2img"),
893+
(txt2img_interface, "Txt2img", "txt2img"),
894+
(img2img_interface, "Img2img", "img2img"),
894895
(extras_interface, "Extras", "extras"),
895896
(pnginfo_interface, "PNG Info", "pnginfo"),
896897
(modelmerger_ui.blocks, "Checkpoint Merger", "modelmerger"),
@@ -941,7 +942,9 @@ def select_img2img_tab(tab):
941942
settings.text_settings.change(fn=update_image_cfg_scale_visibility, inputs=[], outputs=[image_cfg_scale])
942943
demo.load(fn=update_image_cfg_scale_visibility, inputs=[], outputs=[image_cfg_scale])
943944

944-
modelmerger_ui.setup_ui(dummy_component=dummy_component, sd_model_checkpoint_component=settings.component_dict['sd_model_checkpoint'])
945+
modelmerger_ui.setup_ui(dummy_component=dummy_component, sd_model_checkpoint_component=main_entry.sd_model_checkpoint)
946+
947+
main_entry.forge_main_entry()
945948

946949
if ui_settings_from_file != loadsave.ui_settings:
947950
loadsave.dump_defaults()

modules/ui_settings.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from modules.ui_components import FormRow
88
from modules.ui_gradio_extensions import reload_javascript
99
from concurrent.futures import ThreadPoolExecutor, as_completed
10+
from modules_forge import main_entry
1011

1112

1213
def get_value_for_setting(key):
@@ -41,6 +42,9 @@ def fun():
4142

4243
elem_id = f"setting_{key}"
4344

45+
if comp == gr.State:
46+
return gr.State(fun())
47+
4448
if info.refresh is not None:
4549
if is_quicksettings:
4650
res = comp(label=info.label, value=fun(), elem_id=elem_id, **(args or {}))
@@ -125,7 +129,7 @@ def create_ui(self, loadsave, dummy_component):
125129

126130
self.result = gr.HTML(elem_id="settings_result")
127131

128-
self.quicksettings_names = opts.quicksettings_list
132+
self.quicksettings_names = opts.quick_setting_list
129133
self.quicksettings_names = {x: i for i, x in enumerate(self.quicksettings_names) if x != 'quicksettings'}
130134

131135
self.quicksettings_list = []
@@ -289,6 +293,7 @@ def calculate_all_checkpoint_hash_fn(max_thread):
289293

290294
def add_quicksettings(self):
291295
with gr.Row(elem_id="quicksettings", variant="compact"):
296+
main_entry.make_checkpoint_manager_ui()
292297
for _i, k, _item in sorted(self.quicksettings_list, key=lambda x: self.quicksettings_names.get(x[1], x[0])):
293298
component = create_setting_component(k, is_quicksettings=True)
294299
self.component_dict[k] = component
@@ -318,12 +323,15 @@ def add_functionality(self, demo):
318323
show_progress=False,
319324
)
320325

326+
def button_set_checkpoint_change(value, dummy):
327+
return value, opts.dumpjson()
328+
321329
button_set_checkpoint = gr.Button('Change checkpoint', elem_id='change_checkpoint', visible=False)
322330
button_set_checkpoint.click(
323-
fn=lambda value, _: self.run_settings_single(value, key='sd_model_checkpoint'),
331+
fn=button_set_checkpoint_change,
324332
_js="function(v){ var res = desiredCheckpointName; desiredCheckpointName = ''; return [res || v, null]; }",
325-
inputs=[self.component_dict['sd_model_checkpoint'], self.dummy_component],
326-
outputs=[self.component_dict['sd_model_checkpoint'], self.text_settings],
333+
inputs=[main_entry.sd_model_checkpoint, self.dummy_component],
334+
outputs=[main_entry.sd_model_checkpoint, self.text_settings],
327335
)
328336

329337
component_keys = [k for k in opts.data_labels.keys() if k in self.component_dict]

modules_forge/main_entry.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import gradio as gr
2+
3+
from modules import shared_items, shared, ui_common, sd_models
4+
from modules import sd_vae as sd_vae_module
5+
from modules_forge import main_thread
6+
7+
8+
sd_model_checkpoint: gr.Dropdown = None
9+
sd_vae: gr.Dropdown = None
10+
CLIP_stop_at_last_layers: gr.Slider = None
11+
12+
13+
def make_checkpoint_manager_ui():
14+
global sd_model_checkpoint, sd_vae, CLIP_stop_at_last_layers
15+
16+
if shared.opts.sd_model_checkpoint in [None, 'None', 'none', '']:
17+
if len(sd_models.checkpoints_list) == 0:
18+
sd_models.list_models()
19+
if len(sd_models.checkpoints_list) > 0:
20+
shared.opts.set('sd_model_checkpoint', next(iter(sd_models.checkpoints_list.keys())))
21+
22+
sd_model_checkpoint_args = lambda: {"choices": shared_items.list_checkpoint_tiles(shared.opts.sd_checkpoint_dropdown_use_short)}
23+
sd_model_checkpoint = gr.Dropdown(
24+
value=shared.opts.sd_model_checkpoint,
25+
label="Checkpoint",
26+
**sd_model_checkpoint_args()
27+
)
28+
ui_common.create_refresh_button(sd_model_checkpoint, shared_items.refresh_checkpoints, sd_model_checkpoint_args, f"forge_refresh_checkpoint")
29+
30+
sd_vae_args = lambda: {"choices": shared_items.sd_vae_items()}
31+
sd_vae = gr.Dropdown(
32+
value="Automatic",
33+
label="VAE",
34+
**sd_vae_args()
35+
)
36+
ui_common.create_refresh_button(sd_vae, shared_items.refresh_vae_list, sd_vae_args, f"forge_refresh_vae")
37+
38+
CLIP_stop_at_last_layers = gr.Slider(label="Clip skip", value=shared.opts.CLIP_stop_at_last_layers, **{"minimum": 1, "maximum": 12, "step": 1})
39+
40+
return
41+
42+
43+
def checkpoint_change(ckpt_name):
44+
print(f'Checkpoint Selected: {ckpt_name}')
45+
shared.opts.set('sd_model_checkpoint', ckpt_name)
46+
shared.opts.save(shared.config_filename)
47+
48+
sd_models.load_model()
49+
return
50+
51+
52+
def vae_change(vae_name):
53+
print(f'VAE Selected: {vae_name}')
54+
shared.opts.set('sd_vae', vae_name)
55+
sd_vae_module.reload_vae_weights()
56+
return
57+
58+
59+
def clip_skip_change(clip_skip):
60+
print(f'CLIP SKIP Selected: {clip_skip}')
61+
shared.opts.set('CLIP_stop_at_last_layers', clip_skip)
62+
return
63+
64+
65+
def forge_main_entry():
66+
sd_model_checkpoint.change(lambda x: main_thread.async_run(checkpoint_change, x), inputs=[sd_model_checkpoint], show_progress=False)
67+
sd_vae.change(lambda x: main_thread.async_run(vae_change, x), inputs=[sd_vae], show_progress=False)
68+
CLIP_stop_at_last_layers.change(lambda x: main_thread.async_run(clip_skip_change, x), inputs=[CLIP_stop_at_last_layers], show_progress=False)
69+
return

style.css

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,6 @@ div.toprow-compact-tools{
431431
}
432432

433433
#quicksettings > div, #quicksettings > fieldset{
434-
max-width: 36em;
435434
width: fit-content;
436435
flex: 0 1 fit-content;
437436
padding: 0;

0 commit comments

Comments
 (0)