


"""simple docstring"""


from typing import List

from .keymap import KEYMAP, get_character


def _lowerCamelCase      (						_UpperCamelCase ):

			'''simple docstring'''




			def decorator(_UpperCamelCase ):
						__lowerCAmelCase							       =     getattr(_UpperCamelCase       ,					"handle_key"       ,					[] )
						handle += [key]
						setattr(_UpperCamelCase       ,					"handle_key"       ,					_UpperCamelCase )
						return func

			return decorator


def _lowerCamelCase      (						*_UpperCamelCase ):

			'''simple docstring'''




			def decorator(_UpperCamelCase ):
						__lowerCAmelCase							       =     getattr(_UpperCamelCase       ,					"handle_key"       ,					[] )
						handle += keys
						setattr(_UpperCamelCase       ,					"handle_key"       ,					_UpperCamelCase )
						return func

			return decorator
class     _UpperCamelCase    (  lowerCAmelCase__						):
							'''simple docstring'''







							def __new__(	cls       ,  __a       ,  __a       ,  __a      ):
										__lowerCAmelCase							       =     super().__new__(cls       ,  __a       ,  __a       ,  __a      )
										if not hasattr(__a       ,  "key_handler"      ):
													setattr(__a       ,  "key_handler"       ,  {}      )
										setattr(__a       ,  "handle_input"       ,  KeyHandler.handle_input      )

										for value in attrs.values():
													__lowerCAmelCase							       =     getattr(__a       ,  "handle_key"       ,  []      )
													for key in handled_keys:
																__lowerCAmelCase							       =     value
										return new_cls



							@staticmethod
							def      snake_case							(	cls      ):
										__lowerCAmelCase							       =     get_character()
										if char != KEYMAP["undefined"]:
													__lowerCAmelCase							       =     ord(__a      )
										__lowerCAmelCase							       =     cls.key_handler.get(__a      )
										if handler:
													__lowerCAmelCase							       =     char
													return handler(cls      )
										else:
													return None


def _lowerCamelCase      (						cls ):

			'''simple docstring'''




			return KeyHandler(cls.__name__       ,					cls.__bases__       ,					cls.__dict__.copy() )








"""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     _UpperCamelCase    :
								'''simple docstring'''







								@staticmethod
								def      snake_case							(	*__a       ,  **__a      ):
											pass
@is_pipeline_test
@require_torch
@require_vision
class     _UpperCamelCase    (  unittest.TestCase						):
							'''simple docstring'''







							__UpperCAmelCase		:       str							 =MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING

							def      snake_case							(	self       ,  __a       ,  __a       ,  __a      ):
										__lowerCAmelCase							       =     pipeline("visual-question-answering"       ,  model="hf-internal-testing/tiny-vilt-random-vqa"      )
										__lowerCAmelCase							       =     [
										    {
										        "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      snake_case							(	self       ,  __a       ,  __a      ):
										__lowerCAmelCase							       =     vqa_pipeline(__a       ,  top_k=1      )
										self.assertEqual(
										    __a       ,  [
										        [{"score": ANY(__a      ), "answer": ANY(__a      )}],
										        [{"score": ANY(__a      ), "answer": ANY(__a      )}],
										    ]       ,  )

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

										__lowerCAmelCase							       =     vqa_pipeline(image=__a       ,  question="How many cats are there?"       ,  top_k=2      )
										self.assertEqual(
										    __a       ,  [{"score": ANY(__a      ), "answer": ANY(__a      )}, {"score": ANY(__a      ), "answer": ANY(__a      )}]      )

										__lowerCAmelCase							       =     vqa_pipeline({"image": image, "question": question}       ,  top_k=2      )
										self.assertEqual(
										    __a       ,  [{"score": ANY(__a      ), "answer": ANY(__a      )}, {"score": ANY(__a      ), "answer": ANY(__a      )}]      )

							@slow
							@require_torch
							def      snake_case							(	self      ):
										__lowerCAmelCase							       =     pipeline("visual-question-answering"       ,  model="dandelin/vilt-b32-finetuned-vqa"      )
										__lowerCAmelCase							       =     "./tests/fixtures/tests_samples/COCO/000000039769.png"
										__lowerCAmelCase							       =     "How many cats are there?"

										__lowerCAmelCase							       =     vqa_pipeline(image=__a       ,  question=__a       ,  top_k=2      )
										self.assertEqual(
										    nested_simplify(__a       ,  decimals=4      )       ,  [{"score": 0.8_7_9_9, "answer": "2"}, {"score": 0.2_9_6, "answer": "1"}]      )

										__lowerCAmelCase							       =     vqa_pipeline({"image": image, "question": question}       ,  top_k=2      )
										self.assertEqual(
										    nested_simplify(__a       ,  decimals=4      )       ,  [{"score": 0.8_7_9_9, "answer": "2"}, {"score": 0.2_9_6, "answer": "1"}]      )

										__lowerCAmelCase							       =     vqa_pipeline(
										    [{"image": image, "question": question}, {"image": image, "question": question}]       ,  top_k=2      )
										self.assertEqual(
										    nested_simplify(__a       ,  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      snake_case							(	self      ):
										pass




