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






          @staticmethod
          def    __A  (		*a__    ,					**a__  ):
            pass







     def 							SCREAMING_SNAKE_CASE					(      _lowerCamelCase						:	Optional[Any]     )	->				List[str]:
       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				:		Dict	   =       (
    '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    __A  (		self    ,					a__    ,					a__    ,					a__  ):
       _lowerCAmelCase						:	Optional[int]        =					pipeline(
           """document-question-answering"""    ,					model=a__    ,					tokenizer=a__    ,					image_processor=a__  )

       _lowerCAmelCase						:	Tuple        =					INVOICE_URL
       _lowerCAmelCase						:	List[str]        =					list(zip(*apply_tesseract(load_image(a__  )    ,					a__    ,					""""""  )  )  )
       _lowerCAmelCase						:	Union[str, Any]        =					"""What is the placebo?"""
       _lowerCAmelCase						:	str        =					[
           {
               """image""": load_image(a__  ),
               """question""": question,
           },
           {
               """image""": image,
               """question""": question,
           },
           {
               """image""": image,
               """question""": question,
               """word_boxes""": word_boxes,
           },
       ]
       return dqa_pipeline, examples






     def    __A  (		self    ,					a__    ,					a__  ):
       _lowerCAmelCase						:	List[str]        =					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    __A  (		self  ):
       _lowerCAmelCase						:	Tuple        =					pipeline("""document-question-answering"""    ,					model="""hf-internal-testing/tiny-random-layoutlmv2"""  )
       _lowerCAmelCase						:	List[Any]        =					INVOICE_URL
       _lowerCAmelCase						:	Optional[Any]        =					"""How many cats are there?"""

       _lowerCAmelCase						:	str        =					[
           {"""score""": 0.0_0_0_1, """answer""": """oy 2312/2019""", """start""": 38, """end""": 39},
           {"""score""": 0.0_0_0_1, """answer""": """oy 2312/2019 DUE""", """start""": 38, """end""": 40},
       ]
       _lowerCAmelCase						:	Dict        =					dqa_pipeline(image=a__    ,					question=a__    ,					top_k=2  )
       self.assertEqual(nested_simplify(a__    ,					decimals=4  )    ,					a__  )

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

       # We can optionnally pass directly the words and bounding boxes
       _lowerCAmelCase						:	Dict        =					"""./tests/fixtures/tests_samples/COCO/000000039769.png"""
       _lowerCAmelCase						:	Optional[int]        =					[]
       _lowerCAmelCase						:	List[str]        =					[]
       _lowerCAmelCase						:	List[Any]        =					dqa_pipeline(image=a__    ,					question=a__    ,					words=a__    ,					boxes=a__    ,					top_k=2  )
       self.assertEqual(a__    ,					[]  )






     @slow
     @require_torch
     @require_detectrona
     @require_pytesseract
     def    __A  (		self  ):
       _lowerCAmelCase						:	List[str]        =					pipeline(
           """document-question-answering"""    ,					model="""tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa"""    ,					revision="""9977165"""    ,					)
       _lowerCAmelCase						:	int        =					INVOICE_URL
       _lowerCAmelCase						:	Optional[Any]        =					"""What is the invoice number?"""

       _lowerCAmelCase						:	str        =					dqa_pipeline(image=a__    ,					question=a__    ,					top_k=2  )
       self.assertEqual(
           nested_simplify(a__    ,					decimals=4  )    ,					[
               {"""score""": 0.9_9_4_4, """answer""": """us-001""", """start""": 16, """end""": 16},
               {"""score""": 0.0_0_0_9, """answer""": """us-001""", """start""": 16, """end""": 16},
           ]    ,					)

       _lowerCAmelCase						:	Optional[int]        =					dqa_pipeline({"""image""": image, """question""": question}    ,					top_k=2  )
       self.assertEqual(
           nested_simplify(a__    ,					decimals=4  )    ,					[
               {"""score""": 0.9_9_4_4, """answer""": """us-001""", """start""": 16, """end""": 16},
               {"""score""": 0.0_0_0_9, """answer""": """us-001""", """start""": 16, """end""": 16},
           ]    ,					)

       _lowerCAmelCase						:	Tuple        =					dqa_pipeline(
           [{"""image""": image, """question""": question}, {"""image""": image, """question""": question}]    ,					top_k=2  )
       self.assertEqual(
           nested_simplify(a__    ,					decimals=4  )    ,					[
               [
                   {"""score""": 0.9_9_4_4, """answer""": """us-001""", """start""": 16, """end""": 16},
                   {"""score""": 0.0_0_0_9, """answer""": """us-001""", """start""": 16, """end""": 16},
               ],
           ]
           * 2    ,					)






     @slow
     @require_torch
     @require_detectrona
     @require_pytesseract
     def    __A  (		self  ):
       _lowerCAmelCase						:	Optional[Any]        =					pipeline(
           """document-question-answering"""    ,					model="""tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa"""    ,					revision="""9977165"""    ,					max_seq_len=50    ,					)
       _lowerCAmelCase						:	Dict        =					INVOICE_URL
       _lowerCAmelCase						:	Union[str, Any]        =					"""What is the invoice number?"""

       _lowerCAmelCase						:	int        =					dqa_pipeline(image=a__    ,					question=a__    ,					top_k=2  )
       self.assertEqual(
           nested_simplify(a__    ,					decimals=4  )    ,					[
               {"""score""": 0.9_9_7_4, """answer""": """1110212019""", """start""": 23, """end""": 23},
               {"""score""": 0.9_9_4_8, """answer""": """us-001""", """start""": 16, """end""": 16},
           ]    ,					)

       _lowerCAmelCase						:	List[str]        =					dqa_pipeline({"""image""": image, """question""": question}    ,					top_k=2  )
       self.assertEqual(
           nested_simplify(a__    ,					decimals=4  )    ,					[
               {"""score""": 0.9_9_7_4, """answer""": """1110212019""", """start""": 23, """end""": 23},
               {"""score""": 0.9_9_4_8, """answer""": """us-001""", """start""": 16, """end""": 16},
           ]    ,					)

       _lowerCAmelCase						:	Dict        =					dqa_pipeline(
           [{"""image""": image, """question""": question}, {"""image""": image, """question""": question}]    ,					top_k=2  )
       self.assertEqual(
           nested_simplify(a__    ,					decimals=4  )    ,					[
               [
                   {"""score""": 0.9_9_7_4, """answer""": """1110212019""", """start""": 23, """end""": 23},
                   {"""score""": 0.9_9_4_8, """answer""": """us-001""", """start""": 16, """end""": 16},
               ]
           ]
           * 2    ,					)






     @slow
     @require_torch
     @require_pytesseract
     @require_vision
     def    __A  (		self  ):
       _lowerCAmelCase						:	str        =					AutoTokenizer.from_pretrained(
           """impira/layoutlm-document-qa"""    ,					revision="""3dc6de3"""    ,					add_prefix_space=a__  )
       _lowerCAmelCase						:	Tuple        =					pipeline(
           """document-question-answering"""    ,					model="""impira/layoutlm-document-qa"""    ,					tokenizer=a__    ,					revision="""3dc6de3"""    ,					)
       _lowerCAmelCase						:	Optional[int]        =					INVOICE_URL
       _lowerCAmelCase						:	List[Any]        =					"""What is the invoice number?"""

       _lowerCAmelCase						:	int        =					dqa_pipeline(image=a__    ,					question=a__    ,					top_k=2  )
       self.assertEqual(
           nested_simplify(a__    ,					decimals=4  )    ,					[
               {"""score""": 0.4_2_5_1, """answer""": """us-001""", """start""": 16, """end""": 16},
               {"""score""": 0.0_8_1_9, """answer""": """1110212019""", """start""": 23, """end""": 23},
           ]    ,					)

       _lowerCAmelCase						:	int        =					dqa_pipeline({"""image""": image, """question""": question}    ,					top_k=2  )
       self.assertEqual(
           nested_simplify(a__    ,					decimals=4  )    ,					[
               {"""score""": 0.4_2_5_1, """answer""": """us-001""", """start""": 16, """end""": 16},
               {"""score""": 0.0_8_1_9, """answer""": """1110212019""", """start""": 23, """end""": 23},
           ]    ,					)

       _lowerCAmelCase						:	Optional[int]        =					dqa_pipeline(
           [{"""image""": image, """question""": question}, {"""image""": image, """question""": question}]    ,					top_k=2  )
       self.assertEqual(
           nested_simplify(a__    ,					decimals=4  )    ,					[
               [
                   {"""score""": 0.4_2_5_1, """answer""": """us-001""", """start""": 16, """end""": 16},
                   {"""score""": 0.0_8_1_9, """answer""": """1110212019""", """start""": 23, """end""": 23},
               ]
           ]
           * 2    ,					)

       _lowerCAmelCase						:	str        =					list(zip(*apply_tesseract(load_image(a__  )    ,					a__    ,					""""""  )  )  )

       # This model should also work if `image` is set to None
       _lowerCAmelCase						:	Optional[Any]        =					dqa_pipeline({"""image""": None, """word_boxes""": word_boxes, """question""": question}    ,					top_k=2  )
       self.assertEqual(
           nested_simplify(a__    ,					decimals=4  )    ,					[
               {"""score""": 0.4_2_5_1, """answer""": """us-001""", """start""": 16, """end""": 16},
               {"""score""": 0.0_8_1_9, """answer""": """1110212019""", """start""": 23, """end""": 23},
           ]    ,					)






     @slow
     @require_torch
     @require_pytesseract
     @require_vision
     def    __A  (		self  ):
       _lowerCAmelCase						:	Optional[int]        =					AutoTokenizer.from_pretrained(
           """impira/layoutlm-document-qa"""    ,					revision="""3dc6de3"""    ,					add_prefix_space=a__  )
       _lowerCAmelCase						:	List[Any]        =					pipeline(
           """document-question-answering"""    ,					model="""impira/layoutlm-document-qa"""    ,					tokenizer=a__    ,					revision="""3dc6de3"""    ,					max_seq_len=50    ,					)
       _lowerCAmelCase						:	Optional[Any]        =					INVOICE_URL
       _lowerCAmelCase						:	Union[str, Any]        =					"""What is the invoice number?"""

       _lowerCAmelCase						:	List[str]        =					dqa_pipeline(image=a__    ,					question=a__    ,					top_k=2  )
       self.assertEqual(
           nested_simplify(a__    ,					decimals=4  )    ,					[
               {"""score""": 0.9_9_9_9, """answer""": """us-001""", """start""": 16, """end""": 16},
               {"""score""": 0.9_9_9_8, """answer""": """us-001""", """start""": 16, """end""": 16},
           ]    ,					)

       _lowerCAmelCase						:	Union[str, Any]        =					dqa_pipeline(
           [{"""image""": image, """question""": question}, {"""image""": image, """question""": question}]    ,					top_k=2  )
       self.assertEqual(
           nested_simplify(a__    ,					decimals=4  )    ,					[
               [
                   {"""score""": 0.9_9_9_9, """answer""": """us-001""", """start""": 16, """end""": 16},
                   {"""score""": 0.9_9_9_8, """answer""": """us-001""", """start""": 16, """end""": 16},
               ]
           ]
           * 2    ,					)

       _lowerCAmelCase						:	Union[str, Any]        =					list(zip(*apply_tesseract(load_image(a__  )    ,					a__    ,					""""""  )  )  )

       # This model should also work if `image` is set to None
       _lowerCAmelCase						:	int        =					dqa_pipeline({"""image""": None, """word_boxes""": word_boxes, """question""": question}    ,					top_k=2  )
       self.assertEqual(
           nested_simplify(a__    ,					decimals=4  )    ,					[
               {"""score""": 0.9_9_9_9, """answer""": """us-001""", """start""": 16, """end""": 16},
               {"""score""": 0.9_9_9_8, """answer""": """us-001""", """start""": 16, """end""": 16},
           ]    ,					)






     @slow
     @require_torch
     def    __A  (		self  ):
       _lowerCAmelCase						:	str        =					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"""    ,					)

       _lowerCAmelCase						:	List[str]        =					INVOICE_URL
       _lowerCAmelCase						:	int        =					"""What is the invoice number?"""
       _lowerCAmelCase						:	List[str]        =					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    __A  (		self  ):
       pass





"""simple docstring"""




from manim import *
class 						__A							(			SCREAMING_SNAKE_CASE_		):






     def    __A  (		self  ):
       _lowerCAmelCase						:	Any        =					Rectangle(height=0.5    ,					width=0.5  )
       _lowerCAmelCase						:	List[Any]        =					Rectangle(height=0.4_6    ,					width=0.4_6  ).set_stroke(width=0  )

       _lowerCAmelCase						:	List[str]        =					[mem.copy() for i in range(6  )]
       _lowerCAmelCase						:	Any        =					[mem.copy() for i in range(6  )]
       _lowerCAmelCase						:	Optional[int]        =					VGroup(*a__  ).arrange(a__    ,					buff=0  )
       _lowerCAmelCase						:	Tuple        =					VGroup(*a__  ).arrange(a__    ,					buff=0  )
       _lowerCAmelCase						:	Optional[Any]        =					VGroup(a__    ,					a__  ).arrange(a__    ,					buff=0  )
       _lowerCAmelCase						:	Dict        =					Text("""CPU"""    ,					font_size=24  )
       _lowerCAmelCase						:	str        =					Group(a__    ,					a__  ).arrange(a__    ,					buff=0.5    ,					aligned_edge=a__  )
       cpu.move_to([-2.5, -0.5, 0]  )
       self.add(a__  )

       _lowerCAmelCase						:	Dict        =					[mem.copy() for i in range(4  )]
       _lowerCAmelCase						:	Any        =					VGroup(*a__  ).arrange(a__    ,					buff=0  )
       _lowerCAmelCase						:	Tuple        =					Text("""GPU"""    ,					font_size=24  )
       _lowerCAmelCase						:	Optional[int]        =					Group(a__    ,					a__  ).arrange(a__    ,					buff=0.5    ,					aligned_edge=a__  )
       gpu.move_to([-1, -1, 0]  )
       self.add(a__  )

       _lowerCAmelCase						:	Optional[int]        =					[mem.copy() for i in range(6  )]
       _lowerCAmelCase						:	Any        =					VGroup(*a__  ).arrange(a__    ,					buff=0  )

       _lowerCAmelCase						:	List[str]        =					Text("""Model"""    ,					font_size=24  )
       _lowerCAmelCase						:	Any        =					Group(a__    ,					a__  ).arrange(a__    ,					buff=0.5    ,					aligned_edge=a__  )
       model.move_to([3, -1.0, 0]  )
       self.add(a__  )

       _lowerCAmelCase						:	Tuple        =					[]
       for i, rect in enumerate(a__  ):
         rect.set_stroke(a__  )
         # target = fill.copy().set_fill(YELLOW, opacity=0.7)
         # target.move_to(rect)
         # self.add(target)

         _lowerCAmelCase						:	List[str]        =					Rectangle(height=0.4_6 / 4    ,					width=0.4_6 / 3  ).set_stroke(width=0.0  ).set_fill(a__    ,					opacity=0.7  )

         if i == 0:
           cpu_target.next_to(cpu_left_col_base[0].get_corner(DOWN + LEFT  )    ,					buff=0.0_2    ,					direction=a__  )
           cpu_target.set_x(cpu_target.get_x() + 0.1  )
         elif i == 3:
           cpu_target.next_to(cpu_targs[0]    ,					direction=a__    ,					buff=0.0  )
         else:
           cpu_target.next_to(cpu_targs[i - 1]    ,					direction=a__    ,					buff=0.0  )
         self.add(a__  )
         cpu_targs.append(a__  )

       _lowerCAmelCase						:	Any        =					[mem.copy() for i in range(6  )]
       _lowerCAmelCase						:	List[str]        =					VGroup(*a__  ).arrange(a__    ,					buff=0  )

       _lowerCAmelCase						:	int        =					Text("""Loaded Checkpoint"""    ,					font_size=24  )
       _lowerCAmelCase						:	Optional[int]        =					Group(a__    ,					a__  ).arrange(a__    ,					aligned_edge=a__    ,					buff=0.4  )
       checkpoint.move_to([3, 0.5, 0]  )

       _lowerCAmelCase						:	List[Any]        =					Square(side_length=2.2  )
       key.move_to([-5, 2, 0]  )

       _lowerCAmelCase						:	List[str]        =					MarkupText(
           F"<b>Key:</b>\n\n<span fgcolor='{YELLOW}'>●</span> Empty Model"    ,					font_size=18    ,					)

       key_text.move_to([-5, 2.4, 0]  )

       self.add(a__    ,					a__  )

       _lowerCAmelCase						:	int        =					MarkupText(
           F"<span fgcolor='{BLUE}'>●</span> Checkpoint"    ,					font_size=18    ,					)

       blue_text.next_to(a__    ,					DOWN * 2.4    ,					aligned_edge=key_text.get_left()  )

       _lowerCAmelCase						:	List[Any]        =					MarkupText(
           F"Next, a <i><span fgcolor=\"{BLUE}\">second</span></i> model is loaded into memory,\nwith the weights of a <span fgcolor=\"{BLUE}\">single shard</span>."    ,					font_size=24    ,					)
       step_a.move_to([2, 2, 0]  )
       self.play(Write(a__  )    ,					Write(a__  )  )

       self.play(Write(a__    ,					run_time=1  )    ,					Create(a__    ,					run_time=1  )  )

       _lowerCAmelCase						:	int        =					[]
       _lowerCAmelCase						:	List[Any]        =					[]
       for i, rect in enumerate(a__  ):
         _lowerCAmelCase						:	Tuple        =					fill.copy().set_fill(a__    ,					opacity=0.7  )
         target.move_to(a__  )
         first_animations.append(GrowFromCenter(a__    ,					run_time=1  )  )

         _lowerCAmelCase						:	Optional[Any]        =					target.copy()
         cpu_target.generate_target()
         if i < 5:
           cpu_target.target.move_to(cpu_left_col_base[i + 1]  )
         else:
           cpu_target.target.move_to(cpu_right_col_base[i - 5]  )
         second_animations.append(MoveToTarget(a__    ,					run_time=1.5  )  )

       self.play(*a__  )
       self.play(*a__  )
       self.wait()




