"""simple docstring"""






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 				A__       (						*__lowerCAmelCase		,       **__lowerCAmelCase						):







                   """simple docstring"""



                   pass





@is_pipeline_test
@require_torch
@require_vision
class  _A						(       unittest.TestCase	):
      snake_case__			:   Union[str, Any]							  =	MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING

      def 				A__       (						self		,       __lowerCAmelCase		,       __lowerCAmelCase		,       __lowerCAmelCase						):







            """simple docstring"""



            lowercase														=	pipeline("""visual-question-answering"""		,       model="""hf-internal-testing/tiny-vilt-random-vqa"""						)
            lowercase														=	[
                {
                    """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						):







            """simple docstring"""



            lowercase														=	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						):







            """simple docstring"""



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

            lowercase														=	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														=	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						):







            """simple docstring"""



            lowercase														=	pipeline("""visual-question-answering"""		,       model="""dandelin/vilt-b32-finetuned-vqa"""						)
            lowercase														=	"""./tests/fixtures/tests_samples/COCO/000000039769.png"""
            lowercase														=	"""How many cats are there?"""

            lowercase														=	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														=	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														=	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						):







            """simple docstring"""



            pass




"""simple docstring"""






from ..utils import DummyObject, requires_backends





class  _A						(       metaclass=lowerCAmelCase	):
      snake_case__			:   Optional[int]							  =	['torch', 'torchsde']

      def __init__(						self		,       *__lowerCAmelCase		,       **__lowerCAmelCase						):







            """simple docstring"""



            requires_backends(self		,       ["""torch""", """torchsde"""]						)

      @classmethod
      def 				A__       (						cls		,       *__lowerCAmelCase		,       **__lowerCAmelCase						):







            """simple docstring"""



            requires_backends(cls		,       ["""torch""", """torchsde"""]						)







      @classmethod
      def 				A__       (						cls		,       *__lowerCAmelCase		,       **__lowerCAmelCase						):







            """simple docstring"""



            requires_backends(cls		,       ["""torch""", """torchsde"""]						)



