





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


lowercase    : Optional[int]    	=  get_tests_dir("""fixtures""")
lowercase    : List[str]    	=  get_tests_dir("""fixtures/dummy_feature_extractor_config.json""")
lowercase    : Union[str, Any]    	=  get_tests_dir("""fixtures/dummy-config.json""")

class  __A(						unittest.TestCase ):

     def 	_UpperCamelCase     (      self      ):





            """simple docstring"""

            _UpperCamelCase              =						0

     def 	_UpperCamelCase     (      self      ):





            """simple docstring"""

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

     def 	_UpperCamelCase     (      self      ):





            """simple docstring"""

            _UpperCamelCase              =						AutoFeatureExtractor.from_pretrained(A      )
            self.assertIsInstance(A,       A      )

     def 	_UpperCamelCase     (      self      ):





            """simple docstring"""

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

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

                   config_dict.pop('''feature_extractor_type'''      )
                   _UpperCamelCase              =						WavaVecaFeatureExtractor(**A      )

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

                   _UpperCamelCase              =						AutoFeatureExtractor.from_pretrained(A      )

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

            self.assertIsInstance(A,       A      )

     def 	_UpperCamelCase     (      self      ):





            """simple docstring"""

            _UpperCamelCase              =						AutoFeatureExtractor.from_pretrained(A      )
            self.assertIsInstance(A,       A      )

     def 	_UpperCamelCase     (      self      ):





            """simple docstring"""

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

     def 	_UpperCamelCase     (      self      ):





            """simple docstring"""

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

     def 	_UpperCamelCase     (      self      ):





            """simple docstring"""

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

     def 	_UpperCamelCase     (      self      ):





            """simple docstring"""

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

            _UpperCamelCase              =						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      )
                   _UpperCamelCase              =						AutoFeatureExtractor.from_pretrained(A,       trust_remote_code=A      )
            self.assertEqual(reloaded_feature_extractor.__class__.__name__,       '''NewFeatureExtractor'''      )

     def 	_UpperCamelCase     (      self      ):





            """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
                   _UpperCamelCase              =						CustomFeatureExtractor.from_pretrained(A      )
                   with tempfile.TemporaryDirectory() as tmp_dir:
                          feature_extractor.save_pretrained(A      )
                          _UpperCamelCase              =						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 	_UpperCamelCase     (      self      ):





            """simple docstring"""

            class  __A(						__UpperCAmelCase ):
                 __A	  =       True

            try:
                   AutoConfig.register('''custom''',       A      )
                   AutoFeatureExtractor.register(A,       A      )
                   # If remote code is not set, the default is to use local
                   _UpperCamelCase              =						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.
                   _UpperCamelCase              =						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
                   _UpperCamelCase              =						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]













def     SCREAMING_SNAKE_CASE			(      lowerCAmelCase					):
       _UpperCamelCase              =						[]
       if len(lowerCAmelCase					) == 1:
              return [nums.copy()]
       for _ in range(len(lowerCAmelCase					)					):
              _UpperCamelCase              =						nums.pop(0					)
              _UpperCamelCase              =						permute(lowerCAmelCase					)
              for perm in permutations:
                     perm.append(lowerCAmelCase					)
              result.extend(lowerCAmelCase					)
              nums.append(lowerCAmelCase					)
       return result


def     SCREAMING_SNAKE_CASE			(      lowerCAmelCase					):

       def backtrack(lowerCAmelCase					):
              if start == len(lowerCAmelCase					) - 1:
                     output.append(nums[:]					)
              else:
                     for i in range(lowerCAmelCase    ,       len(lowerCAmelCase					)					):
                            _UpperCamelCase       ,		_UpperCamelCase              =						nums[i], nums[start]
                            backtrack(start + 1					)
                            _UpperCamelCase       ,		_UpperCamelCase              =						nums[i], nums[start]  # backtrack

       _UpperCamelCase              =						[]
       backtrack(0					)
       return output


if __name__ == "__main__":
       import doctest

       # use res to print the data in permute2 function
       lowercase    : Optional[Any]    	=  permutea([1, 2, 3])
       print(res)
       doctest.testmod()






