From c4c33a86089cc734fefd6f16eddd01de51655f67 Mon Sep 17 00:00:00 2001 From: Wauplin Date: Mon, 24 Jul 2023 13:28:16 +0200 Subject: [PATCH 1/2] Raise initial HTTPError if pipeline is not cached locally --- src/diffusers/pipelines/pipeline_utils.py | 44 +++++++++++++++-------- 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/src/diffusers/pipelines/pipeline_utils.py b/src/diffusers/pipelines/pipeline_utils.py index 30ef1a7daa41..eae07adacc27 100644 --- a/src/diffusers/pipelines/pipeline_utils.py +++ b/src/diffusers/pipelines/pipeline_utils.py @@ -1248,6 +1248,7 @@ def download(cls, pretrained_model_name, **kwargs) -> Union[str, os.PathLike]: allow_patterns = None ignore_patterns = None + model_info_call_error: Optional[Exception] = None if not local_files_only: try: info = model_info( @@ -1258,6 +1259,7 @@ def download(cls, pretrained_model_name, **kwargs) -> Union[str, os.PathLike]: except HTTPError as e: logger.warn(f"Couldn't connect to the Hub: {e}.\nWill try to load from local cache.") local_files_only = True + model_info_call_error = e # save error to reraise it if model is not cached locally if not local_files_only: config_file = hf_hub_download( @@ -1389,20 +1391,34 @@ def download(cls, pretrained_model_name, **kwargs) -> Union[str, os.PathLike]: user_agent["custom_pipeline"] = custom_pipeline # download all allow_patterns - ignore_patterns - cached_folder = snapshot_download( - pretrained_model_name, - cache_dir=cache_dir, - resume_download=resume_download, - proxies=proxies, - local_files_only=local_files_only, - use_auth_token=use_auth_token, - revision=revision, - allow_patterns=allow_patterns, - ignore_patterns=ignore_patterns, - user_agent=user_agent, - ) - - return cached_folder + try: + return snapshot_download( + pretrained_model_name, + cache_dir=cache_dir, + resume_download=resume_download, + proxies=proxies, + local_files_only=local_files_only, + use_auth_token=use_auth_token, + revision=revision, + allow_patterns=allow_patterns, + ignore_patterns=ignore_patterns, + user_agent=user_agent, + ) + except FileNotFoundError as e: + # Means we tried to load pipeline with `local_files_only=True` but the files have not been found in local cache. + # This can happen in two cases: + # 1. If the user passed `local_files_only=True` => we raise the error directly + # 2. If we forced `local_files_only=True` when `model_info` failed => we raise the initial error + if model_info_call_error is None: + # 1. user passed `local_files_only=True` + raise + else: + # 2. we forced `local_files_only=True` when `model_info` failed + raise EnvironmentError( + f"Cannot load model {pretrained_model_name}: model is not cached locally and an error occured" + " while trying to fetch metadata from the Hub. Please check out the root cause in the stacktrace" + " above." + ) from model_info_call_error @staticmethod def _get_signature_keys(obj): From f6e4c308792cb400ac920d8b3642e6faf8efa003 Mon Sep 17 00:00:00 2001 From: Wauplin Date: Mon, 24 Jul 2023 13:45:45 +0200 Subject: [PATCH 2/2] make style --- src/diffusers/pipelines/pipeline_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/diffusers/pipelines/pipeline_utils.py b/src/diffusers/pipelines/pipeline_utils.py index eae07adacc27..b5397c2daaf8 100644 --- a/src/diffusers/pipelines/pipeline_utils.py +++ b/src/diffusers/pipelines/pipeline_utils.py @@ -1404,7 +1404,7 @@ def download(cls, pretrained_model_name, **kwargs) -> Union[str, os.PathLike]: ignore_patterns=ignore_patterns, user_agent=user_agent, ) - except FileNotFoundError as e: + except FileNotFoundError: # Means we tried to load pipeline with `local_files_only=True` but the files have not been found in local cache. # This can happen in two cases: # 1. If the user passed `local_files_only=True` => we raise the error directly