






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


UpperCamelCase        =				get_tests_dir("fixtures")
UpperCamelCase        =				get_tests_dir("fixtures/dummy_feature_extractor_config.json")
UpperCamelCase        =				get_tests_dir("fixtures/dummy-config.json")

class        lowerCAmelCase_				(      unittest.TestCase	):







				"""simple docstring"""




				def 				__a   ( self     :Optional[int]     ):
					UpperCamelCase__ :Optional[int]						=      0







				def 				__a   ( self     :str     ):
					UpperCamelCase__ :Optional[Any]						=      AutoFeatureExtractor.from_pretrained("""facebook/wav2vec2-base-960h"""     )
					self.assertIsInstance(lowerCamelCase__  ,     lowerCamelCase__     )







				def 				__a   ( self     :Dict     ):
					UpperCamelCase__ :Dict						=      AutoFeatureExtractor.from_pretrained(lowerCamelCase__     )
					self.assertIsInstance(lowerCamelCase__  ,     lowerCamelCase__     )







				def 				__a   ( self     :Optional[int]     ):
					with tempfile.TemporaryDirectory() as tmpdirname:
						UpperCamelCase__ :List[str]						=      WavaVecaConfig()

						# remove feature_extractor_type to make sure config.json alone is enough to load feature processor locally
						UpperCamelCase__ :Tuple						=      AutoFeatureExtractor.from_pretrained(lowerCamelCase__     ).to_dict()

						config_dict.pop("""feature_extractor_type"""     )
						UpperCamelCase__ :Union[str, Any]						=      WavaVecaFeatureExtractor(**lowerCamelCase__     )

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

						UpperCamelCase__ :Optional[Any]						=      AutoFeatureExtractor.from_pretrained(lowerCamelCase__     )

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

					self.assertIsInstance(lowerCamelCase__  ,     lowerCamelCase__     )







				def 				__a   ( self     :Union[str, Any]     ):
					UpperCamelCase__ :Dict						=      AutoFeatureExtractor.from_pretrained(lowerCamelCase__     )
					self.assertIsInstance(lowerCamelCase__  ,     lowerCamelCase__     )







				def 				__a   ( self     :Dict     ):
					with self.assertRaisesRegex(
					    lowerCamelCase__  ,     """bert-base is not a local folder and is not a valid model identifier"""     ):
						UpperCamelCase__ :Dict						=      AutoFeatureExtractor.from_pretrained("""bert-base"""     )







				def 				__a   ( self     :List[Any]     ):
					with self.assertRaisesRegex(
					    lowerCamelCase__  ,     r"""aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)"""     ):
						UpperCamelCase__ :Optional[int]						=      AutoFeatureExtractor.from_pretrained(lowerCamelCase__  ,     revision="""aaaaaa"""     )







				def 				__a   ( self     :int     ):
					with self.assertRaisesRegex(
					    lowerCamelCase__  ,     """hf-internal-testing/config-no-model does not appear to have a file named preprocessor_config.json."""  ,     ):
						UpperCamelCase__ :List[Any]						=      AutoFeatureExtractor.from_pretrained("""hf-internal-testing/config-no-model"""     )







				def 				__a   ( self     :Optional[int]     ):
					# If remote code is not set, we will time out when asking whether to load the model.
					with self.assertRaises(lowerCamelCase__     ):
						UpperCamelCase__ :List[Any]						=      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__     ):
						UpperCamelCase__ :Optional[Any]						=      AutoFeatureExtractor.from_pretrained(
						    """hf-internal-testing/test_dynamic_feature_extractor"""  ,     trust_remote_code=lowerCamelCase__     )

					UpperCamelCase__ :str						=      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__     )
						UpperCamelCase__ :Any						=      AutoFeatureExtractor.from_pretrained(lowerCamelCase__  ,     trust_remote_code=lowerCamelCase__     )
					self.assertEqual(reloaded_feature_extractor.__class__.__name__  ,     """NewFeatureExtractor"""     )







				def 				__a   ( self     :Dict     ):
					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
						UpperCamelCase__ :Any						=      CustomFeatureExtractor.from_pretrained(lowerCamelCase__     )
						with tempfile.TemporaryDirectory() as tmp_dir:
							feature_extractor.save_pretrained(lowerCamelCase__     )
							UpperCamelCase__ :List[Any]						=      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 				__a   ( self     :Optional[int]     ):

					class        lowerCAmelCase_				(      lowercase	):







									"""simple docstring"""




									_snake_case				:   Optional[int]        =			True

					try:
						AutoConfig.register("""custom"""  ,     lowerCamelCase__     )
						AutoFeatureExtractor.register(lowerCamelCase__  ,     lowerCamelCase__     )
						# If remote code is not set, the default is to use local
						UpperCamelCase__ :Optional[Any]						=      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__ :str						=      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
						UpperCamelCase__ :Optional[int]						=      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'''

# Lint as: python3
import itertools
import os
import re


UpperCamelCase      					=re.compile(R"([A-Z]+)([A-Z][a-z])")
UpperCamelCase      					=re.compile(R"([a-z\d])([A-Z])")

UpperCamelCase      					=re.compile(R"(?<!_)_(?!_)")
UpperCamelCase      					=re.compile(R"(_{2,})")

UpperCamelCase      					=R"^\w+(\.\w+)*$"

UpperCamelCase      					=R"<>:/\|?*"



def 				snake_case       (  a_				:			Union[str, Any]						)							->					List[Any]:





      """simple docstring"""

      UpperCamelCase_					: Tuple            =				_uppercase_uppercase_re.sub(r"""\1_\2"""     , a_						)
      UpperCamelCase_					: Any            =				_lowercase_uppercase_re.sub(r"""\1_\2"""     , a_						)
      return name.lower()



def 				snake_case       (  a_				:			str						)							->					Optional[int]:





      """simple docstring"""

      UpperCamelCase_					: Dict            =				_single_underscore_re.split(a_						)
      UpperCamelCase_					: Any            =				[_multiple_underscores_re.split(a_						) for n in name]
      return "".join(n.capitalize() for n in itertools.chain.from_iterable(a_						) if n != """"""						)



def 				snake_case       (  a_				:			str						)							->					List[Any]:





      """simple docstring"""

      if os.path.basename(a_						) != name:
            raise ValueError(f"Should be a dataset name, not a path: {name}"						)
      return camelcase_to_snakecase(a_						)



def 				snake_case       (  a_				:			int     , a_				:			List[Any]						)							->					List[Any]:





      """simple docstring"""

      if os.path.basename(a_						) != name:
            raise ValueError(f"Should be a dataset name, not a path: {name}"						)
      if not re.match(_split_re     , a_						):
            raise ValueError(f"Split name should match '{_split_re}'' but got '{split}'."						)
      return f"{filename_prefix_for_name(a_						)}-{split}"



def 				snake_case       (  a_				:			str     , a_				:			Optional[int]     , a_				:			List[Any]     , a_				:			Optional[int]=None						)							->					Tuple:





      """simple docstring"""

      UpperCamelCase_					: Tuple            =				filename_prefix_for_split(a_     , a_						)
      if filetype_suffix:
            prefix += f".{filetype_suffix}"
      UpperCamelCase_					: List[str]            =				os.path.join(a_     , a_						)
      return f"{filepath}*"



def 				snake_case       (  a_				:			List[Any]     , a_				:			Optional[int]     , a_				:			List[Any]     , a_				:			int=None     , a_				:			Dict=None						)							->					Any:





      """simple docstring"""

      UpperCamelCase_					: Union[str, Any]            =				filename_prefix_for_split(a_     , a_						)
      UpperCamelCase_					: Union[str, Any]            =				os.path.join(a_     , a_						)

      if shard_lengths:
            UpperCamelCase_					: List[Any]            =				len(a_						)
            UpperCamelCase_					: Optional[int]            =				[f"{prefix}-{shard_id:05d}-of-{num_shards:05d}" for shard_id in range(a_						)]
            if filetype_suffix:
                  UpperCamelCase_					: Tuple            =				[filename + f".{filetype_suffix}" for filename in filenames]
            return filenames
      else:
            UpperCamelCase_					: List[Any]            =				prefix
            if filetype_suffix:
                  filename += f".{filetype_suffix}"
            return [filename]






