
"""simple docstring"""




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 	lowerCAmelCase_						:


				"""simple docstring"""



				def __init__(self   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__=2   ,       SCREAMING_SNAKE_CASE__=3   ,       SCREAMING_SNAKE_CASE__=4   ,       SCREAMING_SNAKE_CASE__=2   ,       SCREAMING_SNAKE_CASE__=7   ,       SCREAMING_SNAKE_CASE__=True   ,       SCREAMING_SNAKE_CASE__=True   ,       SCREAMING_SNAKE_CASE__=True   ,       SCREAMING_SNAKE_CASE__=True   ,       SCREAMING_SNAKE_CASE__=99   ,       SCREAMING_SNAKE_CASE__=36   ,       SCREAMING_SNAKE_CASE__=3   ,       SCREAMING_SNAKE_CASE__=4   ,       SCREAMING_SNAKE_CASE__=37   ,       SCREAMING_SNAKE_CASE__="gelu"   ,       SCREAMING_SNAKE_CASE__=0.1   ,       SCREAMING_SNAKE_CASE__=0.1   ,       SCREAMING_SNAKE_CASE__=5_12   ,       SCREAMING_SNAKE_CASE__=16   ,       SCREAMING_SNAKE_CASE__=2   ,       SCREAMING_SNAKE_CASE__=0.02   ,       SCREAMING_SNAKE_CASE__=6   ,       SCREAMING_SNAKE_CASE__=6   ,       SCREAMING_SNAKE_CASE__=3   ,       SCREAMING_SNAKE_CASE__=4   ,       SCREAMING_SNAKE_CASE__=None   ,       SCREAMING_SNAKE_CASE__=10_00   ,       )					->		List[Any]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       Optional[int]           =   parent
											SCREAMING_SNAKE_CASE__						:       str           =   batch_size
											SCREAMING_SNAKE_CASE__						:       int           =   num_channels
											SCREAMING_SNAKE_CASE__						:       Tuple           =   image_size
											SCREAMING_SNAKE_CASE__						:       str           =   patch_size
											SCREAMING_SNAKE_CASE__						:       Optional[Any]           =   text_seq_length
											SCREAMING_SNAKE_CASE__						:       Any           =   is_training
											SCREAMING_SNAKE_CASE__						:       Tuple           =   use_input_mask
											SCREAMING_SNAKE_CASE__						:       Union[str, Any]           =   use_token_type_ids
											SCREAMING_SNAKE_CASE__						:       Optional[int]           =   use_labels
											SCREAMING_SNAKE_CASE__						:       Any           =   vocab_size
											SCREAMING_SNAKE_CASE__						:       List[str]           =   hidden_size
											SCREAMING_SNAKE_CASE__						:       List[Any]           =   num_hidden_layers
											SCREAMING_SNAKE_CASE__						:       str           =   num_attention_heads
											SCREAMING_SNAKE_CASE__						:       List[Any]           =   intermediate_size
											SCREAMING_SNAKE_CASE__						:       Tuple           =   hidden_act
											SCREAMING_SNAKE_CASE__						:       Union[str, Any]           =   hidden_dropout_prob
											SCREAMING_SNAKE_CASE__						:       Any           =   attention_probs_dropout_prob
											SCREAMING_SNAKE_CASE__						:       List[Any]           =   max_position_embeddings
											SCREAMING_SNAKE_CASE__						:       Union[str, Any]           =   type_vocab_size
											SCREAMING_SNAKE_CASE__						:       Union[str, Any]           =   type_sequence_label_size
											SCREAMING_SNAKE_CASE__						:       Union[str, Any]           =   initializer_range
											SCREAMING_SNAKE_CASE__						:       Dict           =   coordinate_size
											SCREAMING_SNAKE_CASE__						:       Optional[int]           =   shape_size
											SCREAMING_SNAKE_CASE__						:       Optional[Any]           =   num_labels
											SCREAMING_SNAKE_CASE__						:       int           =   num_choices
											SCREAMING_SNAKE_CASE__						:       Tuple           =   scope
											SCREAMING_SNAKE_CASE__						:       Dict           =   range_bbox

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



				def 	__magic_name__    (self    )					->		str:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       List[str]           =   ids_tensor([self.batch_size, self.text_seq_length]   ,       self.vocab_size    )

											SCREAMING_SNAKE_CASE__						:       int           =   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]:
																																SCREAMING_SNAKE_CASE__						:       Any           =   bbox[i, j, 3]
																																SCREAMING_SNAKE_CASE__						:       str           =   bbox[i, j, 1]
																																SCREAMING_SNAKE_CASE__						:       Any           =   t
																									if bbox[i, j, 2] < bbox[i, j, 0]:
																																SCREAMING_SNAKE_CASE__						:       int           =   bbox[i, j, 2]
																																SCREAMING_SNAKE_CASE__						:       Union[str, Any]           =   bbox[i, j, 0]
																																SCREAMING_SNAKE_CASE__						:       Union[str, Any]           =   t

											SCREAMING_SNAKE_CASE__						:       List[str]           =   floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]    )

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

											SCREAMING_SNAKE_CASE__						:       Optional[Any]           =   None
											if self.use_token_type_ids:
																		SCREAMING_SNAKE_CASE__						:       List[Any]           =   ids_tensor([self.batch_size, self.text_seq_length]   ,       self.type_vocab_size    )

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

											SCREAMING_SNAKE_CASE__						:       Optional[int]           =   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 	__magic_name__    (self   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__    )					->		List[str]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       Any           =   LayoutLMvaModel(config=A__    )
											model.to(A__    )
											model.eval()

											# text + image
											SCREAMING_SNAKE_CASE__						:       Optional[Any]           =   model(A__   ,       pixel_values=A__    )
											SCREAMING_SNAKE_CASE__						:       Dict           =   model(
											    A__   ,       bbox=A__   ,       pixel_values=A__   ,       attention_mask=A__   ,       token_type_ids=A__    )
											SCREAMING_SNAKE_CASE__						:       List[Any]           =   model(A__   ,       bbox=A__   ,       pixel_values=A__   ,       token_type_ids=A__    )
											SCREAMING_SNAKE_CASE__						:       Union[str, Any]           =   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
											SCREAMING_SNAKE_CASE__						:       Dict           =   model(A__    )
											self.parent.assertEqual(
											    result.last_hidden_state.shape   ,       (self.batch_size, self.text_seq_length, self.hidden_size)    )

											# image only
											SCREAMING_SNAKE_CASE__						:       Optional[int]           =   model(pixel_values=A__    )
											self.parent.assertEqual(
											    result.last_hidden_state.shape   ,       (self.batch_size, self.image_seq_length, self.hidden_size)    )



				def 	__magic_name__    (self   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__    )					->		Optional[Any]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       str           =   self.num_labels
											SCREAMING_SNAKE_CASE__						:       List[Any]           =   LayoutLMvaForSequenceClassification(A__    )
											model.to(A__    )
											model.eval()
											SCREAMING_SNAKE_CASE__						:       List[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 	__magic_name__    (self   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__    )					->		Any:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       Union[str, Any]           =   self.num_labels
											SCREAMING_SNAKE_CASE__						:       Optional[Any]           =   LayoutLMvaForTokenClassification(config=A__    )
											model.to(A__    )
											model.eval()
											SCREAMING_SNAKE_CASE__						:       List[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.text_seq_length, self.num_labels)    )



				def 	__magic_name__    (self   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__    )					->		Tuple:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       str           =   LayoutLMvaForQuestionAnswering(config=A__    )
											model.to(A__    )
											model.eval()
											SCREAMING_SNAKE_CASE__						:       str           =   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 	__magic_name__    (self    )					->		str:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       List[str]           =   self.prepare_config_and_inputs()
											(
											    (
											    SCREAMING_SNAKE_CASE__						
											)      ,      (
											    SCREAMING_SNAKE_CASE__						
											)      ,      (
											    SCREAMING_SNAKE_CASE__						
											)      ,      (
											    SCREAMING_SNAKE_CASE__						
											)      ,      (
											    SCREAMING_SNAKE_CASE__						
											)      ,      (
											    SCREAMING_SNAKE_CASE__						
											)      ,      (
											    SCREAMING_SNAKE_CASE__						
											)      ,      (
											    SCREAMING_SNAKE_CASE__						
											)      ,      						
											)						:       int           =   config_and_inputs
											SCREAMING_SNAKE_CASE__						:       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 	lowerCAmelCase_						(SCREAMING_SNAKE_CASE__ ,					SCREAMING_SNAKE_CASE__ ,					unittest.TestCase  ):


				"""simple docstring"""



				__UpperCamelCase      :						Dict					       =   False
				__UpperCamelCase      :						Any					       =   False
				__UpperCamelCase      :						Tuple					       =   False

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



				def 	__magic_name__    (self   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__    )					->		List[Any]:



											"""simple docstring"""




											return True



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



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       Any           =   LayoutLMvaModelTester(self    )
											SCREAMING_SNAKE_CASE__						:       str           =   ConfigTester(self   ,       config_class=A__   ,       hidden_size=37    )



				def 	__magic_name__    (self   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__=False    )					->		Optional[int]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       Union[str, Any]           =   copy.deepcopy(A__    )
											if model_class in get_values(A__    ):
																		SCREAMING_SNAKE_CASE__						:       Dict           =   {
																		    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__    ):
																									SCREAMING_SNAKE_CASE__						:       str           =   torch.ones(self.model_tester.batch_size   ,       dtype=torch.long   ,       device=A__    )
																		elif model_class in get_values(A__    ):
																									SCREAMING_SNAKE_CASE__						:       Any           =   torch.zeros(
																									    self.model_tester.batch_size   ,       dtype=torch.long   ,       device=A__    )
																									SCREAMING_SNAKE_CASE__						:       Optional[Any]           =   torch.zeros(
																									    self.model_tester.batch_size   ,       dtype=torch.long   ,       device=A__    )
																		elif model_class in [
																		    *get_values(A__    ),
																		]:
																									SCREAMING_SNAKE_CASE__						:       Any           =   torch.zeros(
																									    self.model_tester.batch_size   ,       dtype=torch.long   ,       device=A__    )
																		elif model_class in [
																		    *get_values(A__    ),
																		]:
																									SCREAMING_SNAKE_CASE__						:       Any           =   torch.zeros(
																									    (self.model_tester.batch_size, self.model_tester.text_seq_length)   ,       dtype=torch.long   ,       device=A__   ,       )

											return inputs_dict



				def 	__magic_name__    (self    )					->		int:



											"""simple docstring"""




											self.config_tester.run_common_tests()



				def 	__magic_name__    (self    )					->		Dict:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       int           =   self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*A__    )



				def 	__magic_name__    (self    )					->		Tuple:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       str           =   self.model_tester.prepare_config_and_inputs()
											for type in ["absolute", "relative_key", "relative_key_query"]:
																		SCREAMING_SNAKE_CASE__						:       Tuple           =   type
																		self.model_tester.create_and_check_model(*A__    )



				def 	__magic_name__    (self    )					->		int:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       Union[str, Any]           =   self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_sequence_classification(*A__    )



				def 	__magic_name__    (self    )					->		str:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       Optional[Any]           =   self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_token_classification(*A__    )



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



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       List[str]           =   self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_question_answering(*A__    )



				@slow
				def 	__magic_name__    (self    )					->		List[str]:



											"""simple docstring"""




											for model_name in LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																		SCREAMING_SNAKE_CASE__						:       Optional[int]           =   LayoutLMvaModel.from_pretrained(A__    )
																		self.assertIsNotNone(A__    )






def lowercase_  (   ):
							SCREAMING_SNAKE_CASE__						:       Tuple           =   Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""						)
							return image



@require_torch
class 	lowerCAmelCase_						(unittest.TestCase  ):


				"""simple docstring"""



				@cached_property
				def 	__magic_name__    (self    )					->		str:



											"""simple docstring"""




											return LayoutLMvaImageProcessor(apply_ocr=A__    ) if is_vision_available() else None



				@slow
				def 	__magic_name__    (self    )					->		Dict:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       Optional[int]           =   LayoutLMvaModel.from_pretrained("""microsoft/layoutlmv3-base"""    ).to(A__    )

											SCREAMING_SNAKE_CASE__						:       str           =   self.default_image_processor
											SCREAMING_SNAKE_CASE__						:       Tuple           =   prepare_img()
											SCREAMING_SNAKE_CASE__						:       str           =   image_processor(images=A__   ,       return_tensors="""pt"""    ).pixel_values.to(A__    )

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

											# forward pass
											SCREAMING_SNAKE_CASE__						:       Union[str, Any]           =   model(
											    input_ids=input_ids.to(A__    )   ,       bbox=bbox.to(A__    )   ,       pixel_values=pixel_values.to(A__    )   ,       )

											# verify the logits
											SCREAMING_SNAKE_CASE__						:       Optional[Any]           =   torch.Size((1, 1_99, 7_68)    )
											self.assertEqual(outputs.last_hidden_state.shape   ,       A__    )

											SCREAMING_SNAKE_CASE__						:       Tuple           =   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 requests
from bsa import BeautifulSoup



def        UpperCamelCase    (							lowerCAmelCase__ = "https://www.worldometers.info/coronavirus"		):
					'''simple docstring'''
					lowercase							=						BeautifulSoup(requests.get(lowerCAmelCase__		).text ,			'''html.parser'''		)
					lowercase							=						soup.findAll('''h1'''		)
					lowercase							=						soup.findAll('''div''' ,			{'''class''': '''maincounter-number'''}		)
					keys += soup.findAll('''span''' ,			{'''class''': '''panel-title'''}		)
					values += soup.findAll('''div''' ,			{'''class''': '''number-table-main'''}		)
					return {key.text.strip(): value.text.strip() for key, value in zip(lowerCAmelCase__ ,			lowerCAmelCase__		)}


if __name__ == "__main__":
							print("\033[1m" + "COVID-19 Status of the World" + "\033[0m\n")
							for key, value in world_covidaa_stats().items():
														print(F'{key}\n{value}\n')




