import gradio as gr from core.settings import ( MODEL_TYPE_MAP, MODEL_MAP_CHECKPOINT, FEATURES_CONFIG, TASK_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, prompt_enhancer_defaults from .config_loaders import ( get_cn_defaults, load_ipadapter_config ) def make_update_fn(m_comp, cat_comp, ar_comp, width_comp, height_comp, cn_types, cn_series, cn_filepaths, ipa_preset, lora_acc, cn_acc, ipa_acc, sd3_ipa_acc, flux1_ipa_acc, style_acc, embed_acc, cond_acc, ref_latent_acc, hidream_o1_ref_acc, prompt_comp, neg_prompt_comp, steps_comp, cfg_comp, sampler_comp, scheduler_comp, pid_acc=None, vae_acc=None, joyai_ref_acc=None, krea2_identity_edit_acc=None, krea2_reference_edit_acc=None, qwen_image_edit_acc=None, ref_img_acc=None, sensenova_ref_acc=None, task_type=None, type_comp=None, pe_acc=None, pe_model_acc=None, pe_prompt_acc=None, moss_image_control_acc=None, moss_image_edit_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, {}) arch_enabled_chains = arch_features.get('enabled_chains', []) # Determine current task type for task-level feature filtering current_task = task_type # static task_type for standalone prefixes if type_comp is not None: # unified imagegen: task type comes from extra arg type_arg_idx = 3 if ar_comp else 2 if len(args) > type_arg_idx: type_val = args[type_arg_idx] type_map = {'Txt2Img': 'txt2img', 'Img2Img': 'img2img', 'Inpaint': 'inpaint', 'Outpaint': 'outpaint', 'Hires. Fix': 'hires_fix'} current_task = type_map.get(type_val, 'txt2img') task_enabled_chains = TASK_FEATURES_CONFIG.get(current_task, {}).get('enabled_chains', []) if current_task else arch_enabled_chains enabled_chains = [c for c in arch_enabled_chains if c in task_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 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 sensenova_ref_acc: updates[sensenova_ref_acc] = gr.update(visible=('sensenova_reference' in enabled_chains)) if joyai_ref_acc: updates[joyai_ref_acc] = gr.update(visible=('joyai_image' in enabled_chains)) if krea2_identity_edit_acc: updates[krea2_identity_edit_acc] = gr.update(visible=('krea2_identity_edit' in enabled_chains)) if krea2_reference_edit_acc: updates[krea2_reference_edit_acc] = gr.update(visible=('krea2_style_reference' in enabled_chains)) if qwen_image_edit_acc: updates[qwen_image_edit_acc] = gr.update(visible=('qwen_image_edit' in enabled_chains)) if ref_img_acc: updates[ref_img_acc] = gr.update(visible=('reference_image' 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 pe_acc: updates[pe_acc] = gr.update(visible=('prompt_enhancer' in enabled_chains)) if pe_model_acc or pe_prompt_acc: pe_defaults = prompt_enhancer_defaults(arch_model_type) if pe_model_acc: updates[pe_model_acc] = gr.update(value=pe_defaults['model']) if pe_prompt_acc: updates[pe_prompt_acc] = gr.update(value=pe_defaults['system_prompt']) if moss_image_control_acc: updates[moss_image_control_acc] = gr.update(visible=('moss_image_control' in enabled_chains)) if moss_image_edit_acc: updates[moss_image_edit_acc] = gr.update(visible=('moss_image_edit' in enabled_chains)) 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 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, ar_comp, width_comp, height_comp, cn_types, cn_series, cn_filepaths, arch_comp_ref, ipa_preset, lora_acc, cn_acc, ipa_acc, sd3_ipa_acc, flux1_ipa_acc, style_acc, embed_acc, cond_acc, ref_latent_acc, hidream_o1_ref_acc, prompt_comp, neg_prompt_comp, steps_comp, cfg_comp, sampler_comp, scheduler_comp, pid_acc=None, vae_acc=None, joyai_ref_acc=None, krea2_identity_edit_acc=None, krea2_reference_edit_acc=None, qwen_image_edit_acc=None, ref_img_acc=None, sensenova_ref_acc=None, task_type=None, type_comp=None, pe_acc=None, pe_model_acc=None, pe_prompt_acc=None, moss_image_control_acc=None, moss_image_edit_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, {}) arch_enabled_chains = arch_features.get('enabled_chains', []) # Determine current task type for task-level feature filtering current_task = task_type # static task_type for standalone prefixes if type_comp is not None: # unified imagegen: task type comes from extra arg after model, arch, cat, ar type_arg_idx = 1 if arch_comp_ref: type_arg_idx += 1 if cat_comp_ref: type_arg_idx += 1 if ar_comp: type_arg_idx += 1 if len(args) > type_arg_idx: type_val = args[type_arg_idx] type_map = {'Txt2Img': 'txt2img', 'Img2Img': 'img2img', 'Inpaint': 'inpaint', 'Outpaint': 'outpaint', 'Hires. Fix': 'hires_fix'} current_task = type_map.get(type_val, 'txt2img') task_enabled_chains = TASK_FEATURES_CONFIG.get(current_task, {}).get('enabled_chains', []) if current_task else arch_enabled_chains enabled_chains = [c for c in arch_enabled_chains if c in task_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 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 sensenova_ref_acc: updates[sensenova_ref_acc] = gr.update(visible=('sensenova_reference' in enabled_chains)) if joyai_ref_acc: updates[joyai_ref_acc] = gr.update(visible=('joyai_image' in enabled_chains)) if krea2_identity_edit_acc: updates[krea2_identity_edit_acc] = gr.update(visible=('krea2_identity_edit' in enabled_chains)) if krea2_reference_edit_acc: updates[krea2_reference_edit_acc] = gr.update(visible=('krea2_style_reference' in enabled_chains)) if qwen_image_edit_acc: updates[qwen_image_edit_acc] = gr.update(visible=('qwen_image_edit' in enabled_chains)) if ref_img_acc: updates[ref_img_acc] = gr.update(visible=('reference_image' 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 pe_acc: updates[pe_acc] = gr.update(visible=('prompt_enhancer' in enabled_chains)) if pe_model_acc or pe_prompt_acc: pe_defaults = prompt_enhancer_defaults(arch_model_type) if pe_model_acc: updates[pe_model_acc] = gr.update(value=pe_defaults['model']) if pe_prompt_acc: updates[pe_prompt_acc] = gr.update(value=pe_defaults['system_prompt']) if moss_image_control_acc: updates[moss_image_control_acc] = gr.update(visible=('moss_image_control' in enabled_chains)) if moss_image_edit_acc: updates[moss_image_edit_acc] = gr.update(visible=('moss_image_edit' in enabled_chains)) 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 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) updates = {} for prefix in ["imagegen", "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 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 ["imagegen", "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