

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


__A			:					Tuple						=			get_tests_dir('fixtures')
__A			:					int						=			get_tests_dir('fixtures/dummy_feature_extractor_config.json')
__A			:					Union[str, Any]						=			get_tests_dir('fixtures/dummy-config.json')





class 			_SCREAMING_SNAKE_CASE (   unittest.TestCase		):







   '''simple docstring'''




   def      _snake_case		(       self					:			List[str]			):
       SCREAMING_SNAKE_CASE										=   0





   def      _snake_case		(       self					:			Optional[int]			):
       SCREAMING_SNAKE_CASE										=   AutoFeatureExtractor.from_pretrained("facebook/wav2vec2-base-960h"			)
       self.assertIsInstance(__lowerCamelCase     , __lowerCamelCase			)





   def      _snake_case		(       self					:			Dict			):
       SCREAMING_SNAKE_CASE										=   AutoFeatureExtractor.from_pretrained(__lowerCamelCase			)
       self.assertIsInstance(__lowerCamelCase     , __lowerCamelCase			)





   def      _snake_case		(       self					:			Any			):
       with tempfile.TemporaryDirectory() as tmpdirname:
           SCREAMING_SNAKE_CASE										=   WavaVecaConfig()

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

           config_dict.pop("feature_extractor_type"			)
           SCREAMING_SNAKE_CASE										=   WavaVecaFeatureExtractor(**__lowerCamelCase			)

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

           SCREAMING_SNAKE_CASE										=   AutoFeatureExtractor.from_pretrained(__lowerCamelCase			)

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

       self.assertIsInstance(__lowerCamelCase     , __lowerCamelCase			)





   def      _snake_case		(       self					:			Dict			):
       SCREAMING_SNAKE_CASE										=   AutoFeatureExtractor.from_pretrained(__lowerCamelCase			)
       self.assertIsInstance(__lowerCamelCase     , __lowerCamelCase			)





   def      _snake_case		(       self					:			Union[str, Any]			):
       with self.assertRaisesRegex(
           __lowerCamelCase     , "bert-base is not a local folder and is not a valid model identifier"			):
           SCREAMING_SNAKE_CASE										=   AutoFeatureExtractor.from_pretrained("bert-base"			)





   def      _snake_case		(       self					:			Any			):
       with self.assertRaisesRegex(
           __lowerCamelCase     , r"aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)"			):
           SCREAMING_SNAKE_CASE										=   AutoFeatureExtractor.from_pretrained(__lowerCamelCase     , revision="aaaaaa"			)





   def      _snake_case		(       self					:			str			):
       with self.assertRaisesRegex(
           __lowerCamelCase     , "hf-internal-testing/config-no-model does not appear to have a file named preprocessor_config.json."     , ):
           SCREAMING_SNAKE_CASE										=   AutoFeatureExtractor.from_pretrained("hf-internal-testing/config-no-model"			)





   def      _snake_case		(       self					:			int			):
       # If remote code is not set, we will time out when asking whether to load the model.
       with self.assertRaises(__lowerCamelCase			):
           SCREAMING_SNAKE_CASE										=   AutoFeatureExtractor.from_pretrained(
               "hf-internal-testing/test_dynamic_feature_extractor"			)
       # If remote code is disabled, we can't load this config.
       with self.assertRaises(__lowerCamelCase			):
           SCREAMING_SNAKE_CASE										=   AutoFeatureExtractor.from_pretrained(
               "hf-internal-testing/test_dynamic_feature_extractor"     , trust_remote_code=__lowerCamelCase			)

       SCREAMING_SNAKE_CASE										=   AutoFeatureExtractor.from_pretrained(
           "hf-internal-testing/test_dynamic_feature_extractor"     , trust_remote_code=__lowerCamelCase			)
       self.assertEqual(feature_extractor.__class__.__name__     , "NewFeatureExtractor"			)

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





   def      _snake_case		(       self					:			Optional[Any]			):
       try:
           AutoConfig.register("custom"     , __lowerCamelCase			)
           AutoFeatureExtractor.register(__lowerCamelCase     , __lowerCamelCase			)
           # Trying to register something existing in the Transformers library will raise an error
           with self.assertRaises(__lowerCamelCase			):
               AutoFeatureExtractor.register(__lowerCamelCase     , __lowerCamelCase			)

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

       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      _snake_case		(       self					:			Dict			):





       class 			_SCREAMING_SNAKE_CASE (   __snake_case		):







          '''simple docstring'''




          lowerCamelCase__				=      True

       try:
           AutoConfig.register("custom"     , __lowerCamelCase			)
           AutoFeatureExtractor.register(__lowerCamelCase     , __lowerCamelCase			)
           # If remote code is not set, the default is to use local
           SCREAMING_SNAKE_CASE										=   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.
           SCREAMING_SNAKE_CASE										=   AutoFeatureExtractor.from_pretrained(
               "hf-internal-testing/test_dynamic_feature_extractor"     , trust_remote_code=__lowerCamelCase			)
           self.assertEqual(feature_extractor.__class__.__name__     , "NewFeatureExtractor"			)
           self.assertTrue(feature_extractor.is_local			)

           # If remote is enabled, we load from the Hub
           SCREAMING_SNAKE_CASE										=   AutoFeatureExtractor.from_pretrained(
               "hf-internal-testing/test_dynamic_feature_extractor"     , trust_remote_code=__lowerCamelCase			)
           self.assertEqual(feature_extractor.__class__.__name__     , "NewFeatureExtractor"			)
           self.assertTrue(not hasattr(__lowerCamelCase     , "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]



"""simple docstring"""



from typing import TYPE_CHECKING

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


lowercase_							=							{
    'configuration_mobilebert': [
        'MOBILEBERT_PRETRAINED_CONFIG_ARCHIVE_MAP',
        'MobileBertConfig',
        'MobileBertOnnxConfig',
    ],
    'tokenization_mobilebert': ['MobileBertTokenizer'],
}

try:
		if not is_tokenizers_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		lowercase_							=							['MobileBertTokenizerFast']

try:
		if not is_torch_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		lowercase_							=							[
		    'MOBILEBERT_PRETRAINED_MODEL_ARCHIVE_LIST',
		    'MobileBertForMaskedLM',
		    'MobileBertForMultipleChoice',
		    'MobileBertForNextSentencePrediction',
		    'MobileBertForPreTraining',
		    'MobileBertForQuestionAnswering',
		    'MobileBertForSequenceClassification',
		    'MobileBertForTokenClassification',
		    'MobileBertLayer',
		    'MobileBertModel',
		    'MobileBertPreTrainedModel',
		    'load_tf_weights_in_mobilebert',
		]

try:
		if not is_tf_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		lowercase_							=							[
		    'TF_MOBILEBERT_PRETRAINED_MODEL_ARCHIVE_LIST',
		    'TFMobileBertForMaskedLM',
		    'TFMobileBertForMultipleChoice',
		    'TFMobileBertForNextSentencePrediction',
		    'TFMobileBertForPreTraining',
		    'TFMobileBertForQuestionAnswering',
		    'TFMobileBertForSequenceClassification',
		    'TFMobileBertForTokenClassification',
		    'TFMobileBertMainLayer',
		    'TFMobileBertModel',
		    'TFMobileBertPreTrainedModel',
		]


if TYPE_CHECKING:
		from .configuration_mobilebert import (
		    MOBILEBERT_PRETRAINED_CONFIG_ARCHIVE_MAP,
		    MobileBertConfig,
		    MobileBertOnnxConfig,
		)
		from .tokenization_mobilebert import MobileBertTokenizer

		try:
				if not is_tokenizers_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .tokenization_mobilebert_fast import MobileBertTokenizerFast

		try:
				if not is_torch_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_mobilebert import (
				    MOBILEBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
				    MobileBertForMaskedLM,
				    MobileBertForMultipleChoice,
				    MobileBertForNextSentencePrediction,
				    MobileBertForPreTraining,
				    MobileBertForQuestionAnswering,
				    MobileBertForSequenceClassification,
				    MobileBertForTokenClassification,
				    MobileBertLayer,
				    MobileBertModel,
				    MobileBertPreTrainedModel,
				    load_tf_weights_in_mobilebert,
				)

		try:
				if not is_tf_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_tf_mobilebert import (
				    TF_MOBILEBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
				    TFMobileBertForMaskedLM,
				    TFMobileBertForMultipleChoice,
				    TFMobileBertForNextSentencePrediction,
				    TFMobileBertForPreTraining,
				    TFMobileBertForQuestionAnswering,
				    TFMobileBertForSequenceClassification,
				    TFMobileBertForTokenClassification,
				    TFMobileBertMainLayer,
				    TFMobileBertModel,
				    TFMobileBertPreTrainedModel,
				)

else:
		import sys

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