2525
2626from packaging import version
2727
28- from requests import HTTPError
29-
3028from . import __version__
3129from .dynamic_module_utils import custom_object_save
32- from .utils import (
33- CONFIG_NAME ,
34- HUGGINGFACE_CO_RESOLVE_ENDPOINT ,
35- EntryNotFoundError ,
36- PushToHubMixin ,
37- RepositoryNotFoundError ,
38- RevisionNotFoundError ,
39- cached_path ,
40- copy_func ,
41- hf_bucket_url ,
42- is_offline_mode ,
43- is_remote_url ,
44- is_torch_available ,
45- logging ,
46- )
30+ from .utils import CONFIG_NAME , PushToHubMixin , cached_file , copy_func , is_torch_available , logging
4731
4832
4933logger = logging .get_logger (__name__ )
@@ -591,77 +575,43 @@ def _get_config_dict(
591575 if from_pipeline is not None :
592576 user_agent ["using_pipeline" ] = from_pipeline
593577
594- if is_offline_mode () and not local_files_only :
595- logger .info ("Offline mode: forcing local_files_only=True" )
596- local_files_only = True
597-
598578 pretrained_model_name_or_path = str (pretrained_model_name_or_path )
599- if os .path .isfile (os .path .join (subfolder , pretrained_model_name_or_path )) or is_remote_url (
600- pretrained_model_name_or_path
601- ):
602- config_file = pretrained_model_name_or_path
579+
580+ is_local = os .path .isdir (pretrained_model_name_or_path )
581+ if os .path .isfile (os .path .join (subfolder , pretrained_model_name_or_path )):
582+ # Soecial case when pretrained_model_name_or_path is a local file
583+ resolved_config_file = pretrained_model_name_or_path
584+ is_local = True
603585 else :
604586 configuration_file = kwargs .pop ("_configuration_file" , CONFIG_NAME )
605587
606- if os .path .isdir (os .path .join (pretrained_model_name_or_path , subfolder )):
607- config_file = os .path .join (pretrained_model_name_or_path , subfolder , configuration_file )
608- else :
609- config_file = hf_bucket_url (
588+ try :
589+ # Load from local folder or from cache or download from model Hub and cache
590+ resolved_config_file = cached_file (
610591 pretrained_model_name_or_path ,
611- filename = configuration_file ,
592+ configuration_file ,
593+ cache_dir = cache_dir ,
594+ force_download = force_download ,
595+ proxies = proxies ,
596+ resume_download = resume_download ,
597+ local_files_only = local_files_only ,
598+ use_auth_token = use_auth_token ,
599+ user_agent = user_agent ,
612600 revision = revision ,
613- subfolder = subfolder if len (subfolder ) > 0 else None ,
614- mirror = None ,
601+ subfolder = subfolder ,
602+ )
603+ except EnvironmentError :
604+ # Raise any environment error raise by `cached_file`. It will have a helpful error message adapted to
605+ # the original exception.
606+ raise
607+ except Exception :
608+ # For any other exception, we throw a generic error.
609+ raise EnvironmentError (
610+ f"Can't load the configuration of '{ pretrained_model_name_or_path } '. If you were trying to load it"
611+ " from 'https://huggingface.co/models', make sure you don't have a local directory with the same"
612+ f" name. Otherwise, make sure '{ pretrained_model_name_or_path } ' is the correct path to a directory"
613+ f" containing a { configuration_file } file"
615614 )
616-
617- try :
618- # Load from URL or cache if already cached
619- resolved_config_file = cached_path (
620- config_file ,
621- cache_dir = cache_dir ,
622- force_download = force_download ,
623- proxies = proxies ,
624- resume_download = resume_download ,
625- local_files_only = local_files_only ,
626- use_auth_token = use_auth_token ,
627- user_agent = user_agent ,
628- )
629-
630- except RepositoryNotFoundError :
631- raise EnvironmentError (
632- f"{ pretrained_model_name_or_path } is not a local folder and is not a valid model identifier listed on "
633- "'https://huggingface.co/models'\n If this is a private repository, make sure to pass a token having "
634- "permission to this repo with `use_auth_token` or log in with `huggingface-cli login` and pass "
635- "`use_auth_token=True`."
636- )
637- except RevisionNotFoundError :
638- raise EnvironmentError (
639- f"{ revision } is not a valid git identifier (branch name, tag name or commit id) that exists for this "
640- f"model name. Check the model page at 'https://huggingface.co/{ pretrained_model_name_or_path } ' for "
641- "available revisions."
642- )
643- except EntryNotFoundError :
644- raise EnvironmentError (
645- f"{ pretrained_model_name_or_path } does not appear to have a file named { configuration_file } ."
646- )
647- except HTTPError as err :
648- raise EnvironmentError (
649- f"There was a specific connection error when trying to load { pretrained_model_name_or_path } :\n { err } "
650- )
651- except ValueError :
652- raise EnvironmentError (
653- f"We couldn't connect to '{ HUGGINGFACE_CO_RESOLVE_ENDPOINT } ' to load this model, couldn't find it in"
654- f" the cached files and it looks like { pretrained_model_name_or_path } is not the path to a directory"
655- f" containing a { configuration_file } file.\n Checkout your internet connection or see how to run the"
656- " library in offline mode at 'https://huggingface.co/docs/transformers/installation#offline-mode'."
657- )
658- except EnvironmentError :
659- raise EnvironmentError (
660- f"Can't load config for '{ pretrained_model_name_or_path } '. If you were trying to load it from "
661- "'https://huggingface.co/models', make sure you don't have a local directory with the same name. "
662- f"Otherwise, make sure '{ pretrained_model_name_or_path } ' is the correct path to a directory "
663- f"containing a { configuration_file } file"
664- )
665615
666616 try :
667617 # Load config dict
@@ -671,10 +621,10 @@ def _get_config_dict(
671621 f"It looks like the config file at '{ resolved_config_file } ' is not a valid JSON file."
672622 )
673623
674- if resolved_config_file == config_file :
675- logger .info (f"loading configuration file { config_file } " )
624+ if is_local :
625+ logger .info (f"loading configuration file { resolved_config_file } " )
676626 else :
677- logger .info (f"loading configuration file { config_file } from cache at { resolved_config_file } " )
627+ logger .info (f"loading configuration file { configuration_file } from cache at { resolved_config_file } " )
678628
679629 return config_dict , kwargs
680630
0 commit comments