




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

import transformers
from transformers import (
    CONFIG_MAPPING,
    FEATURE_EXTRACTOR_MAPPING,
    AutoConfig,
    AutoFeatureExtractor,
    WavaVecaConfig,
    WavaVecaFeatureExtractor,
)
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
from test_module.custom_feature_extraction import CustomFeatureExtractor  # noqa E402


lowerCamelCase				:		Any     =   get_tests_dir("fixtures")
lowerCamelCase				:		int     =   get_tests_dir("fixtures/dummy_feature_extractor_config.json")
lowerCamelCase				:		str     =   get_tests_dir("fixtures/dummy-config.json")







class   A(						unittest.TestCase   ):



 '''simple docstring'''


 def 				a__ (				self					:  Optional[Any]  )   ->	Optional[int]:

       """simple docstring"""






       lowerCamelCase_		     =    0

 def 				a__ (				self					:  Any  )   ->	Any:

       """simple docstring"""






       lowerCamelCase_		     =    AutoFeatureExtractor.from_pretrained('facebook/wav2vec2-base-960h'  )
       self.assertIsInstance(A_						,							A_  )

 def 				a__ (				self					:  Any  )   ->	Dict:

       """simple docstring"""






       lowerCamelCase_		     =    AutoFeatureExtractor.from_pretrained(A_  )
       self.assertIsInstance(A_						,							A_  )

 def 				a__ (				self					:  List[str]  )   ->	Optional[Any]:

       """simple docstring"""






       with tempfile.TemporaryDirectory() as tmpdirname:
             lowerCamelCase_		     =    WavaVecaConfig()

             # remove feature_extractor_type to make sure config.json alone is enough to load feature processor locally
             lowerCamelCase_		     =    AutoFeatureExtractor.from_pretrained(A_  ).to_dict()

             config_dict.pop('feature_extractor_type'  )
             lowerCamelCase_		     =    WavaVecaFeatureExtractor(**A_  )

             # save in new folder
             model_config.save_pretrained(A_  )
             config.save_pretrained(A_  )

             lowerCamelCase_		     =    AutoFeatureExtractor.from_pretrained(A_  )

             # make sure private variable is not incorrectly saved
             lowerCamelCase_		     =    json.loads(config.to_json_string()  )
             self.assertTrue('_processor_class' not in dict_as_saved  )

       self.assertIsInstance(A_						,							A_  )

 def 				a__ (				self					:  int  )   ->	Optional[Any]:

       """simple docstring"""






       lowerCamelCase_		     =    AutoFeatureExtractor.from_pretrained(A_  )
       self.assertIsInstance(A_						,							A_  )

 def 				a__ (				self					:  Optional[int]  )   ->	Dict:

       """simple docstring"""






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

 def 				a__ (				self					:  Any  )   ->	str:

       """simple docstring"""






       with self.assertRaisesRegex(
           A_						,							r'aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)'  ):
             lowerCamelCase_		     =    AutoFeatureExtractor.from_pretrained(A_						,							revision='aaaaaa'  )

 def 				a__ (				self					:  List[str]  )   ->	Optional[Any]:

       """simple docstring"""






       with self.assertRaisesRegex(
           A_						,							'hf-internal-testing/config-no-model does not appear to have a file named preprocessor_config.json.'						,							):
             lowerCamelCase_		     =    AutoFeatureExtractor.from_pretrained('hf-internal-testing/config-no-model'  )

 def 				a__ (				self					:  Union[str, Any]  )   ->	str:

       """simple docstring"""






       with self.assertRaises(A_  ):
             lowerCamelCase_		     =    AutoFeatureExtractor.from_pretrained(
                 'hf-internal-testing/test_dynamic_feature_extractor'  )
       # If remote code is disabled, we can't load this config.
       with self.assertRaises(A_  ):
             lowerCamelCase_		     =    AutoFeatureExtractor.from_pretrained(
                 'hf-internal-testing/test_dynamic_feature_extractor'						,							trust_remote_code=A_  )

       lowerCamelCase_		     =    AutoFeatureExtractor.from_pretrained(
           'hf-internal-testing/test_dynamic_feature_extractor'						,							trust_remote_code=A_  )
       self.assertEqual(feature_extractor.__class__.__name__						,							'NewFeatureExtractor'  )

       # Test feature extractor can be reloaded.
       with tempfile.TemporaryDirectory() as tmp_dir:
             feature_extractor.save_pretrained(A_  )
             lowerCamelCase_		     =    AutoFeatureExtractor.from_pretrained(A_						,							trust_remote_code=A_  )
       self.assertEqual(reloaded_feature_extractor.__class__.__name__						,							'NewFeatureExtractor'  )

 def 				a__ (				self					:  Union[str, Any]  )   ->	Any:

       """simple docstring"""






       try:
             AutoConfig.register('custom'						,							A_  )
             AutoFeatureExtractor.register(A_						,							A_  )
             # Trying to register something existing in the Transformers library will raise an error
             with self.assertRaises(A_  ):
                   AutoFeatureExtractor.register(A_						,							A_  )

             # Now that the config is registered, it can be used as any other config with the auto-API
             lowerCamelCase_		     =    CustomFeatureExtractor.from_pretrained(A_  )
             with tempfile.TemporaryDirectory() as tmp_dir:
                   feature_extractor.save_pretrained(A_  )
                   lowerCamelCase_		     =    AutoFeatureExtractor.from_pretrained(A_  )
                   self.assertIsInstance(A_						,							A_  )

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





 def 				a__ (				self					:  Any  )   ->	Any:

       """simple docstring"""






       class   A(						UpperCamelCase   ):



        '''simple docstring'''


        UpperCamelCase						   =						True

       try:
             AutoConfig.register('custom'						,							A_  )
             AutoFeatureExtractor.register(A_						,							A_  )
             # If remote code is not set, the default is to use local
             lowerCamelCase_		     =    AutoFeatureExtractor.from_pretrained(
                 'hf-internal-testing/test_dynamic_feature_extractor'  )
             self.assertEqual(feature_extractor.__class__.__name__						,							'NewFeatureExtractor'  )
             self.assertTrue(feature_extractor.is_local  )

             # If remote code is disabled, we load the local one.
             lowerCamelCase_		     =    AutoFeatureExtractor.from_pretrained(
                 'hf-internal-testing/test_dynamic_feature_extractor'						,							trust_remote_code=A_  )
             self.assertEqual(feature_extractor.__class__.__name__						,							'NewFeatureExtractor'  )
             self.assertTrue(feature_extractor.is_local  )

             # If remote is enabled, we load from the Hub
             lowerCamelCase_		     =    AutoFeatureExtractor.from_pretrained(
                 'hf-internal-testing/test_dynamic_feature_extractor'						,							trust_remote_code=A_  )
             self.assertEqual(feature_extractor.__class__.__name__						,							'NewFeatureExtractor'  )
             self.assertTrue(not hasattr(A_						,							'is_local'  )  )

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









from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


lowerCamelCase				:		Union[str, Any]     =   {
    "configuration_convbert": ["CONVBERT_PRETRAINED_CONFIG_ARCHIVE_MAP", "ConvBertConfig", "ConvBertOnnxConfig"],
    "tokenization_convbert": ["ConvBertTokenizer"],
}

try:
 if not is_tokenizers_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 lowerCamelCase				:		Optional[Any]     =   ["ConvBertTokenizerFast"]

try:
 if not is_torch_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 lowerCamelCase				:		Dict     =   [
     "CONVBERT_PRETRAINED_MODEL_ARCHIVE_LIST",
     "ConvBertForMaskedLM",
     "ConvBertForMultipleChoice",
     "ConvBertForQuestionAnswering",
     "ConvBertForSequenceClassification",
     "ConvBertForTokenClassification",
     "ConvBertLayer",
     "ConvBertModel",
     "ConvBertPreTrainedModel",
     "load_tf_weights_in_convbert",
 ]


try:
 if not is_tf_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 lowerCamelCase				:		Tuple     =   [
     "TF_CONVBERT_PRETRAINED_MODEL_ARCHIVE_LIST",
     "TFConvBertForMaskedLM",
     "TFConvBertForMultipleChoice",
     "TFConvBertForQuestionAnswering",
     "TFConvBertForSequenceClassification",
     "TFConvBertForTokenClassification",
     "TFConvBertLayer",
     "TFConvBertModel",
     "TFConvBertPreTrainedModel",
 ]


if TYPE_CHECKING:
 from .configuration_convbert import CONVBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, ConvBertConfig, ConvBertOnnxConfig
 from .tokenization_convbert import ConvBertTokenizer

 try:
  if not is_tokenizers_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .tokenization_convbert_fast import ConvBertTokenizerFast

 try:
  if not is_torch_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_convbert import (
      CONVBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
      ConvBertForMaskedLM,
      ConvBertForMultipleChoice,
      ConvBertForQuestionAnswering,
      ConvBertForSequenceClassification,
      ConvBertForTokenClassification,
      ConvBertLayer,
      ConvBertModel,
      ConvBertPreTrainedModel,
      load_tf_weights_in_convbert,
  )

 try:
  if not is_tf_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_tf_convbert import (
      TF_CONVBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
      TFConvBertForMaskedLM,
      TFConvBertForMultipleChoice,
      TFConvBertForQuestionAnswering,
      TFConvBertForSequenceClassification,
      TFConvBertForTokenClassification,
      TFConvBertLayer,
      TFConvBertModel,
      TFConvBertPreTrainedModel,
  )


else:
 import sys

 lowerCamelCase				:		Optional[int]     =   _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)



