22import functools
33import json
44import os
5+ import pprint
56from typing import Any , Dict , List , Optional , Union
67import transformers
78from huggingface_hub import HfApi , model_info , hf_hub_download
@@ -33,10 +34,14 @@ def _retrieve_cached_configurations() -> Dict[str, transformers.PretrainedConfig
3334 return res
3435
3536
36- def get_cached_configuration (name : str , ** kwargs ) -> Optional [transformers .PretrainedConfig ]:
37+ def get_cached_configuration (
38+ name : str , exc : bool = False , ** kwargs
39+ ) -> Optional [transformers .PretrainedConfig ]:
3740 """
3841 Returns cached configuration to avoid having to many accesses to internet.
3942 It returns None if not Cache. The list of cached models follows.
43+ If *exc* is True or if environment variable ``NOHTTP`` is defined,
44+ the function raises an exception if *name* is not found.
4045
4146 .. runpython::
4247
@@ -54,8 +59,9 @@ def get_cached_configuration(name: str, **kwargs) -> Optional[transformers.Pretr
5459 conf = copy .deepcopy (conf )
5560 update_config (conf , kwargs )
5661 return conf
57- if os .environ .get ("NOHTTP" , "" ):
58- raise AssertionError (f"Unable to find { name !r} in { sorted (cached )} " )
62+ assert not exc and not os .environ .get (
63+ "NOHTTP" , ""
64+ ), f"Unable to find { name !r} in { pprint .pformat (sorted (cached ))} "
5965 return None
6066
6167
@@ -64,6 +70,7 @@ def get_pretrained_config(
6470 trust_remote_code : bool = True ,
6571 use_preinstalled : bool = True ,
6672 subfolder : Optional [str ] = None ,
73+ use_only_preinstalled : bool = False ,
6774 ** kwargs ,
6875) -> Any :
6976 """
@@ -77,13 +84,20 @@ def get_pretrained_config(
7784 :func:`get_cached_configuration`, the cached list is mostly for
7885 unit tests
7986 :param subfolder: subfolder for the given model id
87+ :param use_only_preinstalled: if True, raises an exception if not preinstalled
8088 :param kwargs: additional kwargs
8189 :return: a configuration
8290 """
8391 if use_preinstalled :
84- conf = get_cached_configuration (model_id , subfolder = subfolder , ** kwargs )
92+ conf = get_cached_configuration (
93+ model_id , exc = use_only_preinstalled , subfolder = subfolder , ** kwargs
94+ )
8595 if conf is not None :
8696 return conf
97+ assert not use_only_preinstalled , (
98+ f"Inconsistencies: use_only_preinstalled={ use_only_preinstalled } , "
99+ f"use_preinstalled={ use_preinstalled !r} "
100+ )
87101 if subfolder :
88102 try :
89103 return transformers .AutoConfig .from_pretrained (
0 commit comments