





"""simple docstring"""




import unittest

import numpy as np

from transformers import AlbertConfig, is_flax_available
from transformers.testing_utils import require_flax, slow

from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask


if is_flax_available():
		import jax.numpy as jnp

		from transformers.models.albert.modeling_flax_albert import (
		    FlaxAlbertForMaskedLM,
		    FlaxAlbertForMultipleChoice,
		    FlaxAlbertForPreTraining,
		    FlaxAlbertForQuestionAnswering,
		    FlaxAlbertForSequenceClassification,
		    FlaxAlbertForTokenClassification,
		    FlaxAlbertModel,
		)


class        __UpperCamelCase				(						unittest.TestCase						):






							def __init__(   self      ,					lowerCAmelCase__      ,					lowerCAmelCase__=13      ,					lowerCAmelCase__=7      ,					lowerCAmelCase__=True      ,					lowerCAmelCase__=True      ,					lowerCAmelCase__=True      ,					lowerCAmelCase__=True      ,					lowerCAmelCase__=99      ,					lowerCAmelCase__=32      ,					lowerCAmelCase__=5      ,					lowerCAmelCase__=4      ,					lowerCAmelCase__=37      ,					lowerCAmelCase__="gelu"      ,					lowerCAmelCase__=0.1      ,					lowerCAmelCase__=0.1      ,					lowerCAmelCase__=512      ,					lowerCAmelCase__=16      ,					lowerCAmelCase__=2      ,					lowerCAmelCase__=0.02      ,					lowerCAmelCase__=4      ,					)	-> Optional[int]:
								a      :		Any	=				parent
								a      :		Any	=				batch_size
								a      :		List[Any]	=				seq_length
								a      :		Union[str, Any]	=				is_training
								a      :		Any	=				use_attention_mask
								a      :		Dict	=				use_token_type_ids
								a      :		Tuple	=				use_labels
								a      :		Tuple	=				vocab_size
								a      :		int	=				hidden_size
								a      :		str	=				num_hidden_layers
								a      :		List[Any]	=				num_attention_heads
								a      :		List[Any]	=				intermediate_size
								a      :		Optional[Any]	=				hidden_act
								a      :		List[Any]	=				hidden_dropout_prob
								a      :		int	=				attention_probs_dropout_prob
								a      :		Union[str, Any]	=				max_position_embeddings
								a      :		Optional[Any]	=				type_vocab_size
								a      :		List[str]	=				type_sequence_label_size
								a      :		Any	=				initializer_range
								a      :		Any	=				num_choices






							def     __a   (   self      )	-> Dict:
								a      :		List[str]	=				ids_tensor([self.batch_size, self.seq_length]      ,					self.vocab_size      )

								a      :		Dict	=				None
								if self.use_attention_mask:
									a      :		Optional[Any]	=				random_attention_mask([self.batch_size, self.seq_length]      )

								a      :		str	=				None
								if self.use_token_type_ids:
									a      :		Dict	=				ids_tensor([self.batch_size, self.seq_length]      ,					self.type_vocab_size      )

								a      :		Union[str, Any]	=				AlbertConfig(
								    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      ,					is_decoder=lowerCAmelCase__      ,					initializer_range=self.initializer_range      ,					)

								return config, input_ids, token_type_ids, attention_mask






							def     __a   (   self      )	-> Any:
								a      :		str	=				self.prepare_config_and_inputs()
								a,       a,       a,       a      :		Optional[Any]	=				config_and_inputs
								a      :		int	=				{"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": attention_mask}
								return config, inputs_dict


@require_flax
class        __UpperCamelCase				(						a__		,       unittest.TestCase						):
							lowerCamelCase   :					int      					=(
							    (
							        FlaxAlbertModel,
							        FlaxAlbertForPreTraining,
							        FlaxAlbertForMaskedLM,
							        FlaxAlbertForMultipleChoice,
							        FlaxAlbertForQuestionAnswering,
							        FlaxAlbertForSequenceClassification,
							        FlaxAlbertForTokenClassification,
							        FlaxAlbertForQuestionAnswering,
							    )
							    if is_flax_available()
							    else ()
							)






							def     __a   (   self      )	-> Tuple:
								a      :		Optional[int]	=				FlaxAlbertModelTester(self      )






							@slow
							def     __a   (   self      )	-> Any:
								for model_class_name in self.all_model_classes:
									a      :		Optional[int]	=				model_class_name.from_pretrained("albert-base-v2"      )
									a      :		Tuple	=				model(np.ones((1, 1)      )      )
									self.assertIsNotNone(lowerCAmelCase__      )


@require_flax
class        __UpperCamelCase				(						unittest.TestCase						):






							@slow
							def     __a   (   self      )	-> Optional[int]:
								a      :		int	=				FlaxAlbertModel.from_pretrained("albert-base-v2"      )
								a      :		int	=				np.array([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]      )
								a      :		Union[str, Any]	=				np.array([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]      )
								a      :		Optional[int]	=				model(lowerCAmelCase__      ,					attention_mask=lowerCAmelCase__      )[0]
								a      :		str	=				(1, 11, 768)
								self.assertEqual(output.shape      ,					lowerCAmelCase__      )
								a      :		Any	=				np.array(
								    [[[-0.6_513, 1.5_035, -0.2_766], [-0.6_515, 1.5_046, -0.2_780], [-0.6_512, 1.5_049, -0.2_784]]]      )

								self.assertTrue(jnp.allclose(output[:, 1:4, 1:4]      ,					lowerCAmelCase__      ,					atol=1E-4      )      )









"""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        __UpperCamelCase				:






									@staticmethod
									def     __a   (   *lowerCAmelCase__      ,					**lowerCAmelCase__      )	-> str:
										pass






		def       _SCREAMING_SNAKE_CASE		(	_lowercase						:    Tuple					) ->Dict:






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


@is_pipeline_test
@require_torch
@require_vision
class        __UpperCamelCase				(						unittest.TestCase						):
							lowerCamelCase   :					Union[str, Any]      					=MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING






							@require_pytesseract
							@require_vision
							def     __a   (   self      ,					lowerCAmelCase__      ,					lowerCAmelCase__      ,					lowerCAmelCase__      )	-> Tuple:
								a      :		Tuple	=				pipeline(
								    "document-question-answering"      ,					model=lowerCAmelCase__      ,					tokenizer=lowerCAmelCase__      ,					image_processor=lowerCAmelCase__      )

								a      :		Optional[int]	=				INVOICE_URL
								a      :		str	=				list(zip(*apply_tesseract(load_image(lowerCAmelCase__      )      ,					lowerCAmelCase__      ,					""      )      )      )
								a      :		Union[str, Any]	=				"What is the placebo?"
								a      :		Dict	=				[
								    {
								        "image": load_image(lowerCAmelCase__      ),
								        "question": question,
								    },
								    {
								        "image": image,
								        "question": question,
								    },
								    {
								        "image": image,
								        "question": question,
								        "word_boxes": word_boxes,
								    },
								]
								return dqa_pipeline, examples






							def     __a   (   self      ,					lowerCAmelCase__      ,					lowerCAmelCase__      )	-> Dict:
								a      :		Tuple	=				dqa_pipeline(lowerCAmelCase__      ,					top_k=2      )
								self.assertEqual(
								    lowerCAmelCase__      ,					[
								        [
								            {"score": ANY(lowerCAmelCase__      ), "answer": ANY(lowerCAmelCase__      ), "start": ANY(lowerCAmelCase__      ), "end": ANY(lowerCAmelCase__      )},
								            {"score": ANY(lowerCAmelCase__      ), "answer": ANY(lowerCAmelCase__      ), "start": ANY(lowerCAmelCase__      ), "end": ANY(lowerCAmelCase__      )},
								        ]
								    ]
								    * 3      ,					)






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

								a      :		Tuple	=				[
								    {"score": 0.0_001, "answer": "oy 2312/2019", "start": 38, "end": 39},
								    {"score": 0.0_001, "answer": "oy 2312/2019 DUE", "start": 38, "end": 40},
								]
								a      :		Optional[int]	=				dqa_pipeline(image=lowerCAmelCase__      ,					question=lowerCAmelCase__      ,					top_k=2      )
								self.assertEqual(nested_simplify(lowerCAmelCase__      ,					decimals=4      )      ,					lowerCAmelCase__      )

								a      :		Optional[int]	=				dqa_pipeline({"image": image, "question": question}      ,					top_k=2      )
								self.assertEqual(nested_simplify(lowerCAmelCase__      ,					decimals=4      )      ,					lowerCAmelCase__      )

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

								# We can optionnally pass directly the words and bounding boxes
								a      :		Optional[int]	=				"./tests/fixtures/tests_samples/COCO/000000039769.png"
								a      :		Tuple	=				[]
								a      :		Optional[int]	=				[]
								a      :		List[str]	=				dqa_pipeline(image=lowerCAmelCase__      ,					question=lowerCAmelCase__      ,					words=lowerCAmelCase__      ,					boxes=lowerCAmelCase__      ,					top_k=2      )
								self.assertEqual(lowerCAmelCase__      ,					[]      )






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

								a      :		int	=				dqa_pipeline(image=lowerCAmelCase__      ,					question=lowerCAmelCase__      ,					top_k=2      )
								self.assertEqual(
								    nested_simplify(lowerCAmelCase__      ,					decimals=4      )      ,					[
								        {"score": 0.9_944, "answer": "us-001", "start": 16, "end": 16},
								        {"score": 0.0_009, "answer": "us-001", "start": 16, "end": 16},
								    ]      ,					)

								a      :		str	=				dqa_pipeline({"image": image, "question": question}      ,					top_k=2      )
								self.assertEqual(
								    nested_simplify(lowerCAmelCase__      ,					decimals=4      )      ,					[
								        {"score": 0.9_944, "answer": "us-001", "start": 16, "end": 16},
								        {"score": 0.0_009, "answer": "us-001", "start": 16, "end": 16},
								    ]      ,					)

								a      :		Any	=				dqa_pipeline(
								    [{"image": image, "question": question}, {"image": image, "question": question}]      ,					top_k=2      )
								self.assertEqual(
								    nested_simplify(lowerCAmelCase__      ,					decimals=4      )      ,					[
								        [
								            {"score": 0.9_944, "answer": "us-001", "start": 16, "end": 16},
								            {"score": 0.0_009, "answer": "us-001", "start": 16, "end": 16},
								        ],
								    ]
								    * 2      ,					)






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

								a      :		List[Any]	=				dqa_pipeline(image=lowerCAmelCase__      ,					question=lowerCAmelCase__      ,					top_k=2      )
								self.assertEqual(
								    nested_simplify(lowerCAmelCase__      ,					decimals=4      )      ,					[
								        {"score": 0.9_974, "answer": "1110212019", "start": 23, "end": 23},
								        {"score": 0.9_948, "answer": "us-001", "start": 16, "end": 16},
								    ]      ,					)

								a      :		str	=				dqa_pipeline({"image": image, "question": question}      ,					top_k=2      )
								self.assertEqual(
								    nested_simplify(lowerCAmelCase__      ,					decimals=4      )      ,					[
								        {"score": 0.9_974, "answer": "1110212019", "start": 23, "end": 23},
								        {"score": 0.9_948, "answer": "us-001", "start": 16, "end": 16},
								    ]      ,					)

								a      :		Tuple	=				dqa_pipeline(
								    [{"image": image, "question": question}, {"image": image, "question": question}]      ,					top_k=2      )
								self.assertEqual(
								    nested_simplify(lowerCAmelCase__      ,					decimals=4      )      ,					[
								        [
								            {"score": 0.9_974, "answer": "1110212019", "start": 23, "end": 23},
								            {"score": 0.9_948, "answer": "us-001", "start": 16, "end": 16},
								        ]
								    ]
								    * 2      ,					)






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

								a      :		List[str]	=				dqa_pipeline(image=lowerCAmelCase__      ,					question=lowerCAmelCase__      ,					top_k=2      )
								self.assertEqual(
								    nested_simplify(lowerCAmelCase__      ,					decimals=4      )      ,					[
								        {"score": 0.4_251, "answer": "us-001", "start": 16, "end": 16},
								        {"score": 0.0_819, "answer": "1110212019", "start": 23, "end": 23},
								    ]      ,					)

								a      :		Dict	=				dqa_pipeline({"image": image, "question": question}      ,					top_k=2      )
								self.assertEqual(
								    nested_simplify(lowerCAmelCase__      ,					decimals=4      )      ,					[
								        {"score": 0.4_251, "answer": "us-001", "start": 16, "end": 16},
								        {"score": 0.0_819, "answer": "1110212019", "start": 23, "end": 23},
								    ]      ,					)

								a      :		List[Any]	=				dqa_pipeline(
								    [{"image": image, "question": question}, {"image": image, "question": question}]      ,					top_k=2      )
								self.assertEqual(
								    nested_simplify(lowerCAmelCase__      ,					decimals=4      )      ,					[
								        [
								            {"score": 0.4_251, "answer": "us-001", "start": 16, "end": 16},
								            {"score": 0.0_819, "answer": "1110212019", "start": 23, "end": 23},
								        ]
								    ]
								    * 2      ,					)

								a      :		Dict	=				list(zip(*apply_tesseract(load_image(lowerCAmelCase__      )      ,					lowerCAmelCase__      ,					""      )      )      )

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






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

								a      :		List[Any]	=				dqa_pipeline(image=lowerCAmelCase__      ,					question=lowerCAmelCase__      ,					top_k=2      )
								self.assertEqual(
								    nested_simplify(lowerCAmelCase__      ,					decimals=4      )      ,					[
								        {"score": 0.9_999, "answer": "us-001", "start": 16, "end": 16},
								        {"score": 0.9_998, "answer": "us-001", "start": 16, "end": 16},
								    ]      ,					)

								a      :		List[str]	=				dqa_pipeline(
								    [{"image": image, "question": question}, {"image": image, "question": question}]      ,					top_k=2      )
								self.assertEqual(
								    nested_simplify(lowerCAmelCase__      ,					decimals=4      )      ,					[
								        [
								            {"score": 0.9_999, "answer": "us-001", "start": 16, "end": 16},
								            {"score": 0.9_998, "answer": "us-001", "start": 16, "end": 16},
								        ]
								    ]
								    * 2      ,					)

								a      :		List[Any]	=				list(zip(*apply_tesseract(load_image(lowerCAmelCase__      )      ,					lowerCAmelCase__      ,					""      )      )      )

								# This model should also work if `image` is set to None
								a      :		Any	=				dqa_pipeline({"image": None, "word_boxes": word_boxes, "question": question}      ,					top_k=2      )
								self.assertEqual(
								    nested_simplify(lowerCAmelCase__      ,					decimals=4      )      ,					[
								        {"score": 0.9_999, "answer": "us-001", "start": 16, "end": 16},
								        {"score": 0.9_998, "answer": "us-001", "start": 16, "end": 16},
								    ]      ,					)






							@slow
							@require_torch
							def     __a   (   self      )	-> int:
								a      :		Tuple	=				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      :		Optional[Any]	=				INVOICE_URL
								a      :		Tuple	=				"What is the invoice number?"
								a      :		List[Any]	=				dqa_pipeline(image=lowerCAmelCase__      ,					question=lowerCAmelCase__      ,					top_k=2      )
								self.assertEqual(nested_simplify(lowerCAmelCase__      ,					decimals=4      )      ,					[{"answer": "us-001"}]      )






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


