



'''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 	_lowerCAmelCase							:


							@staticmethod
							def     _a	(*lowercase       ,      **lowercase							):
										pass







@is_pipeline_test
@require_torch
@require_vision
class 	_lowerCAmelCase							(			unittest.TestCase ):
	__SCREAMING_SNAKE_CASE   :		List[Any]					=					MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING


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


	@require_torch
	def     _a	(self							):
				A_	:						Optional[int]	=     pipeline("""visual-question-answering"""       ,      model="""hf-internal-testing/tiny-vilt-random-vqa"""							)
				A_	:						List[Any]	=     """./tests/fixtures/tests_samples/COCO/000000039769.png"""
				A_	:						Tuple	=     """How many cats are there?"""

				A_	:						List[Any]	=     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[str]	=     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     _a	(self							):
				A_	:						str	=     pipeline("""visual-question-answering"""       ,      model="""dandelin/vilt-b32-finetuned-vqa"""							)
				A_	:						List[str]	=     """./tests/fixtures/tests_samples/COCO/000000039769.png"""
				A_	:						Optional[Any]	=     """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_	:						Dict	=     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[Any]	=     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     _a	(self							):
				pass




'''simple docstring'''




from typing import List, Optional, Union

from ...image_utils import ImageInput
from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy
from ...utils import TensorType







class 	_lowerCAmelCase							(			__UpperCAmelCase ):
	__SCREAMING_SNAKE_CASE   :		Any					=					['image_processor', 'tokenizer']
	__SCREAMING_SNAKE_CASE   :		Union[str, Any]					=					'BlipImageProcessor'
	__SCREAMING_SNAKE_CASE   :		List[Any]					=					('BertTokenizer', 'BertTokenizerFast')


	def __init__(self       ,      lowercase       ,      lowercase							):
				A_	:						List[Any]	=     False
				super().__init__(lowercase       ,      lowercase							)
				A_	:						Tuple	=     self.image_processor


	def __call__(self       ,      lowercase = None       ,      lowercase = None       ,      lowercase = True       ,      lowercase = False       ,      lowercase = None       ,      lowercase = None       ,      lowercase = 0       ,      lowercase = None       ,      lowercase = None       ,      lowercase = False       ,      lowercase = False       ,      lowercase = False       ,      lowercase = False       ,      lowercase = False       ,      lowercase = True       ,      lowercase = None       ,      **lowercase       ,      ):
				if images is None and text is None:
							raise ValueError("""You have to specify either images or text."""							)

				# Get only text
				if images is None:
							A_	:						Optional[Any]	=     self.tokenizer
							A_	:						Tuple	=     self.tokenizer(
							    text=lowercase       ,      add_special_tokens=lowercase       ,      padding=lowercase       ,      truncation=lowercase       ,      max_length=lowercase       ,      stride=lowercase       ,      pad_to_multiple_of=lowercase       ,      return_attention_mask=lowercase       ,      return_overflowing_tokens=lowercase       ,      return_special_tokens_mask=lowercase       ,      return_offsets_mapping=lowercase       ,      return_token_type_ids=lowercase       ,      return_length=lowercase       ,      verbose=lowercase       ,      return_tensors=lowercase       ,      **lowercase       ,      )
							return text_encoding

				# add pixel_values
				A_	:						int	=     self.image_processor(lowercase       ,      return_tensors=lowercase							)

				if text is not None:
							A_	:						Optional[Any]	=     self.tokenizer(
							    text=lowercase       ,      add_special_tokens=lowercase       ,      padding=lowercase       ,      truncation=lowercase       ,      max_length=lowercase       ,      stride=lowercase       ,      pad_to_multiple_of=lowercase       ,      return_attention_mask=lowercase       ,      return_overflowing_tokens=lowercase       ,      return_special_tokens_mask=lowercase       ,      return_offsets_mapping=lowercase       ,      return_token_type_ids=lowercase       ,      return_length=lowercase       ,      verbose=lowercase       ,      return_tensors=lowercase       ,      **lowercase       ,      )
				else:
							A_	:						List[str]	=     None

				if text_encoding is not None:
							encoding_image_processor.update(lowercase							)

				return encoding_image_processor


	def     _a	(self       ,      *lowercase       ,      **lowercase							):
				return self.tokenizer.batch_decode(*lowercase       ,      **lowercase							)


	def     _a	(self       ,      *lowercase       ,      **lowercase							):
				return self.tokenizer.decode(*lowercase       ,      **lowercase							)


	@property
	def     _a	(self							):
				A_	:						int	=     self.tokenizer.model_input_names
				A_	:						Any	=     self.image_processor.model_input_names
				return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names							)							)