Skip to content

Commit

Permalink
double check is redundant
Browse files Browse the repository at this point in the history
  • Loading branch information
ljleb committed Aug 11, 2023
1 parent 291e0d4 commit 8da12dc
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 85 deletions.
30 changes: 12 additions & 18 deletions lib_comfyui/webui/workflow_patcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,12 @@ def create_sampler_hijack(name: str, model, original_function):

@ipc.restrict_to_process('webui')
def sample_img2img_hijack(p, x, *args, original_function, **kwargs):
if getattr(global_state, 'enabled', True):
preprocessed_x = external_code.run_workflow(
workflow_type=default_workflow_types.preprocess_latent_workflow_type,
tab='img2img',
batch_input=x.to(device='cpu'),
)
x = torch.stack(preprocessed_x).to(device=x.device) if isinstance(preprocessed_x, list) else preprocessed_x.to(device=x.device)

processed_x = external_code.run_workflow(
workflow_type=default_workflow_types.preprocess_latent_workflow_type,
tab='img2img',
batch_input=x.to(device='cpu'),
)
x = torch.stack(processed_x).to(device=x.device) if isinstance(processed_x, list) else processed_x.to(device=x.device)
return original_function(p, x, *args, **kwargs)


Expand All @@ -62,15 +60,12 @@ def patch_processing(p):

def p_sample_patch(*args, original_function, is_img2img, **kwargs):
x = original_function(*args, **kwargs)
if getattr(global_state, 'enabled', True):
postprocessed_x = external_code.run_workflow(
workflow_type=default_workflow_types.postprocess_latent_workflow_type,
tab='img2img' if is_img2img else 'txt2img',
batch_input=x.to(device='cpu'),
)
x = torch.stack(postprocessed_x).to(device=x.device) if isinstance(postprocessed_x, list) else postprocessed_x.to(device=x.device)

return x
processed_x = external_code.run_workflow(
workflow_type=default_workflow_types.postprocess_latent_workflow_type,
tab='img2img' if is_img2img else 'txt2img',
batch_input=x.to(device='cpu'),
)
return torch.stack(processed_x).to(device=x.device) if isinstance(processed_x, list) else processed_x.to(device=x.device)


def p_img2img_init(*args, original_function, p_ref, **kwargs):
Expand All @@ -80,5 +75,4 @@ def p_img2img_init(*args, original_function, p_ref, **kwargs):
batch_input=[F.pil_to_tensor(image) / 255 for image in p_ref.init_images],
)
p_ref.init_images = [F.to_pil_image(image_tensor) for image_tensor in preprocessed_images]

return original_function(*args, **kwargs)
135 changes: 70 additions & 65 deletions scripts/comfyui.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def show(self, is_img2img):

def ui(self, is_img2img):
global_state.is_ui_instantiated = True
with gr.Accordion(f"ComfyUI", open=False, elem_id=self.elem_id('accordion')):
with gr.Accordion(f"ComfyUI", open=False, elem_id=self.elem_id('accordion')) as accordion:
return self.get_alwayson_ui(is_img2img)

def get_alwayson_ui(self, is_img2img: bool):
Expand All @@ -35,23 +35,22 @@ def get_alwayson_ui(self, is_img2img: bool):
for workflow_type in workflow_types
}

current_workflow_type_id = gr.Text(
current_workflow_type_id = gr.HTML(
value=workflow_type_ids[first_workflow_type.display_name],
visible=False,
interactive=False,
)
enabled_display_names_json = gr.Textbox(

enabled_display_names_json = gr.HTML(
json.dumps([]),
visible=False,
interactive=False,
)

with gr.Row():
queue_front = gr.Checkbox(
label='Queue front',
elem_id=self.elem_id('queue_front'),
value=True,
)
gr.HTML(value=self.get_iframes_html(is_img2img, workflow_type_ids[first_workflow_type.display_name]))

with gr.Row():
enable = gr.Checkbox(
label='Enable',
elem_id=self.elem_id('enabled'),
Expand All @@ -64,75 +63,81 @@ def get_alwayson_ui(self, is_img2img: bool):
value=first_workflow_type.display_name,
elem_id=self.elem_id('displayed_workflow_type'),
)
current_workflow_display_name.change(
fn=workflow_type_ids.get,
inputs=[current_workflow_display_name],
outputs=[current_workflow_type_id],
)
current_workflow_type_id.change(
fn=None,
_js='changeDisplayedWorkflowType',
inputs=[current_workflow_type_id],
)
current_workflow_display_name.change(
fn=lambda current_workflow_display_name, enabled_display_names_json: current_workflow_display_name in json.loads(enabled_display_names_json),
inputs=[current_workflow_display_name, enabled_display_names_json],
outputs=[enable],
)
enable.select(
fn=lambda current_workflow_display_name, enabled_display_names_json, enable: json.dumps(list(
(set(json.loads(enabled_display_names_json)) | {current_workflow_display_name})
if enable
else (set(json.loads(enabled_display_names_json)) - {current_workflow_display_name})
)),
inputs=[current_workflow_display_name, enabled_display_names_json, enable],
outputs=[enabled_display_names_json]
)
enable_style = gr.HTML()
for comp in [current_workflow_display_name, enabled_display_names_json]:
comp.change(
fn=lambda enabled_display_names_json, current_workflow_display_name: f'''<style>
{f'div#{self.elem_id("displayed_workflow_type")} input,' if current_workflow_display_name in json.loads(enabled_display_names_json) else ''
}{','.join(
f'div#{self.elem_id("displayed_workflow_type")} ul.options > li.item[data-value="{display_name}"]'
for display_name in json.loads(enabled_display_names_json)
)} {{
color: lime;
}}
</style>''',
inputs=[enabled_display_names_json, current_workflow_display_name],
outputs=[enable_style],
)

def update_enabled_workflow_type_ids(enabled_display_names_json):
enabled_display_names = json.loads(enabled_display_names_json)
if not hasattr(global_state, 'enabled_workflow_type_ids'):
global_state.enabled_workflow_type_ids = {}

enabled_workflow_type_ids = {
workflow_type_ids[workflow_type.display_name]: workflow_type.display_name in enabled_display_names
for workflow_type in workflow_types
}
global_state.enabled_workflow_type_ids.update(enabled_workflow_type_ids)

enabled_display_names_json.change(
fn=update_enabled_workflow_type_ids,
inputs=[enabled_display_names_json],
)

with gr.Row():
gr.HTML(value=self.get_iframes_html(is_img2img, workflow_type_ids[first_workflow_type.display_name]))
queue_front = gr.Checkbox(
label='Queue front',
elem_id=self.elem_id('queue_front'),
value=True,
)

with gr.Row():
refresh_button = gr.Button(
value=f'{ui.refresh_symbol} Reload ComfyUI interface (client side)',
value=f'{ui.refresh_symbol} Reload ComfyUI interfaces (client side)',
elem_id=self.elem_id('refresh_button'),
)
refresh_button.click(
fn=None,
_js='reloadComfyuiIFrames'
)

current_workflow_display_name.change(
fn=workflow_type_ids.get,
inputs=[current_workflow_display_name],
outputs=[current_workflow_type_id],
)
current_workflow_type_id.change(
fn=None,
_js='changeDisplayedWorkflowType',
inputs=[current_workflow_type_id],
)
current_workflow_display_name.change(
fn=lambda current_workflow_display_name, enabled_display_names_json: current_workflow_display_name in json.loads(enabled_display_names_json),
inputs=[current_workflow_display_name, enabled_display_names_json],
outputs=[enable],
)
enable.select(
fn=lambda current_workflow_display_name, enabled_display_names_json, enable: json.dumps(list(
(set(json.loads(enabled_display_names_json)) | {current_workflow_display_name})
if enable
else (set(json.loads(enabled_display_names_json)) - {current_workflow_display_name})
)),
inputs=[current_workflow_display_name, enabled_display_names_json, enable],
outputs=[enabled_display_names_json]
)
enable_style = gr.HTML()
for comp in [current_workflow_display_name, enabled_display_names_json]:
comp.change(
fn=lambda enabled_display_names_json, current_workflow_display_name: f'''<style>
{f'div#{self.elem_id("displayed_workflow_type")} input,' if current_workflow_display_name in json.loads(enabled_display_names_json) else ''
}{','.join(
f'div#{self.elem_id("displayed_workflow_type")} ul.options > li.item[data-value="{display_name}"]'
for display_name in json.loads(enabled_display_names_json)
)} {{
color: greenyellow !important;
font-weight: bold;
}}
</style>''',
inputs=[enabled_display_names_json, current_workflow_display_name],
outputs=[enable_style],
)

def update_enabled_workflow_type_ids(enabled_display_names_json):
enabled_display_names = json.loads(enabled_display_names_json)
if not hasattr(global_state, 'enabled_workflow_type_ids'):
global_state.enabled_workflow_type_ids = {}

enabled_workflow_type_ids = {
workflow_type_ids[workflow_type.display_name]: workflow_type.display_name in enabled_display_names
for workflow_type in workflow_types
}
global_state.enabled_workflow_type_ids.update(enabled_workflow_type_ids)

enabled_display_names_json.change(
fn=update_enabled_workflow_type_ids,
inputs=[enabled_display_names_json],
)

self.setup_infotext_updates(workflow_types, xxx2img)

return queue_front,
Expand Down
4 changes: 2 additions & 2 deletions style.css
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ iframe.comfyui-embedded-widget-display {
}

#script_txt2txt_comfyui_refresh_button {
max-width: 20em;
min-width: 20em;
max-width: 25em;
min-width: 25em;
height: 2.4em;
}

0 comments on commit 8da12dc

Please sign in to comment.