






import unittest

from transformers import MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING, is_vision_available
from transformers.pipelines import pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    require_torch,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
   from PIL import Image
else:




   class 							_lowerCAmelCase:





          """simple docstring"""







          @staticmethod
          def      _a       (   *_lowerCamelCase    ,     **_lowerCamelCase				):
            pass




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





       """simple docstring"""







       a							:						List[Any]										=MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING





       def      _a       (   self    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase				):
         UpperCamelCase_:   str        =							pipeline('visual-question-answering'    ,     model='hf-internal-testing/tiny-vilt-random-vqa'				)
         UpperCamelCase_:   Optional[Any]        =							[
             {
                 'image': Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'				),
                 'question': 'How many cats are there?',
             },
             {
                 'image': './tests/fixtures/tests_samples/COCO/000000039769.png',
                 'question': 'How many cats are there?',
             },
         ]
         return vqa_pipeline, examples





       def      _a       (   self    ,     _lowerCamelCase    ,     _lowerCamelCase				):
         UpperCamelCase_:   Optional[int]        =							vqa_pipeline(_lowerCamelCase    ,     top_k=1				)
         self.assertEqual(
             _lowerCamelCase    ,     [
                 [{'score': ANY(_lowerCamelCase				), 'answer': ANY(_lowerCamelCase				)}],
                 [{'score': ANY(_lowerCamelCase				), 'answer': ANY(_lowerCamelCase				)}],
             ]    ,     )





       @require_torch
       def      _a       (   self				):
         UpperCamelCase_:   Tuple        =							pipeline('visual-question-answering'    ,     model='hf-internal-testing/tiny-vilt-random-vqa'				)
         UpperCamelCase_:   Any        =							'./tests/fixtures/tests_samples/COCO/000000039769.png'
         UpperCamelCase_:   int        =							'How many cats are there?'

         UpperCamelCase_:   int        =							vqa_pipeline(image=_lowerCamelCase    ,     question='How many cats are there?'    ,     top_k=2				)
         self.assertEqual(
             _lowerCamelCase    ,     [{'score': ANY(_lowerCamelCase				), 'answer': ANY(_lowerCamelCase				)}, {'score': ANY(_lowerCamelCase				), 'answer': ANY(_lowerCamelCase				)}]				)

         UpperCamelCase_:   Optional[Any]        =							vqa_pipeline({'image': image, 'question': question}    ,     top_k=2				)
         self.assertEqual(
             _lowerCamelCase    ,     [{'score': ANY(_lowerCamelCase				), 'answer': ANY(_lowerCamelCase				)}, {'score': ANY(_lowerCamelCase				), 'answer': ANY(_lowerCamelCase				)}]				)





       @slow
       @require_torch
       def      _a       (   self				):
         UpperCamelCase_:   Optional[Any]        =							pipeline('visual-question-answering'    ,     model='dandelin/vilt-b32-finetuned-vqa'				)
         UpperCamelCase_:   List[str]        =							'./tests/fixtures/tests_samples/COCO/000000039769.png'
         UpperCamelCase_:   str        =							'How many cats are there?'

         UpperCamelCase_:   Optional[Any]        =							vqa_pipeline(image=_lowerCamelCase    ,     question=_lowerCamelCase    ,     top_k=2				)
         self.assertEqual(
             nested_simplify(_lowerCamelCase    ,     decimals=4				)    ,     [{'score': 0.8_7_9_9, 'answer': '2'}, {'score': 0.2_9_6, 'answer': '1'}]				)

         UpperCamelCase_:   Tuple        =							vqa_pipeline({'image': image, 'question': question}    ,     top_k=2				)
         self.assertEqual(
             nested_simplify(_lowerCamelCase    ,     decimals=4				)    ,     [{'score': 0.8_7_9_9, 'answer': '2'}, {'score': 0.2_9_6, 'answer': '1'}]				)

         UpperCamelCase_:   Optional[int]        =							vqa_pipeline(
             [{'image': image, 'question': question}, {'image': image, 'question': question}]    ,     top_k=2				)
         self.assertEqual(
             nested_simplify(_lowerCamelCase    ,     decimals=4				)    ,     [[{'score': 0.8_7_9_9, 'answer': '2'}, {'score': 0.2_9_6, 'answer': '1'}]] * 2    ,     )





       @require_tf
       @unittest.skip('Visual question answering not implemented in TF'				)
       def      _a       (   self				):
         pass


'''simple docstring'''





from typing import TYPE_CHECKING

from ...utils import _LazyModule


UpperCamelCase_    						=       {"""tokenization_byt5""": ["""ByT5Tokenizer"""]}


if TYPE_CHECKING:
							from .tokenization_byta import ByTaTokenizer
else:
							import sys

							UpperCamelCase_    						=       _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)





