


"""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 				SCREAMING_SNAKE_CASE__    :
            """simple docstring"""


            @staticmethod
            def 	lowercase__							(						*snake_case__		,					**snake_case__						):






              """simple docstring"""





              pass

     def  a__      (    SCREAMING_SNAKE_CASE   :   List[Any]    ):


       '''simple docstring'''







       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.
lowerCAmelCase__				    =       (
    '''https://huggingface.co/spaces/impira/docquery/resolve/2f6c96314dc84dfda62d40de9da55f2f5165d403/invoice.png'''
)


@is_pipeline_test
@require_torch
@require_vision
class 				SCREAMING_SNAKE_CASE__    (		unittest.TestCase    ):
       """simple docstring"""


       a  :    Any       =MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING

       @require_pytesseract
       @require_vision
       def 	lowercase__							(						self		,					snake_case__		,					snake_case__		,					snake_case__						):






         """simple docstring"""





         lowerCAmelCase					:					str								=							pipeline(
             "document-question-answering"		,					model=snake_case__		,					tokenizer=snake_case__		,					image_processor=snake_case__						)

         lowerCAmelCase					:					str								=							INVOICE_URL
         lowerCAmelCase					:					Tuple								=							list(zip(*apply_tesseract(load_image(snake_case__						)		,					snake_case__		,					""						)						)						)
         lowerCAmelCase					:					Optional[Any]								=							"What is the placebo?"
         lowerCAmelCase					:					List[Any]								=							[
             {
                 "image": load_image(snake_case__						),
                 "question": question,
             },
             {
                 "image": image,
                 "question": question,
             },
             {
                 "image": image,
                 "question": question,
                 "word_boxes": word_boxes,
             },
         ]
         return dqa_pipeline, examples

       def 	lowercase__							(						self		,					snake_case__		,					snake_case__						):






         """simple docstring"""





         lowerCAmelCase					:					Union[str, Any]								=							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 	lowercase__							(						self						):






         """simple docstring"""





         lowerCAmelCase					:					Optional[Any]								=							pipeline("document-question-answering"		,					model="hf-internal-testing/tiny-random-layoutlmv2"						)
         lowerCAmelCase					:					Optional[Any]								=							INVOICE_URL
         lowerCAmelCase					:					str								=							"How many cats are there?"

         lowerCAmelCase					:					int								=							[
             {"score": 0.0001, "answer": "oy 2312/2019", "start": 38, "end": 39},
             {"score": 0.0001, "answer": "oy 2312/2019 DUE", "start": 38, "end": 40},
         ]
         lowerCAmelCase					:					List[str]								=							dqa_pipeline(image=snake_case__		,					question=snake_case__		,					top_k=2						)
         self.assertEqual(nested_simplify(snake_case__		,					decimals=4						)		,					snake_case__						)

         lowerCAmelCase					:					Any								=							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
         lowerCAmelCase					:					Optional[int]								=							"./tests/fixtures/tests_samples/COCO/000000039769.png"
         lowerCAmelCase					:					List[str]								=							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
         lowerCAmelCase					:					Union[str, Any]								=							"./tests/fixtures/tests_samples/COCO/000000039769.png"
         lowerCAmelCase					:					List[str]								=							[]
         lowerCAmelCase					:					List[str]								=							[]
         lowerCAmelCase					:					str								=							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 	lowercase__							(						self						):






         """simple docstring"""





         lowerCAmelCase					:					int								=							pipeline(
             "document-question-answering"		,					model="tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa"		,					revision="9977165"		,					)
         lowerCAmelCase					:					str								=							INVOICE_URL
         lowerCAmelCase					:					Optional[int]								=							"What is the invoice number?"

         lowerCAmelCase					:					List[Any]								=							dqa_pipeline(image=snake_case__		,					question=snake_case__		,					top_k=2						)
         self.assertEqual(
             nested_simplify(snake_case__		,					decimals=4						)		,					[
                 {"score": 0.9944, "answer": "us-001", "start": 16, "end": 16},
                 {"score": 0.0009, "answer": "us-001", "start": 16, "end": 16},
             ]		,					)

         lowerCAmelCase					:					Tuple								=							dqa_pipeline({"image": image, "question": question}		,					top_k=2						)
         self.assertEqual(
             nested_simplify(snake_case__		,					decimals=4						)		,					[
                 {"score": 0.9944, "answer": "us-001", "start": 16, "end": 16},
                 {"score": 0.0009, "answer": "us-001", "start": 16, "end": 16},
             ]		,					)

         lowerCAmelCase					:					Optional[int]								=							dqa_pipeline(
             [{"image": image, "question": question}, {"image": image, "question": question}]		,					top_k=2						)
         self.assertEqual(
             nested_simplify(snake_case__		,					decimals=4						)		,					[
                 [
                     {"score": 0.9944, "answer": "us-001", "start": 16, "end": 16},
                     {"score": 0.0009, "answer": "us-001", "start": 16, "end": 16},
                 ],
             ]
             * 2		,					)

       @slow
       @require_torch
       @require_detectrona
       @require_pytesseract
       def 	lowercase__							(						self						):






         """simple docstring"""





         lowerCAmelCase					:					Optional[Any]								=							pipeline(
             "document-question-answering"		,					model="tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa"		,					revision="9977165"		,					max_seq_len=50		,					)
         lowerCAmelCase					:					Optional[int]								=							INVOICE_URL
         lowerCAmelCase					:					Union[str, Any]								=							"What is the invoice number?"

         lowerCAmelCase					:					Tuple								=							dqa_pipeline(image=snake_case__		,					question=snake_case__		,					top_k=2						)
         self.assertEqual(
             nested_simplify(snake_case__		,					decimals=4						)		,					[
                 {"score": 0.9974, "answer": "1110212019", "start": 23, "end": 23},
                 {"score": 0.9948, "answer": "us-001", "start": 16, "end": 16},
             ]		,					)

         lowerCAmelCase					:					List[Any]								=							dqa_pipeline({"image": image, "question": question}		,					top_k=2						)
         self.assertEqual(
             nested_simplify(snake_case__		,					decimals=4						)		,					[
                 {"score": 0.9974, "answer": "1110212019", "start": 23, "end": 23},
                 {"score": 0.9948, "answer": "us-001", "start": 16, "end": 16},
             ]		,					)

         lowerCAmelCase					:					Union[str, Any]								=							dqa_pipeline(
             [{"image": image, "question": question}, {"image": image, "question": question}]		,					top_k=2						)
         self.assertEqual(
             nested_simplify(snake_case__		,					decimals=4						)		,					[
                 [
                     {"score": 0.9974, "answer": "1110212019", "start": 23, "end": 23},
                     {"score": 0.9948, "answer": "us-001", "start": 16, "end": 16},
                 ]
             ]
             * 2		,					)

       @slow
       @require_torch
       @require_pytesseract
       @require_vision
       def 	lowercase__							(						self						):






         """simple docstring"""





         lowerCAmelCase					:					Tuple								=							AutoTokenizer.from_pretrained(
             "impira/layoutlm-document-qa"		,					revision="3dc6de3"		,					add_prefix_space=snake_case__						)
         lowerCAmelCase					:					str								=							pipeline(
             "document-question-answering"		,					model="impira/layoutlm-document-qa"		,					tokenizer=snake_case__		,					revision="3dc6de3"		,					)
         lowerCAmelCase					:					str								=							INVOICE_URL
         lowerCAmelCase					:					Dict								=							"What is the invoice number?"

         lowerCAmelCase					:					Optional[Any]								=							dqa_pipeline(image=snake_case__		,					question=snake_case__		,					top_k=2						)
         self.assertEqual(
             nested_simplify(snake_case__		,					decimals=4						)		,					[
                 {"score": 0.4251, "answer": "us-001", "start": 16, "end": 16},
                 {"score": 0.0819, "answer": "1110212019", "start": 23, "end": 23},
             ]		,					)

         lowerCAmelCase					:					Optional[Any]								=							dqa_pipeline({"image": image, "question": question}		,					top_k=2						)
         self.assertEqual(
             nested_simplify(snake_case__		,					decimals=4						)		,					[
                 {"score": 0.4251, "answer": "us-001", "start": 16, "end": 16},
                 {"score": 0.0819, "answer": "1110212019", "start": 23, "end": 23},
             ]		,					)

         lowerCAmelCase					:					Any								=							dqa_pipeline(
             [{"image": image, "question": question}, {"image": image, "question": question}]		,					top_k=2						)
         self.assertEqual(
             nested_simplify(snake_case__		,					decimals=4						)		,					[
                 [
                     {"score": 0.4251, "answer": "us-001", "start": 16, "end": 16},
                     {"score": 0.0819, "answer": "1110212019", "start": 23, "end": 23},
                 ]
             ]
             * 2		,					)

         lowerCAmelCase					:					List[Any]								=							list(zip(*apply_tesseract(load_image(snake_case__						)		,					snake_case__		,					""						)						)						)

         # This model should also work if `image` is set to None
         lowerCAmelCase					:					Optional[Any]								=							dqa_pipeline({"image": None, "word_boxes": word_boxes, "question": question}		,					top_k=2						)
         self.assertEqual(
             nested_simplify(snake_case__		,					decimals=4						)		,					[
                 {"score": 0.4251, "answer": "us-001", "start": 16, "end": 16},
                 {"score": 0.0819, "answer": "1110212019", "start": 23, "end": 23},
             ]		,					)

       @slow
       @require_torch
       @require_pytesseract
       @require_vision
       def 	lowercase__							(						self						):






         """simple docstring"""





         lowerCAmelCase					:					Any								=							AutoTokenizer.from_pretrained(
             "impira/layoutlm-document-qa"		,					revision="3dc6de3"		,					add_prefix_space=snake_case__						)
         lowerCAmelCase					:					Tuple								=							pipeline(
             "document-question-answering"		,					model="impira/layoutlm-document-qa"		,					tokenizer=snake_case__		,					revision="3dc6de3"		,					max_seq_len=50		,					)
         lowerCAmelCase					:					Any								=							INVOICE_URL
         lowerCAmelCase					:					Dict								=							"What is the invoice number?"

         lowerCAmelCase					:					Union[str, Any]								=							dqa_pipeline(image=snake_case__		,					question=snake_case__		,					top_k=2						)
         self.assertEqual(
             nested_simplify(snake_case__		,					decimals=4						)		,					[
                 {"score": 0.9999, "answer": "us-001", "start": 16, "end": 16},
                 {"score": 0.9998, "answer": "us-001", "start": 16, "end": 16},
             ]		,					)

         lowerCAmelCase					:					Union[str, Any]								=							dqa_pipeline(
             [{"image": image, "question": question}, {"image": image, "question": question}]		,					top_k=2						)
         self.assertEqual(
             nested_simplify(snake_case__		,					decimals=4						)		,					[
                 [
                     {"score": 0.9999, "answer": "us-001", "start": 16, "end": 16},
                     {"score": 0.9998, "answer": "us-001", "start": 16, "end": 16},
                 ]
             ]
             * 2		,					)

         lowerCAmelCase					:					Tuple								=							list(zip(*apply_tesseract(load_image(snake_case__						)		,					snake_case__		,					""						)						)						)

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

       @slow
       @require_torch
       def 	lowercase__							(						self						):






         """simple docstring"""





         lowerCAmelCase					:					Optional[int]								=							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"		,					)

         lowerCAmelCase					:					Optional[Any]								=							INVOICE_URL
         lowerCAmelCase					:					Union[str, Any]								=							"What is the invoice number?"
         lowerCAmelCase					:					Union[str, Any]								=							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 	lowercase__							(						self						):






         """simple docstring"""





         pass









"""simple docstring"""







from ...configuration_utils import PretrainedConfig


lowerCAmelCase__				    =       {
    '''google/tapas-base-finetuned-sqa''': (
        '''https://huggingface.co/google/tapas-base-finetuned-sqa/resolve/main/config.json'''
    ),
    '''google/tapas-base-finetuned-wtq''': (
        '''https://huggingface.co/google/tapas-base-finetuned-wtq/resolve/main/config.json'''
    ),
    '''google/tapas-base-finetuned-wikisql-supervised''': (
        '''https://huggingface.co/google/tapas-base-finetuned-wikisql-supervised/resolve/main/config.json'''
    ),
    '''google/tapas-base-finetuned-tabfact''': (
        '''https://huggingface.co/google/tapas-base-finetuned-tabfact/resolve/main/config.json'''
    ),
}


class 				SCREAMING_SNAKE_CASE__    (		lowercase    ):
       """simple docstring"""


       a  :    Any       ="tapas"

       def __init__(						self		,					snake_case__=30_522		,					snake_case__=768		,					snake_case__=12		,					snake_case__=12		,					snake_case__=3_072		,					snake_case__="gelu"		,					snake_case__=0.1		,					snake_case__=0.1		,					snake_case__=1_024		,					snake_case__=[3, 256, 256, 2, 256, 256, 10]		,					snake_case__=0.02		,					snake_case__=1e-12		,					snake_case__=0		,					snake_case__=10.0		,					snake_case__=0		,					snake_case__=1.0		,					snake_case__=None		,					snake_case__=1.0		,					snake_case__=False		,					snake_case__=None		,					snake_case__=1.0		,					snake_case__=1.0		,					snake_case__=False		,					snake_case__=False		,					snake_case__="ratio"		,					snake_case__=None		,					snake_case__=None		,					snake_case__=64		,					snake_case__=32		,					snake_case__=False		,					snake_case__=True		,					snake_case__=False		,					snake_case__=False		,					snake_case__=True		,					snake_case__=False		,					snake_case__=None		,					snake_case__=None		,					**snake_case__		,					):






         """simple docstring"""





         super().__init__(pad_token_id=snake_case__		,					**snake_case__						)

         # BERT hyperparameters (with updated max_position_embeddings and type_vocab_sizes)
         lowerCAmelCase					:					List[Any]								=							vocab_size
         lowerCAmelCase					:					List[str]								=							hidden_size
         lowerCAmelCase					:					Optional[Any]								=							num_hidden_layers
         lowerCAmelCase					:					List[str]								=							num_attention_heads
         lowerCAmelCase					:					Optional[Any]								=							hidden_act
         lowerCAmelCase					:					List[Any]								=							intermediate_size
         lowerCAmelCase					:					Optional[Any]								=							hidden_dropout_prob
         lowerCAmelCase					:					str								=							attention_probs_dropout_prob
         lowerCAmelCase					:					Any								=							max_position_embeddings
         lowerCAmelCase					:					Dict								=							type_vocab_sizes
         lowerCAmelCase					:					Union[str, Any]								=							initializer_range
         lowerCAmelCase					:					str								=							layer_norm_eps

         # Fine-tuning task hyperparameters
         lowerCAmelCase					:					Dict								=							positive_label_weight
         lowerCAmelCase					:					Union[str, Any]								=							num_aggregation_labels
         lowerCAmelCase					:					Optional[Any]								=							aggregation_loss_weight
         lowerCAmelCase					:					List[Any]								=							use_answer_as_supervision
         lowerCAmelCase					:					Dict								=							answer_loss_importance
         lowerCAmelCase					:					List[Any]								=							use_normalized_answer_loss
         lowerCAmelCase					:					List[str]								=							huber_loss_delta
         lowerCAmelCase					:					Optional[int]								=							temperature
         lowerCAmelCase					:					Optional[int]								=							aggregation_temperature
         lowerCAmelCase					:					Any								=							use_gumbel_for_cells
         lowerCAmelCase					:					Union[str, Any]								=							use_gumbel_for_aggregation
         lowerCAmelCase					:					Union[str, Any]								=							average_approximation_function
         lowerCAmelCase					:					int								=							cell_selection_preference
         lowerCAmelCase					:					Dict								=							answer_loss_cutoff
         lowerCAmelCase					:					Optional[int]								=							max_num_rows
         lowerCAmelCase					:					Union[str, Any]								=							max_num_columns
         lowerCAmelCase					:					Any								=							average_logits_per_cell
         lowerCAmelCase					:					List[Any]								=							select_one_column
         lowerCAmelCase					:					Tuple								=							allow_empty_column_selection
         lowerCAmelCase					:					str								=							init_cell_selection_weights_to_zero
         lowerCAmelCase					:					List[Any]								=							reset_position_index_per_cell
         lowerCAmelCase					:					Optional[Any]								=							disable_per_token_loss

         # Aggregation hyperparameters
         lowerCAmelCase					:					List[str]								=							aggregation_labels
         lowerCAmelCase					:					List[str]								=							no_aggregation_label_index

         if isinstance(self.aggregation_labels		,					snake_case__						):
           lowerCAmelCase					:					Union[str, Any]								=							{int(snake_case__						): v for k, v in aggregation_labels.items()}





