


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   A__					:







     """simple docstring"""

     @staticmethod
     def 		a_			(       *__snake_case			,					**__snake_case    ):
          pass






 def 			UpperCAmelCase__						(UpperCamelCase_				):



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

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







    """simple docstring"""

    __magic_name__			  =		MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING
    @require_pytesseract
    @require_vision
    def 		a_			(       self			,					__snake_case			,					__snake_case			,					__snake_case    ):
         snake_case		 =				pipeline(
             '''document-question-answering'''			,					model=__UpperCamelCase			,					tokenizer=__UpperCamelCase			,					image_processor=__UpperCamelCase    )

         snake_case		 =				INVOICE_URL
         snake_case		 =				list(zip(*apply_tesseract(load_image(__UpperCamelCase    )			,					__UpperCamelCase			,					''''''    )    )    )
         snake_case		 =				'''What is the placebo?'''
         snake_case		 =				[
             {
                 '''image''': load_image(__UpperCamelCase    ),
                 '''question''': question,
             },
             {
                 '''image''': image,
                 '''question''': question,
             },
             {
                 '''image''': image,
                 '''question''': question,
                 '''word_boxes''': word_boxes,
             },
         ]
         return dqa_pipeline, examples
    def 		a_			(       self			,					__snake_case			,					__snake_case    ):
         snake_case		 =				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 		a_			(       self    ):
         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.0001, '''answer''': '''oy 2312/2019''', '''start''': 3_8, '''end''': 3_9},
             {'''score''': 0.0001, '''answer''': '''oy 2312/2019 DUE''', '''start''': 3_8, '''end''': 4_0},
         ]
         snake_case		 =				dqa_pipeline(image=__UpperCamelCase			,					question=__UpperCamelCase			,					top_k=2    )
         self.assertEqual(nested_simplify(__UpperCamelCase			,					decimals=4    )			,					__UpperCamelCase    )

         snake_case		 =				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
         snake_case		 =				'''./tests/fixtures/tests_samples/COCO/000000039769.png'''
         snake_case		 =				dqa_pipeline(image=__UpperCamelCase			,					question=__UpperCamelCase			,					top_k=2    )
         self.assertEqual(__UpperCamelCase			,					[]    )

         # 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=__UpperCamelCase			,					question=__UpperCamelCase			,					words=__UpperCamelCase			,					boxes=__UpperCamelCase			,					top_k=2    )
         self.assertEqual(__UpperCamelCase			,					[]    )
    @slow
    @require_torch
    @require_detectrona
    @require_pytesseract
    def 		a_			(       self    ):
         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=__UpperCamelCase			,					question=__UpperCamelCase			,					top_k=2    )
         self.assertEqual(
             nested_simplify(__UpperCamelCase			,					decimals=4    )			,					[
                 {'''score''': 0.9944, '''answer''': '''us-001''', '''start''': 1_6, '''end''': 1_6},
                 {'''score''': 0.0009, '''answer''': '''us-001''', '''start''': 1_6, '''end''': 1_6},
             ]			,					)

         snake_case		 =				dqa_pipeline({'''image''': image, '''question''': question}			,					top_k=2    )
         self.assertEqual(
             nested_simplify(__UpperCamelCase			,					decimals=4    )			,					[
                 {'''score''': 0.9944, '''answer''': '''us-001''', '''start''': 1_6, '''end''': 1_6},
                 {'''score''': 0.0009, '''answer''': '''us-001''', '''start''': 1_6, '''end''': 1_6},
             ]			,					)

         snake_case		 =				dqa_pipeline(
             [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}]			,					top_k=2    )
         self.assertEqual(
             nested_simplify(__UpperCamelCase			,					decimals=4    )			,					[
                 [
                     {'''score''': 0.9944, '''answer''': '''us-001''', '''start''': 1_6, '''end''': 1_6},
                     {'''score''': 0.0009, '''answer''': '''us-001''', '''start''': 1_6, '''end''': 1_6},
                 ],
             ]
             * 2			,					)
    @slow
    @require_torch
    @require_detectrona
    @require_pytesseract
    def 		a_			(       self    ):
         snake_case		 =				pipeline(
             '''document-question-answering'''			,					model='''tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa'''			,					revision='''9977165'''			,					max_seq_len=5_0			,					)
         snake_case		 =				INVOICE_URL
         snake_case		 =				'''What is the invoice number?'''

         snake_case		 =				dqa_pipeline(image=__UpperCamelCase			,					question=__UpperCamelCase			,					top_k=2    )
         self.assertEqual(
             nested_simplify(__UpperCamelCase			,					decimals=4    )			,					[
                 {'''score''': 0.9974, '''answer''': '''1110212019''', '''start''': 2_3, '''end''': 2_3},
                 {'''score''': 0.9948, '''answer''': '''us-001''', '''start''': 1_6, '''end''': 1_6},
             ]			,					)

         snake_case		 =				dqa_pipeline({'''image''': image, '''question''': question}			,					top_k=2    )
         self.assertEqual(
             nested_simplify(__UpperCamelCase			,					decimals=4    )			,					[
                 {'''score''': 0.9974, '''answer''': '''1110212019''', '''start''': 2_3, '''end''': 2_3},
                 {'''score''': 0.9948, '''answer''': '''us-001''', '''start''': 1_6, '''end''': 1_6},
             ]			,					)

         snake_case		 =				dqa_pipeline(
             [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}]			,					top_k=2    )
         self.assertEqual(
             nested_simplify(__UpperCamelCase			,					decimals=4    )			,					[
                 [
                     {'''score''': 0.9974, '''answer''': '''1110212019''', '''start''': 2_3, '''end''': 2_3},
                     {'''score''': 0.9948, '''answer''': '''us-001''', '''start''': 1_6, '''end''': 1_6},
                 ]
             ]
             * 2			,					)
    @slow
    @require_torch
    @require_pytesseract
    @require_vision
    def 		a_			(       self    ):
         snake_case		 =				AutoTokenizer.from_pretrained(
             '''impira/layoutlm-document-qa'''			,					revision='''3dc6de3'''			,					add_prefix_space=__UpperCamelCase    )
         snake_case		 =				pipeline(
             '''document-question-answering'''			,					model='''impira/layoutlm-document-qa'''			,					tokenizer=__UpperCamelCase			,					revision='''3dc6de3'''			,					)
         snake_case		 =				INVOICE_URL
         snake_case		 =				'''What is the invoice number?'''

         snake_case		 =				dqa_pipeline(image=__UpperCamelCase			,					question=__UpperCamelCase			,					top_k=2    )
         self.assertEqual(
             nested_simplify(__UpperCamelCase			,					decimals=4    )			,					[
                 {'''score''': 0.4251, '''answer''': '''us-001''', '''start''': 1_6, '''end''': 1_6},
                 {'''score''': 0.0819, '''answer''': '''1110212019''', '''start''': 2_3, '''end''': 2_3},
             ]			,					)

         snake_case		 =				dqa_pipeline({'''image''': image, '''question''': question}			,					top_k=2    )
         self.assertEqual(
             nested_simplify(__UpperCamelCase			,					decimals=4    )			,					[
                 {'''score''': 0.4251, '''answer''': '''us-001''', '''start''': 1_6, '''end''': 1_6},
                 {'''score''': 0.0819, '''answer''': '''1110212019''', '''start''': 2_3, '''end''': 2_3},
             ]			,					)

         snake_case		 =				dqa_pipeline(
             [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}]			,					top_k=2    )
         self.assertEqual(
             nested_simplify(__UpperCamelCase			,					decimals=4    )			,					[
                 [
                     {'''score''': 0.4251, '''answer''': '''us-001''', '''start''': 1_6, '''end''': 1_6},
                     {'''score''': 0.0819, '''answer''': '''1110212019''', '''start''': 2_3, '''end''': 2_3},
                 ]
             ]
             * 2			,					)

         snake_case		 =				list(zip(*apply_tesseract(load_image(__UpperCamelCase    )			,					__UpperCamelCase			,					''''''    )    )    )

         # 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(__UpperCamelCase			,					decimals=4    )			,					[
                 {'''score''': 0.4251, '''answer''': '''us-001''', '''start''': 1_6, '''end''': 1_6},
                 {'''score''': 0.0819, '''answer''': '''1110212019''', '''start''': 2_3, '''end''': 2_3},
             ]			,					)
    @slow
    @require_torch
    @require_pytesseract
    @require_vision
    def 		a_			(       self    ):
         snake_case		 =				AutoTokenizer.from_pretrained(
             '''impira/layoutlm-document-qa'''			,					revision='''3dc6de3'''			,					add_prefix_space=__UpperCamelCase    )
         snake_case		 =				pipeline(
             '''document-question-answering'''			,					model='''impira/layoutlm-document-qa'''			,					tokenizer=__UpperCamelCase			,					revision='''3dc6de3'''			,					max_seq_len=5_0			,					)
         snake_case		 =				INVOICE_URL
         snake_case		 =				'''What is the invoice number?'''

         snake_case		 =				dqa_pipeline(image=__UpperCamelCase			,					question=__UpperCamelCase			,					top_k=2    )
         self.assertEqual(
             nested_simplify(__UpperCamelCase			,					decimals=4    )			,					[
                 {'''score''': 0.9999, '''answer''': '''us-001''', '''start''': 1_6, '''end''': 1_6},
                 {'''score''': 0.9998, '''answer''': '''us-001''', '''start''': 1_6, '''end''': 1_6},
             ]			,					)

         snake_case		 =				dqa_pipeline(
             [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}]			,					top_k=2    )
         self.assertEqual(
             nested_simplify(__UpperCamelCase			,					decimals=4    )			,					[
                 [
                     {'''score''': 0.9999, '''answer''': '''us-001''', '''start''': 1_6, '''end''': 1_6},
                     {'''score''': 0.9998, '''answer''': '''us-001''', '''start''': 1_6, '''end''': 1_6},
                 ]
             ]
             * 2			,					)

         snake_case		 =				list(zip(*apply_tesseract(load_image(__UpperCamelCase    )			,					__UpperCamelCase			,					''''''    )    )    )

         # 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(__UpperCamelCase			,					decimals=4    )			,					[
                 {'''score''': 0.9999, '''answer''': '''us-001''', '''start''': 1_6, '''end''': 1_6},
                 {'''score''': 0.9998, '''answer''': '''us-001''', '''start''': 1_6, '''end''': 1_6},
             ]			,					)
    @slow
    @require_torch
    def 		a_			(       self    ):
         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=__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 		a_			(       self    ):
         pass











"""simple docstring"""
import json
import os
import tempfile

from transformers.testing_utils import check_json_file_has_correct_format






class 					_UpperCAmelCase   :
					UpperCamelCase      =							None


					def 	lowerCamelCase       (					self			:List[Any]		):
												A		=  self.feature_extraction_class(**self.feat_extract_dict		)
												A		=  json.loads(feat_extract.to_json_string()		)
												for key, value in self.feat_extract_dict.items():
																			self.assertEqual(obj[key]     ,       __UpperCamelCase		)


					def 	lowerCamelCase       (					self			:Dict		):
												A		=  self.feature_extraction_class(**self.feat_extract_dict		)

												with tempfile.TemporaryDirectory() as tmpdirname:
																			A		=  os.path.join(__UpperCamelCase     ,       "feat_extract.json"		)
																			feat_extract_first.to_json_file(__UpperCamelCase		)
																			A		=  self.feature_extraction_class.from_json_file(__UpperCamelCase		)

												self.assertEqual(feat_extract_second.to_dict()     ,       feat_extract_first.to_dict()		)


					def 	lowerCamelCase       (					self			:Dict		):
												A		=  self.feature_extraction_class(**self.feat_extract_dict		)

												with tempfile.TemporaryDirectory() as tmpdirname:
																			A		=  feat_extract_first.save_pretrained(__UpperCamelCase		)[0]
																			check_json_file_has_correct_format(__UpperCamelCase		)
																			A		=  self.feature_extraction_class.from_pretrained(__UpperCamelCase		)

												self.assertEqual(feat_extract_second.to_dict()     ,       feat_extract_first.to_dict()		)


					def 	lowerCamelCase       (					self			:Tuple		):
												A		=  self.feature_extraction_class()
												self.assertIsNotNone(__UpperCamelCase		)


