

"""simple docstring"""





import unittest

from transformers import BarthezTokenizer, BarthezTokenizerFast, BatchEncoding
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow

from ...test_tokenization_common import TokenizerTesterMixin




@require_tokenizers
@require_sentencepiece
@slow  # see https://github.com/huggingface/transformers/issues/11457
class 					_lowerCAmelCase  (	lowercase     ,unittest.TestCase   ):







 """simple docstring"""







 __UpperCAmelCase	:					List[Any]   		=	BarthezTokenizer
 __UpperCAmelCase	:					Optional[Any]   		=	BarthezTokenizerFast
 __UpperCAmelCase	:					str   		=	True
 __UpperCAmelCase	:					Optional[int]   		=	True


 def 					_lowercase (			self							:			Union[str, Any]	):
    super().setUp()

    __lowercase								=						BarthezTokenizerFast.from_pretrained("moussaKam/mbarthez"	)
    tokenizer.save_pretrained(self.tmpdirname	)
    tokenizer.save_pretrained(self.tmpdirname,					legacy_format=UpperCAmelCase__	)
    __lowercase								=						tokenizer


 def 					_lowercase (			self							:			int	):
    __lowercase								=						"<pad>"
    __lowercase								=						1

    self.assertEqual(self.get_tokenizer()._convert_token_to_id(UpperCAmelCase__	),					UpperCAmelCase__	)
    self.assertEqual(self.get_tokenizer()._convert_id_to_token(UpperCAmelCase__	),					UpperCAmelCase__	)


 def 					_lowercase (			self							:			str	):
    __lowercase								=						list(self.get_tokenizer().get_vocab().keys()	)

    self.assertEqual(vocab_keys[0],					"<s>"	)
    self.assertEqual(vocab_keys[1],					"<pad>"	)
    self.assertEqual(vocab_keys[-1],					"<mask>"	)
    self.assertEqual(len(UpperCAmelCase__	),					1_0_1_1_2_2	)


 def 					_lowercase (			self							:			Optional[Any]	):
    self.assertEqual(self.get_tokenizer().vocab_size,					1_0_1_1_2_2	)


 @require_torch
 def 					_lowercase (			self							:			int	):
    __lowercase								=						["A long paragraph for summarization.", "Another paragraph for summarization."]
    __lowercase								=						[0, 5_7, 3_0_1_8, 7_0_3_0_7, 9_1, 2]

    __lowercase								=						self.tokenizer(
        UpperCAmelCase__,					max_length=len(UpperCAmelCase__	),					padding=UpperCAmelCase__,					truncation=UpperCAmelCase__,					return_tensors="pt"	)
    self.assertIsInstance(UpperCAmelCase__,					UpperCAmelCase__	)

    self.assertEqual((2, 6),					batch.input_ids.shape	)
    self.assertEqual((2, 6),					batch.attention_mask.shape	)
    __lowercase								=						batch.input_ids.tolist()[0]
    self.assertListEqual(UpperCAmelCase__,					UpperCAmelCase__	)


 def 					_lowercase (			self							:			Optional[Any]	):
    if not self.test_rust_tokenizer:
       return

    __lowercase								=						self.get_tokenizer()
    __lowercase								=						self.get_rust_tokenizer()

    __lowercase								=						"I was born in 92000, and this is falsé."

    __lowercase								=						tokenizer.tokenize(UpperCAmelCase__	)
    __lowercase								=						rust_tokenizer.tokenize(UpperCAmelCase__	)
    self.assertListEqual(UpperCAmelCase__,					UpperCAmelCase__	)

    __lowercase								=						tokenizer.encode(UpperCAmelCase__,					add_special_tokens=UpperCAmelCase__	)
    __lowercase								=						rust_tokenizer.encode(UpperCAmelCase__,					add_special_tokens=UpperCAmelCase__	)
    self.assertListEqual(UpperCAmelCase__,					UpperCAmelCase__	)

    __lowercase								=						self.get_rust_tokenizer()
    __lowercase								=						tokenizer.encode(UpperCAmelCase__	)
    __lowercase								=						rust_tokenizer.encode(UpperCAmelCase__	)
    self.assertListEqual(UpperCAmelCase__,					UpperCAmelCase__	)



 @slow
 def 					_lowercase (			self							:			Union[str, Any]	):
    # fmt: off
    __lowercase								=						{"input_ids": [[0, 4_9_0, 1_4_3_2_8, 4_5_0_7, 3_5_4, 4_7, 4_3_6_6_9, 9_5, 2_5, 7_8_1_1_7, 2_0_2_1_5, 1_9_7_7_9, 1_9_0, 2_2, 4_0_0, 4, 3_5_3_4_3, 8_0_3_1_0, 6_0_3, 8_6, 2_4_9_3_7, 1_0_5, 3_3_4_3_8, 9_4_7_6_2, 1_9_6, 3_9_6_4_2, 7, 1_5, 1_5_9_3_3, 1_7_3, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [0, 1_0_5_3_4, 8_7, 2_5, 6_6, 3_3_5_8, 1_9_6, 5_5_2_8_9, 8, 8_2_9_6_1, 8_1, 2_2_0_4, 7_5_2_0_3, 7, 1_5, 7_6_3, 1_2_9_5_6, 2_1_6, 1_7_8, 1_4_3_2_8, 9_5_9_5, 1_3_7_7, 6_9_6_9_3, 7, 4_4_8, 7_1_0_2_1, 1_9_6, 1_8_1_0_6, 1_4_3_7, 1_3_9_7_4, 1_0_8, 9_0_8_3, 4, 4_9_3_1_5, 7, 3_9, 8_6, 1_3_2_6, 2_7_9_3, 4_6_3_3_3, 4, 4_4_8, 1_9_6, 7_4_5_8_8, 7, 4_9_3_1_5, 7, 3_9, 2_1, 8_2_2, 3_8_4_7_0, 7_4, 2_1, 6_6_7_2_3, 6_2_4_8_0, 8, 2_2_0_5_0, 5, 2]], "attention_mask": [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]}  # noqa: E501
    # fmt: on

    # moussaKam/mbarthez is a french model. So we also use french texts.
    __lowercase								=						[
        "Le transformeur est un modèle d'apprentissage profond introduit en 2017, "
        "utilisé principalement dans le domaine du traitement automatique des langues (TAL).",
        "À l'instar des réseaux de neurones récurrents (RNN), les transformeurs sont conçus "
        "pour gérer des données séquentielles, telles que le langage naturel, pour des tâches "
        "telles que la traduction et la synthèse de texte.",
    ]

    self.tokenizer_integration_test_util(
        expected_encoding=UpperCAmelCase__,					model_name="moussaKam/mbarthez",					revision="c2e4ecbca5e3cd2c37fe1ac285ca4fbdf1366fb6",					sequences=UpperCAmelCase__,					)







"""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 					_lowercase (			*UpperCAmelCase__							:			Tuple,					**UpperCAmelCase__							:			List[Any]	):
       pass







   def   _A  (  UpperCamelCase_     :       Union[str, Any])	->						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.
_a       =							(
    'https://huggingface.co/spaces/impira/docquery/resolve/2f6c96314dc84dfda62d40de9da55f2f5165d403/invoice.png'
)




@is_pipeline_test
@require_torch
@require_vision
class 					_lowerCAmelCase  (	unittest.TestCase   ):







 """simple docstring"""







 __UpperCAmelCase	:					Tuple   		=	MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING


 @require_pytesseract
 @require_vision
 def 					_lowercase (			self							:			Optional[Any],					UpperCAmelCase__							:			Optional[Any],					UpperCAmelCase__							:			Dict,					UpperCAmelCase__							:			Optional[Any]	):
    __lowercase								=						pipeline(
        "document-question-answering",					model=UpperCAmelCase__,					tokenizer=UpperCAmelCase__,					image_processor=UpperCAmelCase__	)

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


 def 					_lowercase (			self							:			int,					UpperCAmelCase__							:			Tuple,					UpperCAmelCase__							:			Any	):
    __lowercase								=						dqa_pipeline(UpperCAmelCase__,					top_k=2	)
    self.assertEqual(
        UpperCAmelCase__,					[
            [
                {"score": ANY(UpperCAmelCase__	), "answer": ANY(UpperCAmelCase__	), "start": ANY(UpperCAmelCase__	), "end": ANY(UpperCAmelCase__	)},
                {"score": ANY(UpperCAmelCase__	), "answer": ANY(UpperCAmelCase__	), "start": ANY(UpperCAmelCase__	), "end": ANY(UpperCAmelCase__	)},
            ]
        ]
        * 3,					)


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

    __lowercase								=						[
        {"score": 0.0_001, "answer": "oy 2312/2019", "start": 3_8, "end": 3_9},
        {"score": 0.0_001, "answer": "oy 2312/2019 DUE", "start": 3_8, "end": 4_0},
    ]
    __lowercase								=						dqa_pipeline(image=UpperCAmelCase__,					question=UpperCAmelCase__,					top_k=2	)
    self.assertEqual(nested_simplify(UpperCAmelCase__,					decimals=4	),					UpperCAmelCase__	)

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

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

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


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

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

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

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


 @slow
 @require_torch
 @require_detectrona
 @require_pytesseract
 def 					_lowercase (			self							:			Dict	):
    __lowercase								=						pipeline(
        "document-question-answering",					model="tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa",					revision="9977165",					max_seq_len=5_0,					)
    __lowercase								=						INVOICE_URL
    __lowercase								=						"What is the invoice number?"

    __lowercase								=						dqa_pipeline(image=UpperCAmelCase__,					question=UpperCAmelCase__,					top_k=2	)
    self.assertEqual(
        nested_simplify(UpperCAmelCase__,					decimals=4	),					[
            {"score": 0.9_974, "answer": "1110212019", "start": 2_3, "end": 2_3},
            {"score": 0.9_948, "answer": "us-001", "start": 1_6, "end": 1_6},
        ],					)

    __lowercase								=						dqa_pipeline({"image": image, "question": question},					top_k=2	)
    self.assertEqual(
        nested_simplify(UpperCAmelCase__,					decimals=4	),					[
            {"score": 0.9_974, "answer": "1110212019", "start": 2_3, "end": 2_3},
            {"score": 0.9_948, "answer": "us-001", "start": 1_6, "end": 1_6},
        ],					)

    __lowercase								=						dqa_pipeline(
        [{"image": image, "question": question}, {"image": image, "question": question}],					top_k=2	)
    self.assertEqual(
        nested_simplify(UpperCAmelCase__,					decimals=4	),					[
            [
                {"score": 0.9_974, "answer": "1110212019", "start": 2_3, "end": 2_3},
                {"score": 0.9_948, "answer": "us-001", "start": 1_6, "end": 1_6},
            ]
        ]
        * 2,					)


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

    __lowercase								=						dqa_pipeline(image=UpperCAmelCase__,					question=UpperCAmelCase__,					top_k=2	)
    self.assertEqual(
        nested_simplify(UpperCAmelCase__,					decimals=4	),					[
            {"score": 0.4_251, "answer": "us-001", "start": 1_6, "end": 1_6},
            {"score": 0.0_819, "answer": "1110212019", "start": 2_3, "end": 2_3},
        ],					)

    __lowercase								=						dqa_pipeline({"image": image, "question": question},					top_k=2	)
    self.assertEqual(
        nested_simplify(UpperCAmelCase__,					decimals=4	),					[
            {"score": 0.4_251, "answer": "us-001", "start": 1_6, "end": 1_6},
            {"score": 0.0_819, "answer": "1110212019", "start": 2_3, "end": 2_3},
        ],					)

    __lowercase								=						dqa_pipeline(
        [{"image": image, "question": question}, {"image": image, "question": question}],					top_k=2	)
    self.assertEqual(
        nested_simplify(UpperCAmelCase__,					decimals=4	),					[
            [
                {"score": 0.4_251, "answer": "us-001", "start": 1_6, "end": 1_6},
                {"score": 0.0_819, "answer": "1110212019", "start": 2_3, "end": 2_3},
            ]
        ]
        * 2,					)

    __lowercase								=						list(zip(*apply_tesseract(load_image(UpperCAmelCase__	),					UpperCAmelCase__,					""	)	)	)

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


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

    __lowercase								=						dqa_pipeline(image=UpperCAmelCase__,					question=UpperCAmelCase__,					top_k=2	)
    self.assertEqual(
        nested_simplify(UpperCAmelCase__,					decimals=4	),					[
            {"score": 0.9_999, "answer": "us-001", "start": 1_6, "end": 1_6},
            {"score": 0.9_998, "answer": "us-001", "start": 1_6, "end": 1_6},
        ],					)

    __lowercase								=						dqa_pipeline(
        [{"image": image, "question": question}, {"image": image, "question": question}],					top_k=2	)
    self.assertEqual(
        nested_simplify(UpperCAmelCase__,					decimals=4	),					[
            [
                {"score": 0.9_999, "answer": "us-001", "start": 1_6, "end": 1_6},
                {"score": 0.9_998, "answer": "us-001", "start": 1_6, "end": 1_6},
            ]
        ]
        * 2,					)

    __lowercase								=						list(zip(*apply_tesseract(load_image(UpperCAmelCase__	),					UpperCAmelCase__,					""	)	)	)

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


 @slow
 @require_torch
 def 					_lowercase (			self							:			Dict	):
    __lowercase								=						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",					)

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



 @require_tf
 @unittest.skip("Document question answering not implemented in TF"	)
 def 					_lowercase (			self							:			List[Any]	):
    pass




