






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







       '''simple docstring'''




       @staticmethod
       def 		lowerCAmelCase_     (				*UpperCamelCase_       :			str ,	**UpperCamelCase_       :			List[Any]				):


           """simple docstring"""



           pass


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







     '''simple docstring'''
     SCREAMING_SNAKE_CASE   						=				MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING




     def 		lowerCAmelCase_     (				self       :			int ,	UpperCamelCase_       :			Tuple ,	UpperCamelCase_       :			Tuple ,	UpperCamelCase_       :			List[str]				):


         """simple docstring"""



         __A         =	pipeline("""visual-question-answering""" ,	model="""hf-internal-testing/tiny-vilt-random-vqa"""				)
         __A         =	[
             {
                 """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 		lowerCAmelCase_     (				self       :			str ,	UpperCamelCase_       :			str ,	UpperCamelCase_       :			Optional[int]				):


         """simple docstring"""



         __A         =	vqa_pipeline(UpperCamelCase_ ,	top_k=1				)
         self.assertEqual(
             UpperCamelCase_ ,	[
                 [{"""score""": ANY(UpperCamelCase_				), """answer""": ANY(UpperCamelCase_				)}],
                 [{"""score""": ANY(UpperCamelCase_				), """answer""": ANY(UpperCamelCase_				)}],
             ] ,	)




     @require_torch
     def 		lowerCAmelCase_     (				self       :			int				):


         """simple docstring"""



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

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

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




     @slow
     @require_torch
     def 		lowerCAmelCase_     (				self       :			Optional[int]				):


         """simple docstring"""



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

         __A         =	vqa_pipeline(image=UpperCamelCase_ ,	question=UpperCamelCase_ ,	top_k=2				)
         self.assertEqual(
             nested_simplify(UpperCamelCase_ ,	decimals=4				) ,	[{"""score""": 0.8799, """answer""": """2"""}, {"""score""": 0.296, """answer""": """1"""}]				)

         __A         =	vqa_pipeline({"""image""": image, """question""": question} ,	top_k=2				)
         self.assertEqual(
             nested_simplify(UpperCamelCase_ ,	decimals=4				) ,	[{"""score""": 0.8799, """answer""": """2"""}, {"""score""": 0.296, """answer""": """1"""}]				)

         __A         =	vqa_pipeline(
             [{"""image""": image, """question""": question}, {"""image""": image, """question""": question}] ,	top_k=2				)
         self.assertEqual(
             nested_simplify(UpperCamelCase_ ,	decimals=4				) ,	[[{"""score""": 0.8799, """answer""": """2"""}, {"""score""": 0.296, """answer""": """1"""}]] * 2 ,	)





     @require_tf
     @unittest.skip("""Visual question answering not implemented in TF"""				)
     def 		lowerCAmelCase_     (				self       :			List[Any]				):


         """simple docstring"""



         pass












from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding


class       __lowercase      (	lowercase_  ):







     '''simple docstring'''
     SCREAMING_SNAKE_CASE   						=				["image_processor", "tokenizer"]
     SCREAMING_SNAKE_CASE   						=				"AutoImageProcessor"
     SCREAMING_SNAKE_CASE   						=				"AutoTokenizer"




     def __init__(				self       :			str ,	UpperCamelCase_       :			Any ,	UpperCamelCase_       :			str				):


         """simple docstring"""



         super().__init__(UpperCamelCase_ ,	UpperCamelCase_				)
         __A         =	self.image_processor




     def __call__(				self       :			Optional[Any] ,	UpperCamelCase_       :			Optional[Any]=None ,	UpperCamelCase_       :			Any=None ,	UpperCamelCase_       :			List[Any]=None ,	**UpperCamelCase_       :			int				):


         """simple docstring"""



         if text is None and images is None:
             raise ValueError("""You have to specify either text or images. Both cannot be none."""				)

         if text is not None:
             __A         =	self.tokenizer(UpperCamelCase_ ,	return_tensors=UpperCamelCase_ ,	**UpperCamelCase_				)

         if images is not None:
             __A         =	self.image_processor(UpperCamelCase_ ,	return_tensors=UpperCamelCase_ ,	**UpperCamelCase_				)

         if text is not None and images is not None:
             __A         =	image_features.pixel_values
             return encoding
         elif text is not None:
             return encoding
         else:
             return BatchEncoding(data=dict(**UpperCamelCase_				) ,	tensor_type=UpperCamelCase_				)




     def 		lowerCAmelCase_     (				self       :			List[str] ,	*UpperCamelCase_       :			Tuple ,	**UpperCamelCase_       :			List[str]				):


         """simple docstring"""



         return self.tokenizer.batch_decode(*UpperCamelCase_ ,	**UpperCamelCase_				)




     def 		lowerCAmelCase_     (				self       :			Any ,	*UpperCamelCase_       :			int ,	**UpperCamelCase_       :			Tuple				):


         """simple docstring"""



         return self.tokenizer.decode(*UpperCamelCase_ ,	**UpperCamelCase_				)





     @property
     def 		lowerCAmelCase_     (				self       :			Optional[Any]				):


         """simple docstring"""



         return ["input_ids", "attention_mask", "pixel_values"]




