




'''simple docstring'''




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_      :		Union[str, Any]							      =       get_tests_dir("""fixtures""")
a_      :		Union[str, Any]							      =       get_tests_dir("""fixtures/dummy_feature_extractor_config.json""")
a_      :		int							      =       get_tests_dir("""fixtures/dummy-config.json""")


class   __UpperCamelCase       ( unittest.TestCase				):






       def      lowercase__     (							self  ):



              """simple docstring"""







              lowerCamelCase_							=0






       def      lowercase__     (							self  ):



              """simple docstring"""







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






       def      lowercase__     (							self  ):



              """simple docstring"""







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






       def      lowercase__     (							self  ):



              """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(UpperCamelCase__  ).to_dict()

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

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

                     lowerCamelCase_							=AutoFeatureExtractor.from_pretrained(UpperCamelCase__  )

                     # 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(UpperCamelCase__,		UpperCamelCase__  )






       def      lowercase__     (							self  ):



              """simple docstring"""







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






       def      lowercase__     (							self  ):



              """simple docstring"""







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






       def      lowercase__     (							self  ):



              """simple docstring"""







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






       def      lowercase__     (							self  ):



              """simple docstring"""







              with self.assertRaisesRegex(
                  UpperCamelCase__,		'''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      lowercase__     (							self  ):



              """simple docstring"""







              with self.assertRaises(UpperCamelCase__  ):
                     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(UpperCamelCase__  ):
                     lowerCamelCase_							=AutoFeatureExtractor.from_pretrained(
                         '''hf-internal-testing/test_dynamic_feature_extractor''',		trust_remote_code=UpperCamelCase__  )

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

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






       def      lowercase__     (							self  ):



              """simple docstring"""







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

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

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



              """simple docstring"""







              class   __UpperCamelCase       ( _A				):
                     lowercase	:    int      						=True

              try:
                     AutoConfig.register('''custom''',		UpperCamelCase__  )
                     AutoFeatureExtractor.register(UpperCamelCase__,		UpperCamelCase__  )
                     # 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=UpperCamelCase__  )
                     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=UpperCamelCase__  )
                     self.assertEqual(feature_extractor.__class__.__name__,		'''NewFeatureExtractor'''  )
                     self.assertTrue(not hasattr(UpperCamelCase__,		'''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_torch_available


__lowerCAmelCase			:     Any           =   {
    'configuration_mctct': ['MCTCT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'MCTCTConfig'],
    'feature_extraction_mctct': ['MCTCTFeatureExtractor'],
    'processing_mctct': ['MCTCTProcessor'],
}


try:
    if not is_torch_available():
        raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
    pass
else:
    __lowerCAmelCase			:     int           =   [
        'MCTCT_PRETRAINED_MODEL_ARCHIVE_LIST',
        'MCTCTForCTC',
        'MCTCTModel',
        'MCTCTPreTrainedModel',
    ]


if TYPE_CHECKING:
    from .configuration_mctct import MCTCT_PRETRAINED_CONFIG_ARCHIVE_MAP, MCTCTConfig
    from .feature_extraction_mctct import MCTCTFeatureExtractor
    from .processing_mctct import MCTCTProcessor

    try:
        if not is_torch_available():
            raise OptionalDependencyNotAvailable()
    except OptionalDependencyNotAvailable:
        pass
    else:
        from .modeling_mctct import MCTCT_PRETRAINED_MODEL_ARCHIVE_LIST, MCTCTForCTC, MCTCTModel, MCTCTPreTrainedModel

else:
    import sys

    __lowerCAmelCase			:     List[Any]           =   _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)

