@@ -1248,6 +1248,7 @@ def download(cls, pretrained_model_name, **kwargs) -> Union[str, os.PathLike]:
1248
1248
allow_patterns = None
1249
1249
ignore_patterns = None
1250
1250
1251
+ model_info_call_error : Optional [Exception ] = None
1251
1252
if not local_files_only :
1252
1253
try :
1253
1254
info = model_info (
@@ -1258,6 +1259,7 @@ def download(cls, pretrained_model_name, **kwargs) -> Union[str, os.PathLike]:
1258
1259
except HTTPError as e :
1259
1260
logger .warn (f"Couldn't connect to the Hub: { e } .\n Will try to load from local cache." )
1260
1261
local_files_only = True
1262
+ model_info_call_error = e # save error to reraise it if model is not cached locally
1261
1263
1262
1264
if not local_files_only :
1263
1265
config_file = hf_hub_download (
@@ -1389,20 +1391,34 @@ def download(cls, pretrained_model_name, **kwargs) -> Union[str, os.PathLike]:
1389
1391
user_agent ["custom_pipeline" ] = custom_pipeline
1390
1392
1391
1393
# download all allow_patterns - ignore_patterns
1392
- cached_folder = snapshot_download (
1393
- pretrained_model_name ,
1394
- cache_dir = cache_dir ,
1395
- resume_download = resume_download ,
1396
- proxies = proxies ,
1397
- local_files_only = local_files_only ,
1398
- use_auth_token = use_auth_token ,
1399
- revision = revision ,
1400
- allow_patterns = allow_patterns ,
1401
- ignore_patterns = ignore_patterns ,
1402
- user_agent = user_agent ,
1403
- )
1404
-
1405
- return cached_folder
1394
+ try :
1395
+ return snapshot_download (
1396
+ pretrained_model_name ,
1397
+ cache_dir = cache_dir ,
1398
+ resume_download = resume_download ,
1399
+ proxies = proxies ,
1400
+ local_files_only = local_files_only ,
1401
+ use_auth_token = use_auth_token ,
1402
+ revision = revision ,
1403
+ allow_patterns = allow_patterns ,
1404
+ ignore_patterns = ignore_patterns ,
1405
+ user_agent = user_agent ,
1406
+ )
1407
+ except FileNotFoundError :
1408
+ # Means we tried to load pipeline with `local_files_only=True` but the files have not been found in local cache.
1409
+ # This can happen in two cases:
1410
+ # 1. If the user passed `local_files_only=True` => we raise the error directly
1411
+ # 2. If we forced `local_files_only=True` when `model_info` failed => we raise the initial error
1412
+ if model_info_call_error is None :
1413
+ # 1. user passed `local_files_only=True`
1414
+ raise
1415
+ else :
1416
+ # 2. we forced `local_files_only=True` when `model_info` failed
1417
+ raise EnvironmentError (
1418
+ f"Cannot load model { pretrained_model_name } : model is not cached locally and an error occured"
1419
+ " while trying to fetch metadata from the Hub. Please check out the root cause in the stacktrace"
1420
+ " above."
1421
+ ) from model_info_call_error
1406
1422
1407
1423
@staticmethod
1408
1424
def _get_signature_keys (obj ):
0 commit comments