





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




       """simple docstring"""



       @staticmethod
       def 							__lowercase		(					*lowercase					,  **lowercase)			->						Optional[int]:







              '''simple docstring'''

              pass





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




   """simple docstring"""



   __A   :				str         =  MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING

   def 							__lowercase		(					self					,  lowercase					,  lowercase					,  lowercase)			->						Union[str, Any]:







          '''simple docstring'''

          a__						:	str =				pipeline('visual-question-answering'					,  model='hf-internal-testing/tiny-vilt-random-vqa')
          a__						:	int =				[
              {
                  '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 							__lowercase		(					self					,  lowercase					,  lowercase)			->						Dict:







          '''simple docstring'''

          a__						:	str =				vqa_pipeline(lowercase					,  top_k=1)
          self.assertEqual(
              lowercase					,  [
                  [{'score': ANY(lowercase), 'answer': ANY(lowercase)}],
                  [{'score': ANY(lowercase), 'answer': ANY(lowercase)}],
              ]					,  )

   @require_torch
   def 							__lowercase		(					self)			->						str:







          '''simple docstring'''

          a__						:	Tuple =				pipeline('visual-question-answering'					,  model='hf-internal-testing/tiny-vilt-random-vqa')
          a__						:	str =				'./tests/fixtures/tests_samples/COCO/000000039769.png'
          a__						:	Tuple =				'How many cats are there?'

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

          a__						:	List[Any] =				vqa_pipeline({'image': image, 'question': question}					,  top_k=2)
          self.assertEqual(
              lowercase					,  [{'score': ANY(lowercase), 'answer': ANY(lowercase)}, {'score': ANY(lowercase), 'answer': ANY(lowercase)}])

   @slow
   @require_torch
   def 							__lowercase		(					self)			->						Optional[int]:







          '''simple docstring'''

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

          a__						:	Any =				vqa_pipeline(image=lowercase					,  question=lowercase					,  top_k=2)
          self.assertEqual(
              nested_simplify(lowercase					,  decimals=4)					,  [{'score': 0.87_99, 'answer': '2'}, {'score': 0.2_96, 'answer': '1'}])

          a__						:	Union[str, Any] =				vqa_pipeline({'image': image, 'question': question}					,  top_k=2)
          self.assertEqual(
              nested_simplify(lowercase					,  decimals=4)					,  [{'score': 0.87_99, 'answer': '2'}, {'score': 0.2_96, 'answer': '1'}])

          a__						:	Optional[int] =				vqa_pipeline(
              [{'image': image, 'question': question}, {'image': image, 'question': question}]					,  top_k=2)
          self.assertEqual(
              nested_simplify(lowercase					,  decimals=4)					,  [[{'score': 0.87_99, 'answer': '2'}, {'score': 0.2_96, 'answer': '1'}]] * 2					,  )

   @require_tf
   @unittest.skip('Visual question answering not implemented in TF')
   def 							__lowercase		(					self)			->						Dict:







          '''simple docstring'''

          pass







# tests directory-specific settings - this file is run automatically
# by pytest before any tests are run

import sys
import warnings
from os.path import abspath, dirname, join


# allow having multiple repository checkouts and not needing to remember to rerun
# 'pip install -e .[dev]' when switching between checkouts and running tests.
A__								=						abspath(join(dirname(dirname(dirname(__file__))), '''src'''))
sys.path.insert(1, git_repo_path)


# silence FutureWarning warnings in tests since often we can't act on them until
# they become normal warnings - i.e. the tests still need to test the current functionality
warnings.simplefilter(action='''ignore''', category=FutureWarning)



def 			_lowerCAmelCase       (					__lowerCAmelCase      )   ->					str:


				"""simple docstring"""

				from transformers.testing_utils import pytest_addoption_shared

				pytest_addoption_shared(__lowerCAmelCase      )





def 			_lowerCAmelCase       (					__lowerCAmelCase      )   ->					List[Any]:


				"""simple docstring"""

				from transformers.testing_utils import pytest_terminal_summary_main

				snake_case__	:      Dict     		=	terminalreporter.config.getoption('''--make-reports'''      )
				if make_reports:
								pytest_terminal_summary_main(__lowerCAmelCase  ,  id=__lowerCAmelCase      )

