File size: 20,001 Bytes
e699279
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7298549
e699279
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46e2913
7298549
e699279
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7298549
e699279
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46e2913
7298549
e699279
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46e2913
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import gradio as gr
from core.settings import (
    MODEL_TYPE_MAP,
    MODEL_MAP_CHECKPOINT,
    FEATURES_CONFIG,
    ARCHITECTURES_CONFIG,
    MODEL_DEFAULTS_CONFIG,
    ARCH_CATEGORIES_MAP
)
from utils.app_utils import get_model_generation_defaults
from ui.shared.ui_components import RESOLUTION_MAP
from .config_loaders import (
    get_cn_defaults,
    get_anima_cn_defaults,
    get_diffsynth_cn_defaults,
    load_ipadapter_config
)

def make_update_fn(m_comp, cat_comp, cs_comp, ar_comp, width_comp, height_comp, cn_types, cn_series, cn_filepaths, anima_cn_types, anima_cn_series, anima_cn_filepaths, diffsynth_cn_types, diffsynth_cn_series, diffsynth_cn_filepaths, ipa_preset, lora_acc, cn_acc, anima_cn_acc, diffsynth_cn_acc, ipa_acc, sd3_ipa_acc, flux1_ipa_acc, style_acc, embed_acc, cond_acc, ref_latent_acc, hidream_o1_ref_acc, guidance_comp, prompt_comp, neg_prompt_comp, steps_comp, cfg_comp, sampler_comp, scheduler_comp, pid_acc=None, vae_acc=None):
    def update_fn(*args):
        arch = args[0]
        category = args[1]
        current_ar = args[2] if len(args) > 2 else None
        
        if arch == "ALL":
            valid_cats = list(set(cat for cats in ARCH_CATEGORIES_MAP.values() for cat in cats))
        else:
            valid_cats = ARCH_CATEGORIES_MAP.get(arch, [])
        
        cat_choices = ["ALL"] + sorted(valid_cats)
        new_category = category if category in cat_choices else "ALL"

        choices = []
        for name, info in MODEL_MAP_CHECKPOINT.items():
            m_arch = info[2]
            m_cat = info[4] if len(info) > 4 else None
            arch_match = (arch == "ALL" or m_arch == arch)
            cat_match = (new_category == "ALL" or m_cat == new_category)
            if arch_match and cat_match:
                choices.append(name)
        
        val = choices[0] if choices else None
        
        updates = {
            m_comp: gr.update(choices=choices, value=val),
            cat_comp: gr.update(choices=cat_choices, value=new_category)
        }

        m_type = MODEL_TYPE_MAP.get(val, "SDXL") if val else "SDXL"
        
        architectures_dict = ARCHITECTURES_CONFIG.get('architectures', {})
        arch_model_type = architectures_dict.get(m_type, {}).get("model_type", m_type.lower().replace(" ", "").replace(".", ""))

        arch_features = FEATURES_CONFIG.get(arch_model_type, FEATURES_CONFIG.get('default', {}))
        enabled_chains = arch_features.get('enabled_chains', [])

        if lora_acc: updates[lora_acc] = gr.update(visible=('lora' in enabled_chains))
        if cn_acc: updates[cn_acc] = gr.update(visible=('controlnet' in enabled_chains))
        if anima_cn_acc: updates[anima_cn_acc] = gr.update(visible=('anima_controlnet_lllite' in enabled_chains))
        if diffsynth_cn_acc: updates[diffsynth_cn_acc] = gr.update(visible=('controlnet_model_patch' in enabled_chains))
        if ipa_acc: updates[ipa_acc] = gr.update(visible=('ipadapter' in enabled_chains))
        if flux1_ipa_acc: updates[flux1_ipa_acc] = gr.update(visible=('flux1_ipadapter' in enabled_chains))
        if sd3_ipa_acc: updates[sd3_ipa_acc] = gr.update(visible=('sd3_ipadapter' in enabled_chains))
        if style_acc: updates[style_acc] = gr.update(visible=('style' in enabled_chains))
        if embed_acc: updates[embed_acc] = gr.update(visible=('embedding' in enabled_chains))
        if cond_acc: updates[cond_acc] = gr.update(visible=('conditioning' in enabled_chains))
        if ref_latent_acc: updates[ref_latent_acc] = gr.update(visible=('reference_latent' in enabled_chains))
        if hidream_o1_ref_acc: updates[hidream_o1_ref_acc] = gr.update(visible=('hidream_o1_reference' in enabled_chains))
        if pid_acc: updates[pid_acc] = gr.update(visible=('pid' in enabled_chains))
        if vae_acc: updates[vae_acc] = gr.update(visible=('vae' in enabled_chains))

        if cs_comp:
            updates[cs_comp] = gr.update(visible=(arch_model_type == "sd15"))
        if guidance_comp:
            updates[guidance_comp] = gr.update(visible=(arch_model_type == "flux1"))
        
        if ar_comp:
            res_key = arch_model_type
            if res_key not in RESOLUTION_MAP:
                res_key = 'sdxl'
            res_map = RESOLUTION_MAP.get(res_key, {})
            target_ar = current_ar if current_ar in res_map else (list(res_map.keys())[0] if res_map else "1:1 (Square)")
            updates[ar_comp] = gr.update(choices=list(res_map.keys()), value=target_ar)
            if width_comp and height_comp and target_ar in res_map:
                updates[width_comp] = gr.update(value=res_map[target_ar][0])
                updates[height_comp] = gr.update(value=res_map[target_ar][1])
        
        controlnet_key = architectures_dict.get(m_type, {}).get("controlnet_key", m_type)
        
        all_types, default_type, series_choices, default_series, filepath = get_cn_defaults(controlnet_key)
        for t_comp in cn_types:
            updates[t_comp] = gr.update(choices=all_types, value=default_type)
        for s_comp in cn_series:
            updates[s_comp] = gr.update(choices=series_choices, value=default_series)
        for f_comp in cn_filepaths:
            updates[f_comp] = filepath

        anima_all_types, anima_default_type, anima_series_choices, anima_default_series, anima_filepath = get_anima_cn_defaults()
        for t_comp in anima_cn_types:
            updates[t_comp] = gr.update(choices=anima_all_types, value=anima_default_type)
        for s_comp in anima_cn_series:
            updates[s_comp] = gr.update(choices=anima_series_choices, value=anima_default_series)
        for f_comp in anima_cn_filepaths:
            updates[f_comp] = anima_filepath
            
        diffsynth_all_types, diffsynth_default_type, diffsynth_series_choices, diffsynth_default_series, diffsynth_filepath = get_diffsynth_cn_defaults(controlnet_key)
        for t_comp in diffsynth_cn_types:
            updates[t_comp] = gr.update(choices=diffsynth_all_types, value=diffsynth_default_type)
        for s_comp in diffsynth_cn_series:
            updates[s_comp] = gr.update(choices=diffsynth_series_choices, value=diffsynth_default_series)
        for f_comp in diffsynth_cn_filepaths:
            updates[f_comp] = diffsynth_filepath
        
        if ipa_preset and (arch_model_type in ["sdxl", "sd15", "sd35"]):
            config = load_ipadapter_config()
            ipa_arch_key = "SDXL" if arch_model_type in ["sdxl", "sd35"] else "SD1.5"
            std_presets = config.get("IPAdapter_presets", {}).get(ipa_arch_key, [])
            face_presets = config.get("IPAdapter_FaceID_presets", {}).get(ipa_arch_key, [])
            all_ipa_presets = std_presets + face_presets
            default_ipa = all_ipa_presets[0] if all_ipa_presets else None
            updates[ipa_preset] = gr.update(choices=all_ipa_presets, value=default_ipa)
        
        defaults = get_model_generation_defaults(val, arch_model_type, MODEL_DEFAULTS_CONFIG)
        if steps_comp: updates[steps_comp] = gr.update(value=defaults.get('steps'))
        if cfg_comp: updates[cfg_comp] = gr.update(value=defaults.get('cfg'))
        if sampler_comp: updates[sampler_comp] = gr.update(value=defaults.get('sampler_name'))
        if scheduler_comp: updates[scheduler_comp] = gr.update(value=defaults.get('scheduler'))
        if prompt_comp: updates[prompt_comp] = gr.update(value=defaults.get('positive_prompt'))
        if neg_prompt_comp: updates[neg_prompt_comp] = gr.update(value=defaults.get('negative_prompt'))
            
        return updates
    return update_fn


def make_model_change_fn(cat_comp_ref, cs_comp, ar_comp, width_comp, height_comp, cn_types, cn_series, cn_filepaths, anima_cn_types, anima_cn_series, anima_cn_filepaths, diffsynth_cn_types, diffsynth_cn_series, diffsynth_cn_filepaths, arch_comp_ref, ipa_preset, lora_acc, cn_acc, anima_cn_acc, diffsynth_cn_acc, ipa_acc, sd3_ipa_acc, flux1_ipa_acc, style_acc, embed_acc, cond_acc, ref_latent_acc, hidream_o1_ref_acc, guidance_comp, prompt_comp, neg_prompt_comp, steps_comp, cfg_comp, sampler_comp, scheduler_comp, pid_acc=None, vae_acc=None):
    def change_fn(*args):
        model_name = args[0]
        idx = 1
        current_arch = args[idx] if arch_comp_ref and idx < len(args) else None
        if arch_comp_ref: idx += 1
        current_cat = args[idx] if cat_comp_ref and idx < len(args) else None
        if cat_comp_ref: idx += 1
        current_ar = args[idx] if idx < len(args) else None
        
        m_type = MODEL_TYPE_MAP.get(model_name, "SDXL")
        
        m_info = MODEL_MAP_CHECKPOINT.get(model_name)
        m_cat = m_info[4] if m_info and len(m_info) > 4 else None
        if not m_cat: m_cat = "ALL"
        
        updates = {}
        target_arch = m_type
        if arch_comp_ref:
            if current_arch == "ALL":
                updates[arch_comp_ref] = gr.update()
                target_arch = "ALL"
            else:
                updates[arch_comp_ref] = m_type
                
        if cat_comp_ref:
            if target_arch == "ALL":
                valid_cats = list(set(cat for cats in ARCH_CATEGORIES_MAP.values() for cat in cats))
            else:
                valid_cats = ARCH_CATEGORIES_MAP.get(target_arch, [])
            cat_choices = ["ALL"] + sorted(valid_cats)
            
            if current_cat == "ALL":
                updates[cat_comp_ref] = gr.update(choices=cat_choices)
            else:
                updates[cat_comp_ref] = gr.update(choices=cat_choices, value=m_cat)
        
        architectures_dict = ARCHITECTURES_CONFIG.get('architectures', {})
        arch_model_type = architectures_dict.get(m_type, {}).get("model_type", m_type.lower().replace(" ", "").replace(".", ""))

        arch_features = FEATURES_CONFIG.get(arch_model_type, FEATURES_CONFIG.get('default', {}))
        enabled_chains = arch_features.get('enabled_chains', [])

        if lora_acc: updates[lora_acc] = gr.update(visible=('lora' in enabled_chains))
        if cn_acc: updates[cn_acc] = gr.update(visible=('controlnet' in enabled_chains))
        if anima_cn_acc: updates[anima_cn_acc] = gr.update(visible=('anima_controlnet_lllite' in enabled_chains))
        if diffsynth_cn_acc: updates[diffsynth_cn_acc] = gr.update(visible=('controlnet_model_patch' in enabled_chains))
        if ipa_acc: updates[ipa_acc] = gr.update(visible=('ipadapter' in enabled_chains))
        if flux1_ipa_acc: updates[flux1_ipa_acc] = gr.update(visible=('flux1_ipadapter' in enabled_chains))
        if sd3_ipa_acc: updates[sd3_ipa_acc] = gr.update(visible=('sd3_ipadapter' in enabled_chains))
        if style_acc: updates[style_acc] = gr.update(visible=('style' in enabled_chains))
        if embed_acc: updates[embed_acc] = gr.update(visible=('embedding' in enabled_chains))
        if cond_acc: updates[cond_acc] = gr.update(visible=('conditioning' in enabled_chains))
        if ref_latent_acc: updates[ref_latent_acc] = gr.update(visible=('reference_latent' in enabled_chains))
        if hidream_o1_ref_acc: updates[hidream_o1_ref_acc] = gr.update(visible=('hidream_o1_reference' in enabled_chains))
        if pid_acc: updates[pid_acc] = gr.update(visible=('pid' in enabled_chains))
        if vae_acc: updates[vae_acc] = gr.update(visible=('vae' in enabled_chains))

        if cs_comp:
            updates[cs_comp] = gr.update(visible=(arch_model_type == "sd15"))
        if guidance_comp:
            updates[guidance_comp] = gr.update(visible=(arch_model_type == "flux1"))
        
        if ar_comp:
            res_key = arch_model_type
            if res_key not in RESOLUTION_MAP:
                res_key = 'sdxl'
            res_map = RESOLUTION_MAP.get(res_key, {})
            target_ar = current_ar if current_ar in res_map else (list(res_map.keys())[0] if res_map else "1:1 (Square)")
            updates[ar_comp] = gr.update(choices=list(res_map.keys()), value=target_ar)
            if width_comp and height_comp and target_ar in res_map:
                updates[width_comp] = gr.update(value=res_map[target_ar][0])
                updates[height_comp] = gr.update(value=res_map[target_ar][1])
        
        controlnet_key = architectures_dict.get(m_type, {}).get("controlnet_key", m_type)
        
        all_types, default_type, series_choices, default_series, filepath = get_cn_defaults(controlnet_key)
        for t_comp in cn_types:
            updates[t_comp] = gr.update(choices=all_types, value=default_type)
        for s_comp in cn_series:
            updates[s_comp] = gr.update(choices=series_choices, value=default_series)
        for f_comp in cn_filepaths:
            updates[f_comp] = filepath

        anima_all_types, anima_default_type, anima_series_choices, anima_default_series, anima_filepath = get_anima_cn_defaults()
        for t_comp in anima_cn_types:
            updates[t_comp] = gr.update(choices=anima_all_types, value=anima_default_type)
        for s_comp in anima_cn_series:
            updates[s_comp] = gr.update(choices=anima_series_choices, value=anima_default_series)
        for f_comp in anima_cn_filepaths:
            updates[f_comp] = anima_filepath
            
        diffsynth_all_types, diffsynth_default_type, diffsynth_series_choices, diffsynth_default_series, diffsynth_filepath = get_diffsynth_cn_defaults(controlnet_key)
        for t_comp in diffsynth_cn_types:
            updates[t_comp] = gr.update(choices=diffsynth_all_types, value=diffsynth_default_type)
        for s_comp in diffsynth_cn_series:
            updates[s_comp] = gr.update(choices=diffsynth_series_choices, value=diffsynth_default_series)
        for f_comp in diffsynth_cn_filepaths:
            updates[f_comp] = diffsynth_filepath
        
        if ipa_preset and (arch_model_type in ["sdxl", "sd15", "sd35"]):
            config = load_ipadapter_config()
            ipa_arch_key = "SDXL" if arch_model_type in ["sdxl", "sd35"] else "SD1.5"
            std_presets = config.get("IPAdapter_presets", {}).get(ipa_arch_key, [])
            face_presets = config.get("IPAdapter_FaceID_presets", {}).get(ipa_arch_key, [])
            all_ipa_presets = std_presets + face_presets
            default_ipa = all_ipa_presets[0] if all_ipa_presets else None
            updates[ipa_preset] = gr.update(choices=all_ipa_presets, value=default_ipa)
        
        defaults = get_model_generation_defaults(model_name, arch_model_type, MODEL_DEFAULTS_CONFIG)
        if steps_comp: updates[steps_comp] = gr.update(value=defaults.get('steps'))
        if cfg_comp: updates[cfg_comp] = gr.update(value=defaults.get('cfg'))
        if sampler_comp: updates[sampler_comp] = gr.update(value=defaults.get('sampler_name'))
        if scheduler_comp: updates[scheduler_comp] = gr.update(value=defaults.get('scheduler'))
        if prompt_comp: updates[prompt_comp] = gr.update(value=defaults.get('positive_prompt'))
        if neg_prompt_comp: updates[neg_prompt_comp] = gr.update(value=defaults.get('negative_prompt'))
            
        return updates
    return change_fn


def initialize_all_cn_dropdowns(ui_components):
    default_model_name = list(MODEL_MAP_CHECKPOINT.keys())[0] if MODEL_MAP_CHECKPOINT else None
    default_m_type = MODEL_TYPE_MAP.get(default_model_name, "SDXL") if default_model_name else "SDXL"
    architectures_dict = ARCHITECTURES_CONFIG.get('architectures', {})
    controlnet_key = architectures_dict.get(default_m_type, {}).get("controlnet_key", default_m_type)
    
    all_types, default_type, series_choices, default_series, filepath = get_cn_defaults(controlnet_key)
    anima_all_types, anima_default_type, anima_series_choices, anima_default_series, anima_filepath = get_anima_cn_defaults()
    diffsynth_all_types, diffsynth_default_type, diffsynth_series_choices, diffsynth_default_series, diffsynth_filepath = get_diffsynth_cn_defaults(controlnet_key)
    
    updates = {}
    for prefix in ["txt2img", "img2img", "inpaint", "outpaint", "hires_fix"]:
        if f'controlnet_types_{prefix}' in ui_components:
            for type_dd in ui_components[f'controlnet_types_{prefix}']:
                updates[type_dd] = gr.update(choices=all_types, value=default_type)
            for series_dd in ui_components[f'controlnet_series_{prefix}']:
                updates[series_dd] = gr.update(choices=series_choices, value=default_series)
            for filepath_state in ui_components[f'controlnet_filepaths_{prefix}']:
                updates[filepath_state] = filepath

        if f'anima_controlnet_lllite_types_{prefix}' in ui_components:
            for type_dd in ui_components[f'anima_controlnet_lllite_types_{prefix}']:
                updates[type_dd] = gr.update(choices=anima_all_types, value=anima_default_type)
            for series_dd in ui_components[f'anima_controlnet_lllite_series_{prefix}']:
                updates[series_dd] = gr.update(choices=anima_series_choices, value=anima_default_series)
            for filepath_state in ui_components[f'anima_controlnet_lllite_filepaths_{prefix}']:
                updates[filepath_state] = anima_filepath
        
        if f'diffsynth_controlnet_types_{prefix}' in ui_components:
            for type_dd in ui_components[f'diffsynth_controlnet_types_{prefix}']:
                updates[type_dd] = gr.update(choices=diffsynth_all_types, value=diffsynth_default_type)
            for series_dd in ui_components[f'diffsynth_controlnet_series_{prefix}']:
                updates[series_dd] = gr.update(choices=diffsynth_series_choices, value=default_series)
            for filepath_state in ui_components[f'diffsynth_controlnet_filepaths_{prefix}']:
                updates[filepath_state] = diffsynth_filepath
                
    return updates


def initialize_all_ipa_dropdowns(ui_components):
    config = load_ipadapter_config()
    if not config: return {}
    
    default_model_name = list(MODEL_MAP_CHECKPOINT.keys())[0] if MODEL_MAP_CHECKPOINT else None
    default_m_type = MODEL_TYPE_MAP.get(default_model_name, "SDXL") if default_model_name else "SDXL"
    architectures_dict = ARCHITECTURES_CONFIG.get('architectures', {})
    arch_model_type = architectures_dict.get(default_m_type, {}).get("model_type", default_m_type.lower().replace(" ", "").replace(".", ""))
    ipa_arch_key = "SDXL" if arch_model_type in ["sdxl", "sd35"] else "SD1.5"

    unified_presets = config.get("IPAdapter_presets", {}).get(ipa_arch_key, [])
    faceid_presets = config.get("IPAdapter_FaceID_presets", {}).get(ipa_arch_key, [])
    
    all_presets = unified_presets + faceid_presets
    default_preset = all_presets[0] if all_presets else None
    is_faceid_default = default_preset in faceid_presets

    lora_strength_update = gr.update(visible=is_faceid_default)
    
    updates = {}
    for prefix in ["txt2img", "img2img", "inpaint", "outpaint", "hires_fix"]:
        if f'ipadapter_final_preset_{prefix}' in ui_components:
            for lora_strength_slider in ui_components[f'ipadapter_lora_strengths_{prefix}']:
                updates[lora_strength_slider] = lora_strength_update
            updates[ui_components[f'ipadapter_final_preset_{prefix}']] = gr.update(choices=all_presets, value=default_preset)
            updates[ui_components[f'ipadapter_final_lora_strength_{prefix}']] = lora_strength_update
    return updates


def run_on_load(ui_components):
    cn_updates = initialize_all_cn_dropdowns(ui_components)
    ipa_updates = initialize_all_ipa_dropdowns(ui_components)
    return {**cn_updates, **ipa_updates}


def on_aspect_ratio_change(ratio_key, model_display_name):
    m_type = MODEL_TYPE_MAP.get(model_display_name, 'SDXL')
    architectures_dict = ARCHITECTURES_CONFIG.get('architectures', {})
    arch_model_type = architectures_dict.get(m_type, {}).get("model_type", m_type.lower().replace(" ", "").replace(".", ""))
    
    res_map = RESOLUTION_MAP.get(arch_model_type, RESOLUTION_MAP.get("sdxl", {}))
    w, h = res_map.get(ratio_key, (1024, 1024))
    return w, h