






'''simple docstring'''






import unittest

from transformers import LayoutLMConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

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


if is_torch_available():
      import torch

      from transformers import (
          LayoutLMForMaskedLM,
          LayoutLMForQuestionAnswering,
          LayoutLMForSequenceClassification,
          LayoutLMForTokenClassification,
          LayoutLMModel,
      )




class a_     :







   def __init__(self       , __a       , __a=1_3       , __a=7       , __a=True       , __a=True       , __a=True       , __a=True       , __a=9_9       , __a=3_2       , __a=5       , __a=4       , __a=3_7       , __a="gelu"       , __a=0.1       , __a=0.1       , __a=5_1_2       , __a=1_6       , __a=2       , __a=0.02       , __a=3       , __a=4       , __a=None       , __a=1_0_0_0       , )      ->   Tuple:
      """simple docstring"""






      __snake_case					:   Any  				=			parent
      __snake_case					:   List[str]  				=			batch_size
      __snake_case					:   Optional[Any]  				=			seq_length
      __snake_case					:   Any  				=			is_training
      __snake_case					:   Union[str, Any]  				=			use_input_mask
      __snake_case					:   Tuple  				=			use_token_type_ids
      __snake_case					:   List[Any]  				=			use_labels
      __snake_case					:   Any  				=			vocab_size
      __snake_case					:   List[Any]  				=			hidden_size
      __snake_case					:   Dict  				=			num_hidden_layers
      __snake_case					:   Optional[int]  				=			num_attention_heads
      __snake_case					:   Optional[int]  				=			intermediate_size
      __snake_case					:   List[str]  				=			hidden_act
      __snake_case					:   Any  				=			hidden_dropout_prob
      __snake_case					:   Dict  				=			attention_probs_dropout_prob
      __snake_case					:   str  				=			max_position_embeddings
      __snake_case					:   Any  				=			type_vocab_size
      __snake_case					:   Dict  				=			type_sequence_label_size
      __snake_case					:   Any  				=			initializer_range
      __snake_case					:   Union[str, Any]  				=			num_labels
      __snake_case					:   Tuple  				=			num_choices
      __snake_case					:   Optional[Any]  				=			scope
      __snake_case					:   Optional[Any]  				=			range_bbox







   def      SCREAMING_SNAKE_CASE__       (self)      ->   Optional[int]:
      """simple docstring"""






      __snake_case					:   Tuple  				=			ids_tensor([self.batch_size, self.seq_length]       , self.vocab_size)

      __snake_case					:   Dict  				=			ids_tensor([self.batch_size, self.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]:
               __snake_case					:   Dict  				=			bbox[i, j, 3]
               __snake_case					:   Dict  				=			bbox[i, j, 1]
               __snake_case					:   Dict  				=			t
            if bbox[i, j, 2] < bbox[i, j, 0]:
               __snake_case					:   Any  				=			bbox[i, j, 2]
               __snake_case					:   Optional[int]  				=			bbox[i, j, 0]
               __snake_case					:   Tuple  				=			t

      __snake_case					:   Any  				=			None
      if self.use_input_mask:
         __snake_case					:   Any  				=			ids_tensor([self.batch_size, self.seq_length]       , vocab_size=2)

      __snake_case					:   List[str]  				=			None
      if self.use_token_type_ids:
         __snake_case					:   int  				=			ids_tensor([self.batch_size, self.seq_length]       , self.type_vocab_size)

      __snake_case					:   List[Any]  				=			None
      __snake_case					:   List[str]  				=			None
      __snake_case					:   Dict  				=			None
      if self.use_labels:
         __snake_case					:   List[Any]  				=			ids_tensor([self.batch_size]       , self.type_sequence_label_size)
         __snake_case					:   Union[str, Any]  				=			ids_tensor([self.batch_size, self.seq_length]       , self.num_labels)
         __snake_case					:   Optional[Any]  				=			ids_tensor([self.batch_size]       , self.num_choices)

      __snake_case					:   str  				=			self.get_config()

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







   def      SCREAMING_SNAKE_CASE__       (self)      ->   Tuple:
      """simple docstring"""






      return LayoutLMConfig(
          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       , )







   def      SCREAMING_SNAKE_CASE__       (self       , __a       , __a       , __a       , __a       , __a       , __a       , __a       , __a)      ->   List[Any]:
      """simple docstring"""






      __snake_case					:   Any  				=			LayoutLMModel(config=__a)
      model.to(__a)
      model.eval()
      __snake_case					:   Any  				=			model(__a       , __a       , attention_mask=__a       , token_type_ids=__a)
      __snake_case					:   Union[str, Any]  				=			model(__a       , __a       , token_type_ids=__a)
      __snake_case					:   List[str]  				=			model(__a       , __a)
      self.parent.assertEqual(result.last_hidden_state.shape       , (self.batch_size, self.seq_length, self.hidden_size))
      self.parent.assertEqual(result.pooler_output.shape       , (self.batch_size, self.hidden_size))







   def      SCREAMING_SNAKE_CASE__       (self       , __a       , __a       , __a       , __a       , __a       , __a       , __a       , __a)      ->   Union[str, Any]:
      """simple docstring"""






      __snake_case					:   List[Any]  				=			LayoutLMForMaskedLM(config=__a)
      model.to(__a)
      model.eval()
      __snake_case					:   Dict  				=			model(__a       , __a       , attention_mask=__a       , token_type_ids=__a       , labels=__a)
      self.parent.assertEqual(result.logits.shape       , (self.batch_size, self.seq_length, self.vocab_size))







   def      SCREAMING_SNAKE_CASE__       (self       , __a       , __a       , __a       , __a       , __a       , __a       , __a       , __a)      ->   int:
      """simple docstring"""






      __snake_case					:   str  				=			self.num_labels
      __snake_case					:   List[str]  				=			LayoutLMForSequenceClassification(__a)
      model.to(__a)
      model.eval()
      __snake_case					:   int  				=			model(
          __a       , __a       , attention_mask=__a       , token_type_ids=__a       , labels=__a)
      self.parent.assertEqual(result.logits.shape       , (self.batch_size, self.num_labels))







   def      SCREAMING_SNAKE_CASE__       (self       , __a       , __a       , __a       , __a       , __a       , __a       , __a       , __a)      ->   Dict:
      """simple docstring"""






      __snake_case					:   Union[str, Any]  				=			self.num_labels
      __snake_case					:   Optional[int]  				=			LayoutLMForTokenClassification(config=__a)
      model.to(__a)
      model.eval()
      __snake_case					:   Tuple  				=			model(__a       , __a       , attention_mask=__a       , token_type_ids=__a       , labels=__a)
      self.parent.assertEqual(result.logits.shape       , (self.batch_size, self.seq_length, self.num_labels))







   def      SCREAMING_SNAKE_CASE__       (self       , __a       , __a       , __a       , __a       , __a       , __a       , __a       , __a)      ->   Dict:
      """simple docstring"""






      __snake_case					:   Optional[Any]  				=			LayoutLMForQuestionAnswering(config=__a)
      model.to(__a)
      model.eval()
      __snake_case					:   Tuple  				=			model(
          __a       , bbox=__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      SCREAMING_SNAKE_CASE__       (self)      ->   Any:
      """simple docstring"""






      __snake_case					:   Union[str, Any]  				=			self.prepare_config_and_inputs()
      (
          (
          __snake_case
      )				,(
          __snake_case
      )				,(
          __snake_case
      )				,(
          __snake_case
      )				,(
          __snake_case
      )				,(
          __snake_case
      )				,(
          __snake_case
      )				,(
          __snake_case
      )				,
      )					:   str  				=			config_and_inputs
      __snake_case					:   Optional[int]  				=			{
          'input_ids': input_ids,
          'bbox': bbox,
          'token_type_ids': token_type_ids,
          'attention_mask': input_mask,
      }
      return config, inputs_dict




@require_torch
class a_     (     UpperCamelCase_		,   UpperCamelCase_		,   unittest.TestCase				):
   _snake_case	   =				(
       (
           LayoutLMModel,
           LayoutLMForMaskedLM,
           LayoutLMForSequenceClassification,
           LayoutLMForTokenClassification,
           LayoutLMForQuestionAnswering,
       )
       if is_torch_available()
       else None
   )
   _snake_case	   =				(
       {
           """document-question-answering""": LayoutLMForQuestionAnswering,
           """feature-extraction""": LayoutLMModel,
           """fill-mask""": LayoutLMForMaskedLM,
           """text-classification""": LayoutLMForSequenceClassification,
           """token-classification""": LayoutLMForTokenClassification,
           """zero-shot""": LayoutLMForSequenceClassification,
       }
       if is_torch_available()
       else {}
   )
   _snake_case	   =				True







   def      SCREAMING_SNAKE_CASE__       (self)      ->   str:
      """simple docstring"""






      __snake_case					:   List[Any]  				=			LayoutLMModelTester(self)
      __snake_case					:   List[Any]  				=			ConfigTester(self       , config_class=__a       , hidden_size=3_7)







   def      SCREAMING_SNAKE_CASE__       (self)      ->   Dict:
      """simple docstring"""






      self.config_tester.run_common_tests()







   def      SCREAMING_SNAKE_CASE__       (self)      ->   Optional[int]:
      """simple docstring"""






      __snake_case					:   Dict  				=			self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*__a)







   def      SCREAMING_SNAKE_CASE__       (self)      ->   Union[str, Any]:
      """simple docstring"""






      __snake_case					:   List[Any]  				=			self.model_tester.prepare_config_and_inputs()
      for type in ["absolute", "relative_key", "relative_key_query"]:
         __snake_case					:   List[str]  				=			type
         self.model_tester.create_and_check_model(*__a)







   def      SCREAMING_SNAKE_CASE__       (self)      ->   str:
      """simple docstring"""






      __snake_case					:   Optional[int]  				=			self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_masked_lm(*__a)







   def      SCREAMING_SNAKE_CASE__       (self)      ->   Optional[Any]:
      """simple docstring"""






      __snake_case					:   Optional[Any]  				=			self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_sequence_classification(*__a)







   def      SCREAMING_SNAKE_CASE__       (self)      ->   List[Any]:
      """simple docstring"""






      __snake_case					:   Optional[int]  				=			self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_token_classification(*__a)







   def      SCREAMING_SNAKE_CASE__       (self)      ->   Optional[int]:
      """simple docstring"""






      __snake_case					:   Any  				=			self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_question_answering(*__a)







   @unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.')
   def      SCREAMING_SNAKE_CASE__       (self)      ->   List[Any]:
      """simple docstring"""






      pass




def 							_SCREAMING_SNAKE_CASE	(					)     ->			Optional[Any]:







   """simple docstring"""
   # Here we prepare a batch of 2 sequences to test a LayoutLM forward pass on:
   # fmt: off
   __snake_case					:   int  				=			torch.tensor([[1_01,10_19,10_14,10_16,10_37,1_28_49,47_47,10_04,1_42_46,22_78,54_39,45_24,50_02,29_30,21_93,29_30,43_41,32_08,10_05,10_55,21_71,28_48,1_13_00,35_31,1_02],[1_01,40_70,40_34,70_20,10_24,30_58,10_15,10_13,28_61,10_13,60_70,1_92_74,27_72,62_05,2_78_14,1_61_47,1_61_47,43_43,20_47,1_02_83,1_09_69,1_43_89,10_12,23_38,1_02]]	,		device=A	)  # noqa: E231
   __snake_case					:   str  				=			torch.tensor([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],]	,		device=A	)  # noqa: E231
   __snake_case					:   Dict  				=			torch.tensor([[[0,0,0,0],[4_23,2_37,4_40,2_51],[4_27,2_72,4_41,2_87],[4_19,1_15,4_37,1_29],[9_61,8_85,9_92,9_12],[2_56,38,3_30,58],[2_56,38,3_30,58],[3_36,42,3_53,57],[3_60,39,4_01,56],[3_60,39,4_01,56],[4_11,39,4_71,59],[4_79,41,5_28,59],[5_33,39,6_30,60],[67,1_13,1_34,1_31],[1_41,1_15,2_09,1_32],[68,1_49,1_33,1_66],[1_41,1_49,1_87,1_64],[1_95,1_48,2_87,1_65],[1_95,1_48,2_87,1_65],[1_95,1_48,2_87,1_65],[2_95,1_48,3_49,1_65],[4_41,1_49,4_92,1_66],[4_97,1_49,5_46,1_64],[64,2_01,1_25,2_18],[10_00,10_00,10_00,10_00]],[[0,0,0,0],[6_62,1_50,7_54,1_66],[6_65,1_99,7_42,2_11],[5_19,2_13,5_54,2_28],[5_19,2_13,5_54,2_28],[1_34,4_33,1_87,4_54],[1_30,4_67,2_04,4_80],[1_30,4_67,2_04,4_80],[1_30,4_67,2_04,4_80],[1_30,4_67,2_04,4_80],[1_30,4_67,2_04,4_80],[3_14,4_69,3_76,4_82],[5_04,6_84,5_82,7_06],[9_41,8_25,9_73,9_00],[9_41,8_25,9_73,9_00],[9_41,8_25,9_73,9_00],[9_41,8_25,9_73,9_00],[6_10,7_49,6_52,7_65],[1_30,6_59,1_68,6_72],[1_76,6_57,2_37,6_72],[2_38,6_57,3_12,6_72],[4_43,6_53,6_28,6_72],[4_43,6_53,6_28,6_72],[7_16,3_01,8_25,3_17],[10_00,10_00,10_00,10_00]]]	,		device=A	)  # noqa: E231
   __snake_case					:   Optional[int]  				=			torch.tensor([[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]]	,		device=A	)  # noqa: E231
   # these are sequence labels (i.e. at the token level)
   __snake_case					:   int  				=			torch.tensor([[-1_00,10,10,10,9,1,-1_00,7,7,-1_00,7,7,4,2,5,2,8,8,-1_00,-1_00,5,0,3,2,-1_00],[-1_00,12,12,12,-1_00,12,10,-1_00,-1_00,-1_00,-1_00,10,12,9,-1_00,-1_00,-1_00,10,10,10,9,12,-1_00,10,-1_00]]	,		device=A	)  # noqa: E231
   # fmt: on

   return input_ids, attention_mask, bbox, token_type_ids, labels





@require_torch
class a_     (     unittest.TestCase				):







   @slow
   def      SCREAMING_SNAKE_CASE__       (self)      ->   Dict:
      """simple docstring"""






      __snake_case					:   Optional[Any]  				=			LayoutLMModel.from_pretrained('microsoft/layoutlm-base-uncased').to(__a)

      __snake_case				,__snake_case				,__snake_case				,__snake_case				,__snake_case					:   List[Any]  				=			prepare_layoutlm_batch_inputs()

      # forward pass
      __snake_case					:   int  				=			model(input_ids=__a       , bbox=__a       , attention_mask=__a       , token_type_ids=__a)

      # test the sequence output on [0, :3, :3]
      __snake_case					:   str  				=			torch.tensor(
          [[0.1_785, -0.1_947, -0.0_425], [-0.3_254, -0.2_807, 0.2_553], [-0.5_391, -0.3_322, 0.3_364]]       , device=__a       , )

      self.assertTrue(torch.allclose(outputs.last_hidden_state[0, :3, :3]       , __a       , atol=1E-3))

      # test the pooled output on [1, :3]
      __snake_case					:   Union[str, Any]  				=			torch.tensor([-0.6_580, -0.0_214, 0.8_552]       , device=__a)

      self.assertTrue(torch.allclose(outputs.pooler_output[1, :3]       , __a       , atol=1E-3))







   @slow
   def      SCREAMING_SNAKE_CASE__       (self)      ->   Tuple:
      """simple docstring"""






      __snake_case					:   List[str]  				=			LayoutLMForSequenceClassification.from_pretrained('microsoft/layoutlm-base-uncased'       , num_labels=2).to(
          __a)

      __snake_case				,__snake_case				,__snake_case				,__snake_case				,__snake_case					:   Any  				=			prepare_layoutlm_batch_inputs()

      # forward pass
      __snake_case					:   Any  				=			model(
          input_ids=__a       , bbox=__a       , attention_mask=__a       , token_type_ids=__a       , labels=torch.tensor([1, 1]       , device=__a)       , )

      # test whether we get a loss as a scalar
      __snake_case					:   List[Any]  				=			outputs.loss
      __snake_case					:   Tuple  				=			torch.Size([])
      self.assertEqual(loss.shape       , __a)

      # test the shape of the logits
      __snake_case					:   int  				=			outputs.logits
      __snake_case					:   Tuple  				=			torch.Size((2, 2))
      self.assertEqual(logits.shape       , __a)







   @slow
   def      SCREAMING_SNAKE_CASE__       (self)      ->   List[Any]:
      """simple docstring"""






      __snake_case					:   List[str]  				=			LayoutLMForTokenClassification.from_pretrained('microsoft/layoutlm-base-uncased'       , num_labels=1_3).to(
          __a)

      __snake_case				,__snake_case				,__snake_case				,__snake_case				,__snake_case					:   List[str]  				=			prepare_layoutlm_batch_inputs()

      # forward pass
      __snake_case					:   Tuple  				=			model(
          input_ids=__a       , bbox=__a       , attention_mask=__a       , token_type_ids=__a       , labels=__a)

      # test the loss calculation to be around 2.65
      # expected_loss = torch.tensor(2.65, device=torch_device)

      # The loss is currently somewhat random and can vary between 0.1-0.3 atol.
      # self.assertTrue(torch.allclose(outputs.loss, expected_loss, atol=0.1))

      # test the shape of the logits
      __snake_case					:   Union[str, Any]  				=			outputs.logits
      __snake_case					:   Union[str, Any]  				=			torch.Size((2, 2_5, 1_3))
      self.assertEqual(logits.shape       , __a)







   @slow
   def      SCREAMING_SNAKE_CASE__       (self)      ->   str:
      """simple docstring"""






      __snake_case					:   Any  				=			LayoutLMForQuestionAnswering.from_pretrained('microsoft/layoutlm-base-uncased').to(__a)

      __snake_case				,__snake_case				,__snake_case				,__snake_case				,__snake_case					:   Any  				=			prepare_layoutlm_batch_inputs()

      # forward pass
      __snake_case					:   List[str]  				=			model(input_ids=__a       , bbox=__a       , attention_mask=__a       , token_type_ids=__a)

      # test the shape of the logits
      __snake_case					:   Optional[Any]  				=			torch.Size((2, 2_5))
      self.assertEqual(outputs.start_logits.shape       , __a)
      self.assertEqual(outputs.end_logits.shape       , __a)







'''simple docstring'''






import bsa
import requests




def 							_SCREAMING_SNAKE_CASE	(					A	:							bsa.element.ResultSet	)     ->			dict[str, str]:







   """simple docstring"""
   return {
       "name": soup.ha.a.text,
       "genre": soup.find('span'	,		class_='genre'	).text.strip(),
       "rating": soup.strong.text,
       "page_link": F"""https://www.imdb.com{soup.a.get("href"	)}""",
   }




def 							_SCREAMING_SNAKE_CASE	(					A	:							int = 5	)     ->			tuple:







   """simple docstring"""
   __snake_case					:   Optional[int]  				=			int(float(A	)	)
   if num_movies < 1:
      return ()
   __snake_case					:   Union[str, Any]  				=			(
       'https://www.imdb.com/search/title?title_type='
       F"""feature&sort=num_votes,desc&count={num_movies}"""
   )
   __snake_case					:   List[Any]  				=			bsa.BeautifulSoup(requests.get(A	).content	,		'html.parser'	)
   return tuple(
       get_movie_data_from_soup(A	)
       for movie in source.find_all('div'	,		class_='lister-item mode-advanced'	)	)


if __name__ == "__main__":
      import json

      __A     						= int(input('''How many movies would you like to see? '''))
      print(
          ''', '''.join(
              json.dumps(movie, indent=4) for movie in get_imdb_top_movies(num_movies)
          )
      )