Spaces:
Running on Zero
Running on Zero
File size: 6,186 Bytes
7139ce5 79e946f 7139ce5 79e946f 7139ce5 79e946f 7139ce5 79e946f 7139ce5 79e946f | 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 | import gradio as gr
from core.generation_logic import run_cn_preprocessor_entry
from core.pipelines.controlnet_preprocessor import CPU_ONLY_PREPROCESSORS
from utils.app_utils import PREPROCESSOR_MODEL_MAP, PREPROCESSOR_PARAMETER_MAP
from ui.layout import MAX_DYNAMIC_CONTROLS
def attach_event_handlers(ui_components, demo):
def update_cn_input_visibility(choice):
return {
ui_components["cn_image_input"]: gr.update(visible=choice == "Image"),
ui_components["cn_video_input"]: gr.update(visible=choice == "Video")
}
ui_components["cn_input_type"].change(
fn=update_cn_input_visibility,
inputs=[ui_components["cn_input_type"]],
outputs=[ui_components["cn_image_input"], ui_components["cn_video_input"]]
)
def update_preprocessor_models_dropdown(preprocessor_name):
models = PREPROCESSOR_MODEL_MAP.get(preprocessor_name)
if models:
model_filenames = [m[1] for m in models]
return gr.update(choices=model_filenames, value=model_filenames[0], visible=True)
else:
return gr.update(choices=[], value=None, visible=False)
def update_preprocessor_settings_ui(preprocessor_name):
params = PREPROCESSOR_PARAMETER_MAP.get(preprocessor_name, [])
slider_updates, dropdown_updates, checkbox_updates = [], [], []
s_idx, d_idx, c_idx = 0, 0, 0
for param in params:
if s_idx + d_idx + c_idx >= MAX_DYNAMIC_CONTROLS: break
name = param["name"]
ptype = param["type"]
config = param["config"]
label = name.replace('_', ' ').title()
if ptype == "INT" or ptype == "FLOAT":
if s_idx < MAX_DYNAMIC_CONTROLS:
slider_updates.append(gr.update(
label=label,
minimum=config.get('min', 0),
maximum=config.get('max', 255),
step=config.get('step', 0.1 if ptype == "FLOAT" else 1),
value=config.get('default', 0),
visible=True
))
s_idx += 1
elif isinstance(ptype, list):
if d_idx < MAX_DYNAMIC_CONTROLS:
dropdown_updates.append(gr.update(
label=label,
choices=ptype,
value=config.get('default', ptype[0] if ptype else None),
visible=True
))
d_idx += 1
elif ptype == "BOOLEAN":
if c_idx < MAX_DYNAMIC_CONTROLS:
checkbox_updates.append(gr.update(
label=label,
value=config.get('default', False),
visible=True
))
c_idx += 1
for _ in range(s_idx, MAX_DYNAMIC_CONTROLS): slider_updates.append(gr.update(visible=False))
for _ in range(d_idx, MAX_DYNAMIC_CONTROLS): dropdown_updates.append(gr.update(visible=False))
for _ in range(c_idx, MAX_DYNAMIC_CONTROLS): checkbox_updates.append(gr.update(visible=False))
return slider_updates + dropdown_updates + checkbox_updates
def update_run_button_for_cpu(preprocessor_name):
if preprocessor_name in CPU_ONLY_PREPROCESSORS:
return gr.update(value="Run Preprocessor CPU Only", variant="primary"), gr.update(visible=False)
else:
return gr.update(value="Run Preprocessor", variant="primary"), gr.update(visible=True)
ui_components["preprocessor_cn"].change(
fn=update_preprocessor_models_dropdown,
inputs=[ui_components["preprocessor_cn"]],
outputs=[ui_components["preprocessor_model_cn"]]
).then(
fn=update_preprocessor_settings_ui,
inputs=[ui_components["preprocessor_cn"]],
outputs=ui_components["cn_sliders"] + ui_components["cn_dropdowns"] + ui_components["cn_checkboxes"]
).then(
fn=update_run_button_for_cpu,
inputs=[ui_components["preprocessor_cn"]],
outputs=[ui_components["run_cn"], ui_components["zero_gpu_cn"]]
)
all_dynamic_inputs = (
ui_components["cn_sliders"] +
ui_components["cn_dropdowns"] +
ui_components["cn_checkboxes"]
)
ui_components["run_cn"].click(
fn=run_cn_preprocessor_entry,
inputs=[
ui_components["cn_input_type"],
ui_components["cn_image_input"],
ui_components["cn_video_input"],
ui_components["preprocessor_cn"],
ui_components["preprocessor_model_cn"],
ui_components["zero_gpu_cn"],
] + all_dynamic_inputs,
outputs=[ui_components["output_gallery_cn"]]
)
def run_on_load():
all_updates = {}
default_preprocessor = "Canny Edge"
model_update = update_preprocessor_models_dropdown(default_preprocessor)
all_updates[ui_components["preprocessor_model_cn"]] = model_update
settings_outputs = update_preprocessor_settings_ui(default_preprocessor)
dynamic_outputs = ui_components["cn_sliders"] + ui_components["cn_dropdowns"] + ui_components["cn_checkboxes"]
for i, comp in enumerate(dynamic_outputs):
all_updates[comp] = settings_outputs[i]
run_button_update, zero_gpu_update = update_run_button_for_cpu(default_preprocessor)
all_updates[ui_components["run_cn"]] = run_button_update
all_updates[ui_components["zero_gpu_cn"]] = zero_gpu_update
return all_updates
all_load_outputs = [
ui_components["preprocessor_model_cn"],
*ui_components["cn_sliders"],
*ui_components["cn_dropdowns"],
*ui_components["cn_checkboxes"],
ui_components["run_cn"],
ui_components["zero_gpu_cn"]
]
demo.load(
fn=run_on_load,
outputs=all_load_outputs
) |