From 0d6030464b3fa1857bf54b99c86ce003e5ec6905 Mon Sep 17 00:00:00 2001 From: LucipherDev Date: Fri, 27 Dec 2024 23:46:52 +0530 Subject: [PATCH] Remove dependency version constraints (resolves #1) --- models_diffusers/controlnet_svd.py | 151 +++++++++++++++++- .../refUnet_spatial_temporal_condition.py | 13 +- models_diffusers/unet_3d_blocks.py | 4 +- .../unet_spatio_temporal_condition.py | 13 +- .../unet_spatio_temporal_condition_interp.py | 13 +- pyproject.toml | 4 +- requirements.txt | 5 +- 7 files changed, 177 insertions(+), 26 deletions(-) diff --git a/models_diffusers/controlnet_svd.py b/models_diffusers/controlnet_svd.py index 7f7f11f..bd3f7b9 100644 --- a/models_diffusers/controlnet_svd.py +++ b/models_diffusers/controlnet_svd.py @@ -20,7 +20,7 @@ from torch import nn from torch.nn import functional as F from diffusers.configuration_utils import ConfigMixin, register_to_config -from diffusers.loaders import FromOriginalControlnetMixin +# from diffusers.loaders import FromOriginalControlnetMixin from diffusers.utils import BaseOutput, logging from diffusers.models.attention_processor import ( ADDED_KV_ATTENTION_PROCESSORS, @@ -35,9 +35,158 @@ from diffusers.models.modeling_utils import ModelMixin from .unet_3d_blocks import UNetMidBlockSpatioTemporal, get_down_block, get_up_block from diffusers.models import UNetSpatioTemporalConditionModel +from huggingface_hub import hf_hub_download +from io import BytesIO +import os +import requests +from pathlib import Path + +from huggingface_hub.constants import HF_HUB_OFFLINE, HF_HOME +DIFFUSERS_CACHE = os.path.join(HF_HOME, "diffusers") logger = logging.get_logger(__name__) # pylint: disable=invalid-name +class FromOriginalControlnetMixin: + @classmethod + def from_single_file(cls, pretrained_model_link_or_path, **kwargs): + r""" + Instantiate a [`ControlNetModel`] from pretrained controlnet weights saved in the original `.ckpt` or + `.safetensors` format. The pipeline is set in evaluation mode (`model.eval()`) by default. + + Parameters: + pretrained_model_link_or_path (`str` or `os.PathLike`, *optional*): + Can be either: + - A link to the `.ckpt` file (for example + `"https://huggingface.co//blob/main/.ckpt"`) on the Hub. + - A path to a *file* containing all pipeline weights. + torch_dtype (`str` or `torch.dtype`, *optional*): + Override the default `torch.dtype` and load the model with another dtype. If `"auto"` is passed, the + dtype is automatically derived from the model's weights. + force_download (`bool`, *optional*, defaults to `False`): + Whether or not to force the (re-)download of the model weights and configuration files, overriding the + cached versions if they exist. + cache_dir (`Union[str, os.PathLike]`, *optional*): + Path to a directory where a downloaded pretrained model configuration is cached if the standard cache + is not used. + resume_download (`bool`, *optional*, defaults to `False`): + Whether or not to resume downloading the model weights and configuration files. If set to `False`, any + incompletely downloaded files are deleted. + proxies (`Dict[str, str]`, *optional*): + A dictionary of proxy servers to use by protocol or endpoint, for example, `{'http': 'foo.bar:3128', + 'http://hostname': 'foo.bar:4012'}`. The proxies are used on each request. + local_files_only (`bool`, *optional*, defaults to `False`): + Whether to only load local model weights and configuration files or not. If set to True, the model + won't be downloaded from the Hub. + use_auth_token (`str` or *bool*, *optional*): + The token to use as HTTP bearer authorization for remote files. If `True`, the token generated from + `diffusers-cli login` (stored in `~/.huggingface`) is used. + revision (`str`, *optional*, defaults to `"main"`): + The specific model version to use. It can be a branch name, a tag name, a commit id, or any identifier + allowed by Git. + use_safetensors (`bool`, *optional*, defaults to `None`): + If set to `None`, the safetensors weights are downloaded if they're available **and** if the + safetensors library is installed. If set to `True`, the model is forcibly loaded from safetensors + weights. If set to `False`, safetensors weights are not loaded. + image_size (`int`, *optional*, defaults to 512): + The image size the model was trained on. Use 512 for all Stable Diffusion v1 models and the Stable + Diffusion v2 base model. Use 768 for Stable Diffusion v2. + upcast_attention (`bool`, *optional*, defaults to `None`): + Whether the attention computation should always be upcasted. + kwargs (remaining dictionary of keyword arguments, *optional*): + Can be used to overwrite load and saveable variables (for example the pipeline components of the + specific pipeline class). The overwritten components are directly passed to the pipelines `__init__` + method. See example below for more information. + + Examples: + + ```py + from diffusers import StableDiffusionControlNetPipeline, ControlNetModel + + url = "https://huggingface.co/lllyasviel/ControlNet-v1-1/blob/main/control_v11p_sd15_canny.pth" # can also be a local path + model = ControlNetModel.from_single_file(url) + + url = "https://huggingface.co/runwayml/stable-diffusion-v1-5/blob/main/v1-5-pruned.safetensors" # can also be a local path + pipe = StableDiffusionControlNetPipeline.from_single_file(url, controlnet=controlnet) + ``` + """ + # import here to avoid circular dependency + from diffusers.pipelines.stable_diffusion.convert_from_ckpt import download_controlnet_from_original_ckpt + + config_file = kwargs.pop("config_file", None) + cache_dir = kwargs.pop("cache_dir", DIFFUSERS_CACHE) + resume_download = kwargs.pop("resume_download", False) + force_download = kwargs.pop("force_download", False) + proxies = kwargs.pop("proxies", None) + local_files_only = kwargs.pop("local_files_only", HF_HUB_OFFLINE) + use_auth_token = kwargs.pop("use_auth_token", None) + num_in_channels = kwargs.pop("num_in_channels", None) + use_linear_projection = kwargs.pop("use_linear_projection", None) + revision = kwargs.pop("revision", None) + extract_ema = kwargs.pop("extract_ema", False) + image_size = kwargs.pop("image_size", None) + upcast_attention = kwargs.pop("upcast_attention", None) + + torch_dtype = kwargs.pop("torch_dtype", None) + + use_safetensors = kwargs.pop("use_safetensors", None) + + file_extension = pretrained_model_link_or_path.rsplit(".", 1)[-1] + from_safetensors = file_extension == "safetensors" + + if from_safetensors and use_safetensors is False: + raise ValueError("Make sure to install `safetensors` with `pip install safetensors`.") + + # remove huggingface url + for prefix in ["https://huggingface.co/", "huggingface.co/", "hf.co/", "https://hf.co/"]: + if pretrained_model_link_or_path.startswith(prefix): + pretrained_model_link_or_path = pretrained_model_link_or_path[len(prefix) :] + + # Code based on diffusers.pipelines.pipeline_utils.DiffusionPipeline.from_pretrained + ckpt_path = Path(pretrained_model_link_or_path) + if not ckpt_path.is_file(): + # get repo_id and (potentially nested) file path of ckpt in repo + repo_id = "/".join(ckpt_path.parts[:2]) + file_path = "/".join(ckpt_path.parts[2:]) + + if file_path.startswith("blob/"): + file_path = file_path[len("blob/") :] + + if file_path.startswith("main/"): + file_path = file_path[len("main/") :] + + pretrained_model_link_or_path = hf_hub_download( + repo_id, + filename=file_path, + cache_dir=cache_dir, + resume_download=resume_download, + proxies=proxies, + local_files_only=local_files_only, + use_auth_token=use_auth_token, + revision=revision, + force_download=force_download, + ) + + if config_file is None: + config_url = "https://raw.githubusercontent.com/lllyasviel/ControlNet/main/models/cldm_v15.yaml" + config_file = BytesIO(requests.get(config_url).content) + + image_size = image_size or 512 + + controlnet = download_controlnet_from_original_ckpt( + pretrained_model_link_or_path, + original_config_file=config_file, + image_size=image_size, + extract_ema=extract_ema, + num_in_channels=num_in_channels, + upcast_attention=upcast_attention, + from_safetensors=from_safetensors, + use_linear_projection=use_linear_projection, + ) + + if torch_dtype is not None: + controlnet.to(dtype=torch_dtype) + + return controlnet @dataclass class ControlNetOutput(BaseOutput): diff --git a/models_diffusers/refUnet_spatial_temporal_condition.py b/models_diffusers/refUnet_spatial_temporal_condition.py index 865fe1d..34ea4e9 100644 --- a/models_diffusers/refUnet_spatial_temporal_condition.py +++ b/models_diffusers/refUnet_spatial_temporal_condition.py @@ -26,9 +26,7 @@ from typing import Any, Callable, List, Optional, Tuple, Union from diffusers import __version__ from diffusers.utils import ( CONFIG_NAME, - DIFFUSERS_CACHE, FLAX_WEIGHTS_NAME, - HF_HUB_OFFLINE, MIN_PEFT_VERSION, SAFETENSORS_WEIGHTS_NAME, WEIGHTS_NAME, @@ -40,9 +38,12 @@ from diffusers.utils import ( is_torch_version, logging, ) +from huggingface_hub.constants import HF_HUB_OFFLINE, HF_HOME from diffusers.utils.hub_utils import PushToHubMixin from diffusers.models.modeling_utils import load_model_dict_into_meta, load_state_dict +DIFFUSERS_CACHE = os.path.join(HF_HOME, "diffusers") + if is_torch_version(">=", "1.9.0"): _LOW_CPU_MEM_USAGE_DEFAULT = True else: @@ -860,7 +861,7 @@ class UNetSpatioTemporalConditionModel(ModelMixin, ConfigMixin, UNet2DConditionL return_unused_kwargs=True, return_commit_hash=True, force_download=force_download, - resume_download=resume_download, + # resume_download=resume_download, proxies=proxies, local_files_only=local_files_only, use_auth_token=use_auth_token, @@ -886,7 +887,7 @@ class UNetSpatioTemporalConditionModel(ModelMixin, ConfigMixin, UNet2DConditionL weights_name=FLAX_WEIGHTS_NAME, cache_dir=cache_dir, force_download=force_download, - resume_download=resume_download, + # resume_download=resume_download, proxies=proxies, local_files_only=local_files_only, use_auth_token=use_auth_token, @@ -909,7 +910,7 @@ class UNetSpatioTemporalConditionModel(ModelMixin, ConfigMixin, UNet2DConditionL weights_name=_add_variant(SAFETENSORS_WEIGHTS_NAME, variant), cache_dir=cache_dir, force_download=force_download, - resume_download=resume_download, + # resume_download=resume_download, proxies=proxies, local_files_only=local_files_only, use_auth_token=use_auth_token, @@ -928,7 +929,7 @@ class UNetSpatioTemporalConditionModel(ModelMixin, ConfigMixin, UNet2DConditionL weights_name=_add_variant(WEIGHTS_NAME, variant), cache_dir=cache_dir, force_download=force_download, - resume_download=resume_download, + # resume_download=resume_download, proxies=proxies, local_files_only=local_files_only, use_auth_token=use_auth_token, diff --git a/models_diffusers/unet_3d_blocks.py b/models_diffusers/unet_3d_blocks.py index 238bad1..9fdb783 100644 --- a/models_diffusers/unet_3d_blocks.py +++ b/models_diffusers/unet_3d_blocks.py @@ -20,7 +20,7 @@ from torch import nn from diffusers.utils import is_torch_version from diffusers.utils.torch_utils import apply_freeu from diffusers.models.attention import Attention -from diffusers.models.dual_transformer_2d import DualTransformer2DModel +from diffusers.models.transformers.dual_transformer_2d import DualTransformer2DModel from diffusers.models.resnet import ( Downsample2D, ResnetBlock2D, @@ -28,7 +28,7 @@ from diffusers.models.resnet import ( TemporalConvLayer, Upsample2D, ) -from diffusers.models.transformer_2d import Transformer2DModel +from diffusers.models.transformers.transformer_2d import Transformer2DModel from .transformer_temporal import ( TransformerSpatioTemporalModel, TransformerTemporalModel, diff --git a/models_diffusers/unet_spatio_temporal_condition.py b/models_diffusers/unet_spatio_temporal_condition.py index 8493011..61687a7 100644 --- a/models_diffusers/unet_spatio_temporal_condition.py +++ b/models_diffusers/unet_spatio_temporal_condition.py @@ -26,9 +26,7 @@ from typing import Any, Callable, List, Optional, Tuple, Union from diffusers import __version__ from diffusers.utils import ( CONFIG_NAME, - DIFFUSERS_CACHE, FLAX_WEIGHTS_NAME, - HF_HUB_OFFLINE, MIN_PEFT_VERSION, SAFETENSORS_WEIGHTS_NAME, WEIGHTS_NAME, @@ -40,9 +38,12 @@ from diffusers.utils import ( is_torch_version, logging, ) +from huggingface_hub.constants import HF_HUB_OFFLINE, HF_HOME from diffusers.utils.hub_utils import PushToHubMixin from diffusers.models.modeling_utils import load_model_dict_into_meta, load_state_dict +DIFFUSERS_CACHE = os.path.join(HF_HOME, "diffusers") + if is_torch_version(">=", "1.9.0"): _LOW_CPU_MEM_USAGE_DEFAULT = True else: @@ -860,7 +861,7 @@ class UNetSpatioTemporalConditionModel(ModelMixin, ConfigMixin, UNet2DConditionL return_unused_kwargs=True, return_commit_hash=True, force_download=force_download, - resume_download=resume_download, + # resume_download=resume_download, proxies=proxies, local_files_only=local_files_only, use_auth_token=use_auth_token, @@ -886,7 +887,7 @@ class UNetSpatioTemporalConditionModel(ModelMixin, ConfigMixin, UNet2DConditionL weights_name=FLAX_WEIGHTS_NAME, cache_dir=cache_dir, force_download=force_download, - resume_download=resume_download, + # resume_download=resume_download, proxies=proxies, local_files_only=local_files_only, use_auth_token=use_auth_token, @@ -909,7 +910,7 @@ class UNetSpatioTemporalConditionModel(ModelMixin, ConfigMixin, UNet2DConditionL weights_name=_add_variant(SAFETENSORS_WEIGHTS_NAME, variant), cache_dir=cache_dir, force_download=force_download, - resume_download=resume_download, + # resume_download=resume_download, proxies=proxies, local_files_only=local_files_only, use_auth_token=use_auth_token, @@ -928,7 +929,7 @@ class UNetSpatioTemporalConditionModel(ModelMixin, ConfigMixin, UNet2DConditionL weights_name=_add_variant(WEIGHTS_NAME, variant), cache_dir=cache_dir, force_download=force_download, - resume_download=resume_download, + # resume_download=resume_download, proxies=proxies, local_files_only=local_files_only, use_auth_token=use_auth_token, diff --git a/models_diffusers/unet_spatio_temporal_condition_interp.py b/models_diffusers/unet_spatio_temporal_condition_interp.py index 865fe1d..34ea4e9 100644 --- a/models_diffusers/unet_spatio_temporal_condition_interp.py +++ b/models_diffusers/unet_spatio_temporal_condition_interp.py @@ -26,9 +26,7 @@ from typing import Any, Callable, List, Optional, Tuple, Union from diffusers import __version__ from diffusers.utils import ( CONFIG_NAME, - DIFFUSERS_CACHE, FLAX_WEIGHTS_NAME, - HF_HUB_OFFLINE, MIN_PEFT_VERSION, SAFETENSORS_WEIGHTS_NAME, WEIGHTS_NAME, @@ -40,9 +38,12 @@ from diffusers.utils import ( is_torch_version, logging, ) +from huggingface_hub.constants import HF_HUB_OFFLINE, HF_HOME from diffusers.utils.hub_utils import PushToHubMixin from diffusers.models.modeling_utils import load_model_dict_into_meta, load_state_dict +DIFFUSERS_CACHE = os.path.join(HF_HOME, "diffusers") + if is_torch_version(">=", "1.9.0"): _LOW_CPU_MEM_USAGE_DEFAULT = True else: @@ -860,7 +861,7 @@ class UNetSpatioTemporalConditionModel(ModelMixin, ConfigMixin, UNet2DConditionL return_unused_kwargs=True, return_commit_hash=True, force_download=force_download, - resume_download=resume_download, + # resume_download=resume_download, proxies=proxies, local_files_only=local_files_only, use_auth_token=use_auth_token, @@ -886,7 +887,7 @@ class UNetSpatioTemporalConditionModel(ModelMixin, ConfigMixin, UNet2DConditionL weights_name=FLAX_WEIGHTS_NAME, cache_dir=cache_dir, force_download=force_download, - resume_download=resume_download, + # resume_download=resume_download, proxies=proxies, local_files_only=local_files_only, use_auth_token=use_auth_token, @@ -909,7 +910,7 @@ class UNetSpatioTemporalConditionModel(ModelMixin, ConfigMixin, UNet2DConditionL weights_name=_add_variant(SAFETENSORS_WEIGHTS_NAME, variant), cache_dir=cache_dir, force_download=force_download, - resume_download=resume_download, + # resume_download=resume_download, proxies=proxies, local_files_only=local_files_only, use_auth_token=use_auth_token, @@ -928,7 +929,7 @@ class UNetSpatioTemporalConditionModel(ModelMixin, ConfigMixin, UNet2DConditionL weights_name=_add_variant(WEIGHTS_NAME, variant), cache_dir=cache_dir, force_download=force_download, - resume_download=resume_download, + # resume_download=resume_download, proxies=proxies, local_files_only=local_files_only, use_auth_token=use_auth_token, diff --git a/pyproject.toml b/pyproject.toml index 39300cd..012533d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,9 +1,9 @@ [project] name = "comfyui-anidoc" description = "ComfyUI Custom Nodes for 'AniDoc: Animation Creation Made Easier'. This approach automates line art video colorization using a novel model that aligns color information from references, ensures temporal consistency, and reduces manual effort in animation production." -version = "1.0.0" +version = "1.0.1" license = {file = "LICENSE"} -dependencies = ["diffusers==0.24.0", "huggingface_hub==0.25.2", "Pillow", "accelerate", "omegaconf", "opencv-python", "einops", "kornia", "git+https://github.com/XPixelGroup/BasicSR.git"] +dependencies = ["diffusers", "huggingface_hub", "Pillow", "accelerate", "omegaconf", "opencv-python", "einops", "kornia", "git+https://github.com/XPixelGroup/BasicSR.git"] [project.urls] Repository = "https://github.com/LucipherDev/ComfyUI-AniDoc" diff --git a/requirements.txt b/requirements.txt index a6521a0..7a18e72 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,5 @@ -diffusers==0.24.0 -huggingface_hub==0.25.2 - +diffusers +huggingface_hub Pillow accelerate omegaconf