





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





          @staticmethod
          def 	__UpperCamelCase(			*A_	,							**A_       ):
           '''simple docstring'''




           pass



    def 		A_    (					_lowerCAmelCase					)						->			Optional[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	:					Optional[int]			  =					(
    """https://huggingface.co/spaces/impira/docquery/resolve/2f6c96314dc84dfda62d40de9da55f2f5165d403/invoice.png"""
)



@is_pipeline_test
@require_torch
@require_vision
class  A__       (	unittest.TestCase      ):
      _UpperCAmelCase     :str						   =       MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING





      @require_pytesseract
      @require_vision
      def 	__UpperCamelCase(			self	,							A_	,							A_	,							A_       ):
       '''simple docstring'''




       UpperCamelCase     : Dict	     =       pipeline(
           "document-question-answering"	,							model=A_	,							tokenizer=A_	,							image_processor=A_       )

       UpperCamelCase     : Optional[int]	     =       INVOICE_URL
       UpperCamelCase     : Dict	     =       list(zip(*apply_tesseract(load_image(A_       )	,							A_	,							""       )       )       )
       UpperCamelCase     : List[str]	     =       "What is the placebo?"
       UpperCamelCase     : Any	     =       [
           {
               "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	,							A_	,							A_       ):
       '''simple docstring'''




       UpperCamelCase     : int	     =       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       ):
       '''simple docstring'''




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

       UpperCamelCase     : Optional[Any]	     =       [
           {"score": 0.00_01, "answer": "oy 2312/2019", "start": 38, "end": 39},
           {"score": 0.00_01, "answer": "oy 2312/2019 DUE", "start": 38, "end": 40},
       ]
       UpperCamelCase     : List[str]	     =       dqa_pipeline(image=A_	,							question=A_	,							top_k=2       )
       self.assertEqual(nested_simplify(A_	,							decimals=4       )	,							A_       )

       UpperCamelCase     : Optional[int]	     =       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
       UpperCamelCase     : Optional[int]	     =       "./tests/fixtures/tests_samples/COCO/000000039769.png"
       UpperCamelCase     : str	     =       dqa_pipeline(image=A_	,							question=A_	,							top_k=2       )
       self.assertEqual(A_	,							[]       )

       # We can optionnally pass directly the words and bounding boxes
       UpperCamelCase     : Optional[int]	     =       "./tests/fixtures/tests_samples/COCO/000000039769.png"
       UpperCamelCase     : int	     =       []
       UpperCamelCase     : Union[str, Any]	     =       []
       UpperCamelCase     : Tuple	     =       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       ):
       '''simple docstring'''




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

       UpperCamelCase     : Union[str, Any]	     =       dqa_pipeline(image=A_	,							question=A_	,							top_k=2       )
       self.assertEqual(
           nested_simplify(A_	,							decimals=4       )	,							[
               {"score": 0.99_44, "answer": "us-001", "start": 16, "end": 16},
               {"score": 0.00_09, "answer": "us-001", "start": 16, "end": 16},
           ]	,							)

       UpperCamelCase     : str	     =       dqa_pipeline({"image": image, "question": question}	,							top_k=2       )
       self.assertEqual(
           nested_simplify(A_	,							decimals=4       )	,							[
               {"score": 0.99_44, "answer": "us-001", "start": 16, "end": 16},
               {"score": 0.00_09, "answer": "us-001", "start": 16, "end": 16},
           ]	,							)

       UpperCamelCase     : Optional[Any]	     =       dqa_pipeline(
           [{"image": image, "question": question}, {"image": image, "question": question}]	,							top_k=2       )
       self.assertEqual(
           nested_simplify(A_	,							decimals=4       )	,							[
               [
                   {"score": 0.99_44, "answer": "us-001", "start": 16, "end": 16},
                   {"score": 0.00_09, "answer": "us-001", "start": 16, "end": 16},
               ],
           ]
           * 2	,							)





      @slow
      @require_torch
      @require_detectrona
      @require_pytesseract
      def 	__UpperCamelCase(			self       ):
       '''simple docstring'''




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

       UpperCamelCase     : Optional[Any]	     =       dqa_pipeline(image=A_	,							question=A_	,							top_k=2       )
       self.assertEqual(
           nested_simplify(A_	,							decimals=4       )	,							[
               {"score": 0.99_74, "answer": "1110212019", "start": 23, "end": 23},
               {"score": 0.99_48, "answer": "us-001", "start": 16, "end": 16},
           ]	,							)

       UpperCamelCase     : int	     =       dqa_pipeline({"image": image, "question": question}	,							top_k=2       )
       self.assertEqual(
           nested_simplify(A_	,							decimals=4       )	,							[
               {"score": 0.99_74, "answer": "1110212019", "start": 23, "end": 23},
               {"score": 0.99_48, "answer": "us-001", "start": 16, "end": 16},
           ]	,							)

       UpperCamelCase     : str	     =       dqa_pipeline(
           [{"image": image, "question": question}, {"image": image, "question": question}]	,							top_k=2       )
       self.assertEqual(
           nested_simplify(A_	,							decimals=4       )	,							[
               [
                   {"score": 0.99_74, "answer": "1110212019", "start": 23, "end": 23},
                   {"score": 0.99_48, "answer": "us-001", "start": 16, "end": 16},
               ]
           ]
           * 2	,							)





      @slow
      @require_torch
      @require_pytesseract
      @require_vision
      def 	__UpperCamelCase(			self       ):
       '''simple docstring'''




       UpperCamelCase     : int	     =       AutoTokenizer.from_pretrained(
           "impira/layoutlm-document-qa"	,							revision="3dc6de3"	,							add_prefix_space=A_       )
       UpperCamelCase     : List[str]	     =       pipeline(
           "document-question-answering"	,							model="impira/layoutlm-document-qa"	,							tokenizer=A_	,							revision="3dc6de3"	,							)
       UpperCamelCase     : Any	     =       INVOICE_URL
       UpperCamelCase     : List[Any]	     =       "What is the invoice number?"

       UpperCamelCase     : Any	     =       dqa_pipeline(image=A_	,							question=A_	,							top_k=2       )
       self.assertEqual(
           nested_simplify(A_	,							decimals=4       )	,							[
               {"score": 0.42_51, "answer": "us-001", "start": 16, "end": 16},
               {"score": 0.08_19, "answer": "1110212019", "start": 23, "end": 23},
           ]	,							)

       UpperCamelCase     : int	     =       dqa_pipeline({"image": image, "question": question}	,							top_k=2       )
       self.assertEqual(
           nested_simplify(A_	,							decimals=4       )	,							[
               {"score": 0.42_51, "answer": "us-001", "start": 16, "end": 16},
               {"score": 0.08_19, "answer": "1110212019", "start": 23, "end": 23},
           ]	,							)

       UpperCamelCase     : Tuple	     =       dqa_pipeline(
           [{"image": image, "question": question}, {"image": image, "question": question}]	,							top_k=2       )
       self.assertEqual(
           nested_simplify(A_	,							decimals=4       )	,							[
               [
                   {"score": 0.42_51, "answer": "us-001", "start": 16, "end": 16},
                   {"score": 0.08_19, "answer": "1110212019", "start": 23, "end": 23},
               ]
           ]
           * 2	,							)

       UpperCamelCase     : str	     =       list(zip(*apply_tesseract(load_image(A_       )	,							A_	,							""       )       )       )

       # This model should also work if `image` is set to None
       UpperCamelCase     : Optional[int]	     =       dqa_pipeline({"image": None, "word_boxes": word_boxes, "question": question}	,							top_k=2       )
       self.assertEqual(
           nested_simplify(A_	,							decimals=4       )	,							[
               {"score": 0.42_51, "answer": "us-001", "start": 16, "end": 16},
               {"score": 0.08_19, "answer": "1110212019", "start": 23, "end": 23},
           ]	,							)





      @slow
      @require_torch
      @require_pytesseract
      @require_vision
      def 	__UpperCamelCase(			self       ):
       '''simple docstring'''




       UpperCamelCase     : List[str]	     =       AutoTokenizer.from_pretrained(
           "impira/layoutlm-document-qa"	,							revision="3dc6de3"	,							add_prefix_space=A_       )
       UpperCamelCase     : int	     =       pipeline(
           "document-question-answering"	,							model="impira/layoutlm-document-qa"	,							tokenizer=A_	,							revision="3dc6de3"	,							max_seq_len=50	,							)
       UpperCamelCase     : List[str]	     =       INVOICE_URL
       UpperCamelCase     : List[str]	     =       "What is the invoice number?"

       UpperCamelCase     : Dict	     =       dqa_pipeline(image=A_	,							question=A_	,							top_k=2       )
       self.assertEqual(
           nested_simplify(A_	,							decimals=4       )	,							[
               {"score": 0.99_99, "answer": "us-001", "start": 16, "end": 16},
               {"score": 0.99_98, "answer": "us-001", "start": 16, "end": 16},
           ]	,							)

       UpperCamelCase     : int	     =       dqa_pipeline(
           [{"image": image, "question": question}, {"image": image, "question": question}]	,							top_k=2       )
       self.assertEqual(
           nested_simplify(A_	,							decimals=4       )	,							[
               [
                   {"score": 0.99_99, "answer": "us-001", "start": 16, "end": 16},
                   {"score": 0.99_98, "answer": "us-001", "start": 16, "end": 16},
               ]
           ]
           * 2	,							)

       UpperCamelCase     : Dict	     =       list(zip(*apply_tesseract(load_image(A_       )	,							A_	,							""       )       )       )

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





      @slow
      @require_torch
      def 	__UpperCamelCase(			self       ):
       '''simple docstring'''




       UpperCamelCase     : 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"	,							)

       UpperCamelCase     : Optional[Any]	     =       INVOICE_URL
       UpperCamelCase     : Optional[Any]	     =       "What is the invoice number?"
       UpperCamelCase     : Optional[Any]	     =       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       ):
       '''simple docstring'''




       pass







def 		A_    (					_lowerCAmelCase = "The quick brown fox jumps over the lazy dog"							,       )						->			bool:
 UpperCamelCase     : Union[str, Any]	     =       set()

 # Replace all the whitespace in our sentence
 UpperCamelCase     : Union[str, Any]	     =       input_str.replace(" "							,       ""					)
 for alpha in input_str:
  if "a" <= alpha.lower() <= "z":
   frequency.add(alpha.lower()					)
 return len(_lowerCAmelCase					) == 26



def 		A_    (					_lowerCAmelCase = "The quick brown fox jumps over the lazy dog"							,       )						->			bool:
 UpperCamelCase     : List[Any]	     =       [False] * 26
 for char in input_str:
  if char.islower():
   UpperCamelCase     : Tuple	     =       True
  elif char.isupper():
   UpperCamelCase     : str	     =       True
 return all(_lowerCAmelCase					)



def 		A_    (					_lowerCAmelCase = "The quick brown fox jumps over the lazy dog"							,       )						->			bool:
 return len({char for char in input_str.lower() if char.isalpha()}					) == 26



def 		A_    (					)						->			None:
 from timeit import timeit

 UpperCamelCase     : Tuple	     =       "from __main__ import is_pangram, is_pangram_faster, is_pangram_fastest"
 print(timeit("is_pangram()"							,       setup=_lowerCAmelCase					)					)
 print(timeit("is_pangram_faster()"							,       setup=_lowerCAmelCase					)					)
 print(timeit("is_pangram_fastest()"							,       setup=_lowerCAmelCase					)					)
 # 5.348480500048026, 2.6477354579837993, 1.8470395830227062
 # 5.036091582966037, 2.644472333951853,  1.8869528750656173


if __name__ == "__main__":
    import doctest

    doctest.testmod()
    benchmark()
