

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 UpperCamelCase      (      *snake_case__			:   str				,     **snake_case__			:   Union[str, Any]     ):

           """simple docstring"""






           pass


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




@is_pipeline_test
@require_torch
@require_vision
class   UpperCamelCase			(  unittest.TestCase ):
    __UpperCamelCase    =MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING

    @require_pytesseract
    @require_vision
    def UpperCamelCase      (      self			:   List[str]				,     snake_case__			:   int				,     snake_case__			:   Optional[int]				,     snake_case__			:   List[str]     ):

     """simple docstring"""






     SCREAMING_SNAKE_CASE		    =      pipeline(
         'document-question-answering'				,     model=snake_case__				,     tokenizer=snake_case__				,     image_processor=snake_case__     )

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

    def UpperCamelCase      (      self			:   Optional[Any]				,     snake_case__			:   Optional[int]				,     snake_case__			:   int     ):

     """simple docstring"""






     SCREAMING_SNAKE_CASE		    =      dqa_pipeline(snake_case__				,     top_k=2     )
     self.assertEqual(
         snake_case__				,     [
             [
                 {'score': ANY(snake_case__     ), 'answer': ANY(snake_case__     ), 'start': ANY(snake_case__     ), 'end': ANY(snake_case__     )},
                 {'score': ANY(snake_case__     ), 'answer': ANY(snake_case__     ), 'start': ANY(snake_case__     ), 'end': ANY(snake_case__     )},
             ]
         ]
         * 3				,     )

    @require_torch
    @require_detectrona
    @require_pytesseract
    def UpperCamelCase      (      self			:   Any     ):

     """simple docstring"""






     SCREAMING_SNAKE_CASE		    =      pipeline('document-question-answering'				,     model='hf-internal-testing/tiny-random-layoutlmv2'     )
     SCREAMING_SNAKE_CASE		    =      INVOICE_URL
     SCREAMING_SNAKE_CASE		    =      'How many cats are there?'

     SCREAMING_SNAKE_CASE		    =      [
         {'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},
     ]
     SCREAMING_SNAKE_CASE		    =      dqa_pipeline(image=snake_case__				,     question=snake_case__				,     top_k=2     )
     self.assertEqual(nested_simplify(snake_case__				,     decimals=4     )				,     snake_case__     )

     SCREAMING_SNAKE_CASE		    =      dqa_pipeline({'image': image, 'question': question}				,     top_k=2     )
     self.assertEqual(nested_simplify(snake_case__				,     decimals=4     )				,     snake_case__     )

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

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

    @slow
    @require_torch
    @require_detectrona
    @require_pytesseract
    def UpperCamelCase      (      self			:   Any     ):

     """simple docstring"""






     SCREAMING_SNAKE_CASE		    =      pipeline(
         'document-question-answering'				,     model='tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa'				,     revision='9977165'				,     )
     SCREAMING_SNAKE_CASE		    =      INVOICE_URL
     SCREAMING_SNAKE_CASE		    =      'What is the invoice number?'

     SCREAMING_SNAKE_CASE		    =      dqa_pipeline(image=snake_case__				,     question=snake_case__				,     top_k=2     )
     self.assertEqual(
         nested_simplify(snake_case__				,     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},
         ]				,     )

     SCREAMING_SNAKE_CASE		    =      dqa_pipeline({'image': image, 'question': question}				,     top_k=2     )
     self.assertEqual(
         nested_simplify(snake_case__				,     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},
         ]				,     )

     SCREAMING_SNAKE_CASE		    =      dqa_pipeline(
         [{'image': image, 'question': question}, {'image': image, 'question': question}]				,     top_k=2     )
     self.assertEqual(
         nested_simplify(snake_case__				,     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 UpperCamelCase      (      self			:   Optional[Any]     ):

     """simple docstring"""






     SCREAMING_SNAKE_CASE		    =      pipeline(
         'document-question-answering'				,     model='tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa'				,     revision='9977165'				,     max_seq_len=5_0				,     )
     SCREAMING_SNAKE_CASE		    =      INVOICE_URL
     SCREAMING_SNAKE_CASE		    =      'What is the invoice number?'

     SCREAMING_SNAKE_CASE		    =      dqa_pipeline(image=snake_case__				,     question=snake_case__				,     top_k=2     )
     self.assertEqual(
         nested_simplify(snake_case__				,     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},
         ]				,     )

     SCREAMING_SNAKE_CASE		    =      dqa_pipeline({'image': image, 'question': question}				,     top_k=2     )
     self.assertEqual(
         nested_simplify(snake_case__				,     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},
         ]				,     )

     SCREAMING_SNAKE_CASE		    =      dqa_pipeline(
         [{'image': image, 'question': question}, {'image': image, 'question': question}]				,     top_k=2     )
     self.assertEqual(
         nested_simplify(snake_case__				,     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 UpperCamelCase      (      self			:   List[Any]     ):

     """simple docstring"""






     SCREAMING_SNAKE_CASE		    =      AutoTokenizer.from_pretrained(
         'impira/layoutlm-document-qa'				,     revision='3dc6de3'				,     add_prefix_space=snake_case__     )
     SCREAMING_SNAKE_CASE		    =      pipeline(
         'document-question-answering'				,     model='impira/layoutlm-document-qa'				,     tokenizer=snake_case__				,     revision='3dc6de3'				,     )
     SCREAMING_SNAKE_CASE		    =      INVOICE_URL
     SCREAMING_SNAKE_CASE		    =      'What is the invoice number?'

     SCREAMING_SNAKE_CASE		    =      dqa_pipeline(image=snake_case__				,     question=snake_case__				,     top_k=2     )
     self.assertEqual(
         nested_simplify(snake_case__				,     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},
         ]				,     )

     SCREAMING_SNAKE_CASE		    =      dqa_pipeline({'image': image, 'question': question}				,     top_k=2     )
     self.assertEqual(
         nested_simplify(snake_case__				,     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},
         ]				,     )

     SCREAMING_SNAKE_CASE		    =      dqa_pipeline(
         [{'image': image, 'question': question}, {'image': image, 'question': question}]				,     top_k=2     )
     self.assertEqual(
         nested_simplify(snake_case__				,     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				,     )

     SCREAMING_SNAKE_CASE		    =      list(zip(*apply_tesseract(load_image(snake_case__     )				,     snake_case__				,     ''     )     )     )

     # This model should also work if `image` is set to None
     SCREAMING_SNAKE_CASE		    =      dqa_pipeline({'image': None, 'word_boxes': word_boxes, 'question': question}				,     top_k=2     )
     self.assertEqual(
         nested_simplify(snake_case__				,     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 UpperCamelCase      (      self			:   List[str]     ):

     """simple docstring"""






     SCREAMING_SNAKE_CASE		    =      AutoTokenizer.from_pretrained(
         'impira/layoutlm-document-qa'				,     revision='3dc6de3'				,     add_prefix_space=snake_case__     )
     SCREAMING_SNAKE_CASE		    =      pipeline(
         'document-question-answering'				,     model='impira/layoutlm-document-qa'				,     tokenizer=snake_case__				,     revision='3dc6de3'				,     max_seq_len=5_0				,     )
     SCREAMING_SNAKE_CASE		    =      INVOICE_URL
     SCREAMING_SNAKE_CASE		    =      'What is the invoice number?'

     SCREAMING_SNAKE_CASE		    =      dqa_pipeline(image=snake_case__				,     question=snake_case__				,     top_k=2     )
     self.assertEqual(
         nested_simplify(snake_case__				,     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},
         ]				,     )

     SCREAMING_SNAKE_CASE		    =      dqa_pipeline(
         [{'image': image, 'question': question}, {'image': image, 'question': question}]				,     top_k=2     )
     self.assertEqual(
         nested_simplify(snake_case__				,     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				,     )

     SCREAMING_SNAKE_CASE		    =      list(zip(*apply_tesseract(load_image(snake_case__     )				,     snake_case__				,     ''     )     )     )

     # This model should also work if `image` is set to None
     SCREAMING_SNAKE_CASE		    =      dqa_pipeline({'image': None, 'word_boxes': word_boxes, 'question': question}				,     top_k=2     )
     self.assertEqual(
         nested_simplify(snake_case__				,     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 UpperCamelCase      (      self			:   List[Any]     ):

     """simple docstring"""






     SCREAMING_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'				,     )

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




    @require_tf
    @unittest.skip('Document question answering not implemented in TF'     )
    def UpperCamelCase      (      self			:   Tuple     ):

     """simple docstring"""






     pass




def 							__lowerCAmelCase     (							_UpperCamelCase		:	List[str]=2_81_23						)		->				str:
 '''simple docstring'''


 SCREAMING_SNAKE_CASE		    =      [1] * (limit + 1)

 for i in range(2		,				int(limit**0.5						) + 1						):
  sum_divs[i * i] += i
  for k in range(i + 1		,				limit // i + 1						):
   sum_divs[k * i] += k + i

 SCREAMING_SNAKE_CASE		    =      set()
 SCREAMING_SNAKE_CASE		    =      0

 for n in range(1		,				limit + 1						):
  if sum_divs[n] > n:
   abundants.add(_UpperCamelCase						)

  if not any((n - a in abundants) for a in abundants						):
   res += n

 return res


if __name__ == "__main__":
      print(solution())

