


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   __a			:
  @staticmethod
  def 		SCREAMING_SNAKE_CASE__   (   *lowerCAmelCase__				, **lowerCAmelCase__ )					->				Tuple:




         '''simple docstring'''
         pass





@is_pipeline_test
@require_torch
@require_vision
class   __a			(    unittest.TestCase      ):
 __lowercase :							int        = MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING
 def 		SCREAMING_SNAKE_CASE__   (   self				, lowerCAmelCase__				, lowerCAmelCase__				, lowerCAmelCase__ )					->				Optional[Any]:




        '''simple docstring'''
        lowercase__:   Optional[int]  			=   pipeline('visual-question-answering'				, model='hf-internal-testing/tiny-vilt-random-vqa' )
        lowercase__:   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 		SCREAMING_SNAKE_CASE__   (   self				, lowerCAmelCase__				, lowerCAmelCase__ )					->				Union[str, Any]:




        '''simple docstring'''
        lowercase__:   List[Any]  			=   vqa_pipeline(lowerCAmelCase__				, top_k=1 )
        self.assertEqual(
            lowerCAmelCase__				, [
                [{'score': ANY(lowerCAmelCase__ ), 'answer': ANY(lowerCAmelCase__ )}],
                [{'score': ANY(lowerCAmelCase__ ), 'answer': ANY(lowerCAmelCase__ )}],
            ]				, )
 @require_torch
 def 		SCREAMING_SNAKE_CASE__   (   self )					->				int:




        '''simple docstring'''
        lowercase__:   Any  			=   pipeline('visual-question-answering'				, model='hf-internal-testing/tiny-vilt-random-vqa' )
        lowercase__:   str  			=   './tests/fixtures/tests_samples/COCO/000000039769.png'
        lowercase__:   Optional[Any]  			=   'How many cats are there?'

        lowercase__:   Tuple  			=   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__ )}] )

        lowercase__:   str  			=   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 		SCREAMING_SNAKE_CASE__   (   self )					->				List[str]:




        '''simple docstring'''
        lowercase__:   List[Any]  			=   pipeline('visual-question-answering'				, model='dandelin/vilt-b32-finetuned-vqa' )
        lowercase__:   Optional[Any]  			=   './tests/fixtures/tests_samples/COCO/000000039769.png'
        lowercase__:   str  			=   'How many cats are there?'

        lowercase__:   Dict  			=   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'}] )

        lowercase__:   str  			=   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'}] )

        lowercase__:   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 		SCREAMING_SNAKE_CASE__   (   self )					->				Optional[int]:




        '''simple docstring'''
        pass






def    snake_case_     (  snake_case			,							snake_case     )  ->				bool:
       return numa ^ numa < 0


if __name__ == "__main__":
 import doctest

 doctest.testmod()


