






import importlib
import json
import os
import sys
import tempfile
import unittest
from pathlib import Path

import transformers
import transformers.models.auto
from transformers.models.auto.configuration_auto import CONFIG_MAPPING, AutoConfig
from transformers.models.bert.configuration_bert import BertConfig
from transformers.models.roberta.configuration_roberta import RobertaConfig
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


SCREAMING_SNAKE_CASE						:						Any					   =       get_tests_dir('''fixtures/dummy-config.json''')





class       snake_case_       (    unittest.TestCase       ):



	"""simple docstring"""

	def       _UpperCAmelCase						(      self						):


					"""simple docstring"""

					A__							 =       0


	def       _UpperCAmelCase						(      self						):


					"""simple docstring"""

					self.assertIsNotNone(transformers.models.auto.__spec__						)
					self.assertIsNotNone(importlib.util.find_spec('transformers.models.auto'						)						)


	def       _UpperCAmelCase						(      self						):


					"""simple docstring"""

					A__							 =       AutoConfig.from_pretrained('bert-base-uncased'						)
					self.assertIsInstance(__a  ,					__a						)


	def       _UpperCAmelCase						(      self						):


					"""simple docstring"""

					A__							 =       AutoConfig.from_pretrained(__a						)
					self.assertIsInstance(__a  ,					__a						)


	def       _UpperCAmelCase						(      self						):


					"""simple docstring"""

					A__							 =       AutoConfig.from_pretrained(__a						)
					self.assertIsInstance(__a  ,					__a						)


	def       _UpperCAmelCase						(      self						):


					"""simple docstring"""

					A__							 =       AutoConfig.for_model('roberta'						)
					self.assertIsInstance(__a  ,					__a						)


	def       _UpperCAmelCase						(      self						):


					"""simple docstring"""

					with tempfile.TemporaryDirectory() as tmp_dir:
									# This model name contains bert and roberta, but roberta ends up being picked.
									A__							 =       os.path.join(__a  ,					'fake-roberta'						)
									os.makedirs(__a  ,					exist_ok=__a						)
									with open(os.path.join(__a  ,					'config.json'						)  ,					'w'						) as f:
													f.write(json.dumps({}						)						)
									A__							 =       AutoConfig.from_pretrained(__a						)
									self.assertEqual(type(__a						)  ,					__a						)


	def       _UpperCAmelCase						(      self						):


					"""simple docstring"""

					try:
									AutoConfig.register('custom'  ,					__a						)
									# Wrong model type will raise an error
									with self.assertRaises(__a						):
													AutoConfig.register('model'  ,					__a						)
									# Trying to register something existing in the Transformers library will raise an error
									with self.assertRaises(__a						):
													AutoConfig.register('bert'  ,					__a						)

									# Now that the config is registered, it can be used as any other config with the auto-API
									A__							 =       CustomConfig()
									with tempfile.TemporaryDirectory() as tmp_dir:
													config.save_pretrained(__a						)
													A__							 =       AutoConfig.from_pretrained(__a						)
													self.assertIsInstance(__a  ,					__a						)

					finally:
									if "custom" in CONFIG_MAPPING._extra_content:
													del CONFIG_MAPPING._extra_content["custom"]


	def       _UpperCAmelCase						(      self						):


					"""simple docstring"""

					with self.assertRaisesRegex(
					    __a  ,					'bert-base is not a local folder and is not a valid model identifier'						):
									A__							 =       AutoConfig.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\)'						):
									A__							 =       AutoConfig.from_pretrained(__a  ,					revision='aaaaaa'						)


	def       _UpperCAmelCase						(      self						):


					"""simple docstring"""

					with self.assertRaisesRegex(
					    __a  ,					'hf-internal-testing/no-config-test-repo does not appear to have a file named config.json.'  ,					):
									A__							 =       AutoConfig.from_pretrained('hf-internal-testing/no-config-test-repo'						)


	def       _UpperCAmelCase						(      self						):


					"""simple docstring"""

					with self.assertRaises(__a						):
									A__							 =       AutoConfig.from_pretrained('hf-internal-testing/test_dynamic_model'						)
					# If remote code is disabled, we can't load this config.
					with self.assertRaises(__a						):
									A__							 =       AutoConfig.from_pretrained('hf-internal-testing/test_dynamic_model'  ,					trust_remote_code=__a						)

					A__							 =       AutoConfig.from_pretrained('hf-internal-testing/test_dynamic_model'  ,					trust_remote_code=__a						)
					self.assertEqual(config.__class__.__name__  ,					'NewModelConfig'						)

					# Test config can be reloaded.
					with tempfile.TemporaryDirectory() as tmp_dir:
									config.save_pretrained(__a						)
									A__							 =       AutoConfig.from_pretrained(__a  ,					trust_remote_code=__a						)
					self.assertEqual(reloaded_config.__class__.__name__  ,					'NewModelConfig'						)






	def       _UpperCAmelCase						(      self						):


					"""simple docstring"""

					class       snake_case_       (    _lowerCamelCase       ):



						"""simple docstring"""

						SCREAMING_SNAKE_CASE_:							Optional[Any]   			=					"""new-model"""

					try:
									AutoConfig.register('new-model'  ,					__a						)
									# If remote code is not set, the default is to use local
									A__							 =       AutoConfig.from_pretrained('hf-internal-testing/test_dynamic_model'						)
									self.assertEqual(config.__class__.__name__  ,					'NewModelConfigLocal'						)

									# If remote code is disabled, we load the local one.
									A__							 =       AutoConfig.from_pretrained('hf-internal-testing/test_dynamic_model'  ,					trust_remote_code=__a						)
									self.assertEqual(config.__class__.__name__  ,					'NewModelConfigLocal'						)

									# If remote is enabled, we load from the Hub
									A__							 =       AutoConfig.from_pretrained('hf-internal-testing/test_dynamic_model'  ,					trust_remote_code=__a						)
									self.assertEqual(config.__class__.__name__  ,					'NewModelConfig'						)

					finally:
									if "new-model" in CONFIG_MAPPING._extra_content:
													del CONFIG_MAPPING._extra_content["new-model"]










"""simple docstring"""






from ...processing_utils import ProcessorMixin





class       snake_case_       (    _lowerCamelCase       ):



	"""simple docstring"""

	SCREAMING_SNAKE_CASE_:							Union[str, Any]   			=					["""image_processor""", """feature_extractor"""]
	SCREAMING_SNAKE_CASE_:							Optional[int]   			=					"""TvltImageProcessor"""
	SCREAMING_SNAKE_CASE_:							Optional[int]   			=					"""TvltFeatureExtractor"""


	def __init__(      self  ,					__a  ,					__a						):


					"""simple docstring"""

					super().__init__(image_processor=__a  ,					feature_extractor=__a						)

					A__							 =       image_processor
					A__							 =       feature_extractor


	def __call__(      self  ,					__a=None  ,					__a=None  ,					__a=None  ,					__a=None  ,					__a=False  ,					__a=False  ,					*__a  ,					**__a  ,					):


					"""simple docstring"""

					if images is None and audio is None:
									raise ValueError('You need to specify either an `images` or `audio` input to process.'						)

					A__							 =       None
					if images is not None:
									A__							 =       self.image_processor(__a  ,					mask_pixel=__a  ,					*__a  ,					**__a						)
					if images_mixed is not None:
									A__							 =       self.image_processor(__a  ,					is_mixed=__a  ,					*__a  ,					**__a						)
					if audio is not None:
									A__							 =       self.feature_extractor(
									    __a  ,					*__a  ,					sampling_rate=__a  ,					mask_audio=__a  ,					**__a						)

					A__							 =       {}
					if audio is not None:
									output_dict.update(__a						)
					if images is not None:
									output_dict.update(__a						)
					if images_mixed_dict is not None:
									output_dict.update(__a						)
					return output_dict






	@property
	def       _UpperCAmelCase						(      self						):


					"""simple docstring"""

					A__							 =       self.image_processor.model_input_names
					A__							 =       self.feature_extractor.model_input_names
					return list(dict.fromkeys(image_processor_input_names + feature_extractor_input_names						)						)


