mirror of
https://github.com/vladmandic/automatic
synced 2026-09-17 16:24:33 +02:00
Merge branch 'AI-Casanova-ipadapter-end' into dev
This commit is contained in:
+13
-2
@@ -81,7 +81,7 @@ def unapply(pipe): # pylint: disable=arguments-differ
|
||||
pass
|
||||
|
||||
|
||||
def apply(pipe, p: processing.StableDiffusionProcessing, adapter_names=[], adapter_scales=[1.0], adapter_images=[]):
|
||||
def apply(pipe, p: processing.StableDiffusionProcessing, adapter_names=[], adapter_scales=[1.0], adapter_starts=[0.0], adapter_ends=[1.0], adapter_images=[]):
|
||||
global clip_loaded # pylint: disable=global-statement
|
||||
# overrides
|
||||
if hasattr(p, 'ip_adapter_names'):
|
||||
@@ -99,11 +99,19 @@ def apply(pipe, p: processing.StableDiffusionProcessing, adapter_names=[], adapt
|
||||
return False
|
||||
if hasattr(p, 'ip_adapter_scales'):
|
||||
adapter_scales = p.ip_adapter_scales
|
||||
if hasattr(p, 'ip_adapter_starts'):
|
||||
adapter_starts = p.ip_adapter_starts
|
||||
if hasattr(p, 'ip_adapter_ends'):
|
||||
adapter_ends = p.ip_adapter_ends
|
||||
if hasattr(p, 'ip_adapter_images'):
|
||||
adapter_images = p.ip_adapter_images
|
||||
adapter_images = get_images(adapter_images)
|
||||
adapter_scales = get_scales(adapter_scales, adapter_images)
|
||||
|
||||
p.ip_adapter_scales = adapter_scales.copy()
|
||||
adapter_starts = get_scales(adapter_starts, adapter_images)
|
||||
p.ip_adapter_starts = adapter_starts.copy()
|
||||
adapter_ends = get_scales(adapter_ends, adapter_images)
|
||||
p.ip_adapter_ends = adapter_ends.copy()
|
||||
# init code
|
||||
if pipe is None:
|
||||
return False
|
||||
@@ -160,6 +168,9 @@ def apply(pipe, p: processing.StableDiffusionProcessing, adapter_names=[], adapt
|
||||
ip_subfolder = 'models' if shared.sd_model_type == 'sd' else 'sdxl_models'
|
||||
try:
|
||||
pipe.load_ip_adapter([base_repo], subfolder=[ip_subfolder], weight_name=adapters)
|
||||
for i in range(len(adapter_scales)):
|
||||
if adapter_starts[i] > 0:
|
||||
adapter_scales[i] = 0.00
|
||||
pipe.set_ip_adapter_scale(adapter_scales)
|
||||
p.task_args['ip_adapter_image'] = adapter_images
|
||||
p.extra_generation_params["IP Adapter"] = ';'.join([f'{os.path.splitext(adapter)[0]}:{scale}' for adapter, scale in zip(adapter_names, adapter_scales)])
|
||||
|
||||
@@ -118,6 +118,8 @@ class StableDiffusionProcessing:
|
||||
self.ip_adapter_names = []
|
||||
self.ip_adapter_scales = [0.0]
|
||||
self.ip_adapter_images = []
|
||||
self.ip_adapter_starts = [0.0]
|
||||
self.ip_adapter_ends = [1.0]
|
||||
# hdr
|
||||
self.hdr_mode=hdr_mode
|
||||
self.hdr_brightness=hdr_brightness
|
||||
|
||||
@@ -66,6 +66,16 @@ def process_diffusers(p: processing.StableDiffusionProcessing):
|
||||
time.sleep(0.1)
|
||||
if kwargs.get('latents', None) is None:
|
||||
return kwargs
|
||||
if getattr(p, "ip_adapter_names", ["None"])[0] != "None":
|
||||
ip_adapter_scales = list(p.ip_adapter_scales)
|
||||
ip_adapter_starts = list(p.ip_adapter_starts)
|
||||
ip_adapter_ends = list(p.ip_adapter_ends)
|
||||
if any(end != 1 for end in ip_adapter_ends) or any(start != 0 for start in ip_adapter_starts):
|
||||
for i in range(len(ip_adapter_scales)):
|
||||
ip_adapter_scales[i] *= float(step >= pipe.num_timesteps * ip_adapter_starts[i])
|
||||
ip_adapter_scales[i] *= float(step <= pipe.num_timesteps * ip_adapter_ends[i])
|
||||
debug(f"Callback: IP Adapter scales={ip_adapter_scales}")
|
||||
pipe.set_ip_adapter_scale(ip_adapter_scales)
|
||||
if step != pipe.num_timesteps:
|
||||
kwargs = processing_correction.correction_callback(p, timestep, kwargs)
|
||||
if p.scheduled_prompt and 'prompt_embeds' in kwargs and 'negative_prompt_embeds' in kwargs:
|
||||
|
||||
+13
-4
@@ -43,6 +43,8 @@ class Script(scripts.Script):
|
||||
units = []
|
||||
adapters = []
|
||||
scales = []
|
||||
starts = []
|
||||
ends = []
|
||||
files = []
|
||||
galleries = []
|
||||
with gr.Row():
|
||||
@@ -52,6 +54,9 @@ class Script(scripts.Script):
|
||||
with gr.Row():
|
||||
adapters.append(gr.Dropdown(label='Adapter', choices=list(ipadapter.ADAPTERS), value='None'))
|
||||
scales.append(gr.Slider(label='Scale', minimum=0.0, maximum=1.0, step=0.01, value=0.5))
|
||||
with gr.Row():
|
||||
starts.append(gr.Slider(label='Start', minimum=0.0, maximum=1.0, step=0.1, value=0))
|
||||
ends.append(gr.Slider(label='End', minimum=0.0, maximum=1.0, step=0.1, value=1))
|
||||
with gr.Row():
|
||||
files.append(gr.File(label='Input images', file_count='multiple', file_types=['image'], type='file', interactive=True, height=100))
|
||||
with gr.Row():
|
||||
@@ -59,17 +64,21 @@ class Script(scripts.Script):
|
||||
files[i].change(fn=self.load_images, inputs=[files[i]], outputs=[galleries[i]])
|
||||
units.append(unit)
|
||||
num_adapters.change(fn=self.display_units, inputs=[num_adapters], outputs=units)
|
||||
return [num_adapters] + adapters + scales + files
|
||||
return [num_adapters] + adapters + scales + files + starts + ends
|
||||
|
||||
def process(self, p: processing.StableDiffusionProcessing, *args): # pylint: disable=arguments-differ
|
||||
if shared.backend != shared.Backend.DIFFUSERS:
|
||||
return
|
||||
args = list(args)
|
||||
units = args.pop(0)
|
||||
if p.ip_adapter_names is None:
|
||||
if p.ip_adapter_names == []:
|
||||
p.ip_adapter_names = args[:MAX_ADAPTERS][:units]
|
||||
if p.ip_adapter_scales == 0.0:
|
||||
if p.ip_adapter_scales == [0.0]:
|
||||
p.ip_adapter_scales = args[MAX_ADAPTERS:MAX_ADAPTERS*2][:units]
|
||||
if p.ip_adapter_images is None:
|
||||
if p.ip_adapter_images == []:
|
||||
p.ip_adapter_images = args[MAX_ADAPTERS*2:MAX_ADAPTERS*3][:units]
|
||||
if p.ip_adapter_starts == [0.0]:
|
||||
p.ip_adapter_starts = args[MAX_ADAPTERS*3:MAX_ADAPTERS*4][:units]
|
||||
if p.ip_adapter_ends == [1.0]:
|
||||
p.ip_adapter_ends = args[MAX_ADAPTERS*4:MAX_ADAPTERS*5][:units]
|
||||
# ipadapter.apply(shared.sd_model, p, adapter_name, scale, image) # called directly from processing.process_images_inner
|
||||
|
||||
@@ -275,6 +275,8 @@ axis_options = [
|
||||
AxisOption("[FreeU] 2nd stage skip factor", float, apply_setting('freeu_s2')),
|
||||
AxisOption("[IP adapter] Name", str, apply_field('ip_adapter_names'), cost=1.0, choices=lambda: list(ipadapter.ADAPTERS)),
|
||||
AxisOption("[IP adapter] Scale", float, apply_field('ip_adapter_scales')),
|
||||
AxisOption("[IP adapter] Starts", float, apply_field('ip_adapter_starts')),
|
||||
AxisOption("[IP adapter] Ends", float, apply_field('ip_adapter_ends')),
|
||||
]
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user