
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


lowercase        =  get_tests_dir('''fixtures/dummy-config.json''')





class       __A(				unittest.TestCase       ):

					def    lowercase__      (	self					:     Tuple   ):
												lowerCamelCase_							  =					0

					def    lowercase__      (	self					:     Tuple   ):
												self.assertIsNotNone(transformers.models.auto.__spec__   )
												self.assertIsNotNone(importlib.util.find_spec("""transformers.models.auto"""   )   )

					def    lowercase__      (	self					:     List[str]   ):
												lowerCamelCase_							  =					AutoConfig.from_pretrained("""bert-base-uncased"""   )
												self.assertIsInstance(__UpperCamelCase   ,					__UpperCamelCase   )

					def    lowercase__      (	self					:     Optional[int]   ):
												lowerCamelCase_							  =					AutoConfig.from_pretrained(__UpperCamelCase   )
												self.assertIsInstance(__UpperCamelCase   ,					__UpperCamelCase   )

					def    lowercase__      (	self					:     List[Any]   ):
												lowerCamelCase_							  =					AutoConfig.from_pretrained(__UpperCamelCase   )
												self.assertIsInstance(__UpperCamelCase   ,					__UpperCamelCase   )

					def    lowercase__      (	self					:     str   ):
												lowerCamelCase_							  =					AutoConfig.for_model("""roberta"""   )
												self.assertIsInstance(__UpperCamelCase   ,					__UpperCamelCase   )

					def    lowercase__      (	self					:     Optional[Any]   ):
												with tempfile.TemporaryDirectory() as tmp_dir:
																			# This model name contains bert and roberta, but roberta ends up being picked.
																			lowerCamelCase_							  =					os.path.join(__UpperCamelCase   ,					"""fake-roberta"""   )
																			os.makedirs(__UpperCamelCase   ,					exist_ok=__UpperCamelCase   )
																			with open(os.path.join(__UpperCamelCase   ,					"""config.json"""   )   ,					"""w"""   ) as f:
																										f.write(json.dumps({}   )   )
																			lowerCamelCase_							  =					AutoConfig.from_pretrained(__UpperCamelCase   )
																			self.assertEqual(type(__UpperCamelCase   )   ,					__UpperCamelCase   )

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

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

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

					def    lowercase__      (	self					:     str   ):
												with self.assertRaisesRegex(
												    __UpperCamelCase   ,					"""bert-base is not a local folder and is not a valid model identifier"""   ):
																			lowerCamelCase_							  =					AutoConfig.from_pretrained("""bert-base"""   )

					def    lowercase__      (	self					:     Optional[Any]   ):
												with self.assertRaisesRegex(
												    __UpperCamelCase   ,					R"""aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)"""   ):
																			lowerCamelCase_							  =					AutoConfig.from_pretrained(__UpperCamelCase   ,					revision="""aaaaaa"""   )

					def    lowercase__      (	self					:     List[Any]   ):
												with self.assertRaisesRegex(
												    __UpperCamelCase   ,					"""hf-internal-testing/no-config-test-repo does not appear to have a file named config.json."""   ,					):
																			lowerCamelCase_							  =					AutoConfig.from_pretrained("""hf-internal-testing/no-config-test-repo"""   )

					def    lowercase__      (	self					:     int   ):
												# If remote code is not set, we will time out when asking whether to load the model.
												with self.assertRaises(__UpperCamelCase   ):
																			lowerCamelCase_							  =					AutoConfig.from_pretrained("""hf-internal-testing/test_dynamic_model"""   )
												# If remote code is disabled, we can't load this config.
												with self.assertRaises(__UpperCamelCase   ):
																			lowerCamelCase_							  =					AutoConfig.from_pretrained("""hf-internal-testing/test_dynamic_model"""   ,					trust_remote_code=__UpperCamelCase   )

												lowerCamelCase_							  =					AutoConfig.from_pretrained("""hf-internal-testing/test_dynamic_model"""   ,					trust_remote_code=__UpperCamelCase   )
												self.assertEqual(config.__class__.__name__   ,					"""NewModelConfig"""   )

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



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





												class       __A(				UpperCAmelCase       ):
																	SCREAMING_SNAKE_CASE			    =    '''new-model'''

												try:
																			AutoConfig.register("""new-model"""   ,					__UpperCamelCase   )
																			# If remote code is not set, the default is to use local
																			lowerCamelCase_							  =					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.
																			lowerCamelCase_							  =					AutoConfig.from_pretrained("""hf-internal-testing/test_dynamic_model"""   ,					trust_remote_code=__UpperCamelCase   )
																			self.assertEqual(config.__class__.__name__   ,					"""NewModelConfigLocal"""   )

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

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







from typing import List, Optional, Union

from ...image_utils import ImageInput
from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy
from ...utils import TensorType





class       __A(				UpperCAmelCase       ):
					SCREAMING_SNAKE_CASE			    =    ['''image_processor''', '''tokenizer''']
					SCREAMING_SNAKE_CASE			    =    '''BlipImageProcessor'''
					SCREAMING_SNAKE_CASE			    =    '''AutoTokenizer'''

					def __init__(	self					:     Union[str, Any]   ,					__UpperCamelCase					:     List[Any]   ,					__UpperCamelCase					:     List[str]   ):
												lowerCamelCase_							  =					False
												super().__init__(__UpperCamelCase   ,					__UpperCamelCase   )
												lowerCamelCase_							  =					self.image_processor

					def __call__(	self					:     Any   ,					__UpperCamelCase					:     ImageInput = None   ,					__UpperCamelCase					:     Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None   ,					__UpperCamelCase					:     bool = True   ,					__UpperCamelCase					:     Union[bool, str, PaddingStrategy] = False   ,					__UpperCamelCase					:     Union[bool, str, TruncationStrategy] = None   ,					__UpperCamelCase					:     Optional[int] = None   ,					__UpperCamelCase					:     int = 0   ,					__UpperCamelCase					:     Optional[int] = None   ,					__UpperCamelCase					:     Optional[bool] = None   ,					__UpperCamelCase					:     bool = False   ,					__UpperCamelCase					:     bool = False   ,					__UpperCamelCase					:     bool = False   ,					__UpperCamelCase					:     bool = False   ,					__UpperCamelCase					:     bool = False   ,					__UpperCamelCase					:     bool = True   ,					__UpperCamelCase					:     Optional[Union[str, TensorType]] = None   ,					**__UpperCamelCase					:     Optional[Any]   ,					):
												if images is None and text is None:
																			raise ValueError("""You have to specify either images or text."""   )

												# Get only text
												if images is None:
																			lowerCamelCase_							  =					self.tokenizer
																			lowerCamelCase_							  =					self.tokenizer(
																			    text=__UpperCamelCase   ,					add_special_tokens=__UpperCamelCase   ,					padding=__UpperCamelCase   ,					truncation=__UpperCamelCase   ,					max_length=__UpperCamelCase   ,					stride=__UpperCamelCase   ,					pad_to_multiple_of=__UpperCamelCase   ,					return_attention_mask=__UpperCamelCase   ,					return_overflowing_tokens=__UpperCamelCase   ,					return_special_tokens_mask=__UpperCamelCase   ,					return_offsets_mapping=__UpperCamelCase   ,					return_token_type_ids=__UpperCamelCase   ,					return_length=__UpperCamelCase   ,					verbose=__UpperCamelCase   ,					return_tensors=__UpperCamelCase   ,					**__UpperCamelCase   ,					)
																			return text_encoding

												# add pixel_values
												lowerCamelCase_							  =					self.image_processor(__UpperCamelCase   ,					return_tensors=__UpperCamelCase   )

												if text is not None:
																			lowerCamelCase_							  =					self.tokenizer(
																			    text=__UpperCamelCase   ,					add_special_tokens=__UpperCamelCase   ,					padding=__UpperCamelCase   ,					truncation=__UpperCamelCase   ,					max_length=__UpperCamelCase   ,					stride=__UpperCamelCase   ,					pad_to_multiple_of=__UpperCamelCase   ,					return_attention_mask=__UpperCamelCase   ,					return_overflowing_tokens=__UpperCamelCase   ,					return_special_tokens_mask=__UpperCamelCase   ,					return_offsets_mapping=__UpperCamelCase   ,					return_token_type_ids=__UpperCamelCase   ,					return_length=__UpperCamelCase   ,					verbose=__UpperCamelCase   ,					return_tensors=__UpperCamelCase   ,					**__UpperCamelCase   ,					)
												else:
																			lowerCamelCase_							  =					None

												if text_encoding is not None:
																			encoding_image_processor.update(__UpperCamelCase   )

												return encoding_image_processor

					def    lowercase__      (	self					:     Any   ,					*__UpperCamelCase					:     Union[str, Any]   ,					**__UpperCamelCase					:     List[Any]   ):
												return self.tokenizer.batch_decode(*__UpperCamelCase   ,					**__UpperCamelCase   )

					def    lowercase__      (	self					:     List[str]   ,					*__UpperCamelCase					:     List[Any]   ,					**__UpperCamelCase					:     int   ):
												return self.tokenizer.decode(*__UpperCamelCase   ,					**__UpperCamelCase   )



					@property
					# Copied from transformers.models.blip.processing_blip.BlipProcessor.model_input_names
					def    lowercase__      (	self					:     Optional[int]   ):
												lowerCamelCase_							  =					self.tokenizer.model_input_names
												lowerCamelCase_							  =					self.image_processor.model_input_names
												return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names   )   )





