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
+6 -7
View File
@@ -6,14 +6,10 @@
- Quick apply style
- SC LoRA
- DoRA
- SDXS VAE: https://huggingface.co/IDKiro/sdxs-512-0.9
- Control API/CLI
- scripts
- units
- preprocess
- Control API scripts compatibility
## Update for 2024-03-26
## Update for 2024-03-27
- **Features**:
- **Gallery**: list, preview, search through all your images and videos!
@@ -28,7 +24,10 @@
both can still be installed by user if desired
- **Improvements**:
- Styles apply wildcards to params
- Add API endpoint `/sdapi/v1/control` and util `cli/simple-control.py`
- Add API endpoint `/sdapi/v1/control` and util `cli/simple-control.py`
(in addition to previously added `/sdapi/v1/preprocessors` and `/sdapi/v1/masking`)
example:
> simple-control.py --prompt cat --input ~/generative/Samples/cutie-512.png --output /tmp/test.png --processed /tmp/proc.png --type controlnet --control 'Canny:Canny FP16:0.7, OpenPose:OpenPose FP16:0.8'
- Add API endpoint `/sdapi/v1/vqa` and util `cli/simple-vqa.py`
- Make metadata in full screen viewer optional
- Add VAE civitai scan metadata/preview
+35 -9
View File
@@ -71,22 +71,45 @@ def generate(args): # pylint: disable=redefined-outer-name
options['steps'] = int(args.steps)
options['seed'] = int(args.seed)
options['sampler_name'] = args.sampler
if args.control is not None:
options['unit_type'] = args.type
options['control'] = []
for control in args.control.split(','):
u = control.split(':')
if len(u) < 2:
log.error(f'invalid control: {control}')
continue
options['control'].append({
'process': u[0].strip(),
'model': u[1].strip(),
'strength': float(u[2].strip()) if len(u) > 2 else 1.0,
'start': float(u[3].strip()) if len(u) > 3 else 0.0,
'end': float(u[4].strip()) if len(u) > 4 else 1.0,
})
log.info(f"control options: {options['control']}")
if args.mask is not None:
options['mask'] = encode(args.mask)
data = post('/sdapi/v1/control', options)
t1 = time.time()
if 'images' in data:
for i in range(len(data['images'])):
b64 = data['images'][i].split(',',1)[0]
if 'info' in data:
log.info(f'info: {data["info"]}')
def get_image(encoded, output):
if not isinstance(encoded, list):
return
for i in range(len(encoded)):
b64 = encoded[i].split(',',1)[0]
info = data['info']
image = Image.open(io.BytesIO(base64.b64decode(b64)))
log.info(f'received image: size={image.size} time={t1-t0:.2f} info="{info}"')
if args.output:
image.save(args.output)
log.info(f'image saved: size={image.size} filename={args.output}')
if output:
image.save(output)
log.info(f'image saved: size={image.size} filename={output}')
else:
log.warning(f'no images received: {data}')
get_image(data['images'], args.output)
get_image(data['processed'], args.processed)
if __name__ == "__main__":
@@ -98,9 +121,12 @@ if __name__ == "__main__":
parser.add_argument('--negative', required=False, default='', help='negative prompt text')
parser.add_argument('--steps', required=False, default=20, help='number of steps')
parser.add_argument('--seed', required=False, default=-1, help='initial seed')
parser.add_argument('--sampler', required=False, default='Euler a', help='sampler name')
parser.add_argument('--sampler', required=False, default='UniPC', help='sampler name')
parser.add_argument('--output', required=False, default=None, help='output image file')
parser.add_argument('--processed', required=False, default=None, help='processed output file')
parser.add_argument('--model', required=False, help='model name')
parser.add_argument('--type', required=False, help='control type')
parser.add_argument('--control', required=False, help='control units')
args = parser.parse_args()
log.info(f'img2img: {args}')
generate(args)
+85 -41
View File
@@ -1,7 +1,7 @@
from typing import List
from typing import Optional, List
from threading import Lock
from pydantic import BaseModel, Field # pylint: disable=no-name-in-module
from modules import errors, shared
from modules import errors, shared, processing_helpers
from modules.api import models, helpers
from modules.control import run
@@ -9,13 +9,37 @@ from modules.control import run
errors.install()
ReqControl = models.create_model_from_signature(run.control_run, "StableDiffusionProcessingControl")
class ItemControl(BaseModel):
process: str = Field(title="Preprocessor", default="", description="")
model: str = Field(title="Control Model", default="", description="")
strength: float = Field(title="Control model strength", default=1.0, description="")
start: float = Field(title="Control model start", default=0.0, description="")
end: float = Field(title="Control model end", default=1.0, description="")
override: str = Field(title="Override image", default=None, description="")
ReqControl = models.create_model_from_signature(
func = run.control_run,
model_name = "StableDiffusionProcessingControl",
additional_fields = [
{"key": "sampler_name", "type": str, "default": "UniPC"},
{"key": "script_name", "type": str, "default": None},
{"key": "script_args", "type": list, "default": []},
{"key": "send_images", "type": bool, "default": True},
{"key": "save_images", "type": bool, "default": False},
{"key": "alwayson_scripts", "type": dict, "default": {}},
{"key": "ip_adapter", "type": Optional[models.ItemIPAdapter], "default": None, "exclude": True},
{"key": "face", "type": Optional[models.ItemFace], "default": None, "exclude": True},
{"key": "control", "type": Optional[List[ItemControl]], "default": [], "exclude": True},
]
)
class ResControl(BaseModel):
images: List[str] = Field(default=None, title="Image", description="The generated images in base64 format.")
params: dict = Field(default={}, title="Settings", description="Process settings")
info: str = Field(default="", title="Info", description="Process info")
images: List[str] = Field(default=None, title="Images", description="")
processed: List[str] = Field(default=None, title="Processed", description="")
params: dict = Field(default={}, title="Settings", description="")
info: str = Field(default="", title="Info", description="")
class APIControl():
@@ -25,8 +49,7 @@ class APIControl():
def sanitize_args(self, args: dict):
args = vars(args)
"""
args.pop('include_init_images', None) # this is meant to be done by "exclude": True in model
args.pop('sampler_name', None)
args.pop('script_name', None)
args.pop('script_args', None) # will refeed them to the pipeline directly after initializing them
args.pop('alwayson_scripts', None)
@@ -34,7 +57,6 @@ class APIControl():
args.pop('face_id', None)
args.pop('ip_adapter', None)
args.pop('save_images', None)
"""
return args
def sanitize_b64(self, request):
@@ -42,7 +64,6 @@ class APIControl():
for idx in range(0, len(args)):
if isinstance(args[idx], str) and len(args[idx]) >= 1000:
args[idx] = f"<str {len(args[idx])}>"
if hasattr(request, "alwayson_scripts") and request.alwayson_scripts:
for script_name in request.alwayson_scripts.keys():
script_obj = request.alwayson_scripts[script_name]
@@ -51,36 +72,57 @@ class APIControl():
if hasattr(request, "script_args") and request.script_args:
sanitize_str(request.script_args)
def prepare_face_module(self, request):
if hasattr(request, "face") and request.face and not request.script_name and (not request.alwayson_scripts or "face" not in request.alwayson_scripts.keys()):
request.script_name = "face"
request.script_args = [
request.face.mode,
request.face.source_images,
request.face.ip_model,
request.face.ip_override_sampler,
request.face.ip_cache_model,
request.face.ip_strength,
request.face.ip_structure,
request.face.id_strength,
request.face.id_conditioning,
request.face.id_cache,
request.face.pm_trigger,
request.face.pm_strength,
request.face.pm_start,
request.face.fs_cache
def prepare_face_module(self, req):
if hasattr(req, "face") and req.face and not req.script_name and (not req.alwayson_scripts or "face" not in req.alwayson_scripts.keys()):
req.script_name = "face"
req.script_args = [
req.face.mode,
req.face.source_images,
req.face.ip_model,
req.face.ip_override_sampler,
req.face.ip_cache_model,
req.face.ip_strength,
req.face.ip_structure,
req.face.id_strength,
req.face.id_conditioning,
req.face.id_cache,
req.face.pm_trigger,
req.face.pm_strength,
req.face.pm_start,
req.face.fs_cache
]
del request.face
del req.face
def prepare_control(self, req):
from modules.control.unit import Unit, unit_types
req.units = []
if req.unit_type not in unit_types:
shared.log.error(f'Control uknown unit type: type={req.unit_type} available={unit_types}')
return req.control
for u in req.control:
unit = Unit(
enabled = True,
unit_type = req.unit_type,
model_id = u.model,
process_id = u.process,
strength = u.strength,
start = u.start,
end = u.end,
)
if u.override is not None:
unit.override = helpers.decode_base64_to_image(u.override)
req.units.append(unit)
return req.control
def post_control(self, req: ReqControl):
self.prepare_face_module(req)
orig_control = self.prepare_control(req)
del req.control
# prepare args
args = req.copy(update={ # Override __init__ params
# "sampler_name": helpers.validate_sampler_name(req.sampler_name or req.sampler_index),
# "sampler_index": processing_helpers.get_sampler_index(req.sampler_name),
# "do_not_save_samples": not req.save_images,
# "do_not_save_grid": not req.save_images,
"sampler_index": processing_helpers.get_sampler_index(req.sampler_name),
"no_save": not req.save_images,
"is_generator": False,
"inputs": [helpers.decode_base64_to_image(x) for x in req.inputs] if req.inputs else None,
"inits": [helpers.decode_base64_to_image(x) for x in req.inits] if req.inits else None,
@@ -93,21 +135,23 @@ class APIControl():
with self.queue_lock:
shared.state.begin('api-control', api=True)
# selectable_scripts, selectable_script_idx = script.get_selectable_script(req.script_name, script_runner)
# script_args = script.init_script_args(p, req, self.default_script_arg, selectable_scripts, selectable_script_idx, script_runner)
# output_images, _processed_images, output_info = run_control(**args, **script_args)
output_images = []
output_processed = []
output_info = ''
res = run.control_run(**args)
for item in res:
if len(item) > 0 and isinstance(item[0], list):
output_images += item[0]
output_info += item[2]
if len(item) > 0 and (isinstance(item[0], list) or item[0] is None): # output_images
output_images += item[0] if item[0] is not None else []
output_processed += [item[1]] if item[1] is not None else []
output_info += item[2] if len(item) > 2 and item[2] is not None else ''
else:
output_info += item
shared.state.end(api=False)
# return
b64images = list(map(helpers.encode_pil_to_base64, output_images)) if send_images else []
b64processed = list(map(helpers.encode_pil_to_base64, output_processed)) if send_images else []
self.sanitize_b64(req)
return ResControl(images=b64images, params=vars(req), info=output_info)
req.units = orig_control
return ResControl(images=b64images, processed=b64processed, params=vars(req), info=output_info)
+23 -7
View File
@@ -380,9 +380,14 @@ StableDiffusionImg2ImgProcessingAPI = ResImg2Img
# helper function
def create_model_from_signature(func: Callable, model_name: str, base_model: Type[BaseModel] = BaseModel, exclude_fields: List[str] = []):
def create_model_from_signature(func: Callable, model_name: str, base_model: Type[BaseModel] = BaseModel, additional_fields: List = [], exclude_fields: List[str] = []):
from PIL import Image
class Config:
extra = 'allow'
args, _, varkw, defaults, kwonlyargs, kwonlydefaults, annotations = inspect.getfullargspec(func)
config = Config if varkw else None # Allow extra params if there is a **kwargs parameter in the function signature
defaults = defaults or []
args = args or []
for arg in exclude_fields:
@@ -398,17 +403,28 @@ def create_model_from_signature(func: Callable, model_name: str, base_model: Typ
annotations[k] = str
elif str(v) == 'typing.List[modules.control.unit.Unit]':
annotations[k] = List[str]
params = {param: (annotations.get(param, Any), default) for param, default in zip(args, defaults)}
model_fields = {param: (annotations.get(param, Any), default) for param, default in zip(args, defaults)}
class Config:
extra = 'allow'
for fld in additional_fields:
model_def = ModelDef(
field=underscore(fld["key"]),
field_alias=fld["key"],
field_type=fld["type"],
field_value=fld["default"],
field_exclude=fld["exclude"] if "exclude" in fld else False)
model_fields[model_def.field] = (model_def.field_type, Field(default=model_def.field_value, alias=model_def.field_alias, exclude=model_def.field_exclude))
config = Config if varkw else None # Allow extra params if there is a **kwargs parameter in the function signature
for fld in exclude_fields:
if fld in model_fields:
del model_fields[fld]
return create_model(
model = create_model(
model_name,
**params,
**model_fields,
**keyword_only_params,
__base__=base_model,
__config__=config,
)
model.__config__.allow_population_by_field_name = True
model.__config__.allow_mutation = True
return model
+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