





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


_lowerCamelCase      							=   get_tests_dir('fixtures/dummy-config.json')







class       __A							(   unittest.TestCase	):




      """simple docstring"""







      def 					__snake_case      (		self):



             """simple docstring"""





             _lowerCamelCase				:  List[str]						=		0


      def 					__snake_case      (		self):



             """simple docstring"""





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


      def 					__snake_case      (		self):



             """simple docstring"""





             _lowerCamelCase				:  Union[str, Any]						=		AutoConfig.from_pretrained('''bert-base-uncased''')
             self.assertIsInstance(a__   ,				a__)


      def 					__snake_case      (		self):



             """simple docstring"""





             _lowerCamelCase				:  int						=		AutoConfig.from_pretrained(a__)
             self.assertIsInstance(a__   ,				a__)


      def 					__snake_case      (		self):



             """simple docstring"""





             _lowerCamelCase				:  Optional[int]						=		AutoConfig.from_pretrained(a__)
             self.assertIsInstance(a__   ,				a__)


      def 					__snake_case      (		self):



             """simple docstring"""





             _lowerCamelCase				:  Dict						=		AutoConfig.for_model('''roberta''')
             self.assertIsInstance(a__   ,				a__)


      def 					__snake_case      (		self):



             """simple docstring"""





             with tempfile.TemporaryDirectory() as tmp_dir:
                    # This model name contains bert and roberta, but roberta ends up being picked.
                    _lowerCamelCase				:  List[str]						=		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				:  Any						=		AutoConfig.from_pretrained(a__)
                    self.assertEqual(type(a__)   ,				a__)


      def 					__snake_case      (		self):



             """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				:  int						=		CustomConfig()
                    with tempfile.TemporaryDirectory() as tmp_dir:
                           config.save_pretrained(a__)
                           _lowerCamelCase				:  str						=		AutoConfig.from_pretrained(a__)
                           self.assertIsInstance(a__   ,				a__)

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


      def 					__snake_case      (		self):



             """simple docstring"""





             with self.assertRaisesRegex(
                 a__   ,				'''bert-base is not a local folder and is not a valid model identifier'''):
                    _lowerCamelCase				:  List[str]						=		AutoConfig.from_pretrained('''bert-base''')


      def 					__snake_case      (		self):



             """simple docstring"""





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


      def 					__snake_case      (		self):



             """simple docstring"""





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


      def 					__snake_case      (		self):



             """simple docstring"""





             with self.assertRaises(a__):
                    _lowerCamelCase				:  Dict						=		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				:  Tuple						=		AutoConfig.from_pretrained('''hf-internal-testing/test_dynamic_model'''   ,				trust_remote_code=a__)

             _lowerCamelCase				:  Dict						=		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				:  Tuple						=		AutoConfig.from_pretrained(a__   ,				trust_remote_code=a__)
             self.assertEqual(reloaded_config.__class__.__name__   ,				'''NewModelConfig''')



      def 					__snake_case      (		self):



             """simple docstring"""





             class       __A							(   lowerCamelCase__	):




                   """simple docstring"""







                   UpperCAmelCase__							=   """new-model"""

             try:
                    AutoConfig.register('''new-model'''   ,				a__)
                    # If remote code is not set, the default is to use local
                    _lowerCamelCase				:  str						=		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				:  Optional[Any]						=		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				:  Tuple						=		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"]








from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


_lowerCamelCase      							=   {
    'configuration_clipseg': [
        'CLIPSEG_PRETRAINED_CONFIG_ARCHIVE_MAP',
        'CLIPSegConfig',
        'CLIPSegTextConfig',
        'CLIPSegVisionConfig',
    ],
    'processing_clipseg': ['CLIPSegProcessor'],
}

try:
       if not is_torch_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       pass
else:
       _lowerCamelCase      							=   [
           'CLIPSEG_PRETRAINED_MODEL_ARCHIVE_LIST',
           'CLIPSegModel',
           'CLIPSegPreTrainedModel',
           'CLIPSegTextModel',
           'CLIPSegVisionModel',
           'CLIPSegForImageSegmentation',
       ]

if TYPE_CHECKING:
       from .configuration_clipseg import (
           CLIPSEG_PRETRAINED_CONFIG_ARCHIVE_MAP,
           CLIPSegConfig,
           CLIPSegTextConfig,
           CLIPSegVisionConfig,
       )
       from .processing_clipseg import CLIPSegProcessor

       try:
              if not is_torch_available():
                     raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
              pass
       else:
              from .modeling_clipseg import (
                  CLIPSEG_PRETRAINED_MODEL_ARCHIVE_LIST,
                  CLIPSegForImageSegmentation,
                  CLIPSegModel,
                  CLIPSegPreTrainedModel,
                  CLIPSegTextModel,
                  CLIPSegVisionModel,
              )

else:
       import sys

       _lowerCamelCase      							=   _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)

