
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     =get_tests_dir("""fixtures""")
_lowerCamelCase     =get_tests_dir("""fixtures/dummy_feature_extractor_config.json""")
_lowerCamelCase     =get_tests_dir("""fixtures/dummy-config.json""")





class 				A__						(      unittest.TestCase):
      def 		UpperCamelCase__				( self ):
       lowerCamelCase						:      Any							     = 0
      def 		UpperCamelCase__				( self ):
       lowerCamelCase						:      Dict							     = AutoFeatureExtractor.from_pretrained("""facebook/wav2vec2-base-960h""" )
       self.assertIsInstance(__magic_name__       ,    __magic_name__ )
      def 		UpperCamelCase__				( self ):
       lowerCamelCase						:      List[Any]							     = AutoFeatureExtractor.from_pretrained(__magic_name__ )
       self.assertIsInstance(__magic_name__       ,    __magic_name__ )
      def 		UpperCamelCase__				( self ):







       with tempfile.TemporaryDirectory() as tmpdirname:
        lowerCamelCase						:      List[Any]							     = WavaVecaConfig()

        # remove feature_extractor_type to make sure config.json alone is enough to load feature processor locally
        lowerCamelCase						:      int							     = AutoFeatureExtractor.from_pretrained(__magic_name__ ).to_dict()

        config_dict.pop("""feature_extractor_type""" )
        lowerCamelCase						:      List[Any]							     = WavaVecaFeatureExtractor(**__magic_name__ )

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

        lowerCamelCase						:      Union[str, Any]							     = AutoFeatureExtractor.from_pretrained(__magic_name__ )

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

       self.assertIsInstance(__magic_name__       ,    __magic_name__ )
      def 		UpperCamelCase__				( self ):
       lowerCamelCase						:      Optional[Any]							     = AutoFeatureExtractor.from_pretrained(__magic_name__ )
       self.assertIsInstance(__magic_name__       ,    __magic_name__ )
      def 		UpperCamelCase__				( self ):







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







       with self.assertRaisesRegex(
           __magic_name__       ,    r"""aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)""" ):
        lowerCamelCase						:      Union[str, Any]							     = AutoFeatureExtractor.from_pretrained(__magic_name__       ,    revision="""aaaaaa""" )
      def 		UpperCamelCase__				( self ):







       with self.assertRaisesRegex(
           __magic_name__       ,    """hf-internal-testing/config-no-model does not appear to have a file named preprocessor_config.json."""       ,    ):
        lowerCamelCase						:      Tuple							     = AutoFeatureExtractor.from_pretrained("""hf-internal-testing/config-no-model""" )
      def 		UpperCamelCase__				( self ):







       with self.assertRaises(__magic_name__ ):
        lowerCamelCase						:      Union[str, 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(__magic_name__ ):
        lowerCamelCase						:      Any							     = AutoFeatureExtractor.from_pretrained(
            """hf-internal-testing/test_dynamic_feature_extractor"""       ,    trust_remote_code=__magic_name__ )

       lowerCamelCase						:      List[Any]							     = AutoFeatureExtractor.from_pretrained(
           """hf-internal-testing/test_dynamic_feature_extractor"""       ,    trust_remote_code=__magic_name__ )
       self.assertEqual(feature_extractor.__class__.__name__       ,    """NewFeatureExtractor""" )

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







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

        # Now that the config is registered, it can be used as any other config with the auto-API
        lowerCamelCase						:      Dict							     = CustomFeatureExtractor.from_pretrained(__magic_name__ )
        with tempfile.TemporaryDirectory() as tmp_dir:
         feature_extractor.save_pretrained(__magic_name__ )
         lowerCamelCase						:      Optional[int]							     = AutoFeatureExtractor.from_pretrained(__magic_name__ )
         self.assertIsInstance(__magic_name__       ,    __magic_name__ )

       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 ):





       class 				A__						(      a_):
             _UpperCAmelCase							:       Dict       			=     True

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

        # If remote is enabled, we load from the Hub
        lowerCamelCase						:      List[Any]							     = AutoFeatureExtractor.from_pretrained(
            """hf-internal-testing/test_dynamic_feature_extractor"""       ,    trust_remote_code=__magic_name__ )
        self.assertEqual(feature_extractor.__class__.__name__       ,    """NewFeatureExtractor""" )
        self.assertTrue(not hasattr(__magic_name__       ,    """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_flax_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


UpperCAmelCase   :    Tuple     =		{
    "configuration_blenderbot_small": [
        "BLENDERBOT_SMALL_PRETRAINED_CONFIG_ARCHIVE_MAP",
        "BlenderbotSmallConfig",
        "BlenderbotSmallOnnxConfig",
    ],
    "tokenization_blenderbot_small": ["BlenderbotSmallTokenizer"],
}

try:
		if not is_tokenizers_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		UpperCAmelCase   :    List[Any]     =		["BlenderbotSmallTokenizerFast"]

try:
		if not is_torch_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		UpperCAmelCase   :    List[str]     =		[
		    "BLENDERBOT_SMALL_PRETRAINED_MODEL_ARCHIVE_LIST",
		    "BlenderbotSmallForCausalLM",
		    "BlenderbotSmallForConditionalGeneration",
		    "BlenderbotSmallModel",
		    "BlenderbotSmallPreTrainedModel",
		]

try:
		if not is_tf_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		UpperCAmelCase   :    Any     =		[
		    "TFBlenderbotSmallForConditionalGeneration",
		    "TFBlenderbotSmallModel",
		    "TFBlenderbotSmallPreTrainedModel",
		]

try:
		if not is_flax_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		UpperCAmelCase   :    List[Any]     =		[
		    "FlaxBlenderbotSmallForConditionalGeneration",
		    "FlaxBlenderbotSmallModel",
		    "FlaxBlenderbotSmallPreTrainedModel",
		]

if TYPE_CHECKING:
		from .configuration_blenderbot_small import (
		    BLENDERBOT_SMALL_PRETRAINED_CONFIG_ARCHIVE_MAP,
		    BlenderbotSmallConfig,
		    BlenderbotSmallOnnxConfig,
		)
		from .tokenization_blenderbot_small import BlenderbotSmallTokenizer

		try:
				if not is_tokenizers_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .tokenization_blenderbot_small_fast import BlenderbotSmallTokenizerFast

		try:
				if not is_torch_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_blenderbot_small import (
				    BLENDERBOT_SMALL_PRETRAINED_MODEL_ARCHIVE_LIST,
				    BlenderbotSmallForCausalLM,
				    BlenderbotSmallForConditionalGeneration,
				    BlenderbotSmallModel,
				    BlenderbotSmallPreTrainedModel,
				)

		try:
				if not is_tf_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_tf_blenderbot_small import (
				    TFBlenderbotSmallForConditionalGeneration,
				    TFBlenderbotSmallModel,
				    TFBlenderbotSmallPreTrainedModel,
				)

		try:
				if not is_flax_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_flax_blenderbot_small import (
				    FlaxBlenderbotSmallForConditionalGeneration,
				    FlaxBlenderbotSmallModel,
				    FlaxBlenderbotSmallPreTrainedModel,
				)

else:
		import sys

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

