2022-09-03 12:08:45 +03:00
2022-10-22 14:07:00 +03:00
import gradio as gr
2022-09-03 12:08:45 +03:00
2023-08-01 07:43:43 +03:00
from modules import sd_models , sd_vae , errors , extras , call_queue
from modules . ui_components import FormRow
2023-05-31 22:40:09 +03:00
from modules . ui_common import create_refresh_button
2022-10-16 22:06:21 +03:00
2023-08-01 07:43:43 +03:00
def update_interp_description ( value ) :
interp_description_css = " <p style= ' margin-bottom: 2.5em ' > {} </p> "
interp_descriptions = {
" No interpolation " : interp_description_css . format ( " No interpolation will be used. Requires one model; A. Allows for format conversion and VAE baking. " ) ,
" Weighted sum " : interp_description_css . format ( " A weighted sum will be used for interpolation. Requires two models; A and B. The result is calculated as A * (1 - M) + B * M " ) ,
" Add difference " : interp_description_css . format ( " The difference between the last two models will be added to the first. Requires three models; A, B and C. The result is calculated as A + (B - C) * M " )
}
return interp_descriptions [ value ]
2022-10-14 11:56:41 +03:00
2022-09-03 12:08:45 +03:00
2023-08-01 07:43:43 +03:00
def modelmerger ( * args ) :
2022-10-04 14:35:12 +03:00
try :
2023-08-01 07:43:43 +03:00
results = extras . run_modelmerger ( * args )
except Exception as e :
errors . report ( " Error loading/saving model file " , exc_info = True )
sd_models . list_models ( ) # to remove the potentially missing models from the list
return [ * [ gr . Dropdown . update ( choices = sd_models . checkpoint_tiles ( ) ) for _ in range ( 4 ) ] , f " Error merging checkpoints: { e } " ]
return results
2023-03-28 22:23:40 +03:00
2023-08-01 07:43:43 +03:00
class UiCheckpointMerger :
def __init__ ( self ) :
with gr . Blocks ( analytics_enabled = False ) as modelmerger_interface :
2023-08-04 07:50:17 +03:00
with gr . Row ( equal_height = False ) :
2023-08-01 07:43:43 +03:00
with gr . Column ( variant = ' compact ' ) :
self . interp_description = gr . HTML ( value = update_interp_description ( " Weighted sum " ) , elem_id = " modelmerger_interp_description " )
2023-03-28 22:23:40 +03:00
2023-08-01 07:43:43 +03:00
with FormRow ( elem_id = " modelmerger_models " ) :
self . primary_model_name = gr . Dropdown ( sd_models . checkpoint_tiles ( ) , elem_id = " modelmerger_primary_model_name " , label = " Primary model (A) " )
create_refresh_button ( self . primary_model_name , sd_models . list_models , lambda : { " choices " : sd_models . checkpoint_tiles ( ) } , " refresh_checkpoint_A " )
2023-03-28 22:23:40 +03:00
2023-08-01 07:43:43 +03:00
self . secondary_model_name = gr . Dropdown ( sd_models . checkpoint_tiles ( ) , elem_id = " modelmerger_secondary_model_name " , label = " Secondary model (B) " )
create_refresh_button ( self . secondary_model_name , sd_models . list_models , lambda : { " choices " : sd_models . checkpoint_tiles ( ) } , " refresh_checkpoint_B " )
2023-04-29 19:39:22 +03:00
2023-08-01 07:43:43 +03:00
self . tertiary_model_name = gr . Dropdown ( sd_models . checkpoint_tiles ( ) , elem_id = " modelmerger_tertiary_model_name " , label = " Tertiary model (C) " )
create_refresh_button ( self . tertiary_model_name , sd_models . list_models , lambda : { " choices " : sd_models . checkpoint_tiles ( ) } , " refresh_checkpoint_C " )
2023-04-29 19:39:22 +03:00
2023-08-01 07:43:43 +03:00
self . custom_name = gr . Textbox ( label = " Custom Name (Optional) " , elem_id = " modelmerger_custom_name " )
self . interp_amount = gr . Slider ( minimum = 0.0 , maximum = 1.0 , step = 0.05 , label = ' Multiplier (M) - set to 0 to get model A ' , value = 0.3 , elem_id = " modelmerger_interp_amount " )
self . interp_method = gr . Radio ( choices = [ " No interpolation " , " Weighted sum " , " Add difference " ] , value = " Weighted sum " , label = " Interpolation Method " , elem_id = " modelmerger_interp_method " )
self . interp_method . change ( fn = update_interp_description , inputs = [ self . interp_method ] , outputs = [ self . interp_description ] )
2023-01-01 16:51:12 +03:00
2023-01-04 20:10:40 +03:00
with FormRow ( ) :
2023-08-01 07:43:43 +03:00
self . checkpoint_format = gr . Radio ( choices = [ " ckpt " , " safetensors " ] , value = " safetensors " , label = " Checkpoint format " , elem_id = " modelmerger_checkpoint_format " )
self . save_as_half = gr . Checkbox ( value = False , label = " Save as float16 " , elem_id = " modelmerger_save_as_half " )
2023-01-09 23:35:40 +03:00
with FormRow ( ) :
2023-08-01 07:43:43 +03:00
with gr . Column ( ) :
self . config_source = gr . Radio ( choices = [ " A, B or C " , " B " , " C " , " Don ' t " ] , value = " A, B or C " , label = " Copy config from " , type = " index " , elem_id = " modelmerger_config_method " )
2023-01-09 23:35:40 +03:00
2023-08-01 07:43:43 +03:00
with gr . Column ( ) :
with FormRow ( ) :
self . bake_in_vae = gr . Dropdown ( choices = [ " None " ] + list ( sd_vae . vae_dict ) , value = " None " , label = " Bake in VAE " , elem_id = " modelmerger_bake_in_vae " )
create_refresh_button ( self . bake_in_vae , sd_vae . refresh_vae_list , lambda : { " choices " : [ " None " ] + list ( sd_vae . vae_dict ) } , " modelmerger_refresh_bake_in_vae " )
2023-01-04 20:10:40 +03:00
with FormRow ( ) :
2023-08-01 07:43:43 +03:00
self . discard_weights = gr . Textbox ( value = " " , label = " Discard weights with matching name " , elem_id = " modelmerger_discard_weights " )
2022-10-02 15:03:39 +03:00
2023-08-01 08:27:54 +03:00
with gr . Accordion ( " Metadata " , open = False ) as metadata_editor :
with FormRow ( ) :
self . save_metadata = gr . Checkbox ( value = True , label = " Save metadata " , elem_id = " modelmerger_save_metadata " )
self . add_merge_recipe = gr . Checkbox ( value = True , label = " Add merge recipe metadata " , elem_id = " modelmerger_add_recipe " )
self . copy_metadata_fields = gr . Checkbox ( value = True , label = " Copy metadata from merged models " , elem_id = " modelmerger_copy_metadata " )
self . metadata_json = gr . TextArea ( ' {} ' , label = " Metadata in JSON format " )
self . read_metadata = gr . Button ( " Read metadata from selected checkpoints " )
with FormRow ( ) :
2023-08-01 07:43:43 +03:00
self . modelmerger_merge = gr . Button ( elem_id = " modelmerger_merge " , value = " Merge " , variant = ' primary ' )
2023-05-10 23:41:08 +03:00
2023-08-01 07:43:43 +03:00
with gr . Column ( variant = ' compact ' , elem_id = " modelmerger_results_container " ) :
with gr . Group ( elem_id = " modelmerger_results_panel " ) :
self . modelmerger_result = gr . HTML ( elem_id = " modelmerger_result " , show_label = False )
2022-09-10 11:10:00 +03:00
2023-08-01 08:27:54 +03:00
self . metadata_editor = metadata_editor
2023-08-01 07:43:43 +03:00
self . blocks = modelmerger_interface
2023-01-03 20:23:17 +03:00
2023-08-01 07:43:43 +03:00
def setup_ui ( self , dummy_component , sd_model_checkpoint_component ) :
2023-08-01 08:27:54 +03:00
self . checkpoint_format . change ( lambda fmt : gr . update ( visible = fmt == ' safetensors ' ) , inputs = [ self . checkpoint_format ] , outputs = [ self . metadata_editor ] , show_progress = False )
self . read_metadata . click ( extras . read_metadata , inputs = [ self . primary_model_name , self . secondary_model_name , self . tertiary_model_name ] , outputs = [ self . metadata_json ] )
2023-08-01 07:43:43 +03:00
self . modelmerger_merge . click ( fn = lambda : ' ' , inputs = [ ] , outputs = [ self . modelmerger_result ] )
self . modelmerger_merge . click (
fn = call_queue . wrap_gradio_gpu_call ( modelmerger , extra_outputs = lambda : [ gr . update ( ) for _ in range ( 4 ) ] ) ,
2023-01-19 09:25:37 +03:00
_js = ' modelmerger ' ,
2022-09-29 00:59:44 +03:00
inputs = [
2023-01-19 09:25:37 +03:00
dummy_component ,
2023-08-01 07:43:43 +03:00
self . primary_model_name ,
self . secondary_model_name ,
self . tertiary_model_name ,
self . interp_method ,
self . interp_amount ,
self . save_as_half ,
self . custom_name ,
self . checkpoint_format ,
self . config_source ,
self . bake_in_vae ,
self . discard_weights ,
self . save_metadata ,
2023-08-01 08:27:54 +03:00
self . add_merge_recipe ,
self . copy_metadata_fields ,
self . metadata_json ,
2022-09-29 00:59:44 +03:00
] ,
outputs = [
2023-08-01 07:43:43 +03:00
self . primary_model_name ,
self . secondary_model_name ,
self . tertiary_model_name ,
sd_model_checkpoint_component ,
self . modelmerger_result ,
2022-09-29 00:59:44 +03:00
]
)
2022-09-23 22:49:21 +03:00
2023-08-01 07:43:43 +03:00
# Required as a workaround for change() event not triggering when loading values from ui-config.json
self . interp_description . value = update_interp_description ( self . interp_method . value )
2023-06-03 13:55:35 +03:00