Skip to content

Commit

Permalink
Repair infotext parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
catboxanon authored Jul 26, 2023
1 parent e9679f8 commit dc54ef7
Showing 1 changed file with 41 additions and 4 deletions.
45 changes: 41 additions & 4 deletions scripts/controlnet.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import gc
import os
import logging
import re
from collections import OrderedDict
from copy import copy
from typing import Dict, Optional, Tuple
Expand Down Expand Up @@ -265,9 +266,8 @@ def ui(self, is_img2img):
with gr.Column():
controls += (self.uigroup(f"ControlNet", is_img2img, elem_id_tabname),)

if shared.opts.data.get("control_net_sync_field_args", False):
for _, field_name in self.infotext_fields:
self.paste_field_names.append(field_name)
for _, field_name in self.infotext_fields:
self.paste_field_names.append(field_name)

return controls

Expand Down Expand Up @@ -1040,7 +1040,7 @@ def on_ui_settings():
shared.opts.add_option("control_net_allow_script_control", shared.OptionInfo(
False, "Allow other script to control this extension", gr.Checkbox, {"interactive": True}, section=section))
shared.opts.add_option("control_net_sync_field_args", shared.OptionInfo(
False, "Passing ControlNet parameters with \"Send to img2img\"", gr.Checkbox, {"interactive": True}, section=section))
False, "Parse ControlNet parameters in infotext", gr.Checkbox, {"interactive": True}, section=section))
shared.opts.add_option("controlnet_show_batch_images_in_ui", shared.OptionInfo(
False, "Show batch images in gradio gallery output", gr.Checkbox, {"interactive": True}, section=section))
shared.opts.add_option("controlnet_increment_seed_during_batch", shared.OptionInfo(
Expand All @@ -1050,7 +1050,44 @@ def on_ui_settings():
shared.opts.add_option("controlnet_disable_openpose_edit", shared.OptionInfo(
False, "Disable openpose edit", gr.Checkbox, {"interactive": True}, section=section))

def on_infotext_pasted(infotext: str, results: dict):
results_controlnet = {k: v for k, v in results.items() if k.startswith('ControlNet')}

if not shared.opts.data.get("control_net_sync_field_args", False):
for k in results_controlnet.keys():
del results[k]
return

# Don't modify infotext if ControlNet keys are already in the expected format
if len([x for x in results_controlnet.keys() if 'ControlNet-' in x and ' Enabled' in x]) > 0:
return

re_param_controlnet_code = r'\s?([a-z\s\/]+):\s((?:\((?:[\d\.]+(?:,\s)?)+\))|.*?),'
re_param_controlnet = re.compile(re_param_controlnet_code)

for k, v in results_controlnet.items():
# Modify keys to use original format
k = k.replace('ControlNet ', 'ControlNet-')
v += ','
infotext_controlnet = dict(re_param_controlnet.findall(v))

results[f'{k} Enabled'] = True
if 'preprocessor' in infotext_controlnet and infotext_controlnet['preprocessor'] != 'None':
results[f'{k} Preprocessor'] = infotext_controlnet['preprocessor']
if 'model' in infotext_controlnet and infotext_controlnet['model'] != 'None':
results[f'{k} Model'] = infotext_controlnet['model']
if 'weight' in infotext_controlnet:
results[f'{k} Weight'] = infotext_controlnet['weight']
if 'starting/ending' in infotext_controlnet:
infotext_controlnet['starting/ending'] = infotext_controlnet['starting/ending'].replace('(','').replace(')','')
start = infotext_controlnet['starting/ending'].split(',')[0].strip()
end = infotext_controlnet['starting/ending'].split(',')[-1].strip()
if start:
results[f'{k} Guidance Start'] = start
if end:
results[f'{k} Guidance End'] = end

batch_hijack.instance.do_hijack()
script_callbacks.on_ui_settings(on_ui_settings)
script_callbacks.on_infotext_pasted(on_infotext_pasted)
script_callbacks.on_after_component(ControlNetUiGroup.on_after_component)

0 comments on commit dc54ef7

Please sign in to comment.