



import copy
import unittest

from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
						import torch

						from transformers import (
						    MODEL_FOR_MULTIPLE_CHOICE_MAPPING,
						    MODEL_FOR_QUESTION_ANSWERING_MAPPING,
						    MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
						    MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING,
						    LayoutLMvaConfig,
						    LayoutLMvaForQuestionAnswering,
						    LayoutLMvaForSequenceClassification,
						    LayoutLMvaForTokenClassification,
						    LayoutLMvaModel,
						)
						from transformers.models.layoutlmva.modeling_layoutlmva import LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST

if is_vision_available():
						from PIL import Image

						from transformers import LayoutLMvaImageProcessor



class 				__snake_case						:




							def __init__(       self     :       List[str]				,				A_     :       Any				,				A_     :       List[Any]=2				,				A_     :       Union[str, Any]=3				,				A_     :       Tuple=4				,				A_     :       Optional[Any]=2				,				A_     :       Union[str, Any]=7				,				A_     :       Optional[int]=True				,				A_     :       Union[str, Any]=True				,				A_     :       Optional[Any]=True				,				A_     :       Optional[int]=True				,				A_     :       List[Any]=9_9				,				A_     :       str=3_6				,				A_     :       int=3				,				A_     :       Dict=4				,				A_     :       Tuple=3_7				,				A_     :       Optional[int]="gelu"				,				A_     :       Union[str, Any]=0.1				,				A_     :       int=0.1				,				A_     :       str=5_1_2				,				A_     :       List[str]=1_6				,				A_     :       List[Any]=2				,				A_     :       List[Any]=0.02				,				A_     :       List[Any]=6				,				A_     :       Union[str, Any]=6				,				A_     :       List[Any]=3				,				A_     :       Tuple=4				,				A_     :       Union[str, Any]=None				,				A_     :       Any=1_0_0_0				,				):
									lowerCAmelCase_			:     Union[str, Any]									= parent
									lowerCAmelCase_			:     Any									= batch_size
									lowerCAmelCase_			:     List[Any]									= num_channels
									lowerCAmelCase_			:     Dict									= image_size
									lowerCAmelCase_			:     Tuple									= patch_size
									lowerCAmelCase_			:     Optional[Any]									= text_seq_length
									lowerCAmelCase_			:     Any									= is_training
									lowerCAmelCase_			:     str									= use_input_mask
									lowerCAmelCase_			:     Optional[int]									= use_token_type_ids
									lowerCAmelCase_			:     int									= use_labels
									lowerCAmelCase_			:     Tuple									= vocab_size
									lowerCAmelCase_			:     Optional[int]									= hidden_size
									lowerCAmelCase_			:     Union[str, Any]									= num_hidden_layers
									lowerCAmelCase_			:     List[Any]									= num_attention_heads
									lowerCAmelCase_			:     Optional[Any]									= intermediate_size
									lowerCAmelCase_			:     List[str]									= hidden_act
									lowerCAmelCase_			:     str									= hidden_dropout_prob
									lowerCAmelCase_			:     str									= attention_probs_dropout_prob
									lowerCAmelCase_			:     int									= max_position_embeddings
									lowerCAmelCase_			:     List[Any]									= type_vocab_size
									lowerCAmelCase_			:     Dict									= type_sequence_label_size
									lowerCAmelCase_			:     Dict									= initializer_range
									lowerCAmelCase_			:     Optional[Any]									= coordinate_size
									lowerCAmelCase_			:     Tuple									= shape_size
									lowerCAmelCase_			:     int									= num_labels
									lowerCAmelCase_			:     List[str]									= num_choices
									lowerCAmelCase_			:     Union[str, Any]									= scope
									lowerCAmelCase_			:     List[str]									= range_bbox

									# LayoutLMv3's sequence length equals the number of text tokens + number of patches + 1 (we add 1 for the CLS token)
									lowerCAmelCase_			:     Optional[int]									= text_seq_length
									lowerCAmelCase_			:     Any									= (image_size // patch_size) ** 2 + 1
									lowerCAmelCase_			:     Tuple									= self.text_seq_length + self.image_seq_length




							def    UpperCAmelCase__			(       self     :       Any):
									lowerCAmelCase_			:     List[str]									= ids_tensor([self.batch_size, self.text_seq_length]				,				self.vocab_size)

									lowerCAmelCase_			:     str									= ids_tensor([self.batch_size, self.text_seq_length, 4]				,				self.range_bbox)
									# Ensure that bbox is legal
									for i in range(bbox.shape[0]):
											for j in range(bbox.shape[1]):
													if bbox[i, j, 3] < bbox[i, j, 1]:
															lowerCAmelCase_			:     Tuple									= bbox[i, j, 3]
															lowerCAmelCase_			:     List[str]									= bbox[i, j, 1]
															lowerCAmelCase_			:     Dict									= t
													if bbox[i, j, 2] < bbox[i, j, 0]:
															lowerCAmelCase_			:     str									= bbox[i, j, 2]
															lowerCAmelCase_			:     List[Any]									= bbox[i, j, 0]
															lowerCAmelCase_			:     List[Any]									= t

									lowerCAmelCase_			:     Tuple									= floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

									lowerCAmelCase_			:     Tuple									= None
									if self.use_input_mask:
											lowerCAmelCase_			:     Optional[Any]									= random_attention_mask([self.batch_size, self.text_seq_length])

									lowerCAmelCase_			:     List[Any]									= None
									if self.use_token_type_ids:
											lowerCAmelCase_			:     Union[str, Any]									= ids_tensor([self.batch_size, self.text_seq_length]				,				self.type_vocab_size)

									lowerCAmelCase_			:     Union[str, Any]									= None
									lowerCAmelCase_			:     Union[str, Any]									= None
									if self.use_labels:
											lowerCAmelCase_			:     Union[str, Any]									= ids_tensor([self.batch_size]				,				self.type_sequence_label_size)
											lowerCAmelCase_			:     Optional[Any]									= ids_tensor([self.batch_size, self.text_seq_length]				,				self.num_labels)

									lowerCAmelCase_			:     Union[str, Any]									= LayoutLMvaConfig(
									    vocab_size=self.vocab_size				,				hidden_size=self.hidden_size				,				num_hidden_layers=self.num_hidden_layers				,				num_attention_heads=self.num_attention_heads				,				intermediate_size=self.intermediate_size				,				hidden_act=self.hidden_act				,				hidden_dropout_prob=self.hidden_dropout_prob				,				attention_probs_dropout_prob=self.attention_probs_dropout_prob				,				max_position_embeddings=self.max_position_embeddings				,				type_vocab_size=self.type_vocab_size				,				initializer_range=self.initializer_range				,				coordinate_size=self.coordinate_size				,				shape_size=self.shape_size				,				input_size=self.image_size				,				patch_size=self.patch_size				,				)

									return config, input_ids, bbox, pixel_values, token_type_ids, input_mask, sequence_labels, token_labels




							def    UpperCAmelCase__			(       self     :       Optional[int]				,				A_     :       str				,				A_     :       Optional[Any]				,				A_     :       Optional[Any]				,				A_     :       List[Any]				,				A_     :       Union[str, Any]				,				A_     :       str				,				A_     :       Optional[int]				,				A_     :       str):
									lowerCAmelCase_			:     str									= LayoutLMvaModel(config=A_)
									model.to(A_)
									model.eval()

									# text + image
									lowerCAmelCase_			:     List[str]									= model(A_				,				pixel_values=A_)
									lowerCAmelCase_			:     Optional[int]									= model(
									    A_				,				bbox=A_				,				pixel_values=A_				,				attention_mask=A_				,				token_type_ids=A_)
									lowerCAmelCase_			:     Optional[Any]									= model(A_				,				bbox=A_				,				pixel_values=A_				,				token_type_ids=A_)
									lowerCAmelCase_			:     List[str]									= model(A_				,				bbox=A_				,				pixel_values=A_)

									self.parent.assertEqual(result.last_hidden_state.shape				,				(self.batch_size, self.seq_length, self.hidden_size))

									# text only
									lowerCAmelCase_			:     List[Any]									= model(A_)
									self.parent.assertEqual(
									    result.last_hidden_state.shape				,				(self.batch_size, self.text_seq_length, self.hidden_size))

									# image only
									lowerCAmelCase_			:     List[str]									= model(pixel_values=A_)
									self.parent.assertEqual(
									    result.last_hidden_state.shape				,				(self.batch_size, self.image_seq_length, self.hidden_size))




							def    UpperCAmelCase__			(       self     :       Dict				,				A_     :       Any				,				A_     :       List[str]				,				A_     :       Tuple				,				A_     :       Union[str, Any]				,				A_     :       List[Any]				,				A_     :       str				,				A_     :       Tuple				,				A_     :       Union[str, Any]):
									lowerCAmelCase_			:     Optional[Any]									= self.num_labels
									lowerCAmelCase_			:     Union[str, Any]									= LayoutLMvaForSequenceClassification(A_)
									model.to(A_)
									model.eval()
									lowerCAmelCase_			:     Union[str, Any]									= model(
									    A_				,				bbox=A_				,				pixel_values=A_				,				attention_mask=A_				,				token_type_ids=A_				,				labels=A_				,				)
									self.parent.assertEqual(result.logits.shape				,				(self.batch_size, self.num_labels))




							def    UpperCAmelCase__			(       self     :       Union[str, Any]				,				A_     :       Tuple				,				A_     :       Dict				,				A_     :       List[Any]				,				A_     :       Any				,				A_     :       List[str]				,				A_     :       List[str]				,				A_     :       Any				,				A_     :       Dict):
									lowerCAmelCase_			:     str									= self.num_labels
									lowerCAmelCase_			:     Dict									= LayoutLMvaForTokenClassification(config=A_)
									model.to(A_)
									model.eval()
									lowerCAmelCase_			:     Dict									= model(
									    A_				,				bbox=A_				,				pixel_values=A_				,				attention_mask=A_				,				token_type_ids=A_				,				labels=A_				,				)
									self.parent.assertEqual(result.logits.shape				,				(self.batch_size, self.text_seq_length, self.num_labels))




							def    UpperCAmelCase__			(       self     :       Union[str, Any]				,				A_     :       List[str]				,				A_     :       Dict				,				A_     :       List[str]				,				A_     :       List[str]				,				A_     :       List[str]				,				A_     :       Any				,				A_     :       List[Any]				,				A_     :       Union[str, Any]):
									lowerCAmelCase_			:     Any									= LayoutLMvaForQuestionAnswering(config=A_)
									model.to(A_)
									model.eval()
									lowerCAmelCase_			:     int									= model(
									    A_				,				bbox=A_				,				pixel_values=A_				,				attention_mask=A_				,				token_type_ids=A_				,				start_positions=A_				,				end_positions=A_				,				)
									self.parent.assertEqual(result.start_logits.shape				,				(self.batch_size, self.seq_length))
									self.parent.assertEqual(result.end_logits.shape				,				(self.batch_size, self.seq_length))







							def    UpperCAmelCase__			(       self     :       Any):
									lowerCAmelCase_			:     Optional[Any]									= self.prepare_config_and_inputs()
									(
									    (
									    lowerCAmelCase_
									)					,  (
									    lowerCAmelCase_
									)					,  (
									    lowerCAmelCase_
									)					,  (
									    lowerCAmelCase_
									)					,  (
									    lowerCAmelCase_
									)					,  (
									    lowerCAmelCase_
									)					,  (
									    lowerCAmelCase_
									)					,  (
									    lowerCAmelCase_
									)					,  
									)			:     Optional[Any]									= config_and_inputs
									lowerCAmelCase_			:     List[str]									= {
									    '''input_ids''': input_ids,
									    '''bbox''': bbox,
									    '''pixel_values''': pixel_values,
									    '''token_type_ids''': token_type_ids,
									    '''attention_mask''': input_mask,
									}
									return config, inputs_dict





@require_torch
class 				__snake_case						(     UpperCamelCase_						,UpperCamelCase_						,unittest.TestCase						):
							_a			      = False
							_a			      = False
							_a			      = False

							_a			      = (
							    (
							        LayoutLMvaModel,
							        LayoutLMvaForSequenceClassification,
							        LayoutLMvaForTokenClassification,
							        LayoutLMvaForQuestionAnswering,
							    )
							    if is_torch_available()
							    else ()
							)
							_a			      = (
							    {'''document-question-answering''': LayoutLMvaForQuestionAnswering, '''feature-extraction''': LayoutLMvaModel}
							    if is_torch_available()
							    else {}
							)




							def    UpperCAmelCase__			(       self     :       List[str]				,				A_     :       Optional[int]				,				A_     :       Dict				,				A_     :       Optional[int]				,				A_     :       Dict				,				A_     :       List[str]):
									# `DocumentQuestionAnsweringPipeline` is expected to work with this model, but it combines the text and visual
									# embedding along the sequence dimension (dim 1), which causes an error during post-processing as `p_mask` has
									# the sequence dimension of the text embedding only.
									# (see the line `embedding_output = torch.cat([embedding_output, visual_embeddings], dim=1)`)
									return True




							def    UpperCAmelCase__			(       self     :       Any):
									lowerCAmelCase_			:     List[Any]									= LayoutLMvaModelTester(self)
									lowerCAmelCase_			:     Optional[Any]									= ConfigTester(self				,				config_class=A_				,				hidden_size=3_7)




							def    UpperCAmelCase__			(       self     :       Union[str, Any]				,				A_     :       int				,				A_     :       List[str]				,				A_     :       int=False):
									lowerCAmelCase_			:     Any									= copy.deepcopy(A_)
									if model_class in get_values(A_):
											lowerCAmelCase_			:     Union[str, Any]									= {
											    k: v.unsqueeze(1).expand(-1				,				self.model_tester.num_choices				,				-1).contiguous()
											    if isinstance(A_				,				torch.Tensor) and v.ndim > 1
											    else v
											    for k, v in inputs_dict.items()
											}
									if return_labels:
											if model_class in get_values(A_):
													lowerCAmelCase_			:     List[str]									= torch.ones(self.model_tester.batch_size				,				dtype=torch.long				,				device=A_)
											elif model_class in get_values(A_):
													lowerCAmelCase_			:     str									= torch.zeros(
													    self.model_tester.batch_size				,				dtype=torch.long				,				device=A_)
													lowerCAmelCase_			:     Union[str, Any]									= torch.zeros(
													    self.model_tester.batch_size				,				dtype=torch.long				,				device=A_)
											elif model_class in [
											    *get_values(A_),
											]:
													lowerCAmelCase_			:     int									= torch.zeros(
													    self.model_tester.batch_size				,				dtype=torch.long				,				device=A_)
											elif model_class in [
											    *get_values(A_),
											]:
													lowerCAmelCase_			:     List[str]									= torch.zeros(
													    (self.model_tester.batch_size, self.model_tester.text_seq_length)				,				dtype=torch.long				,				device=A_				,				)

									return inputs_dict




							def    UpperCAmelCase__			(       self     :       Union[str, Any]):
									self.config_tester.run_common_tests()




							def    UpperCAmelCase__			(       self     :       Optional[int]):
									lowerCAmelCase_			:     List[str]									= self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*A_)




							def    UpperCAmelCase__			(       self     :       Optional[Any]):
									lowerCAmelCase_			:     Optional[Any]									= self.model_tester.prepare_config_and_inputs()
									for type in ["absolute", "relative_key", "relative_key_query"]:
											lowerCAmelCase_			:     str									= type
											self.model_tester.create_and_check_model(*A_)




							def    UpperCAmelCase__			(       self     :       str):
									lowerCAmelCase_			:     Optional[Any]									= self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_sequence_classification(*A_)




							def    UpperCAmelCase__			(       self     :       List[Any]):
									lowerCAmelCase_			:     str									= self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_token_classification(*A_)




							def    UpperCAmelCase__			(       self     :       Optional[int]):
									lowerCAmelCase_			:     int									= self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_question_answering(*A_)







							@slow
							def    UpperCAmelCase__			(       self     :       List[str]):
									for model_name in LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											lowerCAmelCase_			:     Optional[int]									= LayoutLMvaModel.from_pretrained(A_)
											self.assertIsNotNone(A_)





def 			UpperCamelCase(  ):
		lowerCAmelCase_			:     str									= Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' )
		return image





@require_torch
class 				__snake_case						(     unittest.TestCase						):




							@cached_property
							def    UpperCAmelCase__			(       self     :       Tuple):
									return LayoutLMvaImageProcessor(apply_ocr=A_) if is_vision_available() else None







							@slow
							def    UpperCAmelCase__			(       self     :       int):
									lowerCAmelCase_			:     str									= LayoutLMvaModel.from_pretrained('''microsoft/layoutlmv3-base''').to(A_)

									lowerCAmelCase_			:     Tuple									= self.default_image_processor
									lowerCAmelCase_			:     List[Any]									= prepare_img()
									lowerCAmelCase_			:     List[str]									= image_processor(images=A_				,				return_tensors='''pt''').pixel_values.to(A_)

									lowerCAmelCase_			:     List[str]									= torch.tensor([[1, 2]])
									lowerCAmelCase_			:     Optional[Any]									= torch.tensor([[1, 2, 3, 4], [5, 6, 7, 8]]).unsqueeze(0)

									# forward pass
									lowerCAmelCase_			:     List[Any]									= model(
									    input_ids=input_ids.to(A_)				,				bbox=bbox.to(A_)				,				pixel_values=pixel_values.to(A_)				,				)

									# verify the logits
									lowerCAmelCase_			:     List[str]									= torch.Size((1, 1_9_9, 7_6_8))
									self.assertEqual(outputs.last_hidden_state.shape				,				A_)

									lowerCAmelCase_			:     str									= torch.tensor(
									    [[-0.0529, 0.3618, 0.1632], [-0.1587, -0.1667, -0.0400], [-0.1557, -0.1671, -0.0505]]).to(A_)

									self.assertTrue(torch.allclose(outputs.last_hidden_state[0, :3, :3]				,				A_				,				atol=1e-4))










import string


def 			UpperCamelCase(  __UpperCamelCase							:		str ):
		for key in range(len(string.ascii_uppercase ) ):
				lowerCAmelCase_			:     List[Any]									= ''''''
				for symbol in message:
						if symbol in string.ascii_uppercase:
								lowerCAmelCase_			:     Optional[int]									= string.ascii_uppercase.find(__UpperCamelCase )
								lowerCAmelCase_			:     Optional[int]									= num - key
								if num < 0:
										lowerCAmelCase_			:     int									= num + len(string.ascii_uppercase )
								lowerCAmelCase_			:     int									= translated + string.ascii_uppercase[num]
						else:
								lowerCAmelCase_			:     Optional[int]									= translated + symbol
				print(f"""Decryption using Key #{key}: {translated}""" )


def 			UpperCamelCase(  ):
		lowerCAmelCase_			:     Dict									= input('''Encrypted message: ''' )
		lowerCAmelCase_			:     List[str]									= message.upper()
		decrypt(__UpperCamelCase )


if __name__ == "__main__":
						import doctest

						doctest.testmod()
						main()





