





'''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     UpperCamelCase__  (    *__a	:   Any   ,							**__a	:   List[str]   ):
													pass






					def 					_lowerCamelCase							(					lowercase		: Dict		)      ->      Union[str, Any]:
									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_   :       Dict       =     (
    '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  =MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING




				@require_pytesseract
				@require_vision
				def     UpperCamelCase__  (    self	:   Union[str, Any]   ,							__a	:   List[Any]   ,							__a	:   Dict   ,							__a	:   Any   ):
								_a				      =					pipeline(
								    "document-question-answering"   ,							model=__a   ,							tokenizer=__a   ,							image_processor=__a   )

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




				def     UpperCamelCase__  (    self	:   List[Any]   ,							__a	:   Dict   ,							__a	:   Any   ):
								_a				      =					dqa_pipeline(__a   ,							top_k=2   )
								self.assertEqual(
								    __a   ,							[
								        [
								            {"score": ANY(__a   ), "answer": ANY(__a   ), "start": ANY(__a   ), "end": ANY(__a   )},
								            {"score": ANY(__a   ), "answer": ANY(__a   ), "start": ANY(__a   ), "end": ANY(__a   )},
								        ]
								    ]
								    * 3   ,							)




				@require_torch
				@require_detectrona
				@require_pytesseract
				def     UpperCamelCase__  (    self	:   Tuple   ):
								_a				      =					pipeline("document-question-answering"   ,							model="hf-internal-testing/tiny-random-layoutlmv2"   )
								_a				      =					INVOICE_URL
								_a				      =					"How many cats are there?"

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

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

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

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




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

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

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

								_a				      =					dqa_pipeline(
								    [{"image": image, "question": question}, {"image": image, "question": question}]   ,							top_k=2   )
								self.assertEqual(
								    nested_simplify(__a   ,							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     UpperCamelCase__  (    self	:   Any   ):
								_a				      =					pipeline(
								    "document-question-answering"   ,							model="tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa"   ,							revision="9977165"   ,							max_seq_len=50   ,							)
								_a				      =					INVOICE_URL
								_a				      =					"What is the invoice number?"

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

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

								_a				      =					dqa_pipeline(
								    [{"image": image, "question": question}, {"image": image, "question": question}]   ,							top_k=2   )
								self.assertEqual(
								    nested_simplify(__a   ,							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     UpperCamelCase__  (    self	:   Any   ):
								_a				      =					AutoTokenizer.from_pretrained(
								    "impira/layoutlm-document-qa"   ,							revision="3dc6de3"   ,							add_prefix_space=__a   )
								_a				      =					pipeline(
								    "document-question-answering"   ,							model="impira/layoutlm-document-qa"   ,							tokenizer=__a   ,							revision="3dc6de3"   ,							)
								_a				      =					INVOICE_URL
								_a				      =					"What is the invoice number?"

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

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

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

								_a				      =					list(zip(*apply_tesseract(load_image(__a   )   ,							__a   ,							""   )   )   )

								# This model should also work if `image` is set to None
								_a				      =					dqa_pipeline({"image": None, "word_boxes": word_boxes, "question": question}   ,							top_k=2   )
								self.assertEqual(
								    nested_simplify(__a   ,							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     UpperCamelCase__  (    self	:   Dict   ):
								_a				      =					AutoTokenizer.from_pretrained(
								    "impira/layoutlm-document-qa"   ,							revision="3dc6de3"   ,							add_prefix_space=__a   )
								_a				      =					pipeline(
								    "document-question-answering"   ,							model="impira/layoutlm-document-qa"   ,							tokenizer=__a   ,							revision="3dc6de3"   ,							max_seq_len=50   ,							)
								_a				      =					INVOICE_URL
								_a				      =					"What is the invoice number?"

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

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

								_a				      =					list(zip(*apply_tesseract(load_image(__a   )   ,							__a   ,							""   )   )   )

								# This model should also work if `image` is set to None
								_a				      =					dqa_pipeline({"image": None, "word_boxes": word_boxes, "question": question}   ,							top_k=2   )
								self.assertEqual(
								    nested_simplify(__a   ,							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     UpperCamelCase__  (    self	:   Tuple   ):
								_a				      =					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"   ,							)

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




				@require_tf
				@unittest.skip("Document question answering not implemented in TF"   )
				def     UpperCamelCase__  (    self	:   Tuple   ):
								pass




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


__UpperCamelCase     		=							logging.get_logger(__name__)

__UpperCamelCase     		=							{
    'microsoft/trocr-base-handwritten': (
        'https://huggingface.co/microsoft/trocr-base-handwritten/resolve/main/config.json'
    ),
    # See all TrOCR models at https://huggingface.co/models?filter=trocr
}







class 							lowerCamelCase__						(				UpperCAmelCase   ):






			"""simple docstring"""






			_UpperCamelCase		:       Dict 			=		'trocr'
			_UpperCamelCase		:       Union[str, Any] 			=		['past_key_values']
			_UpperCamelCase		:       Union[str, Any] 			=		{
			    'num_attention_heads': 'decoder_attention_heads',
			    'hidden_size': 'd_model',
			    'num_hidden_layers': 'decoder_layers',
			}




			def __init__(				self   ,    snake_case=50265   ,    snake_case=1024   ,    snake_case=12   ,    snake_case=16   ,    snake_case=4096   ,    snake_case="gelu"   ,    snake_case=512   ,    snake_case=0.1   ,    snake_case=0.0   ,    snake_case=0.0   ,    snake_case=2   ,    snake_case=0.02   ,    snake_case=0.0   ,    snake_case=True   ,    snake_case=False   ,    snake_case=True   ,    snake_case=True   ,    snake_case=1   ,    snake_case=0   ,    snake_case=2   ,    **snake_case   ,    ):

						'''simple docstring'''


						UpperCamelCase__       =							vocab_size
						UpperCamelCase__       =							d_model
						UpperCamelCase__       =							decoder_layers
						UpperCamelCase__       =							decoder_attention_heads
						UpperCamelCase__       =							decoder_ffn_dim
						UpperCamelCase__       =							activation_function
						UpperCamelCase__       =							max_position_embeddings
						UpperCamelCase__       =							dropout
						UpperCamelCase__       =							attention_dropout
						UpperCamelCase__       =							activation_dropout
						UpperCamelCase__       =							init_std
						UpperCamelCase__       =							decoder_layerdrop
						UpperCamelCase__       =							use_cache
						UpperCamelCase__       =							scale_embedding
						UpperCamelCase__       =							use_learned_position_embeddings
						UpperCamelCase__       =							layernorm_embedding

						super().__init__(
						    pad_token_id=snake_case   ,    bos_token_id=snake_case   ,    eos_token_id=snake_case   ,    decoder_start_token_id=snake_case   ,    **snake_case   ,    )


