Spaces:
Running
on
Zero
Running
on
Zero
| import gradio as gr | |
| import yaml | |
| import os | |
| import shutil | |
| from functools import lru_cache | |
| from core.settings import * | |
| from utils.app_utils import * | |
| from core.generation_logic import * | |
| from comfy_integration.nodes import SAMPLER_CHOICES, SCHEDULER_CHOICES | |
| from utils.app_utils import save_uploaded_file_with_hash | |
| from ui.shared.ui_components import RESOLUTION_MAP, MAX_EMBEDDINGS, MAX_CONDITIONINGS, MAX_LORAS | |
| def attach_event_handlers(ui_components, demo): | |
| def create_lora_event_handlers(prefix): | |
| lora_rows = ui_components[f'lora_rows_{prefix}'] | |
| lora_ids = ui_components[f'lora_ids_{prefix}'] | |
| lora_scales = ui_components[f'lora_scales_{prefix}'] | |
| lora_uploads = ui_components[f'lora_uploads_{prefix}'] | |
| count_state = ui_components[f'lora_count_state_{prefix}'] | |
| add_button = ui_components[f'add_lora_button_{prefix}'] | |
| del_button = ui_components[f'delete_lora_button_{prefix}'] | |
| def add_lora_row(c): | |
| updates = {} | |
| if c < MAX_LORAS: | |
| c += 1 | |
| updates[lora_rows[c - 1]] = gr.update(visible=True) | |
| updates[count_state] = c | |
| updates[add_button] = gr.update(visible=c < MAX_LORAS) | |
| updates[del_button] = gr.update(visible=c > 1) | |
| return updates | |
| def del_lora_row(c): | |
| updates = {} | |
| if c > 1: | |
| updates[lora_rows[c - 1]] = gr.update(visible=False) | |
| updates[lora_ids[c - 1]] = "" | |
| updates[lora_scales[c - 1]] = 0.0 | |
| updates[lora_uploads[c - 1]] = None | |
| c -= 1 | |
| updates[count_state] = c | |
| updates[add_button] = gr.update(visible=True) | |
| updates[del_button] = gr.update(visible=c > 1) | |
| return updates | |
| add_outputs = [count_state, add_button, del_button] + lora_rows | |
| del_outputs = [count_state, add_button, del_button] + lora_rows + lora_ids + lora_scales + lora_uploads | |
| add_button.click(add_lora_row, [count_state], add_outputs, show_progress=False) | |
| del_button.click(del_lora_row, [count_state], del_outputs, show_progress=False) | |
| def create_embedding_event_handlers(prefix): | |
| rows = ui_components[f'embedding_rows_{prefix}'] | |
| ids = ui_components[f'embeddings_ids_{prefix}'] | |
| files = ui_components[f'embeddings_files_{prefix}'] | |
| count_state = ui_components[f'embedding_count_state_{prefix}'] | |
| add_button = ui_components[f'add_embedding_button_{prefix}'] | |
| del_button = ui_components[f'delete_embedding_button_{prefix}'] | |
| def add_row(c): | |
| c += 1 | |
| return { | |
| count_state: c, | |
| rows[c - 1]: gr.update(visible=True), | |
| add_button: gr.update(visible=c < MAX_EMBEDDINGS), | |
| del_button: gr.update(visible=True) | |
| } | |
| def del_row(c): | |
| c -= 1 | |
| return { | |
| count_state: c, | |
| rows[c]: gr.update(visible=False), | |
| ids[c]: "", | |
| files[c]: None, | |
| add_button: gr.update(visible=True), | |
| del_button: gr.update(visible=c > 0) | |
| } | |
| add_outputs = [count_state, add_button, del_button] + rows | |
| del_outputs = [count_state, add_button, del_button] + rows + ids + files | |
| add_button.click(fn=add_row, inputs=[count_state], outputs=add_outputs, show_progress=False) | |
| del_button.click(fn=del_row, inputs=[count_state], outputs=del_outputs, show_progress=False) | |
| def create_conditioning_event_handlers(prefix): | |
| rows = ui_components[f'conditioning_rows_{prefix}'] | |
| prompts = ui_components[f'conditioning_prompts_{prefix}'] | |
| count_state = ui_components[f'conditioning_count_state_{prefix}'] | |
| add_button = ui_components[f'add_conditioning_button_{prefix}'] | |
| del_button = ui_components[f'delete_conditioning_button_{prefix}'] | |
| def add_row(c): | |
| c += 1 | |
| return { | |
| count_state: c, | |
| rows[c - 1]: gr.update(visible=True), | |
| add_button: gr.update(visible=c < MAX_CONDITIONINGS), | |
| del_button: gr.update(visible=True), | |
| } | |
| def del_row(c): | |
| c -= 1 | |
| return { | |
| count_state: c, | |
| rows[c]: gr.update(visible=False), | |
| prompts[c]: "", | |
| add_button: gr.update(visible=True), | |
| del_button: gr.update(visible=c > 0), | |
| } | |
| add_outputs = [count_state, add_button, del_button] + rows | |
| del_outputs = [count_state, add_button, del_button] + rows + prompts | |
| add_button.click(fn=add_row, inputs=[count_state], outputs=add_outputs, show_progress=False) | |
| del_button.click(fn=del_row, inputs=[count_state], outputs=del_outputs, show_progress=False) | |
| def on_vae_upload(file_obj): | |
| if not file_obj: | |
| return gr.update(), gr.update(), None | |
| hashed_filename = save_uploaded_file_with_hash(file_obj, VAE_DIR) | |
| return hashed_filename, "File", file_obj | |
| def on_lora_upload(file_obj): | |
| if not file_obj: | |
| return gr.update(), gr.update() | |
| hashed_filename = save_uploaded_file_with_hash(file_obj, LORA_DIR) | |
| return hashed_filename, "File" | |
| def on_embedding_upload(file_obj): | |
| if not file_obj: | |
| return gr.update(), gr.update(), None | |
| hashed_filename = save_uploaded_file_with_hash(file_obj, EMBEDDING_DIR) | |
| return hashed_filename, "File", file_obj | |
| def create_run_event(prefix: str, task_type: str): | |
| run_inputs_map = { | |
| 'model_display_name': ui_components[f'base_model_{prefix}'], | |
| 'positive_prompt': ui_components[f'prompt_{prefix}'], | |
| 'negative_prompt': ui_components[f'neg_prompt_{prefix}'], | |
| 'seed': ui_components[f'seed_{prefix}'], | |
| 'batch_size': ui_components[f'batch_size_{prefix}'], | |
| 'guidance_scale': ui_components[f'cfg_{prefix}'], | |
| 'num_inference_steps': ui_components[f'steps_{prefix}'], | |
| 'sampler': ui_components[f'sampler_{prefix}'], | |
| 'scheduler': ui_components[f'scheduler_{prefix}'], | |
| 'zero_gpu_duration': ui_components[f'zero_gpu_{prefix}'], | |
| 'civitai_api_key': ui_components.get(f'civitai_api_key_{prefix}'), | |
| 'clip_skip': ui_components[f'clip_skip_{prefix}'], | |
| 'task_type': gr.State(task_type) | |
| } | |
| if task_type not in ['img2img', 'inpaint']: | |
| run_inputs_map.update({'width': ui_components[f'width_{prefix}'], 'height': ui_components[f'height_{prefix}']}) | |
| task_specific_map = { | |
| 'img2img': {'img2img_image': f'input_image_{prefix}', 'img2img_denoise': f'denoise_{prefix}'}, | |
| 'inpaint': {'inpaint_image_dict': f'input_image_dict_{prefix}'}, | |
| 'outpaint': {'outpaint_image': f'input_image_{prefix}', 'outpaint_left': f'outpaint_left_{prefix}', 'outpaint_top': f'outpaint_top_{prefix}', 'outpaint_right': f'outpaint_right_{prefix}', 'outpaint_bottom': f'outpaint_bottom_{prefix}'}, | |
| 'hires_fix': {'hires_image': f'input_image_{prefix}', 'hires_upscaler': f'hires_upscaler_{prefix}', 'hires_scale_by': f'hires_scale_by_{prefix}', 'hires_denoise': f'denoise_{prefix}'} | |
| } | |
| if task_type in task_specific_map: | |
| for key, comp_name in task_specific_map[task_type].items(): | |
| run_inputs_map[key] = ui_components[comp_name] | |
| lora_data_components = ui_components.get(f'all_lora_components_flat_{prefix}', []) | |
| embedding_data_components = ui_components.get(f'all_embedding_components_flat_{prefix}', []) | |
| conditioning_data_components = ui_components.get(f'all_conditioning_components_flat_{prefix}', []) | |
| run_inputs_map['vae_source'] = ui_components.get(f'vae_source_{prefix}') | |
| run_inputs_map['vae_id'] = ui_components.get(f'vae_id_{prefix}') | |
| run_inputs_map['vae_file'] = ui_components.get(f'vae_file_{prefix}') | |
| input_keys = list(run_inputs_map.keys()) | |
| input_list_flat = [v for v in run_inputs_map.values() if v is not None] | |
| input_list_flat += lora_data_components + embedding_data_components + conditioning_data_components | |
| def create_ui_inputs_dict(*args): | |
| valid_keys = [k for k in input_keys if run_inputs_map[k] is not None] | |
| ui_dict = dict(zip(valid_keys, args[:len(valid_keys)])) | |
| arg_idx = len(valid_keys) | |
| ui_dict['lora_data'] = list(args[arg_idx : arg_idx + len(lora_data_components)]) | |
| arg_idx += len(lora_data_components) | |
| ui_dict['embedding_data'] = list(args[arg_idx : arg_idx + len(embedding_data_components)]) | |
| arg_idx += len(embedding_data_components) | |
| ui_dict['conditioning_data'] = list(args[arg_idx : arg_idx + len(conditioning_data_components)]) | |
| return ui_dict | |
| ui_components[f'run_{prefix}'].click( | |
| fn=lambda *args, progress=gr.Progress(track_tqdm=True): generate_image_wrapper(create_ui_inputs_dict(*args), progress), | |
| inputs=input_list_flat, | |
| outputs=[ui_components[f'result_{prefix}']] | |
| ) | |
| newbie_input_keys = [k for k in ui_components if k.startswith('newbie_') or k.startswith('char')] | |
| newbie_input_components = [ui_components[k] for k in newbie_input_keys] | |
| def create_newbie_ui_inputs_dict(*args): | |
| return dict(zip(newbie_input_keys, args)) | |
| if 'newbie_run_button' in ui_components: | |
| ui_components['newbie_run_button'].click( | |
| fn=lambda *args, progress=gr.Progress(track_tqdm=True): generate_xml_wrapper(create_newbie_ui_inputs_dict(*args), progress), | |
| inputs=newbie_input_components, | |
| outputs=[ui_components['newbie_status'], ui_components['newbie_xml_output']] | |
| ) | |
| for prefix, task_type in [ | |
| ("txt2img", "txt2img"), ("img2img", "img2img"), ("inpaint", "inpaint"), | |
| ("outpaint", "outpaint"), ("hires_fix", "hires_fix"), | |
| ]: | |
| if f'add_lora_button_{prefix}' in ui_components: | |
| create_lora_event_handlers(prefix) | |
| lora_uploads = ui_components[f'lora_uploads_{prefix}'] | |
| lora_ids = ui_components[f'lora_ids_{prefix}'] | |
| lora_sources = ui_components[f'lora_sources_{prefix}'] | |
| for i in range(MAX_LORAS): | |
| lora_uploads[i].upload( | |
| fn=on_lora_upload, | |
| inputs=[lora_uploads[i]], | |
| outputs=[lora_ids[i], lora_sources[i]], | |
| show_progress=False | |
| ) | |
| if f'add_embedding_button_{prefix}' in ui_components: | |
| create_embedding_event_handlers(prefix) | |
| if f'embeddings_uploads_{prefix}' in ui_components: | |
| emb_uploads = ui_components[f'embeddings_uploads_{prefix}'] | |
| emb_ids = ui_components[f'embeddings_ids_{prefix}'] | |
| emb_sources = ui_components[f'embeddings_sources_{prefix}'] | |
| emb_files = ui_components[f'embeddings_files_{prefix}'] | |
| for i in range(MAX_EMBEDDINGS): | |
| emb_uploads[i].upload( | |
| fn=on_embedding_upload, | |
| inputs=[emb_uploads[i]], | |
| outputs=[emb_ids[i], emb_sources[i], emb_files[i]], | |
| show_progress=False | |
| ) | |
| if f'add_conditioning_button_{prefix}' in ui_components: create_conditioning_event_handlers(prefix) | |
| if f'vae_source_{prefix}' in ui_components: | |
| upload_button = ui_components.get(f'vae_upload_button_{prefix}') | |
| if upload_button: | |
| upload_button.upload( | |
| fn=on_vae_upload, | |
| inputs=[upload_button], | |
| outputs=[ | |
| ui_components[f'vae_id_{prefix}'], | |
| ui_components[f'vae_source_{prefix}'], | |
| ui_components[f'vae_file_{prefix}'] | |
| ] | |
| ) | |
| create_run_event(prefix, task_type) | |
| def on_aspect_ratio_change(ratio_key, model_display_name): | |
| model_type = MODEL_TYPE_MAP.get(model_display_name, 'sdxl').lower() | |
| res_map = RESOLUTION_MAP.get(model_type, RESOLUTION_MAP.get("sdxl", {})) | |
| w, h = res_map.get(ratio_key, (1024, 1024)) | |
| return w, h | |
| for prefix in ["txt2img", "img2img", "inpaint", "outpaint", "hires_fix"]: | |
| if f'aspect_ratio_{prefix}' in ui_components: | |
| aspect_ratio_dropdown = ui_components[f'aspect_ratio_{prefix}'] | |
| width_component = ui_components[f'width_{prefix}'] | |
| height_component = ui_components[f'height_{prefix}'] | |
| model_dropdown = ui_components[f'base_model_{prefix}'] | |
| aspect_ratio_dropdown.change(fn=on_aspect_ratio_change, inputs=[aspect_ratio_dropdown, model_dropdown], outputs=[width_component, height_component], show_progress=False) | |
| if 'view_mode_inpaint' in ui_components: | |
| def toggle_inpaint_fullscreen_view(view_mode): | |
| is_fullscreen = (view_mode == "Fullscreen View") | |
| other_elements_visible = not is_fullscreen | |
| editor_height = 800 if is_fullscreen else 272 | |
| return { | |
| ui_components['model_and_run_row_inpaint']: gr.update(visible=other_elements_visible), | |
| ui_components['prompts_column_inpaint']: gr.update(visible=other_elements_visible), | |
| ui_components['params_and_gallery_row_inpaint']: gr.update(visible=other_elements_visible), | |
| ui_components['accordion_wrapper_inpaint']: gr.update(visible=other_elements_visible), | |
| ui_components['input_image_dict_inpaint']: gr.update(height=editor_height), | |
| } | |
| output_components = [ | |
| ui_components['model_and_run_row_inpaint'], ui_components['prompts_column_inpaint'], | |
| ui_components['params_and_gallery_row_inpaint'], ui_components['accordion_wrapper_inpaint'], | |
| ui_components['input_image_dict_inpaint'] | |
| ] | |
| ui_components['view_mode_inpaint'].change(fn=toggle_inpaint_fullscreen_view, inputs=[ui_components['view_mode_inpaint']], outputs=output_components, show_progress=False) | |
| def initialize_all_cn_dropdowns(): | |
| return {} | |
| def run_on_load(): | |
| return initialize_all_cn_dropdowns() | |
| all_load_outputs = [] | |
| if all_load_outputs: | |
| demo.load( | |
| fn=run_on_load, | |
| outputs=all_load_outputs | |
| ) |