






"""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        UpperCamelCase__ :







     """simple docstring"""







     @staticmethod
     def 				a__			(		*UpperCamelCase_ :	List[Any]				,    **UpperCamelCase_ :	List[Any] ):





            '''simple docstring'''

            pass


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



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







    """simple docstring"""
    __UpperCAmelCase           = MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING







    @require_pytesseract
    @require_vision
    def 				a__			(		self :	Optional[int]				,    UpperCamelCase_ :	int				,    UpperCamelCase_ :	Union[str, Any]				,    UpperCamelCase_ :	Optional[int] ):





           '''simple docstring'''

           __magic_name__      				=    pipeline(
               'document-question-answering'				,    model=UpperCamelCase_				,    tokenizer=UpperCamelCase_				,    image_processor=UpperCamelCase_ )

           __magic_name__      				=    INVOICE_URL
           __magic_name__      				=    list(zip(*apply_tesseract(load_image(UpperCamelCase_ )				,    UpperCamelCase_				,    '' ) ) )
           __magic_name__      				=    'What is the placebo?'
           __magic_name__      				=    [
               {
                   '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 :	Dict				,    UpperCamelCase_ :	int				,    UpperCamelCase_ :	Union[str, Any] ):





           '''simple docstring'''

           __magic_name__      				=    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 :	int ):





           '''simple docstring'''

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

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

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

           # We can optionnally pass directly the words and bounding boxes
           __magic_name__      				=    './tests/fixtures/tests_samples/COCO/000000039769.png'
           __magic_name__      				=    []
           __magic_name__      				=    []
           __magic_name__      				=    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 :	Tuple ):





           '''simple docstring'''

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

           __magic_name__      				=    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},
               ]				,    )

           __magic_name__      				=    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},
               ]				,    )

           __magic_name__      				=    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 				a__			(		self :	Union[str, Any] ):





           '''simple docstring'''

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

           __magic_name__      				=    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},
               ]				,    )

           __magic_name__      				=    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},
               ]				,    )

           __magic_name__      				=    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 				a__			(		self :	Any ):





           '''simple docstring'''

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

           __magic_name__      				=    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},
               ]				,    )

           __magic_name__      				=    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},
               ]				,    )

           __magic_name__      				=    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				,    )

           __magic_name__      				=    list(zip(*apply_tesseract(load_image(UpperCamelCase_ )				,    UpperCamelCase_				,    '' ) ) )

           # This model should also work if `image` is set to None
           __magic_name__      				=    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 				a__			(		self :	Tuple ):





           '''simple docstring'''

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

           __magic_name__      				=    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},
               ]				,    )

           __magic_name__      				=    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				,    )

           __magic_name__      				=    list(zip(*apply_tesseract(load_image(UpperCamelCase_ )				,    UpperCamelCase_				,    '' ) ) )

           # This model should also work if `image` is set to None
           __magic_name__      				=    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 				a__			(		self :	Any ):





           '''simple docstring'''

           __magic_name__      				=    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'				,    )

           __magic_name__      				=    INVOICE_URL
           __magic_name__      				=    'What is the invoice number?'
           __magic_name__      				=    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 :	List[str] ):





           '''simple docstring'''

           pass







"""simple docstring"""
def 							A						(				__snake_case: int  )    ->		int:

       """simple docstring"""





       if divisor % 5 == 0 or divisor % 2 == 0:
              return 0
       __magic_name__      				=    1
       __magic_name__      				=    1
       while repunit:
              __magic_name__      				=    (1_0 * repunit + 1) % divisor
              repunit_index += 1
       return repunit_index


def 							A						(				__snake_case: int = 1_0_0_0_0_0_0  )    ->		int:

       """simple docstring"""





       __magic_name__      				=    limit - 1
       if divisor % 2 == 0:
              divisor += 1
       while least_divisible_repunit(__snake_case  ) <= limit:
              divisor += 2
       return divisor


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