
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       lowerCAmelCase			(*snake_case_ :					Dict					,				**snake_case_ :					str   ):
          pass





      def    __UpperCamelCase     (				lowerCAmelCase__							:				Dict  ):
       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.
lowercase__       =(
    'https://huggingface.co/spaces/impira/docquery/resolve/2f6c96314dc84dfda62d40de9da55f2f5165d403/invoice.png'
)



@is_pipeline_test
@require_torch
@require_vision
class 		UpperCamelCase__  (       unittest.TestCase    ):
   _SCREAMING_SNAKE_CASE      :							Optional[int]	       =      MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING
   @require_pytesseract
   @require_vision
   def       lowerCAmelCase			(self :					List[Any]					,				snake_case_ :					Tuple					,				snake_case_ :					List[str]					,				snake_case_ :					int   ):
    __a :  Union[str, Any]						=  pipeline(
        '''document-question-answering'''					,				model=snake_case_					,				tokenizer=snake_case_					,				image_processor=snake_case_   )

    __a :  Union[str, Any]						=  INVOICE_URL
    __a :  Optional[Any]						=  list(zip(*apply_tesseract(load_image(snake_case_   )					,				snake_case_					,				''''''   )   )   )
    __a :  int						=  '''What is the placebo?'''
    __a :  Tuple						=  [
        {
            '''image''': load_image(snake_case_   ),
            '''question''': question,
        },
        {
            '''image''': image,
            '''question''': question,
        },
        {
            '''image''': image,
            '''question''': question,
            '''word_boxes''': word_boxes,
        },
    ]
    return dqa_pipeline, examples
   def       lowerCAmelCase			(self :					Optional[Any]					,				snake_case_ :					Dict					,				snake_case_ :					Optional[Any]   ):
    __a :  Dict						=  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       lowerCAmelCase			(self :					Optional[Any]   ):
    __a :  int						=  pipeline('''document-question-answering'''					,				model='''hf-internal-testing/tiny-random-layoutlmv2'''   )
    __a :  int						=  INVOICE_URL
    __a :  Tuple						=  '''How many cats are there?'''

    __a :  str						=  [
        {'''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},
    ]
    __a :  List[Any]						=  dqa_pipeline(image=snake_case_					,				question=snake_case_					,				top_k=2   )
    self.assertEqual(nested_simplify(snake_case_					,				decimals=4   )					,				snake_case_   )

    __a :  int						=  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
    __a :  int						=  '''./tests/fixtures/tests_samples/COCO/000000039769.png'''
    __a :  Tuple						=  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
    __a :  List[str]						=  '''./tests/fixtures/tests_samples/COCO/000000039769.png'''
    __a :  int						=  []
    __a :  Optional[Any]						=  []
    __a :  str						=  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       lowerCAmelCase			(self :					List[Any]   ):
    __a :  Union[str, Any]						=  pipeline(
        '''document-question-answering'''					,				model='''tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa'''					,				revision='''9977165'''					,				)
    __a :  Dict						=  INVOICE_URL
    __a :  List[Any]						=  '''What is the invoice number?'''

    __a :  Union[str, Any]						=  dqa_pipeline(image=snake_case_					,				question=snake_case_					,				top_k=2   )
    self.assertEqual(
        nested_simplify(snake_case_					,				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},
        ]					,				)

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

    __a :  int						=  dqa_pipeline(
        [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}]					,				top_k=2   )
    self.assertEqual(
        nested_simplify(snake_case_					,				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       lowerCAmelCase			(self :					str   ):
    __a :  str						=  pipeline(
        '''document-question-answering'''					,				model='''tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa'''					,				revision='''9977165'''					,				max_seq_len=5_0					,				)
    __a :  Tuple						=  INVOICE_URL
    __a :  Union[str, Any]						=  '''What is the invoice number?'''

    __a :  Optional[Any]						=  dqa_pipeline(image=snake_case_					,				question=snake_case_					,				top_k=2   )
    self.assertEqual(
        nested_simplify(snake_case_					,				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},
        ]					,				)

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

    __a :  Dict						=  dqa_pipeline(
        [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}]					,				top_k=2   )
    self.assertEqual(
        nested_simplify(snake_case_					,				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       lowerCAmelCase			(self :					int   ):
    __a :  Tuple						=  AutoTokenizer.from_pretrained(
        '''impira/layoutlm-document-qa'''					,				revision='''3dc6de3'''					,				add_prefix_space=snake_case_   )
    __a :  Tuple						=  pipeline(
        '''document-question-answering'''					,				model='''impira/layoutlm-document-qa'''					,				tokenizer=snake_case_					,				revision='''3dc6de3'''					,				)
    __a :  Tuple						=  INVOICE_URL
    __a :  Tuple						=  '''What is the invoice number?'''

    __a :  Dict						=  dqa_pipeline(image=snake_case_					,				question=snake_case_					,				top_k=2   )
    self.assertEqual(
        nested_simplify(snake_case_					,				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},
        ]					,				)

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

    __a :  Union[str, Any]						=  dqa_pipeline(
        [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}]					,				top_k=2   )
    self.assertEqual(
        nested_simplify(snake_case_					,				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					,				)

    __a :  Dict						=  list(zip(*apply_tesseract(load_image(snake_case_   )					,				snake_case_					,				''''''   )   )   )

    # This model should also work if `image` is set to None
    __a :  List[Any]						=  dqa_pipeline({'''image''': None, '''word_boxes''': word_boxes, '''question''': question}					,				top_k=2   )
    self.assertEqual(
        nested_simplify(snake_case_					,				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       lowerCAmelCase			(self :					List[str]   ):
    __a :  str						=  AutoTokenizer.from_pretrained(
        '''impira/layoutlm-document-qa'''					,				revision='''3dc6de3'''					,				add_prefix_space=snake_case_   )
    __a :  List[str]						=  pipeline(
        '''document-question-answering'''					,				model='''impira/layoutlm-document-qa'''					,				tokenizer=snake_case_					,				revision='''3dc6de3'''					,				max_seq_len=5_0					,				)
    __a :  Tuple						=  INVOICE_URL
    __a :  List[Any]						=  '''What is the invoice number?'''

    __a :  List[str]						=  dqa_pipeline(image=snake_case_					,				question=snake_case_					,				top_k=2   )
    self.assertEqual(
        nested_simplify(snake_case_					,				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},
        ]					,				)

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

    __a :  Optional[int]						=  list(zip(*apply_tesseract(load_image(snake_case_   )					,				snake_case_					,				''''''   )   )   )

    # This model should also work if `image` is set to None
    __a :  str						=  dqa_pipeline({'''image''': None, '''word_boxes''': word_boxes, '''question''': question}					,				top_k=2   )
    self.assertEqual(
        nested_simplify(snake_case_					,				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       lowerCAmelCase			(self :					Optional[Any]   ):
    __a :  Union[str, Any]						=  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 :  Tuple						=  INVOICE_URL
    __a :  Optional[Any]						=  '''What is the invoice number?'''
    __a :  Optional[int]						=  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       lowerCAmelCase			(self :					Tuple   ):
    pass


def    __UpperCamelCase     (				lowerCAmelCase__							:				int = 5_0_0_0_0_0_0_0  ):
 __a :  int						=  set()
 __a :  str						=  int((limit - 2_4) ** (1 / 2)  )

 __a :  int						=  set(range(3  ,   prime_square_limit + 1  ,   2  )  )
 primes.add(2  )
 for p in range(3  ,   prime_square_limit + 1  ,   2  ):
  if p not in primes:
   continue
  primes.difference_update(set(range(p * p  ,   prime_square_limit + 1  ,   lowerCAmelCase__  )  )  )

 for primea in primes:
  __a :  Union[str, Any]						=  primea * primea
  for primea in primes:
   __a :  Union[str, Any]						=  primea * primea * primea
   if square + cube >= limit - 1_6:
    break
   for primea in primes:
    __a :  int						=  primea * primea * primea * primea
    __a :  Union[str, Any]						=  square + cube + tetr
    if total >= limit:
     break
    ret.add(lowerCAmelCase__  )

 return len(lowerCAmelCase__  )


if __name__ == "__main__":
      print(F"""{solution() = }""")
