import argparse import gc import os import os.path import re import shutil from importlib import reload from pprint import pprint import gradio as gr from modules import (devices, script_callbacks, scripts, sd_hijack, sd_models,sd_vae, shared) from modules.scripts import basedir from modules.sd_models import checkpoints_loaded from modules.shared import opts from modules.ui import create_output_panel, create_refresh_button import scripts.mergers.mergers import scripts.mergers.pluslora import scripts.mergers.xyplot reload(scripts.mergers.mergers) # update without restarting web-ui.bat reload(scripts.mergers.xyplot) reload(scripts.mergers.pluslora) import csv import scripts.mergers.pluslora as pluslora from scripts.mergers.mergers import (TYPESEG, freezemtime, rwmergelog, simggen,smergegen) from scripts.mergers.xyplot import freezetime, nulister, numaker, numanager gensets=argparse.Namespace() def on_ui_train_tabs(params): txt2img_preview_params=params.txt2img_preview_params gensets.txt2img_preview_params=txt2img_preview_params return None path_root = basedir() def on_ui_tabs(): weights_presets="" userfilepath = os.path.join(path_root, "scripts","mbwpresets.txt") if os.path.isfile(userfilepath): try: with open(userfilepath) as f: weights_presets = f.read() filepath = userfilepath except OSError as e: pass else: filepath = os.path.join(path_root, "scripts","mbwpresets_master.txt") try: with open(filepath) as f: weights_presets = f.read() shutil.copyfile(filepath, userfilepath) except OSError as e: pass with gr.Blocks() as supermergerui: with gr.Tab("Merge"): with gr.Row().style(equal_height=False): with gr.Column(scale = 3): gr.HTML(value="
Merge models and load it for generation
") with gr.Row(): model_a = gr.Dropdown(sd_models.checkpoint_tiles(),elem_id="model_converter_model_name",label="Model A",interactive=True) create_refresh_button(model_a, sd_models.list_models,lambda: {"choices": sd_models.checkpoint_tiles()},"refresh_checkpoint_Z") model_b = gr.Dropdown(sd_models.checkpoint_tiles(),elem_id="model_converter_model_name",label="Model B",interactive=True) create_refresh_button(model_b, sd_models.list_models,lambda: {"choices": sd_models.checkpoint_tiles()},"refresh_checkpoint_Z") model_c = gr.Dropdown(sd_models.checkpoint_tiles(),elem_id="model_converter_model_name",label="Model C",interactive=True) create_refresh_button(model_c, sd_models.list_models,lambda: {"choices": sd_models.checkpoint_tiles()},"refresh_checkpoint_Z") mode = gr.Radio(label = "Merge Mode",choices = ["Weight sum:A*(1-alpha)+B*alpha", "Add difference:A+(B-C)*alpha", "Triple sum:A*(1-alpha-beta)+B*alpha+C*beta", "sum Twice:(A*(1-alpha)+B*alpha)*(1-beta)+C*beta", ], value = "Weight sum:A*(1-alpha)+B*alpha") calcmode = gr.Radio(label = "Calcutation Mode",choices = ["normal", "cosineA", "cosineB", "smoothAdd","tensor"], value = "normal") with gr.Row(): useblocks = gr.Checkbox(label="use MBW") base_alpha = gr.Slider(label="alpha", minimum=-1.0, maximum=2, step=0.001, value=0.5) base_beta = gr.Slider(label="beta", minimum=-1.0, maximum=2, step=0.001, value=0.25) #weights = gr.Textbox(label="weights,base alpha,IN00,IN02,...IN11,M00,OUT00,...,OUT11",lines=2,value="0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5") with gr.Row(): merge = gr.Button(elem_id="model_merger_merge", value="Merge!",variant='primary') mergeandgen = gr.Button(elem_id="model_merger_merge", value="Merge&Gen",variant='primary') gen = gr.Button(elem_id="model_merger_merge", value="Gen",variant='primary') stopmerge = gr.Button(elem_id="stopmerge", value="Stop",variant='primary') with gr.Row(): with gr.Column(scale = 4): save_sets = gr.CheckboxGroup(["save model", "overwrite","safetensors","fp16","save metadata"], value=["safetensors"], label="save settings") with gr.Column(scale = 2): id_sets = gr.CheckboxGroup(["image", "PNG info"], label="write merged model ID to") with gr.Row(): with gr.Column(min_width = 50, scale=2): with gr.Row(): custom_name = gr.Textbox(label="Custom Name (Optional)", elem_id="model_converter_custom_name") mergeid = gr.Textbox(label="merge from ID", elem_id="model_converter_custom_name",value = "-1") with gr.Column(min_width = 50, scale=1): with gr.Row():s_reverse= gr.Button(value="Set from ID(-1 for last)",variant='primary') with gr.Accordion("Restore faces, Tiling, Hires. fix, Batch size",open = False): batch_size = denois_str = gr.Slider(minimum=0, maximum=8, step=1, label='Batch size', value=1, elem_id="sm_txt2img_batch_size") genoptions = gr.CheckboxGroup(label = "Gen Options",choices=["Restore faces", "Tiling", "Hires. fix"], visible = True,interactive=True,type="value") with gr.Row(elem_id="txt2img_hires_fix_row1", variant="compact"): hrupscaler = gr.Dropdown(label="Upscaler", elem_id="txt2img_hr_upscaler", choices=[*shared.latent_upscale_modes, *[x.name for x in shared.sd_upscalers]], value=shared.latent_upscale_default_mode) hr2ndsteps = gr.Slider(minimum=0, maximum=150, step=1, label='Hires steps', value=0, elem_id="txt2img_hires_steps") denois_str = gr.Slider(minimum=0.0, maximum=1.0, step=0.01, label='Denoising strength', value=0.7, elem_id="txt2img_denoising_strength") hr_scale = gr.Slider(minimum=1.0, maximum=4.0, step=0.05, label="Upscale by", value=2.0, elem_id="txt2img_hr_scale") hiresfix = [genoptions,hrupscaler,hr2ndsteps,denois_str,hr_scale] with gr.Accordion("Elemental Merge",open = False): with gr.Row(): esettings1 = gr.CheckboxGroup(label = "settings",choices=["print change"],type="value",interactive=True) with gr.Row(): deep = gr.Textbox(label="Blocks:Element:Ratio,Blocks:Element:Ratio,...",lines=2,value="") with gr.Accordion("Tensor Merge",open = False,visible=False): tensor = gr.Textbox(label="Blocks:Tensors",lines=2,value="") with gr.Row(): x_type = gr.Dropdown(label="X type", choices=[x for x in TYPESEG], value="alpha", type="index") x_randseednum = gr.Number(value=3, label="number of -1", interactive=True, visible = True) xgrid = gr.Textbox(label="Sequential Merge Parameters",lines=3,value="0.25,0.5,0.75") y_type = gr.Dropdown(label="Y type", choices=[y for y in TYPESEG], value="none", type="index") ygrid = gr.Textbox(label="Y grid (Disabled if blank)",lines=3,value="",visible =False) with gr.Row(): gengrid = gr.Button(elem_id="model_merger_merge", value="Sequential XY Merge and Generation",variant='primary') stopgrid = gr.Button(elem_id="model_merger_merge", value="Stop XY",variant='primary') s_reserve1 = gr.Button(value="Reserve XY Plot",variant='primary') dtrue = gr.Checkbox(value = True, visible = False) dfalse = gr.Checkbox(value = False,visible = False) dummy_t = gr.Textbox(value = "",visible = False) blockid=["BASE","IN00","IN01","IN02","IN03","IN04","IN05","IN06","IN07","IN08","IN09","IN10","IN11","M00","OUT00","OUT01","OUT02","OUT03","OUT04","OUT05","OUT06","OUT07","OUT08","OUT09","OUT10","OUT11"] with gr.Column(scale = 2): currentmodel = gr.Textbox(label="Current Model",lines=1,value="") submit_result = gr.Textbox(label="Message") mgallery, mgeninfo, mhtmlinfo, mhtmllog = create_output_panel("txt2img", opts.outdir_txt2img_samples) with gr.Row(visible = False) as row_inputers: inputer = gr.Textbox(label="",lines=1,value="") addtox = gr.Button(value="Add to Sequence X") addtoy = gr.Button(value="Add to Sequence Y") with gr.Row(visible = False) as row_blockids: blockids = gr.CheckboxGroup(label = "block IDs",choices=[x for x in blockid],type="value",interactive=True) with gr.Row(visible = False) as row_calcmode: calcmodes = gr.CheckboxGroup(label = "calcmode",choices=["normal", "cosineA", "cosineB", "smoothAdd","tensor"],type="value",interactive=True) with gr.Row(visible = False) as row_checkpoints: checkpoints = gr.CheckboxGroup(label = "checkpoint",choices=[x.model_name for x in sd_models.checkpoints_list.values()],type="value",interactive=True) with gr.Row(visible = False) as row_esets: esettings = gr.CheckboxGroup(label = "effective chekcer settings",choices=["save csv","save anime gif","not save grid","print change"],type="value",interactive=True) with gr.Tab("Weights Setting"): with gr.Row(): setalpha = gr.Button(elem_id="copytogen", value="set to alpha",variant='primary') readalpha = gr.Button(elem_id="copytogen", value="read from alpha",variant='primary') setbeta = gr.Button(elem_id="copytogen", value="set to beta",variant='primary') readbeta = gr.Button(elem_id="copytogen", value="read from beta",variant='primary') setx = gr.Button(elem_id="copytogen", value="set to X",variant='primary') with gr.Row(): weights_a = gr.Textbox(label="weights for alpha, base alpha,IN00,IN02,...IN11,M00,OUT00,...,OUT11",value = "0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5") weights_b = gr.Textbox(label="weights,for beta, base beta,IN00,IN02,...IN11,M00,OUT00,...,OUT11",value = "0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2") with gr.Row(): base= gr.Slider(label="Base", minimum=0, maximum=1, step =0.01, value=0.5) in00 = gr.Slider(label="IN00", minimum=0, maximum=1, step=0.01, value=0.5) in01 = gr.Slider(label="IN01", minimum=0, maximum=1, step=0.01, value=0.5) in02 = gr.Slider(label="IN02", minimum=0, maximum=1, step=0.01, value=0.5) in03 = gr.Slider(label="IN03", minimum=0, maximum=1, step=0.01, value=0.5) with gr.Row(): in04 = gr.Slider(label="IN04", minimum=0, maximum=1, step=0.01, value=0.5) in05 = gr.Slider(label="IN05", minimum=0, maximum=1, step=0.01, value=0.5) in06 = gr.Slider(label="IN06", minimum=0, maximum=1, step=0.01, value=0.5) in07 = gr.Slider(label="IN07", minimum=0, maximum=1, step=0.01, value=0.5) in08 = gr.Slider(label="IN08", minimum=0, maximum=1, step=0.01, value=0.5) in09 = gr.Slider(label="IN09", minimum=0, maximum=1, step=0.01, value=0.5) with gr.Row(): in10 = gr.Slider(label="IN10", minimum=0, maximum=1, step=0.01, value=0.5) in11 = gr.Slider(label="IN11", minimum=0, maximum=1, step=0.01, value=0.5) mi00 = gr.Slider(label="M00", minimum=0, maximum=1, step=0.01, value=0.5) ou00 = gr.Slider(label="OUT00", minimum=0, maximum=1, step=0.01, value=0.5) ou01 = gr.Slider(label="OUT01", minimum=0, maximum=1, step=0.01, value=0.5) ou02 = gr.Slider(label="OUT02", minimum=0, maximum=1, step=0.01, value=0.5) with gr.Row(): ou03 = gr.Slider(label="OUT03", minimum=0, maximum=1, step=0.01, value=0.5) ou04 = gr.Slider(label="OUT04", minimum=0, maximum=1, step=0.01, value=0.5) ou05 = gr.Slider(label="OUT05", minimum=0, maximum=1, step=0.01, value=0.5) ou06 = gr.Slider(label="OUT06", minimum=0, maximum=1, step=0.01, value=0.5) ou07 = gr.Slider(label="OUT07", minimum=0, maximum=1, step=0.01, value=0.5) ou08 = gr.Slider(label="OUT08", minimum=0, maximum=1, step=0.01, value=0.5) with gr.Row(): ou09 = gr.Slider(label="OUT09", minimum=0, maximum=1, step=0.01, value=0.5) ou10 = gr.Slider(label="OUT10", minimum=0, maximum=1, step=0.01, value=0.5) ou11 = gr.Slider(label="OUT11", minimum=0, maximum=1, step=0.01, value=0.5) with gr.Tab("Weights Presets"): with gr.Row(): s_reloadtext = gr.Button(value="Reload Presets",variant='primary') s_reloadtags = gr.Button(value="Reload Tags",variant='primary') s_savetext = gr.Button(value="Save Presets",variant='primary') s_openeditor = gr.Button(value="Open TextEditor",variant='primary') weightstags= gr.Textbox(label="available",lines = 2,value=tagdicter(weights_presets),visible =True,interactive =True) wpresets= gr.TextArea(label="",value=weights_presets,visible =True,interactive = True) with gr.Tab("Reservation"): with gr.Row(): s_reserve = gr.Button(value="Reserve XY Plot",variant='primary') s_reloadreserve = gr.Button(value="Reloat List",variant='primary') s_startreserve = gr.Button(value="Start XY plot",variant='primary') s_delreserve = gr.Button(value="Delete list(-1 for all)",variant='primary') s_delnum = gr.Number(value=1, label="Delete num : ", interactive=True, visible = True,precision =0) with gr.Row(): numaframe = gr.Dataframe( headers=["No.","status","xtype","xmenber", "ytype","ymenber","model A","model B","model C","alpha","beta","mode","use MBW","weights alpha","weights beta"], row_count=5,) # with gr.Tab("manual"): # with gr.Row(): # gr.HTML(value=" exampls: Change base alpha from 0.1 to 0.9
0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9
If you want to display the original model as well for comparison
0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1
For block-by-block merging
0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5
1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1