


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


class    _lowerCAmelCase			(		unittest.TestCase      ):



				"""simple docstring"""



				def 					__magic_name__							(      self    :    int):






											'''simple docstring'''







											snake_case__	  =       0



				def 					__magic_name__							(      self    :    str):






											'''simple docstring'''







											snake_case__	  =       AutoFeatureExtractor.from_pretrained("""facebook/wav2vec2-base-960h""")
											self.assertIsInstance(__lowerCamelCase			,		__lowerCamelCase)



				def 					__magic_name__							(      self    :    Union[str, Any]):






											'''simple docstring'''







											snake_case__	  =       AutoFeatureExtractor.from_pretrained(__lowerCamelCase)
											self.assertIsInstance(__lowerCamelCase			,		__lowerCamelCase)



				def 					__magic_name__							(      self    :    Dict):






											'''simple docstring'''







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

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

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

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

																		snake_case__	  =       AutoFeatureExtractor.from_pretrained(__lowerCamelCase)

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

											self.assertIsInstance(__lowerCamelCase			,		__lowerCamelCase)



				def 					__magic_name__							(      self    :    int):






											'''simple docstring'''







											snake_case__	  =       AutoFeatureExtractor.from_pretrained(__lowerCamelCase)
											self.assertIsInstance(__lowerCamelCase			,		__lowerCamelCase)



				def 					__magic_name__							(      self    :    int):






											'''simple docstring'''







											with self.assertRaisesRegex(
											    __lowerCamelCase			,		"""bert-base is not a local folder and is not a valid model identifier"""):
																		snake_case__	  =       AutoFeatureExtractor.from_pretrained("""bert-base""")



				def 					__magic_name__							(      self    :    int):






											'''simple docstring'''







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



				def 					__magic_name__							(      self    :    List[str]):






											'''simple docstring'''







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



				def 					__magic_name__							(      self    :    Optional[Any]):






											'''simple docstring'''







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

											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)
																		snake_case__	  =       AutoFeatureExtractor.from_pretrained(__lowerCamelCase			,		trust_remote_code=__lowerCamelCase)
											self.assertEqual(reloaded_feature_extractor.__class__.__name__			,		"""NewFeatureExtractor""")



				def 					__magic_name__							(      self    :    List[str]):






											'''simple docstring'''







											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
																		snake_case__	  =       CustomFeatureExtractor.from_pretrained(__lowerCamelCase)
																		with tempfile.TemporaryDirectory() as tmp_dir:
																									feature_extractor.save_pretrained(__lowerCamelCase)
																									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 					__magic_name__							(      self    :    int):






											'''simple docstring'''







											class    _lowerCAmelCase			(		lowerCamelCase__      ):



															"""simple docstring"""



															_lowercase				:   Any      =  True

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








from typing import TYPE_CHECKING

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


a__       =     {
    """configuration_clip""": [
        """CLIP_PRETRAINED_CONFIG_ARCHIVE_MAP""",
        """CLIPConfig""",
        """CLIPOnnxConfig""",
        """CLIPTextConfig""",
        """CLIPVisionConfig""",
    ],
    """processing_clip""": ["""CLIPProcessor"""],
    """tokenization_clip""": ["""CLIPTokenizer"""],
}

try:
	if not is_tokenizers_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	a__       =     ["""CLIPTokenizerFast"""]

try:
	if not is_vision_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	a__       =     ["""CLIPFeatureExtractor"""]
	a__       =     ["""CLIPImageProcessor"""]

try:
	if not is_torch_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	a__       =     [
	    """CLIP_PRETRAINED_MODEL_ARCHIVE_LIST""",
	    """CLIPModel""",
	    """CLIPPreTrainedModel""",
	    """CLIPTextModel""",
	    """CLIPTextModelWithProjection""",
	    """CLIPVisionModel""",
	    """CLIPVisionModelWithProjection""",
	]

try:
	if not is_tf_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	a__       =     [
	    """TF_CLIP_PRETRAINED_MODEL_ARCHIVE_LIST""",
	    """TFCLIPModel""",
	    """TFCLIPPreTrainedModel""",
	    """TFCLIPTextModel""",
	    """TFCLIPVisionModel""",
	]

try:
	if not is_flax_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	a__       =     [
	    """FlaxCLIPModel""",
	    """FlaxCLIPPreTrainedModel""",
	    """FlaxCLIPTextModel""",
	    """FlaxCLIPTextPreTrainedModel""",
	    """FlaxCLIPVisionModel""",
	    """FlaxCLIPVisionPreTrainedModel""",
	]


if TYPE_CHECKING:
	from .configuration_clip import (
	    CLIP_PRETRAINED_CONFIG_ARCHIVE_MAP,
	    CLIPConfig,
	    CLIPOnnxConfig,
	    CLIPTextConfig,
	    CLIPVisionConfig,
	)
	from .processing_clip import CLIPProcessor
	from .tokenization_clip import CLIPTokenizer

	try:
		if not is_tokenizers_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .tokenization_clip_fast import CLIPTokenizerFast

	try:
		if not is_vision_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .feature_extraction_clip import CLIPFeatureExtractor
		from .image_processing_clip import CLIPImageProcessor

	try:
		if not is_torch_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .modeling_clip import (
		    CLIP_PRETRAINED_MODEL_ARCHIVE_LIST,
		    CLIPModel,
		    CLIPPreTrainedModel,
		    CLIPTextModel,
		    CLIPTextModelWithProjection,
		    CLIPVisionModel,
		    CLIPVisionModelWithProjection,
		)

	try:
		if not is_tf_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .modeling_tf_clip import (
		    TF_CLIP_PRETRAINED_MODEL_ARCHIVE_LIST,
		    TFCLIPModel,
		    TFCLIPPreTrainedModel,
		    TFCLIPTextModel,
		    TFCLIPVisionModel,
		)

	try:
		if not is_flax_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .modeling_flax_clip import (
		    FlaxCLIPModel,
		    FlaxCLIPPreTrainedModel,
		    FlaxCLIPTextModel,
		    FlaxCLIPTextPreTrainedModel,
		    FlaxCLIPVisionModel,
		    FlaxCLIPVisionPreTrainedModel,
		)


else:
	import sys

	a__       =     _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
