File size: 10,578 Bytes
0163a2c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 | import gradio as gr
from modules import script_callbacks, scripts, shared
from modules.processing import (Processed, StableDiffusionProcessing,
StableDiffusionProcessingImg2Img)
from modules.scripts import PostprocessBatchListArgs, PostprocessImageArgs
from scripts.animatediff_cn import AnimateDiffControl
from scripts.animatediff_infv2v import AnimateDiffInfV2V
from scripts.animatediff_latent import AnimateDiffI2VLatent
from scripts.animatediff_logger import logger_animatediff as logger
from scripts.animatediff_lora import AnimateDiffLora
from scripts.animatediff_mm import mm_animatediff as motion_module
from scripts.animatediff_prompt import AnimateDiffPromptSchedule
from scripts.animatediff_output import AnimateDiffOutput
from scripts.animatediff_ui import AnimateDiffProcess, AnimateDiffUiGroup, supported_save_formats
from scripts.animatediff_infotext import update_infotext, infotext_pasted
from scripts.animatediff_xyz import patch_xyz, xyz_attrs
script_dir = scripts.basedir()
motion_module.set_script_dir(script_dir)
class AnimateDiffScript(scripts.Script):
def __init__(self):
self.lora_hacker = None
self.cfg_hacker = None
self.cn_hacker = None
self.prompt_scheduler = None
self.hacked = False
self.infotext_fields: List[Tuple[gr.components.IOComponent, str]] = []
self.paste_field_names: List[str] = []
def title(self):
return "AnimateDiff"
def show(self, is_img2img):
return scripts.AlwaysVisible
def ui(self, is_img2img):
unit = AnimateDiffUiGroup().render(
is_img2img,
motion_module.get_model_dir(),
self.infotext_fields,
self.paste_field_names
)
return (unit,)
def before_process(self, p: StableDiffusionProcessing, params: AnimateDiffProcess):
if p.is_api and isinstance(params, dict):
self.ad_params = AnimateDiffProcess(**params)
params = self.ad_params
# apply XYZ settings
params.apply_xyz()
xyz_attrs.clear()
if params.enable:
logger.info("AnimateDiff process start.")
params.set_p(p)
motion_module.inject(p.sd_model, params.model)
self.prompt_scheduler = AnimateDiffPromptSchedule()
self.lora_hacker = AnimateDiffLora(motion_module.mm.is_v2)
self.lora_hacker.hack()
self.cfg_hacker = AnimateDiffInfV2V(p, self.prompt_scheduler)
self.cfg_hacker.hack(params)
self.cn_hacker = AnimateDiffControl(p, self.prompt_scheduler)
self.cn_hacker.hack(params)
update_infotext(p, params)
self.hacked = True
elif self.hacked:
self.cn_hacker.restore()
self.cfg_hacker.restore()
self.lora_hacker.restore()
motion_module.restore(p.sd_model)
self.hacked = False
def before_process_batch(self, p: StableDiffusionProcessing, params: AnimateDiffProcess, **kwargs):
if p.is_api and isinstance(params, dict): params = self.ad_params
if params.enable and isinstance(p, StableDiffusionProcessingImg2Img) and not hasattr(p, '_animatediff_i2i_batch'):
AnimateDiffI2VLatent().randomize(p, params)
def postprocess_batch_list(self, p: StableDiffusionProcessing, pp: PostprocessBatchListArgs, params: AnimateDiffProcess, **kwargs):
if p.is_api and isinstance(params, dict): params = self.ad_params
if params.enable:
self.prompt_scheduler.save_infotext_img(p)
def postprocess_image(self, p: StableDiffusionProcessing, pp: PostprocessImageArgs, params: AnimateDiffProcess, *args):
if p.is_api and isinstance(params, dict): params = self.ad_params
if params.enable and isinstance(p, StableDiffusionProcessingImg2Img) and hasattr(p, '_animatediff_paste_to_full'):
p.paste_to = p._animatediff_paste_to_full[p.batch_index]
def postprocess(self, p: StableDiffusionProcessing, res: Processed, params: AnimateDiffProcess):
if p.is_api and isinstance(params, dict): params = self.ad_params
if params.enable:
self.prompt_scheduler.save_infotext_txt(res)
self.cn_hacker.restore()
self.cfg_hacker.restore()
self.lora_hacker.restore()
motion_module.restore(p.sd_model)
self.hacked = False
AnimateDiffOutput().output(p, res, params)
logger.info("AnimateDiff process end.")
def on_ui_settings():
section = ("animatediff", "AnimateDiff")
s3_selection =("animatediff", "AnimateDiff AWS")
shared.opts.add_option(
"animatediff_model_path",
shared.OptionInfo(
None,
"Path to save AnimateDiff motion modules",
gr.Textbox,
section=section,
),
)
shared.opts.add_option(
"animatediff_default_save_formats",
shared.OptionInfo(
["GIF", "PNG"],
"Default Save Formats",
gr.CheckboxGroup,
{"choices": supported_save_formats},
section=section
).needs_restart()
)
shared.opts.add_option(
"animatediff_optimize_gif_palette",
shared.OptionInfo(
False,
"Calculate the optimal GIF palette, improves quality significantly, removes banding",
gr.Checkbox,
section=section
)
)
shared.opts.add_option(
"animatediff_optimize_gif_gifsicle",
shared.OptionInfo(
False,
"Optimize GIFs with gifsicle, reduces file size",
gr.Checkbox,
section=section
)
)
shared.opts.add_option(
key="animatediff_mp4_crf",
info=shared.OptionInfo(
default=23,
label="MP4 Quality (CRF)",
component=gr.Slider,
component_args={
"minimum": 0,
"maximum": 51,
"step": 1},
section=section
)
.link("docs", "https://trac.ffmpeg.org/wiki/Encode/H.264#crf")
.info("17 for best quality, up to 28 for smaller size")
)
shared.opts.add_option(
key="animatediff_mp4_preset",
info=shared.OptionInfo(
default="",
label="MP4 Encoding Preset",
component=gr.Dropdown,
component_args={"choices": ["", 'veryslow', 'slower', 'slow', 'medium', 'fast', 'faster', 'veryfast', 'superfast', 'ultrafast']},
section=section,
)
.link("docs", "https://trac.ffmpeg.org/wiki/Encode/H.264#Preset")
.info("encoding speed, use the slowest you can tolerate")
)
shared.opts.add_option(
key="animatediff_mp4_tune",
info=shared.OptionInfo(
default="",
label="MP4 Tune encoding for content type",
component=gr.Dropdown,
component_args={"choices": ["", "film", "animation", "grain"]},
section=section
)
.link("docs", "https://trac.ffmpeg.org/wiki/Encode/H.264#Tune")
.info("optimize for specific content types")
)
shared.opts.add_option(
"animatediff_webp_quality",
shared.OptionInfo(
80,
"WebP Quality (if lossless=True, increases compression and CPU usage)",
gr.Slider,
{
"minimum": 1,
"maximum": 100,
"step": 1},
section=section
)
)
shared.opts.add_option(
"animatediff_webp_lossless",
shared.OptionInfo(
False,
"Save WebP in lossless format (highest quality, largest file size)",
gr.Checkbox,
section=section
)
)
shared.opts.add_option(
"animatediff_save_to_custom",
shared.OptionInfo(
False,
"Save frames to stable-diffusion-webui/outputs/{ txt|img }2img-images/AnimateDiff/{gif filename}/{date} "
"instead of stable-diffusion-webui/outputs/{ txt|img }2img-images/{date}/.",
gr.Checkbox,
section=section
)
)
shared.opts.add_option(
"animatediff_xformers",
shared.OptionInfo(
"Optimize attention layers with xformers",
"When you have --xformers in your command line args, you want AnimateDiff to ",
gr.Radio,
{"choices": ["Optimize attention layers with xformers",
"Optimize attention layers with sdp (torch >= 2.0.0 required)",
"Do not optimize attention layers"]},
section=section
)
)
shared.opts.add_option(
"animatediff_disable_lcm",
shared.OptionInfo(
False,
"Disable LCM",
gr.Checkbox,
section=section
)
)
shared.opts.add_option(
"animatediff_s3_enable",
shared.OptionInfo(
False,
"Enable to Store file in object storage that supports the s3 protocol",
gr.Checkbox,
section=s3_selection
)
)
shared.opts.add_option(
"animatediff_s3_host",
shared.OptionInfo(
None,
"S3 protocol host",
gr.Textbox,
section=s3_selection,
),
)
shared.opts.add_option(
"animatediff_s3_port",
shared.OptionInfo(
None,
"S3 protocol port",
gr.Textbox,
section=s3_selection,
),
)
shared.opts.add_option(
"animatediff_s3_access_key",
shared.OptionInfo(
None,
"S3 protocol access_key",
gr.Textbox,
section=s3_selection,
),
)
shared.opts.add_option(
"animatediff_s3_secret_key",
shared.OptionInfo(
None,
"S3 protocol secret_key",
gr.Textbox,
section=s3_selection,
),
)
shared.opts.add_option(
"animatediff_s3_storge_bucket",
shared.OptionInfo(
None,
"Bucket for file storage",
gr.Textbox,
section=s3_selection,
),
)
patch_xyz()
script_callbacks.on_ui_settings(on_ui_settings)
script_callbacks.on_after_component(AnimateDiffUiGroup.on_after_component)
script_callbacks.on_before_ui(AnimateDiffUiGroup.on_before_ui)
script_callbacks.on_infotext_pasted(infotext_pasted)
|