






"""simple docstring"""



import unittest

from transformers import MarkupLMConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device
from transformers.utils import cached_property

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 (
     MarkupLMForQuestionAnswering,
     MarkupLMForSequenceClassification,
     MarkupLMForTokenClassification,
     MarkupLMModel,
 )

# TODO check dependencies
from transformers import MarkupLMFeatureExtractor, MarkupLMProcessor, MarkupLMTokenizer




class        lowerCAmelCase__       :
   def __init__(						self	:     Optional[Any]				,       snake_case__	:     Optional[int]				,       snake_case__	:     Optional[int]=1_3				,       snake_case__	:     List[Any]=7				,       snake_case__	:     Any=True				,       snake_case__	:     Optional[int]=True				,       snake_case__	:     Union[str, Any]=True				,       snake_case__	:     List[Any]=True				,       snake_case__	:     Tuple=9_9				,       snake_case__	:     Optional[Any]=3_2				,       snake_case__	:     int=5				,       snake_case__	:     int=4				,       snake_case__	:     Optional[int]=3_7				,       snake_case__	:     Union[str, Any]="gelu"				,       snake_case__	:     int=0.1				,       snake_case__	:     str=0.1				,       snake_case__	:     int=5_1_2				,       snake_case__	:     Optional[Any]=1_6				,       snake_case__	:     str=2				,       snake_case__	:     int=0.02				,       snake_case__	:     Optional[int]=3				,       snake_case__	:     int=None				,       snake_case__	:     Any=2_0				,       snake_case__	:     str=3_0				,       snake_case__	:     str=2				,       snake_case__	:     Optional[int]=2				,       snake_case__	:     Optional[Any]=1_0				,       ):


        '''simple docstring'''
        UpperCAmelCase__		:  Union[str, Any]				=				parent
        UpperCAmelCase__		:  List[Any]				=				batch_size
        UpperCAmelCase__		:  Tuple				=				seq_length
        UpperCAmelCase__		:  Optional[int]				=				is_training
        UpperCAmelCase__		:  List[Any]				=				use_input_mask
        UpperCAmelCase__		:  Optional[int]				=				use_token_type_ids
        UpperCAmelCase__		:  str				=				use_labels
        UpperCAmelCase__		:  int				=				vocab_size
        UpperCAmelCase__		:  List[Any]				=				hidden_size
        UpperCAmelCase__		:  Dict				=				num_hidden_layers
        UpperCAmelCase__		:  Any				=				num_attention_heads
        UpperCAmelCase__		:  Optional[Any]				=				intermediate_size
        UpperCAmelCase__		:  Optional[int]				=				hidden_act
        UpperCAmelCase__		:  Tuple				=				hidden_dropout_prob
        UpperCAmelCase__		:  Any				=				attention_probs_dropout_prob
        UpperCAmelCase__		:  Union[str, Any]				=				max_position_embeddings
        UpperCAmelCase__		:  int				=				type_vocab_size
        UpperCAmelCase__		:  Union[str, Any]				=				type_sequence_label_size
        UpperCAmelCase__		:  Any				=				initializer_range
        UpperCAmelCase__		:  Optional[Any]				=				num_labels
        UpperCAmelCase__		:  Optional[Any]				=				scope
        UpperCAmelCase__		:  List[Any]				=				max_xpath_tag_unit_embeddings
        UpperCAmelCase__		:  Dict				=				max_xpath_subs_unit_embeddings
        UpperCAmelCase__		:  Union[str, Any]				=				tag_pad_id
        UpperCAmelCase__		:  Optional[int]				=				subs_pad_id
        UpperCAmelCase__		:  Optional[int]				=				max_depth
   def 	__a				(						self	:     Optional[int]				):


        '''simple docstring'''
        UpperCAmelCase__		:  Optional[int]				=				ids_tensor([self.batch_size, self.seq_length]				,       self.vocab_size				)

        UpperCAmelCase__		:  str				=				ids_tensor(
            [self.batch_size, self.seq_length, self.max_depth]				,       self.max_xpath_tag_unit_embeddings				)

        UpperCAmelCase__		:  int				=				ids_tensor(
            [self.batch_size, self.seq_length, self.max_depth]				,       self.max_xpath_subs_unit_embeddings				)

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

        UpperCAmelCase__		:  Any				=				None
        if self.use_token_type_ids:
             UpperCAmelCase__		:  Optional[int]				=				ids_tensor([self.batch_size, self.seq_length]				,       self.type_vocab_size				)

        UpperCAmelCase__		:  Dict				=				None
        UpperCAmelCase__		:  Optional[int]				=				None
        if self.use_labels:
             UpperCAmelCase__		:  Optional[int]				=				ids_tensor([self.batch_size]				,       self.type_sequence_label_size				)
             UpperCAmelCase__		:  Tuple				=				ids_tensor([self.batch_size, self.seq_length]				,       self.num_labels				)

        UpperCAmelCase__		:  int				=				self.get_config()

        return (
            config,
            input_ids,
            xpath_tags_seq,
            xpath_subs_seq,
            token_type_ids,
            input_mask,
            sequence_labels,
            token_labels,
        )
   def 	__a				(						self	:     Union[str, Any]				):


        '''simple docstring'''
        return MarkupLMConfig(
            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				,       max_xpath_tag_unit_embeddings=self.max_xpath_tag_unit_embeddings				,       max_xpath_subs_unit_embeddings=self.max_xpath_subs_unit_embeddings				,       tag_pad_id=self.tag_pad_id				,       subs_pad_id=self.subs_pad_id				,       max_depth=self.max_depth				,       )
   def 	__a				(						self	:     Tuple				,       snake_case__	:     Any				,       snake_case__	:     str				,       snake_case__	:     List[str]				,       snake_case__	:     Any				,       snake_case__	:     Optional[Any]				,       snake_case__	:     int				,       snake_case__	:     List[str]				,       snake_case__	:     str				,       ):


        '''simple docstring'''
        UpperCAmelCase__		:  List[Any]				=				MarkupLMModel(config=snake_case__				)
        model.to(snake_case__				)
        model.eval()
        print("Configs:"				,       model.config.tag_pad_id				,       model.config.subs_pad_id				)
        UpperCAmelCase__		:  Tuple				=				model(snake_case__				,       attention_mask=snake_case__				,       token_type_ids=snake_case__				)
        UpperCAmelCase__		:  List[Any]				=				model(snake_case__				,       token_type_ids=snake_case__				)
        UpperCAmelCase__		:  Union[str, Any]				=				model(snake_case__				)
        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 	__a				(						self	:     List[Any]				,       snake_case__	:     Any				,       snake_case__	:     List[str]				,       snake_case__	:     str				,       snake_case__	:     List[Any]				,       snake_case__	:     List[Any]				,       snake_case__	:     str				,       snake_case__	:     Dict				,       snake_case__	:     Any				,       ):


        '''simple docstring'''
        UpperCAmelCase__		:  Optional[int]				=				self.num_labels
        UpperCAmelCase__		:  Any				=				MarkupLMForSequenceClassification(snake_case__				)
        model.to(snake_case__				)
        model.eval()
        UpperCAmelCase__		:  Tuple				=				model(
            snake_case__				,       xpath_tags_seq=snake_case__				,       xpath_subs_seq=snake_case__				,       attention_mask=snake_case__				,       token_type_ids=snake_case__				,       labels=snake_case__				,       )
        self.parent.assertEqual(result.logits.shape				,       (self.batch_size, self.num_labels)				)
   def 	__a				(						self	:     List[str]				,       snake_case__	:     List[str]				,       snake_case__	:     Optional[int]				,       snake_case__	:     str				,       snake_case__	:     Optional[int]				,       snake_case__	:     Union[str, Any]				,       snake_case__	:     Dict				,       snake_case__	:     Optional[int]				,       snake_case__	:     Any				,       ):


        '''simple docstring'''
        UpperCAmelCase__		:  Dict				=				self.num_labels
        UpperCAmelCase__		:  List[Any]				=				MarkupLMForTokenClassification(config=snake_case__				)
        model.to(snake_case__				)
        model.eval()
        UpperCAmelCase__		:  Dict				=				model(
            snake_case__				,       xpath_tags_seq=snake_case__				,       xpath_subs_seq=snake_case__				,       attention_mask=snake_case__				,       token_type_ids=snake_case__				,       labels=snake_case__				,       )
        self.parent.assertEqual(result.logits.shape				,       (self.batch_size, self.seq_length, self.num_labels)				)
   def 	__a				(						self	:     Tuple				,       snake_case__	:     str				,       snake_case__	:     Optional[int]				,       snake_case__	:     Optional[int]				,       snake_case__	:     List[str]				,       snake_case__	:     Optional[Any]				,       snake_case__	:     Optional[int]				,       snake_case__	:     Optional[int]				,       snake_case__	:     Any				,       ):


        '''simple docstring'''
        UpperCAmelCase__		:  str				=				MarkupLMForQuestionAnswering(config=snake_case__				)
        model.to(snake_case__				)
        model.eval()
        UpperCAmelCase__		:  Dict				=				model(
            snake_case__				,       xpath_tags_seq=snake_case__				,       xpath_subs_seq=snake_case__				,       attention_mask=snake_case__				,       token_type_ids=snake_case__				,       start_positions=snake_case__				,       end_positions=snake_case__				,       )
        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 	__a				(						self	:     Optional[int]				):


        '''simple docstring'''
        UpperCAmelCase__		:  Dict				=				self.prepare_config_and_inputs()
        (
            (
            UpperCAmelCase__
        )				,							(
            UpperCAmelCase__
        )				,							(
            UpperCAmelCase__
        )				,							(
            UpperCAmelCase__
        )				,							(
            UpperCAmelCase__
        )				,							(
            UpperCAmelCase__
        )				,							(
            UpperCAmelCase__
        )				,							(
            UpperCAmelCase__
        )				,							
        )		:  Any				=				config_and_inputs
        UpperCAmelCase__		:  List[Any]				=				{
            "input_ids": input_ids,
            "xpath_tags_seq": xpath_tags_seq,
            "xpath_subs_seq": xpath_subs_seq,
            "token_type_ids": token_type_ids,
            "attention_mask": input_mask,
        }
        return config, inputs_dict




@require_torch
class        lowerCAmelCase__       (  __magic_name__				,		__magic_name__				,		unittest.TestCase					):
   SCREAMING_SNAKE_CASE_					      =(
       (
           MarkupLMModel,
           MarkupLMForSequenceClassification,
           MarkupLMForTokenClassification,
           MarkupLMForQuestionAnswering,
       )
       if is_torch_available()
       else None
   )
   SCREAMING_SNAKE_CASE_					      =(
       {
           '''feature-extraction''': MarkupLMModel,
           '''question-answering''': MarkupLMForQuestionAnswering,
           '''text-classification''': MarkupLMForSequenceClassification,
           '''token-classification''': MarkupLMForTokenClassification,
           '''zero-shot''': MarkupLMForSequenceClassification,
       }
       if is_torch_available()
       else {}
   )
   def 	__a				(						self	:     Union[str, Any]				,       snake_case__	:     Optional[Any]				,       snake_case__	:     Union[str, Any]				,       snake_case__	:     Dict				,       snake_case__	:     List[Any]				,       snake_case__	:     Optional[Any]				):


        '''simple docstring'''
        # ValueError: Nodes must be of type `List[str]` (single pretokenized example), or `List[List[str]]`
        # (batch of pretokenized examples).
        return True
   def 	__a				(						self	:     Tuple				):


        '''simple docstring'''
        UpperCAmelCase__		:  Optional[Any]				=				MarkupLMModelTester(self				)
        UpperCAmelCase__		:  Tuple				=				ConfigTester(self				,       config_class=snake_case__				,       hidden_size=3_7				)
   def 	__a				(						self	:     int				):


        '''simple docstring'''
        self.config_tester.run_common_tests()
   def 	__a				(						self	:     Dict				):


        '''simple docstring'''
        UpperCAmelCase__		:  Dict				=				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*snake_case__				)
   def 	__a				(						self	:     int				):


        '''simple docstring'''
        UpperCAmelCase__		:  Union[str, Any]				=				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_sequence_classification(*snake_case__				)
   def 	__a				(						self	:     List[Any]				):


        '''simple docstring'''
        UpperCAmelCase__		:  Optional[int]				=				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_token_classification(*snake_case__				)




   def 	__a				(						self	:     Any				):


        '''simple docstring'''
        UpperCAmelCase__		:  Tuple				=				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_question_answering(*snake_case__				)




def      SCREAMING_SNAKE_CASE__    (   )->						Union[str, Any]:






     '''simple docstring'''



     UpperCAmelCase__		:  Dict				=				"\n    <!DOCTYPE html>\n    <html>\n    <head>\n    <title>Page Title</title>\n    </head>\n    <body>\n\n    <h1>This is a Heading</h1>\n    <p>This is a paragraph.</p>\n\n    </body>\n    </html>\n    "

     return html_string




@require_torch
class        lowerCAmelCase__       (  unittest.TestCase					):
   @cached_property
   def 	__a				(						self	:     int				):


        '''simple docstring'''
        # TODO use from_pretrained here
        UpperCAmelCase__		:  List[Any]				=				MarkupLMFeatureExtractor()
        UpperCAmelCase__		:  Dict				=				MarkupLMTokenizer.from_pretrained("microsoft/markuplm-base"				)

        return MarkupLMProcessor(snake_case__				,       snake_case__				)




   @slow
   def 	__a				(						self	:     Dict				):


        '''simple docstring'''
        UpperCAmelCase__		:  Union[str, Any]				=				MarkupLMModel.from_pretrained("microsoft/markuplm-base"				).to(snake_case__				)

        UpperCAmelCase__		:  Dict				=				self.default_processor

        UpperCAmelCase__		:  Any				=				processor(prepare_html_string()				,       return_tensors="pt"				)
        UpperCAmelCase__		:  Optional[int]				=				inputs.to(snake_case__				)

        # forward pass
        with torch.no_grad():
             UpperCAmelCase__		:  List[Any]				=				model(**snake_case__				)

        # verify the last hidden states
        UpperCAmelCase__		:  str				=				torch.Size([1, 1_4, 7_6_8]				)
        self.assertEqual(outputs.last_hidden_state.shape				,       snake_case__				)

        UpperCAmelCase__		:  Union[str, Any]				=				torch.tensor(
            [[0.0675, -0.0052, 0.5001], [-0.2281, 0.0802, 0.2192], [-0.0583, -0.3311, 0.1185]]				).to(snake_case__				)

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










"""simple docstring"""



import shutil

import requests


def      SCREAMING_SNAKE_CASE__    (   snake_case    :      str       , snake_case    :      bool = False       , snake_case    :      str = "."							)->						dict:






     '''simple docstring'''



     UpperCAmelCase__		:  Union[str, Any]				=				"https://api.nasa.gov/planetary/apod"
     return requests.get(snake_case       , params={"api_key": api_key}							).json()


def      SCREAMING_SNAKE_CASE__    (   snake_case    :      str       , snake_case    :      str = "."							)->						dict:






     '''simple docstring'''



     UpperCAmelCase__		:  List[Any]				=				get_apod_data(snake_case							)
     UpperCAmelCase__		:  str				=				apod_data["url"]
     UpperCAmelCase__		:  Tuple				=				img_url.split("/"							)[-1]
     UpperCAmelCase__		:  Dict				=				requests.get(snake_case       , stream=snake_case							)

     with open(f'{path}/{img_name}'       , "wb+"							) as img_file:
          shutil.copyfileobj(response.raw       , snake_case							)
     del response
     return apod_data


def      SCREAMING_SNAKE_CASE__    (   snake_case    :      str							)->						dict:






     '''simple docstring'''



     UpperCAmelCase__		:  List[str]				=				"https://images-api.nasa.gov/search"
     return requests.get(snake_case       , params={"q": query}							).json()


if __name__ == "__main__":
 print(save_apod("""YOUR API KEY"""))
 _lowerCAmelCase     :     List[Any]            =	get_archive_data("""apollo 2011""")["""collection"""]["""items"""]
 print(apollo_aaaa_items[0]["""data"""][0]["""description"""])


