





'''simple docstring'''




import unittest

from transformers import MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING, AutoTokenizer, is_vision_available
from transformers.pipelines import pipeline
from transformers.pipelines.document_question_answering import apply_tesseract
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_detectrona,
    require_pytesseract,
    require_tf,
    require_torch,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
 from PIL import Image

 from transformers.image_utils import load_image
else:
 class 			_lowerCAmelCase		:






      """simple docstring"""






      @staticmethod
      def      lowerCAmelCase			(   *__snake_case							:			Optional[Any]   ,       **__snake_case							:			List[str]						)->	str:
         pass





 def 	__lowerCamelCase			(      __lowerCAmelCase       :							Optional[Any]		)		->  int:
    return None


# This is a pinned image from a specific revision of a document question answering space, hosted by HuggingFace,
# so we can expect it to be available.
_SCREAMING_SNAKE_CASE							     =			(
    "https://huggingface.co/spaces/impira/docquery/resolve/2f6c96314dc84dfda62d40de9da55f2f5165d403/invoice.png"
)
@is_pipeline_test
@require_torch
@require_vision
class 			_lowerCAmelCase		(			unittest.TestCase	):






     """simple docstring"""






     snake_case_						      =   MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING



     @require_pytesseract
     @require_vision
     def      lowerCAmelCase			(   self							:			Optional[Any]   ,       __snake_case							:			Union[str, Any]   ,       __snake_case							:			Any   ,       __snake_case							:			Optional[int]						)->	Dict:
        snake_case 						=   pipeline(
            """document-question-answering"""   ,       model=__snake_case   ,       tokenizer=__snake_case   ,       image_processor=__snake_case						)

        snake_case 						=   INVOICE_URL
        snake_case 						=   list(zip(*apply_tesseract(load_image(__snake_case						)   ,       __snake_case   ,       """"""						)						)						)
        snake_case 						=   """What is the placebo?"""
        snake_case 						=   [
            {
                """image""": load_image(__snake_case						),
                """question""": question,
            },
            {
                """image""": image,
                """question""": question,
            },
            {
                """image""": image,
                """question""": question,
                """word_boxes""": word_boxes,
            },
        ]
        return dqa_pipeline, examples



     def      lowerCAmelCase			(   self							:			Optional[int]   ,       __snake_case							:			Dict   ,       __snake_case							:			Optional[int]						)->	List[Any]:
        snake_case 						=   dqa_pipeline(__snake_case   ,       top_k=2						)
        self.assertEqual(
            __snake_case   ,       [
                [
                    {"""score""": ANY(__snake_case						), """answer""": ANY(__snake_case						), """start""": ANY(__snake_case						), """end""": ANY(__snake_case						)},
                    {"""score""": ANY(__snake_case						), """answer""": ANY(__snake_case						), """start""": ANY(__snake_case						), """end""": ANY(__snake_case						)},
                ]
            ]
            * 3   ,       )



     @require_torch
     @require_detectrona
     @require_pytesseract
     def      lowerCAmelCase			(   self							:			Optional[int]						)->	Optional[Any]:
        snake_case 						=   pipeline("""document-question-answering"""   ,       model="""hf-internal-testing/tiny-random-layoutlmv2"""						)
        snake_case 						=   INVOICE_URL
        snake_case 						=   """How many cats are there?"""

        snake_case 						=   [
            {"""score""": 0.00_01, """answer""": """oy 2312/2019""", """start""": 38, """end""": 39},
            {"""score""": 0.00_01, """answer""": """oy 2312/2019 DUE""", """start""": 38, """end""": 40},
        ]
        snake_case 						=   dqa_pipeline(image=__snake_case   ,       question=__snake_case   ,       top_k=2						)
        self.assertEqual(nested_simplify(__snake_case   ,       decimals=4						)   ,       __snake_case						)

        snake_case 						=   dqa_pipeline({"""image""": image, """question""": question}   ,       top_k=2						)
        self.assertEqual(nested_simplify(__snake_case   ,       decimals=4						)   ,       __snake_case						)

        # This image does not detect ANY text in it, meaning layoutlmv2 should fail.
        # Empty answer probably
        snake_case 						=   """./tests/fixtures/tests_samples/COCO/000000039769.png"""
        snake_case 						=   dqa_pipeline(image=__snake_case   ,       question=__snake_case   ,       top_k=2						)
        self.assertEqual(__snake_case   ,       []						)

        # We can optionnally pass directly the words and bounding boxes
        snake_case 						=   """./tests/fixtures/tests_samples/COCO/000000039769.png"""
        snake_case 						=   []
        snake_case 						=   []
        snake_case 						=   dqa_pipeline(image=__snake_case   ,       question=__snake_case   ,       words=__snake_case   ,       boxes=__snake_case   ,       top_k=2						)
        self.assertEqual(__snake_case   ,       []						)



     @slow
     @require_torch
     @require_detectrona
     @require_pytesseract
     def      lowerCAmelCase			(   self							:			List[Any]						)->	Dict:
        snake_case 						=   pipeline(
            """document-question-answering"""   ,       model="""tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa"""   ,       revision="""9977165"""   ,       )
        snake_case 						=   INVOICE_URL
        snake_case 						=   """What is the invoice number?"""

        snake_case 						=   dqa_pipeline(image=__snake_case   ,       question=__snake_case   ,       top_k=2						)
        self.assertEqual(
            nested_simplify(__snake_case   ,       decimals=4						)   ,       [
                {"""score""": 0.99_44, """answer""": """us-001""", """start""": 16, """end""": 16},
                {"""score""": 0.00_09, """answer""": """us-001""", """start""": 16, """end""": 16},
            ]   ,       )

        snake_case 						=   dqa_pipeline({"""image""": image, """question""": question}   ,       top_k=2						)
        self.assertEqual(
            nested_simplify(__snake_case   ,       decimals=4						)   ,       [
                {"""score""": 0.99_44, """answer""": """us-001""", """start""": 16, """end""": 16},
                {"""score""": 0.00_09, """answer""": """us-001""", """start""": 16, """end""": 16},
            ]   ,       )

        snake_case 						=   dqa_pipeline(
            [{"""image""": image, """question""": question}, {"""image""": image, """question""": question}]   ,       top_k=2						)
        self.assertEqual(
            nested_simplify(__snake_case   ,       decimals=4						)   ,       [
                [
                    {"""score""": 0.99_44, """answer""": """us-001""", """start""": 16, """end""": 16},
                    {"""score""": 0.00_09, """answer""": """us-001""", """start""": 16, """end""": 16},
                ],
            ]
            * 2   ,       )



     @slow
     @require_torch
     @require_detectrona
     @require_pytesseract
     def      lowerCAmelCase			(   self							:			Dict						)->	List[Any]:
        snake_case 						=   pipeline(
            """document-question-answering"""   ,       model="""tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa"""   ,       revision="""9977165"""   ,       max_seq_len=50   ,       )
        snake_case 						=   INVOICE_URL
        snake_case 						=   """What is the invoice number?"""

        snake_case 						=   dqa_pipeline(image=__snake_case   ,       question=__snake_case   ,       top_k=2						)
        self.assertEqual(
            nested_simplify(__snake_case   ,       decimals=4						)   ,       [
                {"""score""": 0.99_74, """answer""": """1110212019""", """start""": 23, """end""": 23},
                {"""score""": 0.99_48, """answer""": """us-001""", """start""": 16, """end""": 16},
            ]   ,       )

        snake_case 						=   dqa_pipeline({"""image""": image, """question""": question}   ,       top_k=2						)
        self.assertEqual(
            nested_simplify(__snake_case   ,       decimals=4						)   ,       [
                {"""score""": 0.99_74, """answer""": """1110212019""", """start""": 23, """end""": 23},
                {"""score""": 0.99_48, """answer""": """us-001""", """start""": 16, """end""": 16},
            ]   ,       )

        snake_case 						=   dqa_pipeline(
            [{"""image""": image, """question""": question}, {"""image""": image, """question""": question}]   ,       top_k=2						)
        self.assertEqual(
            nested_simplify(__snake_case   ,       decimals=4						)   ,       [
                [
                    {"""score""": 0.99_74, """answer""": """1110212019""", """start""": 23, """end""": 23},
                    {"""score""": 0.99_48, """answer""": """us-001""", """start""": 16, """end""": 16},
                ]
            ]
            * 2   ,       )



     @slow
     @require_torch
     @require_pytesseract
     @require_vision
     def      lowerCAmelCase			(   self							:			Optional[Any]						)->	Tuple:
        snake_case 						=   AutoTokenizer.from_pretrained(
            """impira/layoutlm-document-qa"""   ,       revision="""3dc6de3"""   ,       add_prefix_space=__snake_case						)
        snake_case 						=   pipeline(
            """document-question-answering"""   ,       model="""impira/layoutlm-document-qa"""   ,       tokenizer=__snake_case   ,       revision="""3dc6de3"""   ,       )
        snake_case 						=   INVOICE_URL
        snake_case 						=   """What is the invoice number?"""

        snake_case 						=   dqa_pipeline(image=__snake_case   ,       question=__snake_case   ,       top_k=2						)
        self.assertEqual(
            nested_simplify(__snake_case   ,       decimals=4						)   ,       [
                {"""score""": 0.42_51, """answer""": """us-001""", """start""": 16, """end""": 16},
                {"""score""": 0.08_19, """answer""": """1110212019""", """start""": 23, """end""": 23},
            ]   ,       )

        snake_case 						=   dqa_pipeline({"""image""": image, """question""": question}   ,       top_k=2						)
        self.assertEqual(
            nested_simplify(__snake_case   ,       decimals=4						)   ,       [
                {"""score""": 0.42_51, """answer""": """us-001""", """start""": 16, """end""": 16},
                {"""score""": 0.08_19, """answer""": """1110212019""", """start""": 23, """end""": 23},
            ]   ,       )

        snake_case 						=   dqa_pipeline(
            [{"""image""": image, """question""": question}, {"""image""": image, """question""": question}]   ,       top_k=2						)
        self.assertEqual(
            nested_simplify(__snake_case   ,       decimals=4						)   ,       [
                [
                    {"""score""": 0.42_51, """answer""": """us-001""", """start""": 16, """end""": 16},
                    {"""score""": 0.08_19, """answer""": """1110212019""", """start""": 23, """end""": 23},
                ]
            ]
            * 2   ,       )

        snake_case 						=   list(zip(*apply_tesseract(load_image(__snake_case						)   ,       __snake_case   ,       """"""						)						)						)

        # This model should also work if `image` is set to None
        snake_case 						=   dqa_pipeline({"""image""": None, """word_boxes""": word_boxes, """question""": question}   ,       top_k=2						)
        self.assertEqual(
            nested_simplify(__snake_case   ,       decimals=4						)   ,       [
                {"""score""": 0.42_51, """answer""": """us-001""", """start""": 16, """end""": 16},
                {"""score""": 0.08_19, """answer""": """1110212019""", """start""": 23, """end""": 23},
            ]   ,       )



     @slow
     @require_torch
     @require_pytesseract
     @require_vision
     def      lowerCAmelCase			(   self							:			List[Any]						)->	Union[str, Any]:
        snake_case 						=   AutoTokenizer.from_pretrained(
            """impira/layoutlm-document-qa"""   ,       revision="""3dc6de3"""   ,       add_prefix_space=__snake_case						)
        snake_case 						=   pipeline(
            """document-question-answering"""   ,       model="""impira/layoutlm-document-qa"""   ,       tokenizer=__snake_case   ,       revision="""3dc6de3"""   ,       max_seq_len=50   ,       )
        snake_case 						=   INVOICE_URL
        snake_case 						=   """What is the invoice number?"""

        snake_case 						=   dqa_pipeline(image=__snake_case   ,       question=__snake_case   ,       top_k=2						)
        self.assertEqual(
            nested_simplify(__snake_case   ,       decimals=4						)   ,       [
                {"""score""": 0.99_99, """answer""": """us-001""", """start""": 16, """end""": 16},
                {"""score""": 0.99_98, """answer""": """us-001""", """start""": 16, """end""": 16},
            ]   ,       )

        snake_case 						=   dqa_pipeline(
            [{"""image""": image, """question""": question}, {"""image""": image, """question""": question}]   ,       top_k=2						)
        self.assertEqual(
            nested_simplify(__snake_case   ,       decimals=4						)   ,       [
                [
                    {"""score""": 0.99_99, """answer""": """us-001""", """start""": 16, """end""": 16},
                    {"""score""": 0.99_98, """answer""": """us-001""", """start""": 16, """end""": 16},
                ]
            ]
            * 2   ,       )

        snake_case 						=   list(zip(*apply_tesseract(load_image(__snake_case						)   ,       __snake_case   ,       """"""						)						)						)

        # This model should also work if `image` is set to None
        snake_case 						=   dqa_pipeline({"""image""": None, """word_boxes""": word_boxes, """question""": question}   ,       top_k=2						)
        self.assertEqual(
            nested_simplify(__snake_case   ,       decimals=4						)   ,       [
                {"""score""": 0.99_99, """answer""": """us-001""", """start""": 16, """end""": 16},
                {"""score""": 0.99_98, """answer""": """us-001""", """start""": 16, """end""": 16},
            ]   ,       )



     @slow
     @require_torch
     def      lowerCAmelCase			(   self							:			int						)->	Union[str, Any]:
        snake_case 						=   pipeline(
            """document-question-answering"""   ,       model="""naver-clova-ix/donut-base-finetuned-docvqa"""   ,       tokenizer=AutoTokenizer.from_pretrained("""naver-clova-ix/donut-base-finetuned-docvqa"""						)   ,       feature_extractor="""naver-clova-ix/donut-base-finetuned-docvqa"""   ,       )

        snake_case 						=   INVOICE_URL
        snake_case 						=   """What is the invoice number?"""
        snake_case 						=   dqa_pipeline(image=__snake_case   ,       question=__snake_case   ,       top_k=2						)
        self.assertEqual(nested_simplify(__snake_case   ,       decimals=4						)   ,       [{"""answer""": """us-001"""}]						)



     @require_tf
     @unittest.skip("""Document question answering not implemented in TF"""						)
     def      lowerCAmelCase			(   self							:			str						)->	int:
        pass








'''simple docstring'''




import logging

from transformers.configuration_utils import PretrainedConfig


_SCREAMING_SNAKE_CASE							     =			logging.getLogger(__name__)
class 			_lowerCAmelCase		(			A__	):






     """simple docstring"""






     snake_case_						      =   "masked_bert"



     def __init__(   self							:			Any   ,       __snake_case							:			Optional[Any]=3_05_22   ,       __snake_case							:			List[Any]=7_68   ,       __snake_case							:			int=12   ,       __snake_case							:			List[str]=12   ,       __snake_case							:			int=30_72   ,       __snake_case							:			Optional[int]="gelu"   ,       __snake_case							:			Any=0.1   ,       __snake_case							:			Tuple=0.1   ,       __snake_case							:			List[str]=5_12   ,       __snake_case							:			Tuple=2   ,       __snake_case							:			Tuple=0.02   ,       __snake_case							:			Tuple=1e-12   ,       __snake_case							:			Union[str, Any]=0   ,       __snake_case							:			int="topK"   ,       __snake_case							:			int="constant"   ,       __snake_case							:			List[str]=0.0   ,       **__snake_case							:			Any   ,       )->	Union[str, Any]:
        super().__init__(pad_token_id=__snake_case   ,       **__snake_case						)

        snake_case 						=   vocab_size
        snake_case 						=   hidden_size
        snake_case 						=   num_hidden_layers
        snake_case 						=   num_attention_heads
        snake_case 						=   hidden_act
        snake_case 						=   intermediate_size
        snake_case 						=   hidden_dropout_prob
        snake_case 						=   attention_probs_dropout_prob
        snake_case 						=   max_position_embeddings
        snake_case 						=   type_vocab_size
        snake_case 						=   initializer_range
        snake_case 						=   layer_norm_eps
        snake_case 						=   pruning_method
        snake_case 						=   mask_init
        snake_case 						=   mask_scale

