Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix forge bugs #162

Merged
merged 6 commits into from
Jul 31, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 35 additions & 17 deletions scripts/model_mixer.py
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,11 @@ def mm_list_models():

# save current checkpoint_info and call register() again to restore
checkpoint_info = shared.sd_model.sd_checkpoint_info if shared.sd_model is not None else None
orig_list_models()
if orig_list_models is not None:
orig_list_models()
else:
sd_models.list_models()

if checkpoint_info is not None:
if getattr(sd_models.model_data, "loaded_sd_models", None) is None:
return
Expand Down Expand Up @@ -899,7 +903,7 @@ def _model_option_ui(self, n, isxl):
mm_alpha = gr.Slider(label=f"Multiplier for Model {name}", minimum=-1.0, maximum=2, step=0.001, value=0.5)
with gr.Group():
with gr.Row():
with gr.Group(Visible=True) as mbw_advanced:
with gr.Group(visible=True) as mbw_advanced:
mm_usembws = gr.Dropdown(["ALL","BASE","INP*","MID","OUT*"]+BLOCKID[1:], value=[], multiselect=True, label="Merge Block Weights", show_label=False, info="or use Merge Block Weights for selected blocks")
with gr.Group(visible=False) as mbw_simple:
mm_usembws_simple = gr.CheckboxGroup(["BASE","INP*","MID","OUT*"], value=[], label="Merge Block Weights", show_label=False, info="or use Merge Block Weights for selected blocks")
Expand Down Expand Up @@ -1107,7 +1111,7 @@ def check_calc_settings(calc_settings):
with gr.Accordion("Load settings", open=False, elem_classes=["model_mixer_load_settings"]) as mbw_load_settings:
with gr.Group(), gr.Tabs():
with gr.Tab("from image"):
with gr.Row(size=3):
with gr.Row():
with gr.Column(elem_classes=["mm_infotext_image"], scale=1):
infotext_image1 = gr.Image(elem_id="mm_infotext_image1", type="pil")
infotext_image1_load = gr.Button('Load settings', variant='secondary', elem_id=f'mm_load_settings_from_image1')
Expand Down Expand Up @@ -1157,7 +1161,7 @@ def check_calc_settings(calc_settings):
mulweight = gr.Button(elem_classes=["reset"], value="Mul")

with gr.Tab("Weight Sum Calculator"):
gr.HTML(label="weight sum helper", show_labe=False, value="<p>Calculate alphas of series of model weights. e.g.) model_a * ca + model_b * cb ... = model_a * (1 - alpha) + model_b * alpha...</p>")
gr.HTML(label="weight sum helper", show_label=False, value="<p>Calculate alphas of series of model weights. e.g.) model_a * ca + model_b * cb ... = model_a * (1 - alpha) + model_b * alpha...</p>")
with gr.Row():
model_weights = [0.0] * num_models
for n in range(num_models):
Expand Down Expand Up @@ -1306,7 +1310,7 @@ def calc_weights_for_sum(*weights):
with gr.Column(variant="compact"):
with gr.Row():
not_elements = gr.Checkbox(value=False, label="Not Elements", show_label=False, info="NOT", scale=1, min_width=30, elem_classes=["not-button"])
elements = gr.Dropdown(["time_embed", "time_embed.0", "time_embed.2", "out", "out.0", "out.2"], values=[], label="Elements", show_label=False, multiselect=True, info="Select Elements", elem_id="mm_elemental_elements", scale=7)
elements = gr.Dropdown(["time_embed", "time_embed.0", "time_embed.2", "out", "out.0", "out.2"], value=[], label="Elements", show_label=False, multiselect=True, info="Select Elements", elem_id="mm_elemental_elements", scale=7)
with gr.Column(variant="compact"):
with gr.Row():
elemental_ratio = gr.Slider(minimum=0, maximum=2, value=0.5, step=0.01, label="Ratio", scale=8)
Expand Down Expand Up @@ -1898,7 +1902,7 @@ def download_ui(i):
checkbox = gr.Checkbox(label=f"", show_label=False, value=False, visible=use_model_dl, elem_classes=["downloaded"], interactive=False, container=False)
with gr.Column(scale=10, min_width=160):
downfile = gr.HTML(label=f"Download File {i}", show_label=False, value="<p></p>")
with gr.Column(scale=1, label="Download Button", visible=use_model_dl, show_label=False, min_width=10):
with gr.Column(scale=1, visible=use_model_dl, min_width=10):
downbtn = gr.Button(' ', elem_classes=["download"])

downbtn.click(fn=downloader, inputs=[downloadfile], outputs=[checkbox, download_status], show_progress=False)
Expand Down Expand Up @@ -2938,18 +2942,30 @@ def on_app_started(demo, app):
is_txt2imgs += [False]

for _id, is_txt2img in zip(generates, is_txt2imgs):
dependencies = [x for x in demo.dependencies if (("trigger" in x and x["trigger"] == "click" and _id in x["targets"]) or
if hasattr(demo, "dependencies"):
dependencies = [x for x in demo.dependencies if (("trigger" in x and x["trigger"] == "click" and _id in x["targets"]) or
("trigger" not in x and isinstance(x["targets"][0], tuple) and "click" in x["targets"][0] and _id in x["targets"][0]))]
else: # gradio4
dependencies = [{"js": x.js, "inputs": [b._id for b in x.inputs]} for x in demo.fns.values() if (_id, "click") in x.targets and x.js is not None]

dependency = None

for d in dependencies:
if "js" in d and d["js"] in [ "submit", "submit_img2img", "submit_txt2img"]: # submit_txt2img for SD.Next
dependency = d

if dependency is None:
continue
for d in dependencies:
if any(js in d["js"] for js in [ "submit(", "submit_img2img(", "submit_txt2img("]): # fix for webui-forge
dependency = d

if dependency is None:
continue

params = [params for params in demo.fns if compare_components_with_ids(params.inputs, dependency["inputs"])]
if hasattr(demo, "dependencies"):
params = [params for params in demo.fns if compare_components_with_ids(params.inputs, dependency["inputs"])]
else: # gradio4
params = [params for params in demo.fns.values() if compare_components_with_ids(params.inputs, dependency["inputs"])]

if is_txt2img:
MM.components["txt2img_elem_ids"] = [x.elem_id if hasattr(x,"elem_id") else "None" for x in params[0].inputs]
Expand Down Expand Up @@ -6168,9 +6184,6 @@ def hook_sd_models(demo, app):
"""hook some sd_models methods"""
global orig_list_models

if getattr(sd_models.model_data, "loaded_sd_models", None) is None:
return

def hook_reuse_model_from_already_loaded(sd_model, checkpoint_info, timer):
already_loaded = None
loaded_model = None
Expand Down Expand Up @@ -6253,12 +6266,17 @@ def set_sd_model(v, already_loaded=False):
def unload():
global orig_list_models

sd_models.reuse_model_from_already_loaded = sd_models.mm_orig_reuse_model_from_already_loaded
sd_models.list_models = orig_list_models
if orig_list_models is not None:
sd_models.list_models = orig_list_models
orig_list_models = None
sd_models.model_data.set_sd_model = sd_models.model_data.mm_orig_set_sd_model
delattr(sd_models, "mm_orig_reuse_model_from_already_loaded")
delattr(sd_models.model_data, "mm_orig_set_sd_model")

if getattr(sd_models.model_data, "mm_orig_set_sd_model", None) is not None:
sd_models.model_data.set_sd_model = sd_models.model_data.mm_orig_set_sd_model
delattr(sd_models.model_data, "mm_orig_set_sd_model")

if getattr(sd_models, "mm_orig_reuse_model_from_already_loaded", None) is not None:
sd_models.reuse_model_from_already_loaded = sd_models.mm_orig_reuse_model_from_already_loaded
delattr(sd_models, "mm_orig_reuse_model_from_already_loaded")


script_callbacks.on_app_started(hook_sd_models)
Expand Down