'''simple docstring'''




import importlib
import json
import os
import sys
import tempfile
import unittest
from pathlib import Path

import transformers
import transformers.models.auto
from transformers.models.auto.configuration_auto import CONFIG_MAPPING, AutoConfig
from transformers.models.bert.configuration_bert import BertConfig
from transformers.models.roberta.configuration_roberta import RobertaConfig
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, get_tests_dir


sys.path.append(str(Path(__file__).parent.parent.parent.parent / "utils"))

from test_module.custom_configuration import CustomConfig  # noqa E402


__UpperCAmelCase												=get_tests_dir("fixtures/dummy-config.json")




class    a__				(       unittest.TestCase			):






     def        SCREAMING_SNAKE_CASE__       (			self		:		Tuple   ):





         """simple docstring"""



         __lowerCamelCase          =  0






     def        SCREAMING_SNAKE_CASE__       (			self		:		Tuple   ):





         """simple docstring"""



         self.assertIsNotNone(transformers.models.auto.__spec__   )
         self.assertIsNotNone(importlib.util.find_spec('''transformers.models.auto'''   )   )






     def        SCREAMING_SNAKE_CASE__       (			self		:		Any   ):





         """simple docstring"""



         __lowerCamelCase          =  AutoConfig.from_pretrained('''bert-base-uncased'''   )
         self.assertIsInstance(a  ,    a   )






     def        SCREAMING_SNAKE_CASE__       (			self		:		Tuple   ):





         """simple docstring"""



         __lowerCamelCase          =  AutoConfig.from_pretrained(a   )
         self.assertIsInstance(a  ,    a   )






     def        SCREAMING_SNAKE_CASE__       (			self		:		Union[str, Any]   ):





         """simple docstring"""



         __lowerCamelCase          =  AutoConfig.from_pretrained(a   )
         self.assertIsInstance(a  ,    a   )






     def        SCREAMING_SNAKE_CASE__       (			self		:		str   ):





         """simple docstring"""



         __lowerCamelCase          =  AutoConfig.for_model('''roberta'''   )
         self.assertIsInstance(a  ,    a   )






     def        SCREAMING_SNAKE_CASE__       (			self		:		int   ):





         """simple docstring"""



         with tempfile.TemporaryDirectory() as tmp_dir:
             # This model name contains bert and roberta, but roberta ends up being picked.
             __lowerCamelCase          =  os.path.join(a  ,    '''fake-roberta'''   )
             os.makedirs(a  ,    exist_ok=a   )
             with open(os.path.join(a  ,    '''config.json'''   )  ,    '''w'''   ) as f:
                 f.write(json.dumps({}   )   )
             __lowerCamelCase          =  AutoConfig.from_pretrained(a   )
             self.assertEqual(type(a   )  ,    a   )






     def        SCREAMING_SNAKE_CASE__       (			self		:		str   ):





         """simple docstring"""



         try:
             AutoConfig.register('''custom'''  ,    a   )
             # Wrong model type will raise an error
             with self.assertRaises(a   ):
                 AutoConfig.register('''model'''  ,    a   )
             # Trying to register something existing in the Transformers library will raise an error
             with self.assertRaises(a   ):
                 AutoConfig.register('''bert'''  ,    a   )

             # Now that the config is registered, it can be used as any other config with the auto-API
             __lowerCamelCase          =  CustomConfig()
             with tempfile.TemporaryDirectory() as tmp_dir:
                 config.save_pretrained(a   )
                 __lowerCamelCase          =  AutoConfig.from_pretrained(a   )
                 self.assertIsInstance(a  ,    a   )

         finally:
             if "custom" in CONFIG_MAPPING._extra_content:
                 del CONFIG_MAPPING._extra_content["custom"]






     def        SCREAMING_SNAKE_CASE__       (			self		:		List[Any]   ):





         """simple docstring"""



         with self.assertRaisesRegex(
             a  ,    '''bert-base is not a local folder and is not a valid model identifier'''   ):
             __lowerCamelCase          =  AutoConfig.from_pretrained('''bert-base'''   )






     def        SCREAMING_SNAKE_CASE__       (			self		:		int   ):





         """simple docstring"""



         with self.assertRaisesRegex(
             a  ,    R'''aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)'''   ):
             __lowerCamelCase          =  AutoConfig.from_pretrained(a  ,    revision='''aaaaaa'''   )






     def        SCREAMING_SNAKE_CASE__       (			self		:		str   ):





         """simple docstring"""



         with self.assertRaisesRegex(
             a  ,    '''hf-internal-testing/no-config-test-repo does not appear to have a file named config.json.'''  ,    ):
             __lowerCamelCase          =  AutoConfig.from_pretrained('''hf-internal-testing/no-config-test-repo'''   )






     def        SCREAMING_SNAKE_CASE__       (			self		:		Optional[Any]   ):





         """simple docstring"""



         with self.assertRaises(a   ):
             __lowerCamelCase          =  AutoConfig.from_pretrained('''hf-internal-testing/test_dynamic_model'''   )
         # If remote code is disabled, we can't load this config.
         with self.assertRaises(a   ):
             __lowerCamelCase          =  AutoConfig.from_pretrained('''hf-internal-testing/test_dynamic_model'''  ,    trust_remote_code=a   )

         __lowerCamelCase          =  AutoConfig.from_pretrained('''hf-internal-testing/test_dynamic_model'''  ,    trust_remote_code=a   )
         self.assertEqual(config.__class__.__name__  ,    '''NewModelConfig'''   )

         # Test config can be reloaded.
         with tempfile.TemporaryDirectory() as tmp_dir:
             config.save_pretrained(a   )
             __lowerCamelCase          =  AutoConfig.from_pretrained(a  ,    trust_remote_code=a   )
         self.assertEqual(reloaded_config.__class__.__name__  ,    '''NewModelConfig'''   )






     def        SCREAMING_SNAKE_CASE__       (			self		:		Optional[int]   ):





         """simple docstring"""



         class    a__				(       UpperCAmelCase__			):
              lowerCamelCase  :					Optional[int]							="new-model"

         try:
             AutoConfig.register('''new-model'''  ,    a   )
             # If remote code is not set, the default is to use local
             __lowerCamelCase          =  AutoConfig.from_pretrained('''hf-internal-testing/test_dynamic_model'''   )
             self.assertEqual(config.__class__.__name__  ,    '''NewModelConfigLocal'''   )

             # If remote code is disabled, we load the local one.
             __lowerCamelCase          =  AutoConfig.from_pretrained('''hf-internal-testing/test_dynamic_model'''  ,    trust_remote_code=a   )
             self.assertEqual(config.__class__.__name__  ,    '''NewModelConfigLocal'''   )

             # If remote is enabled, we load from the Hub
             __lowerCamelCase          =  AutoConfig.from_pretrained('''hf-internal-testing/test_dynamic_model'''  ,    trust_remote_code=a   )
             self.assertEqual(config.__class__.__name__  ,    '''NewModelConfig'''   )

         finally:
             if "new-model" in CONFIG_MAPPING._extra_content:
                 del CONFIG_MAPPING._extra_content["new-model"]

'''simple docstring'''




def __lowerCAmelCase			(							UpperCamelCase__    )     ->   str:
    return "".join(chr(ord(UpperCamelCase__    ) - 32    ) if '''a''' <= char <= '''z''' else char for char in word    )


if __name__ == "__main__":
  from doctest import testmod

  testmod()
