






import unittest

from transformers import MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING, is_vision_available
from transformers.pipelines import pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    require_torch,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
 from PIL import Image
else:



 class 							_UpperCamelCase			:


       @staticmethod
       def  __UpperCAmelCase		(	*__UpperCamelCase					,						**__UpperCamelCase	)->	Optional[int]:
            pass



@is_pipeline_test
@require_torch
@require_vision
class 							_UpperCamelCase			(unittest.TestCase			):
      snake_case_  =							MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING


      def  __UpperCAmelCase		(	self					,						__UpperCamelCase					,						__UpperCamelCase					,						__UpperCamelCase	)->	Any:
           __lowerCAmelCase								=					pipeline("visual-question-answering"					,						model="hf-internal-testing/tiny-vilt-random-vqa"	)
           __lowerCAmelCase								=					[
               {
                   "image": Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"	),
                   "question": "How many cats are there?",
               },
               {
                   "image": "./tests/fixtures/tests_samples/COCO/000000039769.png",
                   "question": "How many cats are there?",
               },
           ]
           return vqa_pipeline, examples


      def  __UpperCAmelCase		(	self					,						__UpperCamelCase					,						__UpperCamelCase	)->	int:
           __lowerCAmelCase								=					vqa_pipeline(__UpperCamelCase					,						top_k=1	)
           self.assertEqual(
               __UpperCamelCase					,						[
                   [{"score": ANY(__UpperCamelCase	), "answer": ANY(__UpperCamelCase	)}],
                   [{"score": ANY(__UpperCamelCase	), "answer": ANY(__UpperCamelCase	)}],
               ]					,						)


      @require_torch
      def  __UpperCAmelCase		(	self	)->	Optional[int]:
           __lowerCAmelCase								=					pipeline("visual-question-answering"					,						model="hf-internal-testing/tiny-vilt-random-vqa"	)
           __lowerCAmelCase								=					"./tests/fixtures/tests_samples/COCO/000000039769.png"
           __lowerCAmelCase								=					"How many cats are there?"

           __lowerCAmelCase								=					vqa_pipeline(image=__UpperCamelCase					,						question="How many cats are there?"					,						top_k=2	)
           self.assertEqual(
               __UpperCamelCase					,						[{"score": ANY(__UpperCamelCase	), "answer": ANY(__UpperCamelCase	)}, {"score": ANY(__UpperCamelCase	), "answer": ANY(__UpperCamelCase	)}]	)

           __lowerCAmelCase								=					vqa_pipeline({"image": image, "question": question}					,						top_k=2	)
           self.assertEqual(
               __UpperCamelCase					,						[{"score": ANY(__UpperCamelCase	), "answer": ANY(__UpperCamelCase	)}, {"score": ANY(__UpperCamelCase	), "answer": ANY(__UpperCamelCase	)}]	)


      @slow
      @require_torch
      def  __UpperCAmelCase		(	self	)->	str:
           __lowerCAmelCase								=					pipeline("visual-question-answering"					,						model="dandelin/vilt-b32-finetuned-vqa"	)
           __lowerCAmelCase								=					"./tests/fixtures/tests_samples/COCO/000000039769.png"
           __lowerCAmelCase								=					"How many cats are there?"

           __lowerCAmelCase								=					vqa_pipeline(image=__UpperCamelCase					,						question=__UpperCamelCase					,						top_k=2	)
           self.assertEqual(
               nested_simplify(__UpperCamelCase					,						decimals=4	)					,						[{"score": 0.8_7_9_9, "answer": "2"}, {"score": 0.2_9_6, "answer": "1"}]	)

           __lowerCAmelCase								=					vqa_pipeline({"image": image, "question": question}					,						top_k=2	)
           self.assertEqual(
               nested_simplify(__UpperCamelCase					,						decimals=4	)					,						[{"score": 0.8_7_9_9, "answer": "2"}, {"score": 0.2_9_6, "answer": "1"}]	)

           __lowerCAmelCase								=					vqa_pipeline(
               [{"image": image, "question": question}, {"image": image, "question": question}]					,						top_k=2	)
           self.assertEqual(
               nested_simplify(__UpperCamelCase					,						decimals=4	)					,						[[{"score": 0.8_7_9_9, "answer": "2"}, {"score": 0.2_9_6, "answer": "1"}]] * 2					,						)


      @require_tf
      @unittest.skip("Visual question answering not implemented in TF"	)
      def  __UpperCAmelCase		(	self	)->	Union[str, Any]:
           pass














from ...configuration_utils import PretrainedConfig
from ...utils import logging


lowerCamelCase			:					Any         =	logging.get_logger(__name__)

lowerCamelCase			:					Dict         =	{
    '''microsoft/markuplm-base''': '''https://huggingface.co/microsoft/markuplm-base/resolve/main/config.json''',
    '''microsoft/markuplm-large''': '''https://huggingface.co/microsoft/markuplm-large/resolve/main/config.json''',
}



class 							_UpperCamelCase			(a_			):
      snake_case_  =							"""markuplm"""


      def __init__(	self					,						__UpperCamelCase=3_0_5_2_2					,						__UpperCamelCase=7_6_8					,						__UpperCamelCase=1_2					,						__UpperCamelCase=1_2					,						__UpperCamelCase=3_0_7_2					,						__UpperCamelCase="gelu"					,						__UpperCamelCase=0.1					,						__UpperCamelCase=0.1					,						__UpperCamelCase=5_1_2					,						__UpperCamelCase=2					,						__UpperCamelCase=0.0_2					,						__UpperCamelCase=1e-12					,						__UpperCamelCase=0					,						__UpperCamelCase=0					,						__UpperCamelCase=2					,						__UpperCamelCase=2_5_6					,						__UpperCamelCase=1_0_2_4					,						__UpperCamelCase=2_1_6					,						__UpperCamelCase=1_0_0_1					,						__UpperCamelCase=3_2					,						__UpperCamelCase=5_0					,						__UpperCamelCase="absolute"					,						__UpperCamelCase=True					,						__UpperCamelCase=None					,						**__UpperCamelCase					,						)->	Dict:
           super().__init__(
               pad_token_id=__UpperCamelCase					,						bos_token_id=__UpperCamelCase					,						eos_token_id=__UpperCamelCase					,						**__UpperCamelCase					,						)
           __lowerCAmelCase								=					vocab_size
           __lowerCAmelCase								=					hidden_size
           __lowerCAmelCase								=					num_hidden_layers
           __lowerCAmelCase								=					num_attention_heads
           __lowerCAmelCase								=					hidden_act
           __lowerCAmelCase								=					intermediate_size
           __lowerCAmelCase								=					hidden_dropout_prob
           __lowerCAmelCase								=					attention_probs_dropout_prob
           __lowerCAmelCase								=					max_position_embeddings
           __lowerCAmelCase								=					type_vocab_size
           __lowerCAmelCase								=					initializer_range
           __lowerCAmelCase								=					layer_norm_eps
           __lowerCAmelCase								=					position_embedding_type
           __lowerCAmelCase								=					use_cache
           __lowerCAmelCase								=					classifier_dropout
           # additional properties
           __lowerCAmelCase								=					max_depth
           __lowerCAmelCase								=					max_xpath_tag_unit_embeddings
           __lowerCAmelCase								=					max_xpath_subs_unit_embeddings
           __lowerCAmelCase								=					tag_pad_id
           __lowerCAmelCase								=					subs_pad_id
           __lowerCAmelCase								=					xpath_unit_hidden_size






