



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



        @staticmethod
        def    __UpperCAmelCase    (       *__lowerCamelCase    :	List[str]							,       **__lowerCamelCase    :	Any				):



          """simple docstring"""
          pass




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



@is_pipeline_test
@require_torch
@require_vision
class     UpperCAmelCase    (	unittest.TestCase  ):
   A__		:	Dict           =  MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING



   @require_pytesseract
   @require_vision
   def    __UpperCAmelCase    (       self    :	Tuple							,       __lowerCamelCase    :	Dict							,       __lowerCamelCase    :	List[Any]							,       __lowerCamelCase    :	List[Any]				):



     """simple docstring"""
     _snake_case   	=      pipeline(
         '''document-question-answering'''							,       model=__lowerCamelCase							,       tokenizer=__lowerCamelCase							,       image_processor=__lowerCamelCase				)

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



   def    __UpperCAmelCase    (       self    :	Union[str, Any]							,       __lowerCamelCase    :	Any							,       __lowerCamelCase    :	Optional[Any]				):



     """simple docstring"""
     _snake_case   	=      dqa_pipeline(__lowerCamelCase							,       top_k=2				)
     self.assertEqual(
         __lowerCamelCase							,       [
             [
                 {'''score''': ANY(__lowerCamelCase				), '''answer''': ANY(__lowerCamelCase				), '''start''': ANY(__lowerCamelCase				), '''end''': ANY(__lowerCamelCase				)},
                 {'''score''': ANY(__lowerCamelCase				), '''answer''': ANY(__lowerCamelCase				), '''start''': ANY(__lowerCamelCase				), '''end''': ANY(__lowerCamelCase				)},
             ]
         ]
         * 3							,       )



   @require_torch
   @require_detectrona
   @require_pytesseract
   def    __UpperCAmelCase    (       self    :	Union[str, Any]				):



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

     _snake_case   	=      [
         {'''score''': 0.0_0_0_1, '''answer''': '''oy 2312/2019''', '''start''': 3_8, '''end''': 3_9},
         {'''score''': 0.0_0_0_1, '''answer''': '''oy 2312/2019 DUE''', '''start''': 3_8, '''end''': 4_0},
     ]
     _snake_case   	=      dqa_pipeline(image=__lowerCamelCase							,       question=__lowerCamelCase							,       top_k=2				)
     self.assertEqual(nested_simplify(__lowerCamelCase							,       decimals=4				)							,       __lowerCamelCase				)

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

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

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



   @slow
   @require_torch
   @require_detectrona
   @require_pytesseract
   def    __UpperCAmelCase    (       self    :	List[str]				):



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

     _snake_case   	=      dqa_pipeline(image=__lowerCamelCase							,       question=__lowerCamelCase							,       top_k=2				)
     self.assertEqual(
         nested_simplify(__lowerCamelCase							,       decimals=4				)							,       [
             {'''score''': 0.9_9_4_4, '''answer''': '''us-001''', '''start''': 1_6, '''end''': 1_6},
             {'''score''': 0.0_0_0_9, '''answer''': '''us-001''', '''start''': 1_6, '''end''': 1_6},
         ]							,       )

     _snake_case   	=      dqa_pipeline({'''image''': image, '''question''': question}							,       top_k=2				)
     self.assertEqual(
         nested_simplify(__lowerCamelCase							,       decimals=4				)							,       [
             {'''score''': 0.9_9_4_4, '''answer''': '''us-001''', '''start''': 1_6, '''end''': 1_6},
             {'''score''': 0.0_0_0_9, '''answer''': '''us-001''', '''start''': 1_6, '''end''': 1_6},
         ]							,       )

     _snake_case   	=      dqa_pipeline(
         [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}]							,       top_k=2				)
     self.assertEqual(
         nested_simplify(__lowerCamelCase							,       decimals=4				)							,       [
             [
                 {'''score''': 0.9_9_4_4, '''answer''': '''us-001''', '''start''': 1_6, '''end''': 1_6},
                 {'''score''': 0.0_0_0_9, '''answer''': '''us-001''', '''start''': 1_6, '''end''': 1_6},
             ],
         ]
         * 2							,       )



   @slow
   @require_torch
   @require_detectrona
   @require_pytesseract
   def    __UpperCAmelCase    (       self    :	str				):



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

     _snake_case   	=      dqa_pipeline(image=__lowerCamelCase							,       question=__lowerCamelCase							,       top_k=2				)
     self.assertEqual(
         nested_simplify(__lowerCamelCase							,       decimals=4				)							,       [
             {'''score''': 0.9_9_7_4, '''answer''': '''1110212019''', '''start''': 2_3, '''end''': 2_3},
             {'''score''': 0.9_9_4_8, '''answer''': '''us-001''', '''start''': 1_6, '''end''': 1_6},
         ]							,       )

     _snake_case   	=      dqa_pipeline({'''image''': image, '''question''': question}							,       top_k=2				)
     self.assertEqual(
         nested_simplify(__lowerCamelCase							,       decimals=4				)							,       [
             {'''score''': 0.9_9_7_4, '''answer''': '''1110212019''', '''start''': 2_3, '''end''': 2_3},
             {'''score''': 0.9_9_4_8, '''answer''': '''us-001''', '''start''': 1_6, '''end''': 1_6},
         ]							,       )

     _snake_case   	=      dqa_pipeline(
         [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}]							,       top_k=2				)
     self.assertEqual(
         nested_simplify(__lowerCamelCase							,       decimals=4				)							,       [
             [
                 {'''score''': 0.9_9_7_4, '''answer''': '''1110212019''', '''start''': 2_3, '''end''': 2_3},
                 {'''score''': 0.9_9_4_8, '''answer''': '''us-001''', '''start''': 1_6, '''end''': 1_6},
             ]
         ]
         * 2							,       )



   @slow
   @require_torch
   @require_pytesseract
   @require_vision
   def    __UpperCAmelCase    (       self    :	int				):



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

     _snake_case   	=      dqa_pipeline(image=__lowerCamelCase							,       question=__lowerCamelCase							,       top_k=2				)
     self.assertEqual(
         nested_simplify(__lowerCamelCase							,       decimals=4				)							,       [
             {'''score''': 0.4_2_5_1, '''answer''': '''us-001''', '''start''': 1_6, '''end''': 1_6},
             {'''score''': 0.0_8_1_9, '''answer''': '''1110212019''', '''start''': 2_3, '''end''': 2_3},
         ]							,       )

     _snake_case   	=      dqa_pipeline({'''image''': image, '''question''': question}							,       top_k=2				)
     self.assertEqual(
         nested_simplify(__lowerCamelCase							,       decimals=4				)							,       [
             {'''score''': 0.4_2_5_1, '''answer''': '''us-001''', '''start''': 1_6, '''end''': 1_6},
             {'''score''': 0.0_8_1_9, '''answer''': '''1110212019''', '''start''': 2_3, '''end''': 2_3},
         ]							,       )

     _snake_case   	=      dqa_pipeline(
         [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}]							,       top_k=2				)
     self.assertEqual(
         nested_simplify(__lowerCamelCase							,       decimals=4				)							,       [
             [
                 {'''score''': 0.4_2_5_1, '''answer''': '''us-001''', '''start''': 1_6, '''end''': 1_6},
                 {'''score''': 0.0_8_1_9, '''answer''': '''1110212019''', '''start''': 2_3, '''end''': 2_3},
             ]
         ]
         * 2							,       )

     _snake_case   	=      list(zip(*apply_tesseract(load_image(__lowerCamelCase				)							,       __lowerCamelCase							,       ''''''				)				)				)

     # This model should also work if `image` is set to None
     _snake_case   	=      dqa_pipeline({'''image''': None, '''word_boxes''': word_boxes, '''question''': question}							,       top_k=2				)
     self.assertEqual(
         nested_simplify(__lowerCamelCase							,       decimals=4				)							,       [
             {'''score''': 0.4_2_5_1, '''answer''': '''us-001''', '''start''': 1_6, '''end''': 1_6},
             {'''score''': 0.0_8_1_9, '''answer''': '''1110212019''', '''start''': 2_3, '''end''': 2_3},
         ]							,       )



   @slow
   @require_torch
   @require_pytesseract
   @require_vision
   def    __UpperCAmelCase    (       self    :	List[Any]				):



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

     _snake_case   	=      dqa_pipeline(image=__lowerCamelCase							,       question=__lowerCamelCase							,       top_k=2				)
     self.assertEqual(
         nested_simplify(__lowerCamelCase							,       decimals=4				)							,       [
             {'''score''': 0.9_9_9_9, '''answer''': '''us-001''', '''start''': 1_6, '''end''': 1_6},
             {'''score''': 0.9_9_9_8, '''answer''': '''us-001''', '''start''': 1_6, '''end''': 1_6},
         ]							,       )

     _snake_case   	=      dqa_pipeline(
         [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}]							,       top_k=2				)
     self.assertEqual(
         nested_simplify(__lowerCamelCase							,       decimals=4				)							,       [
             [
                 {'''score''': 0.9_9_9_9, '''answer''': '''us-001''', '''start''': 1_6, '''end''': 1_6},
                 {'''score''': 0.9_9_9_8, '''answer''': '''us-001''', '''start''': 1_6, '''end''': 1_6},
             ]
         ]
         * 2							,       )

     _snake_case   	=      list(zip(*apply_tesseract(load_image(__lowerCamelCase				)							,       __lowerCamelCase							,       ''''''				)				)				)

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



   @slow
   @require_torch
   def    __UpperCAmelCase    (       self    :	Union[str, Any]				):



     """simple docstring"""
     _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'''							,       )

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





   @require_tf
   @unittest.skip('''Document question answering not implemented in TF'''				)
   def    __UpperCAmelCase    (       self    :	List[Any]				):



     """simple docstring"""
     pass








'''simple docstring'''



import random
import unittest

import torch

from diffusers import IFInpaintingPipeline
from diffusers.utils import floats_tensor
from diffusers.utils.import_utils import is_xformers_available
from diffusers.utils.testing_utils import skip_mps, torch_device

from ..pipeline_params import (
    TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS,
    TEXT_GUIDED_IMAGE_INPAINTING_PARAMS,
)
from ..test_pipelines_common import PipelineTesterMixin
from . import IFPipelineTesterMixin







@skip_mps
class 		lowerCAmelCase__  ( a					,   a					,   unittest.TestCase   ):



 """simple docstring"""



 lowerCAmelCase__       = IFInpaintingPipeline
 lowerCAmelCase__       = TEXT_GUIDED_IMAGE_INPAINTING_PARAMS - {"width", "height"}
 lowerCAmelCase__       = TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS
 lowerCAmelCase__       = PipelineTesterMixin.required_optional_params - {"latents"}
 def  UpperCAmelCase__						(						self   :			Optional[int]     )    ->					Dict:


      """simple docstring"""




      return self._get_dummy_components()
 def  UpperCAmelCase__						(						self   :			str   ,				__SCREAMING_SNAKE_CASE   :			List[Any]   ,				__SCREAMING_SNAKE_CASE   :			Dict=0     )    ->					Any:


      """simple docstring"""




      if str(__SCREAMING_SNAKE_CASE     ).startswith("""mps"""     ):
           __SCREAMING_SNAKE_CASE      = torch.manual_seed(__SCREAMING_SNAKE_CASE     )
      else:
           __SCREAMING_SNAKE_CASE      = torch.Generator(device=__SCREAMING_SNAKE_CASE     ).manual_seed(__SCREAMING_SNAKE_CASE     )

      __SCREAMING_SNAKE_CASE      = floats_tensor((1, 3, 32, 32)   ,				rng=random.Random(__SCREAMING_SNAKE_CASE     )     ).to(__SCREAMING_SNAKE_CASE     )
      __SCREAMING_SNAKE_CASE      = floats_tensor((1, 3, 32, 32)   ,				rng=random.Random(__SCREAMING_SNAKE_CASE     )     ).to(__SCREAMING_SNAKE_CASE     )

      __SCREAMING_SNAKE_CASE      = {
          """prompt""": """A painting of a squirrel eating a burger""",
          """image""": image,
          """mask_image""": mask_image,
          """generator""": generator,
          """num_inference_steps""": 2,
          """output_type""": """numpy""",
      }

      return inputs
 @unittest.skipIf(
     torch_device != """cuda""" or not is_xformers_available()   ,				reason="""XFormers attention is only available with CUDA and `xformers` installed"""   ,				)
 def  UpperCAmelCase__						(						self   :			str     )    ->					Optional[int]:


      """simple docstring"""




      self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1E-3     )
 def  UpperCAmelCase__						(						self   :			str     )    ->					List[str]:


      """simple docstring"""




      self._test_save_load_optional_components()
 @unittest.skipIf(torch_device != """cuda"""   ,				reason="""float16 requires CUDA"""     )
 def  UpperCAmelCase__						(						self   :			Dict     )    ->					int:


      """simple docstring"""




      super().test_save_load_floataa(expected_max_diff=1E-1     )
 def  UpperCAmelCase__						(						self   :			List[Any]     )    ->					Optional[int]:


      """simple docstring"""




      self._test_attention_slicing_forward_pass(expected_max_diff=1E-2     )
 def  UpperCAmelCase__						(						self   :			Union[str, Any]     )    ->					List[Any]:


      """simple docstring"""




      self._test_save_load_local()


 def  UpperCAmelCase__						(						self   :			Any     )    ->					List[Any]:


      """simple docstring"""




      self._test_inference_batch_single_identical(
          expected_max_diff=1E-2   ,				)






