|  | 
| 19 | 19 | import re | 
| 20 | 20 | import sys | 
| 21 | 21 | import tempfile | 
| 22 |  | -import traceback | 
| 23 | 22 | import warnings | 
| 24 | 23 | from pathlib import Path | 
| 25 | 24 | from typing import Dict, List, Optional, Union | 
|  | 
| 35 | 34 |     snapshot_download, | 
| 36 | 35 |     upload_folder, | 
| 37 | 36 | ) | 
| 38 |  | -from huggingface_hub.constants import HF_HUB_CACHE, HF_HUB_DISABLE_TELEMETRY, HF_HUB_OFFLINE | 
|  | 37 | +from huggingface_hub.constants import HF_HUB_DISABLE_TELEMETRY, HF_HUB_OFFLINE | 
| 39 | 38 | from huggingface_hub.file_download import REGEX_COMMIT_HASH | 
| 40 | 39 | from huggingface_hub.utils import ( | 
| 41 | 40 |     EntryNotFoundError, | 
| @@ -197,78 +196,6 @@ def extract_commit_hash(resolved_file: Optional[str], commit_hash: Optional[str] | 
| 197 | 196 |     return commit_hash if REGEX_COMMIT_HASH.match(commit_hash) else None | 
| 198 | 197 | 
 | 
| 199 | 198 | 
 | 
| 200 |  | -# Old default cache path, potentially to be migrated. | 
| 201 |  | -# This logic was more or less taken from `transformers`, with the following differences: | 
| 202 |  | -# - Diffusers doesn't use custom environment variables to specify the cache path. | 
| 203 |  | -# - There is no need to migrate the cache format, just move the files to the new location. | 
| 204 |  | -hf_cache_home = os.path.expanduser( | 
| 205 |  | -    os.getenv("HF_HOME", os.path.join(os.getenv("XDG_CACHE_HOME", "~/.cache"), "huggingface")) | 
| 206 |  | -) | 
| 207 |  | -old_diffusers_cache = os.path.join(hf_cache_home, "diffusers") | 
| 208 |  | - | 
| 209 |  | - | 
| 210 |  | -def move_cache(old_cache_dir: Optional[str] = None, new_cache_dir: Optional[str] = None) -> None: | 
| 211 |  | -    if new_cache_dir is None: | 
| 212 |  | -        new_cache_dir = HF_HUB_CACHE | 
| 213 |  | -    if old_cache_dir is None: | 
| 214 |  | -        old_cache_dir = old_diffusers_cache | 
| 215 |  | - | 
| 216 |  | -    old_cache_dir = Path(old_cache_dir).expanduser() | 
| 217 |  | -    new_cache_dir = Path(new_cache_dir).expanduser() | 
| 218 |  | -    for old_blob_path in old_cache_dir.glob("**/blobs/*"): | 
| 219 |  | -        if old_blob_path.is_file() and not old_blob_path.is_symlink(): | 
| 220 |  | -            new_blob_path = new_cache_dir / old_blob_path.relative_to(old_cache_dir) | 
| 221 |  | -            new_blob_path.parent.mkdir(parents=True, exist_ok=True) | 
| 222 |  | -            os.replace(old_blob_path, new_blob_path) | 
| 223 |  | -            try: | 
| 224 |  | -                os.symlink(new_blob_path, old_blob_path) | 
| 225 |  | -            except OSError: | 
| 226 |  | -                logger.warning( | 
| 227 |  | -                    "Could not create symlink between old cache and new cache. If you use an older version of diffusers again, files will be re-downloaded." | 
| 228 |  | -                ) | 
| 229 |  | -    # At this point, old_cache_dir contains symlinks to the new cache (it can still be used). | 
| 230 |  | - | 
| 231 |  | - | 
| 232 |  | -cache_version_file = os.path.join(HF_HUB_CACHE, "version_diffusers_cache.txt") | 
| 233 |  | -if not os.path.isfile(cache_version_file): | 
| 234 |  | -    cache_version = 0 | 
| 235 |  | -else: | 
| 236 |  | -    with open(cache_version_file) as f: | 
| 237 |  | -        try: | 
| 238 |  | -            cache_version = int(f.read()) | 
| 239 |  | -        except ValueError: | 
| 240 |  | -            cache_version = 0 | 
| 241 |  | - | 
| 242 |  | -if cache_version < 1: | 
| 243 |  | -    old_cache_is_not_empty = os.path.isdir(old_diffusers_cache) and len(os.listdir(old_diffusers_cache)) > 0 | 
| 244 |  | -    if old_cache_is_not_empty: | 
| 245 |  | -        logger.warning( | 
| 246 |  | -            "The cache for model files in Diffusers v0.14.0 has moved to a new location. Moving your " | 
| 247 |  | -            "existing cached models. This is a one-time operation, you can interrupt it or run it " | 
| 248 |  | -            "later by calling `diffusers.utils.hub_utils.move_cache()`." | 
| 249 |  | -        ) | 
| 250 |  | -        try: | 
| 251 |  | -            move_cache() | 
| 252 |  | -        except Exception as e: | 
| 253 |  | -            trace = "\n".join(traceback.format_tb(e.__traceback__)) | 
| 254 |  | -            logger.error( | 
| 255 |  | -                f"There was a problem when trying to move your cache:\n\n{trace}\n{e.__class__.__name__}: {e}\n\nPlease " | 
| 256 |  | -                "file an issue at https://github.com/huggingface/diffusers/issues/new/choose, copy paste this whole " | 
| 257 |  | -                "message and we will do our best to help." | 
| 258 |  | -            ) | 
| 259 |  | - | 
| 260 |  | -if cache_version < 1: | 
| 261 |  | -    try: | 
| 262 |  | -        os.makedirs(HF_HUB_CACHE, exist_ok=True) | 
| 263 |  | -        with open(cache_version_file, "w") as f: | 
| 264 |  | -            f.write("1") | 
| 265 |  | -    except Exception: | 
| 266 |  | -        logger.warning( | 
| 267 |  | -            f"There was a problem when trying to write in your cache folder ({HF_HUB_CACHE}). Please, ensure " | 
| 268 |  | -            "the directory exists and can be written to." | 
| 269 |  | -        ) | 
| 270 |  | - | 
| 271 |  | - | 
| 272 | 199 | def _add_variant(weights_name: str, variant: Optional[str] = None) -> str: | 
| 273 | 200 |     if variant is not None: | 
| 274 | 201 |         splits = weights_name.split(".") | 
|  | 
0 commit comments