2121import re
2222import shutil
2323import sys
24+ import tempfile
2425from pathlib import Path
2526from typing import Dict , Optional , Union
2627from urllib import request
2728
28- from huggingface_hub import HfFolder , hf_hub_download , model_info
29- import huggingface_hub
29+ from huggingface_hub import get_token , hf_hub_download , model_info
3030from packaging import version
3131
32- cached_download = None
33-
3432from .. import __version__
3533from . import DIFFUSERS_DYNAMIC_MODULE_NAME , HF_MODULES_CACHE , logging
3634
4240
4341logger = logging .get_logger (__name__ ) # pylint: disable=invalid-name
4442
45- # https://github.com/huggingface/huggingface_hub/releases/tag/v0.26.0
46- # `cached_download(), url_to_filename(), filename_to_url() methods are now completely removed.
47- # From now on, you will have to use hf_hub_download() to benefit from the new cache layout.`
48- if hasattr (huggingface_hub , "__version__" ):
49- current_version = version .parse (huggingface_hub .__version__ )
50- target_version = version .parse ("0.26.0" )
51-
52- if current_version < target_version :
53- try :
54- from huggingface_hub import cached_download
55-
56- except ImportError :
57- logger .error (
58- f"huggingface_hub version { current_version } is below 0.26.0, but 'cached_download' could not be imported. It might have been removed or deprecated in this version as well."
59- )
60- else :
61- logger .error ("Could not determine huggingface_hub version. Unable to conditionally import 'cached_download'." )
62-
6343
6444def get_diffusers_versions ():
6545 url = "https://pypi.org/pypi/diffusers/json"
@@ -303,15 +283,17 @@ def get_cached_module_file(
303283 # community pipeline on GitHub
304284 github_url = COMMUNITY_PIPELINES_URL .format (revision = revision , pipeline = pretrained_model_name_or_path )
305285 try :
306- resolved_module_file = cached_download (
307- github_url ,
308- cache_dir = cache_dir ,
309- force_download = force_download ,
310- proxies = proxies ,
311- resume_download = resume_download ,
312- local_files_only = local_files_only ,
313- use_auth_token = False ,
314- )
286+ # Given that cached download has been removed, try using just urlopen
287+ fd , resolved_module_file = tempfile .mkstemp (dir = cache_dir )
288+ try :
289+ response = request .urlopen (github_url )
290+ with os .fdopen (fd , "wb" ) as f :
291+ f .write (response .read ())
292+ except Exception :
293+ os .remove (resolved_module_file )
294+ raise EnvironmentError (
295+ f"Failed to download community pipeline from { github_url } . Please check if the url is correct."
296+ )
315297 submodule = "git"
316298 module_file = pretrained_model_name_or_path + ".py"
317299 except EnvironmentError :
@@ -328,7 +310,7 @@ def get_cached_module_file(
328310 proxies = proxies ,
329311 resume_download = resume_download ,
330312 local_files_only = local_files_only ,
331- use_auth_token = use_auth_token ,
313+ token = use_auth_token ,
332314 )
333315 submodule = os .path .join ("local" , "--" .join (pretrained_model_name_or_path .split ("/" )))
334316 except EnvironmentError :
@@ -356,7 +338,7 @@ def get_cached_module_file(
356338 if isinstance (use_auth_token , str ):
357339 token = use_auth_token
358340 elif use_auth_token is True :
359- token = HfFolder . get_token ()
341+ token = get_token ()
360342 else :
361343 token = None
362344
0 commit comments