






import warnings
from typing import List, Optional, Union

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




class 		UpperCAmelCase_						(	SCREAMING_SNAKE_CASE__					):


						'''simple docstring'''




						A     :   Tuple						=					['image_processor', 'tokenizer']
						A     :   Dict						=					'LayoutLMv2ImageProcessor'
						A     :   Optional[Any]						=					('LayoutXLMTokenizer', 'LayoutXLMTokenizerFast')






						def __init__(	self		,				_SCREAMING_SNAKE_CASE=None		,				_SCREAMING_SNAKE_CASE=None		,				**_SCREAMING_SNAKE_CASE				) ->				str:
								if "feature_extractor" in kwargs:
										warnings.warn(
										    "The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`"
										    " instead."		,				_SCREAMING_SNAKE_CASE		,				)
										snake_case_				:							Dict						 =       kwargs.pop("feature_extractor"				)

								snake_case_				:							Optional[Any]						 =       image_processor if image_processor is not None else feature_extractor
								if image_processor is None:
										raise ValueError("You need to specify an `image_processor`."				)
								if tokenizer is None:
										raise ValueError("You need to specify a `tokenizer`."				)

								super().__init__(_SCREAMING_SNAKE_CASE		,				_SCREAMING_SNAKE_CASE				)






						def __call__(	self		,				_SCREAMING_SNAKE_CASE		,				_SCREAMING_SNAKE_CASE = None		,				_SCREAMING_SNAKE_CASE = None		,				_SCREAMING_SNAKE_CASE = None		,				_SCREAMING_SNAKE_CASE = None		,				_SCREAMING_SNAKE_CASE = True		,				_SCREAMING_SNAKE_CASE = False		,				_SCREAMING_SNAKE_CASE = None		,				_SCREAMING_SNAKE_CASE = None		,				_SCREAMING_SNAKE_CASE = 0		,				_SCREAMING_SNAKE_CASE = None		,				_SCREAMING_SNAKE_CASE = None		,				_SCREAMING_SNAKE_CASE = None		,				_SCREAMING_SNAKE_CASE = False		,				_SCREAMING_SNAKE_CASE = False		,				_SCREAMING_SNAKE_CASE = False		,				_SCREAMING_SNAKE_CASE = False		,				_SCREAMING_SNAKE_CASE = True		,				_SCREAMING_SNAKE_CASE = None		,				**_SCREAMING_SNAKE_CASE		,				) ->				BatchEncoding:
								# verify input
								if self.image_processor.apply_ocr and (boxes is not None):
										raise ValueError(
										    "You cannot provide bounding boxes "
										    "if you initialized the image processor with apply_ocr set to True."				)

								if self.image_processor.apply_ocr and (word_labels is not None):
										raise ValueError(
										    "You cannot provide word labels if you initialized the image processor with apply_ocr set to True."				)

								if return_overflowing_tokens is True and return_offsets_mapping is False:
										raise ValueError("You cannot return overflowing tokens without returning the offsets mapping."				)

								# first, apply the image processor
								snake_case_				:							Union[str, Any]						 =       self.image_processor(images=_SCREAMING_SNAKE_CASE		,				return_tensors=_SCREAMING_SNAKE_CASE				)

								# second, apply the tokenizer
								if text is not None and self.image_processor.apply_ocr and text_pair is None:
										if isinstance(_SCREAMING_SNAKE_CASE		,				_SCREAMING_SNAKE_CASE				):
												snake_case_				:							Optional[Any]						 =       [text]  # add batch dimension (as the image processor always adds a batch dimension)
										snake_case_				:							str						 =       features["words"]

								snake_case_				:							Optional[int]						 =       self.tokenizer(
								    text=text if text is not None else features["words"]		,				text_pair=text_pair if text_pair is not None else None		,				boxes=boxes if boxes is not None else features["boxes"]		,				word_labels=_SCREAMING_SNAKE_CASE		,				add_special_tokens=_SCREAMING_SNAKE_CASE		,				padding=_SCREAMING_SNAKE_CASE		,				truncation=_SCREAMING_SNAKE_CASE		,				max_length=_SCREAMING_SNAKE_CASE		,				stride=_SCREAMING_SNAKE_CASE		,				pad_to_multiple_of=_SCREAMING_SNAKE_CASE		,				return_token_type_ids=_SCREAMING_SNAKE_CASE		,				return_attention_mask=_SCREAMING_SNAKE_CASE		,				return_overflowing_tokens=_SCREAMING_SNAKE_CASE		,				return_special_tokens_mask=_SCREAMING_SNAKE_CASE		,				return_offsets_mapping=_SCREAMING_SNAKE_CASE		,				return_length=_SCREAMING_SNAKE_CASE		,				verbose=_SCREAMING_SNAKE_CASE		,				return_tensors=_SCREAMING_SNAKE_CASE		,				**_SCREAMING_SNAKE_CASE		,				)

								# add pixel values
								snake_case_				:							Dict						 =       features.pop("pixel_values"				)
								if return_overflowing_tokens is True:
										snake_case_				:							List[Any]						 =       self.get_overflowing_images(_SCREAMING_SNAKE_CASE		,				encoded_inputs["overflow_to_sample_mapping"]				)
								snake_case_				:							List[str]						 =       images

								return encoded_inputs






						def       _lowerCAmelCase       (	self		,				_SCREAMING_SNAKE_CASE		,				_SCREAMING_SNAKE_CASE				) ->				Optional[Any]:
								# in case there's an overflow, ensure each `input_ids` sample is mapped to its corresponding image
								snake_case_				:							Any						 =       []
								for sample_idx in overflow_to_sample_mapping:
										images_with_overflow.append(images[sample_idx]				)

								if len(_SCREAMING_SNAKE_CASE				) != len(_SCREAMING_SNAKE_CASE				):
										raise ValueError(
										    "Expected length of images to be the same as the length of `overflow_to_sample_mapping`, but got"
										    f''' {len(_SCREAMING_SNAKE_CASE				)} and {len(_SCREAMING_SNAKE_CASE				)}'''				)

								return images_with_overflow






						def       _lowerCAmelCase       (	self		,				*_SCREAMING_SNAKE_CASE		,				**_SCREAMING_SNAKE_CASE				) ->				Optional[Any]:
								return self.tokenizer.batch_decode(*_SCREAMING_SNAKE_CASE		,				**_SCREAMING_SNAKE_CASE				)






						def       _lowerCAmelCase       (	self		,				*_SCREAMING_SNAKE_CASE		,				**_SCREAMING_SNAKE_CASE				) ->				Optional[Any]:
								return self.tokenizer.decode(*_SCREAMING_SNAKE_CASE		,				**_SCREAMING_SNAKE_CASE				)






						@property
						def       _lowerCAmelCase       (	self				) ->				Optional[int]:
								return ["input_ids", "bbox", "attention_mask", "image"]






						@property
						def       _lowerCAmelCase       (	self				) ->				Dict:
								warnings.warn(
								    "`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead."		,				_SCREAMING_SNAKE_CASE		,				)
								return self.image_processor_class






						@property
						def       _lowerCAmelCase       (	self				) ->				Optional[int]:
								warnings.warn(
								    "`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead."		,				_SCREAMING_SNAKE_CASE		,				)
								return self.image_processor








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  :							Optional[Any]			  =    get_tests_dir('''fixtures/dummy-config.json''')




class 		UpperCAmelCase_						(	unittest.TestCase					):


						'''simple docstring'''




						def       _lowerCAmelCase       (	self				) ->				int:
								snake_case_				:							str						 =       0






						def       _lowerCAmelCase       (	self				) ->				str:
								self.assertIsNotNone(transformers.models.auto.__spec__				)
								self.assertIsNotNone(importlib.util.find_spec("transformers.models.auto"				)				)






						def       _lowerCAmelCase       (	self				) ->				int:
								snake_case_				:							List[Any]						 =       AutoConfig.from_pretrained("bert-base-uncased"				)
								self.assertIsInstance(_SCREAMING_SNAKE_CASE		,				_SCREAMING_SNAKE_CASE				)






						def       _lowerCAmelCase       (	self				) ->				Optional[int]:
								snake_case_				:							List[str]						 =       AutoConfig.from_pretrained(_SCREAMING_SNAKE_CASE				)
								self.assertIsInstance(_SCREAMING_SNAKE_CASE		,				_SCREAMING_SNAKE_CASE				)






						def       _lowerCAmelCase       (	self				) ->				str:
								snake_case_				:							str						 =       AutoConfig.from_pretrained(_SCREAMING_SNAKE_CASE				)
								self.assertIsInstance(_SCREAMING_SNAKE_CASE		,				_SCREAMING_SNAKE_CASE				)






						def       _lowerCAmelCase       (	self				) ->				Dict:
								snake_case_				:							Tuple						 =       AutoConfig.for_model("roberta"				)
								self.assertIsInstance(_SCREAMING_SNAKE_CASE		,				_SCREAMING_SNAKE_CASE				)






						def       _lowerCAmelCase       (	self				) ->				Tuple:
								with tempfile.TemporaryDirectory() as tmp_dir:
										# This model name contains bert and roberta, but roberta ends up being picked.
										snake_case_				:							str						 =       os.path.join(_SCREAMING_SNAKE_CASE		,				"fake-roberta"				)
										os.makedirs(_SCREAMING_SNAKE_CASE		,				exist_ok=_SCREAMING_SNAKE_CASE				)
										with open(os.path.join(_SCREAMING_SNAKE_CASE		,				"config.json"				)		,				"w"				) as f:
												f.write(json.dumps({}				)				)
										snake_case_				:							int						 =       AutoConfig.from_pretrained(_SCREAMING_SNAKE_CASE				)
										self.assertEqual(type(_SCREAMING_SNAKE_CASE				)		,				_SCREAMING_SNAKE_CASE				)






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

										# Now that the config is registered, it can be used as any other config with the auto-API
										snake_case_				:							Union[str, Any]						 =       CustomConfig()
										with tempfile.TemporaryDirectory() as tmp_dir:
												config.save_pretrained(_SCREAMING_SNAKE_CASE				)
												snake_case_				:							int						 =       AutoConfig.from_pretrained(_SCREAMING_SNAKE_CASE				)
												self.assertIsInstance(_SCREAMING_SNAKE_CASE		,				_SCREAMING_SNAKE_CASE				)

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






						def       _lowerCAmelCase       (	self				) ->				int:
								with self.assertRaisesRegex(
								    _SCREAMING_SNAKE_CASE		,				"bert-base is not a local folder and is not a valid model identifier"				):
										snake_case_				:							Optional[Any]						 =       AutoConfig.from_pretrained("bert-base"				)






						def       _lowerCAmelCase       (	self				) ->				Tuple:
								with self.assertRaisesRegex(
								    _SCREAMING_SNAKE_CASE		,				r"aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)"				):
										snake_case_				:							List[Any]						 =       AutoConfig.from_pretrained(_SCREAMING_SNAKE_CASE		,				revision="aaaaaa"				)






						def       _lowerCAmelCase       (	self				) ->				str:
								with self.assertRaisesRegex(
								    _SCREAMING_SNAKE_CASE		,				"hf-internal-testing/no-config-test-repo does not appear to have a file named config.json."		,				):
										snake_case_				:							Dict						 =       AutoConfig.from_pretrained("hf-internal-testing/no-config-test-repo"				)






						def       _lowerCAmelCase       (	self				) ->				Optional[int]:
								# If remote code is not set, we will time out when asking whether to load the model.
								with self.assertRaises(_SCREAMING_SNAKE_CASE				):
										snake_case_				:							Tuple						 =       AutoConfig.from_pretrained("hf-internal-testing/test_dynamic_model"				)
								# If remote code is disabled, we can't load this config.
								with self.assertRaises(_SCREAMING_SNAKE_CASE				):
										snake_case_				:							Optional[Any]						 =       AutoConfig.from_pretrained("hf-internal-testing/test_dynamic_model"		,				trust_remote_code=_SCREAMING_SNAKE_CASE				)

								snake_case_				:							List[Any]						 =       AutoConfig.from_pretrained("hf-internal-testing/test_dynamic_model"		,				trust_remote_code=_SCREAMING_SNAKE_CASE				)
								self.assertEqual(config.__class__.__name__		,				"NewModelConfig"				)

								# Test config can be reloaded.
								with tempfile.TemporaryDirectory() as tmp_dir:
										config.save_pretrained(_SCREAMING_SNAKE_CASE				)
										snake_case_				:							Optional[Any]						 =       AutoConfig.from_pretrained(_SCREAMING_SNAKE_CASE		,				trust_remote_code=_SCREAMING_SNAKE_CASE				)
								self.assertEqual(reloaded_config.__class__.__name__		,				"NewModelConfig"				)






						def       _lowerCAmelCase       (	self				) ->				Optional[Any]:




								class 		UpperCAmelCase_						(	SCREAMING_SNAKE_CASE__					):


														'''simple docstring'''




														A     :   List[Any]						=					'new-model'

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

										# If remote is enabled, we load from the Hub
										snake_case_				:							List[str]						 =       AutoConfig.from_pretrained("hf-internal-testing/test_dynamic_model"		,				trust_remote_code=_SCREAMING_SNAKE_CASE				)
										self.assertEqual(config.__class__.__name__		,				"NewModelConfig"				)

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