control api

This commit is contained in:
Vladimir Mandic
2024-03-27 11:33:29 -04:00
parent 738f115d06
commit 89e9debbcd
11 changed files with 199 additions and 83 deletions
+5
View File
@@ -165,6 +165,9 @@ class Processor():
if self.processor_id != processor_id:
self.reset()
self.config(processor_id)
if processor_id not in config:
log.error(f'Control Processor unknown: id="{processor_id}" available={list(config)}')
return f'Processor failed to load: {processor_id}'
cls = config[processor_id]['class']
log.debug(f'Control Processor loading: id="{processor_id}" class={cls.__name__}')
debug(f'Control Processor config={self.load_config}')
@@ -221,6 +224,8 @@ class Processor():
if image_input is None:
# log.error('Control Processor: no input')
return image_process
if self.processor_id not in config:
return image_process
if config[self.processor_id].get('dirty', False):
processor_id = self.processor_id
config[processor_id].pop('dirty')
+10 -14
View File
@@ -56,6 +56,7 @@ def control_run(units: List[unit.Unit] = [], inputs: List[Image.Image] = [], ini
enable_hr: bool = False, hr_sampler_index: int = None, hr_denoising_strength: float = 0.3, hr_upscaler: str = None, hr_force: bool = False, hr_second_pass_steps: int = 20,
hr_scale: float = 1.0, hr_resize_x: int = 0, hr_resize_y: int = 0, refiner_steps: int = 5, refiner_start: float = 0.0, refiner_prompt: str = '', refiner_negative: str = '',
video_skip_frames: int = 0, video_type: str = 'None', video_duration: float = 2.0, video_loop: bool = False, video_pad: int = 0, video_interpolate: int = 0,
no_save: bool = False,
*input_script_args
):
global instance, pipe, original_pipeline # pylint: disable=global-statement
@@ -133,6 +134,8 @@ def control_run(units: List[unit.Unit] = [], inputs: List[Image.Image] = [], ini
p.refiner_start = refiner_start
p.refiner_prompt = refiner_prompt
p.refiner_negative = refiner_negative
p.do_not_save_grid = no_save
p.do_not_save_samples = no_save
if p.enable_hr and (p.hr_resize_x == 0 or p.hr_resize_y == 0):
p.hr_upscale_to_x, p.hr_upscale_to_y = 8 * int(p.width * p.hr_scale / 8), 8 * int(p.height * p.hr_scale / 8)
@@ -305,8 +308,7 @@ def control_run(units: List[unit.Unit] = [], inputs: List[Image.Image] = [], ini
try:
video = cv2.VideoCapture(inputs)
if not video.isOpened():
if is_generator:
yield terminate(f'Control: video open failed: path={inputs}')
yield terminate(f'Control: video open failed: path={inputs}')
return
frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
fps = int(video.get(cv2.CAP_PROP_FPS))
@@ -317,8 +319,7 @@ def control_run(units: List[unit.Unit] = [], inputs: List[Image.Image] = [], ini
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
shared.log.debug(f'Control: input video: path={inputs} frames={frames} fps={fps} size={w}x{h} codec={codec}')
except Exception as e:
if is_generator:
yield terminate(f'Control: video open failed: path={inputs} {e}')
yield terminate(f'Control: video open failed: path={inputs} {e}')
return
while status:
@@ -332,8 +333,7 @@ def control_run(units: List[unit.Unit] = [], inputs: List[Image.Image] = [], ini
continue
if shared.state.interrupted:
shared.state.interrupted = False
if is_generator:
yield terminate('Control interrupted')
yield terminate('Control interrupted')
return
# get input
if isinstance(input_image, str):
@@ -416,8 +416,7 @@ def control_run(units: List[unit.Unit] = [], inputs: List[Image.Image] = [], ini
if len(p.extra_generation_params["Control process"]) == 0:
p.extra_generation_params["Control process"] = None
if any(img is None for img in processed_images):
if is_generator:
yield terminate('Control: attempting process but output is none')
yield terminate('Control: attempting process but output is none')
return
if len(processed_images) > 1:
processed_image = [np.array(i) for i in processed_images]
@@ -429,8 +428,7 @@ def control_run(units: List[unit.Unit] = [], inputs: List[Image.Image] = [], ini
debug(f'Control: inputs match: input={len(processed_images)} models={len(selected_models)}')
p.init_images = processed_images
elif isinstance(selected_models, list) and len(processed_images) != len(selected_models):
if is_generator:
yield terminate(f'Control: number of inputs does not match: input={len(processed_images)} models={len(selected_models)}')
yield terminate(f'Control: number of inputs does not match: input={len(processed_images)} models={len(selected_models)}')
return
elif selected_models is not None:
if len(processed_images) > 1:
@@ -446,8 +444,7 @@ def control_run(units: List[unit.Unit] = [], inputs: List[Image.Image] = [], ini
p.task_args['ref_image'] = p.ref_image
debug(f'Control: process=None image={p.ref_image}')
if p.ref_image is None:
if is_generator:
yield terminate('Control: attempting reference mode but image is none')
yield terminate('Control: attempting reference mode but image is none')
return
elif unit_type == 'controlnet' and input_type == 1: # Init image same as control
p.task_args['control_image'] = p.init_images # switch image and control_image
@@ -507,8 +504,7 @@ def control_run(units: List[unit.Unit] = [], inputs: List[Image.Image] = [], ini
# final check
if has_models:
if unit_type in ['controlnet', 't2i adapter', 'lite', 'xs'] and p.task_args.get('image', None) is None and getattr(p, 'init_images', None) is None:
if is_generator:
yield terminate(f'Control: mode={p.extra_generation_params.get("Control mode", None)} input image is none')
yield terminate(f'Control: mode={p.extra_generation_params.get("Control mode", None)} input image is none')
return
# resize mask
+21 -5
View File
@@ -12,6 +12,7 @@ from modules.control.units import reference # pylint: disable=unused-import
default_device = None
default_dtype = None
unit_types = ['t2i adapter', 'controlnet', 'xs', 'lite', 'reference', 'ip']
class Unit(): # mashup of gradio controls and mapping to actual implementation classes
@@ -135,22 +136,34 @@ class Unit(): # mashup of gradio controls and mapping to actual implementation c
# bind ui controls to properties if present
if self.type == 't2i adapter':
if model_id is not None:
model_id.change(fn=self.adapter.load, inputs=[model_id], outputs=[result_txt], show_progress=True)
if isinstance(model_id, str):
self.adapter.load(model_id)
else:
model_id.change(fn=self.adapter.load, inputs=[model_id], outputs=[result_txt], show_progress=True)
if extra_controls is not None and len(extra_controls) > 0:
extra_controls[0].change(fn=adapter_extra, inputs=extra_controls)
elif self.type == 'controlnet':
if model_id is not None:
model_id.change(fn=self.controlnet.load, inputs=[model_id], outputs=[result_txt], show_progress=True)
if isinstance(model_id, str):
self.controlnet.load(model_id)
else:
model_id.change(fn=self.controlnet.load, inputs=[model_id], outputs=[result_txt], show_progress=True)
if extra_controls is not None and len(extra_controls) > 0:
extra_controls[0].change(fn=controlnet_extra, inputs=extra_controls)
elif self.type == 'xs':
if model_id is not None:
model_id.change(fn=self.controlnet.load, inputs=[model_id, extra_controls[0]], outputs=[result_txt], show_progress=True)
if isinstance(model_id, str):
self.controlnet.load(model_id)
else:
model_id.change(fn=self.controlnet.load, inputs=[model_id, extra_controls[0]], outputs=[result_txt], show_progress=True)
if extra_controls is not None and len(extra_controls) > 0:
extra_controls[0].change(fn=controlnetxs_extra, inputs=extra_controls)
elif self.type == 'lite':
if model_id is not None:
model_id.change(fn=self.controlnet.load, inputs=[model_id], outputs=[result_txt], show_progress=True)
if isinstance(model_id, str):
self.controlnet.load(model_id)
else:
model_id.change(fn=self.controlnet.load, inputs=[model_id], outputs=[result_txt], show_progress=True)
if extra_controls is not None and len(extra_controls) > 0:
extra_controls[0].change(fn=controlnetxs_extra, inputs=extra_controls)
elif self.type == 'reference':
@@ -164,7 +177,10 @@ class Unit(): # mashup of gradio controls and mapping to actual implementation c
if model_strength is not None:
model_strength.change(fn=strength_change, inputs=[model_strength])
if process_id is not None:
process_id.change(fn=self.process.load, inputs=[process_id], outputs=[result_txt], show_progress=True)
if isinstance(process_id, str):
self.process.load(process_id)
else:
process_id.change(fn=self.process.load, inputs=[process_id], outputs=[result_txt], show_progress=True)
if reset_btn is not None:
reset_btn.click(fn=reset, inputs=[], outputs=[enabled_cb, model_id, process_id, model_strength])
if preview_btn is not None:
+3
View File
@@ -149,6 +149,9 @@ class ControlNet():
if model_id is None or model_id == 'None':
self.reset()
return
if model_id not in all_models:
log.error(f'Control {what} unknown model: id="{model_id}" available={list(all_models)}')
return
model_path = all_models[model_id]
if model_path == '':
return
+3
View File
@@ -85,6 +85,9 @@ class ControlLLLite():
if model_id is None or model_id == 'None':
self.reset()
return
if model_id not in all_models:
log.error(f'Control {what} unknown model: id="{model_id}" available={list(all_models)}')
return
model_path = all_models[model_id]
if model_path == '':
return
+5
View File
@@ -86,6 +86,9 @@ class Adapter():
if model_id is None or model_id == 'None':
self.reset()
return
if model_id not in all_models:
log.error(f'Control {what} unknown model: id="{model_id}" available={list(all_models)}')
return
model_path = all_models[model_id]
if model_path is None:
log.error(f'Control {what} model load failed: id="{model_id}" error=unknown model id')
@@ -117,6 +120,8 @@ class AdapterPipeline():
if isinstance(adapter, list) and len(adapter) > 1:
adapter = MultiAdapter(adapter)
adapter.to(device=pipeline.device, dtype=pipeline.dtype)
if pipeline.__class__.__name__ == 'StableDiffusionAdapterPipeline' or pipeline.__class__.__name__ == 'StableDiffusionXLAdapterPipeline':
pass # already initialized
if detect.is_sdxl(pipeline):
self.pipeline = StableDiffusionXLAdapterPipeline(
vae=pipeline.vae,
+3
View File
@@ -81,6 +81,9 @@ class ControlNetXS():
if model_id is None or model_id == 'None':
self.reset()
return
if model_id not in all_models:
log.error(f'Control {what} unknown model: id="{model_id}" available={list(all_models)}')
return
model_path = all_models[model_id]
if model_path == '':
return