






"""simple docstring"""
import json
import os
import shutil
import tempfile
import unittest

import numpy as np

from transformers import BertTokenizerFast
from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES, BertTokenizer
from transformers.testing_utils import require_tokenizers, require_vision
from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available


if is_vision_available():
							from PIL import Image

							from transformers import VisionTextDualEncoderProcessor, ViTImageProcessor



@require_tokenizers
@require_vision
class      SCREAMING_SNAKE_CASE  (				unittest.TestCase						):





	"""simple docstring"""




	def    lowerCamelCase(self							):
		A_		:						int	    =   tempfile.mkdtemp()

		# fmt: off
		A_		:						Union[str, Any]	    =   ["""[UNK]""", """[CLS]""", """[SEP]""", """[PAD]""", """[MASK]""", """want""", """##want""", """##ed""", """wa""", """un""", """runn""", """##ing""", """,""", """low""", """lowest"""]
		# fmt: on
		A_		:						int	    =   os.path.join(self.tmpdirname     ,     VOCAB_FILES_NAMES["""vocab_file"""]							)
		with open(self.vocab_file     ,     """w"""     ,     encoding="""utf-8"""							) as vocab_writer:
			vocab_writer.write("""""".join([x + """\n""" for x in vocab_tokens]							)							)

		A_		:						str	    =   {
		    """do_resize""": True,
		    """size""": {"""height""": 18, """width""": 18},
		    """do_normalize""": True,
		    """image_mean""": [0.5, 0.5, 0.5],
		    """image_std""": [0.5, 0.5, 0.5],
		}
		A_		:						str	    =   os.path.join(self.tmpdirname     ,     lowerCAmelCase_							)
		with open(self.image_processor_file     ,     """w"""     ,     encoding="""utf-8"""							) as fp:
			json.dump(lowerCAmelCase_     ,     lowerCAmelCase_							)





	def    lowerCamelCase(self     ,     **lowerCAmelCase_							):
		return BertTokenizer.from_pretrained(self.tmpdirname     ,     **lowerCAmelCase_							)





	def    lowerCamelCase(self     ,     **lowerCAmelCase_							):
		return ViTImageProcessor.from_pretrained(self.tmpdirname     ,     **lowerCAmelCase_							)





	def    lowerCamelCase(self							):
		shutil.rmtree(self.tmpdirname							)





	def    lowerCamelCase(self							):
		A_		:						List[str]	    =   [np.random.randint(255     ,     size=(3, 30, 400)     ,     dtype=np.uinta							)]

		A_		:						Dict	    =   [Image.fromarray(np.moveaxis(lowerCAmelCase_     ,     0     ,     -1							)							) for x in image_inputs]

		return image_inputs





	def    lowerCamelCase(self							):
		A_		:						int	    =   self.get_tokenizer()
		A_		:						Optional[Any]	    =   self.get_image_processor()

		A_		:						Optional[int]	    =   VisionTextDualEncoderProcessor(tokenizer=lowerCAmelCase_     ,     image_processor=lowerCAmelCase_							)

		processor.save_pretrained(self.tmpdirname							)
		A_		:						Union[str, Any]	    =   VisionTextDualEncoderProcessor.from_pretrained(self.tmpdirname							)

		self.assertEqual(processor.tokenizer.get_vocab()     ,     tokenizer.get_vocab()							)
		self.assertIsInstance(processor.tokenizer     ,     (BertTokenizer, BertTokenizerFast)							)

		self.assertEqual(processor.image_processor.to_json_string()     ,     image_processor.to_json_string()							)
		self.assertIsInstance(processor.image_processor     ,     lowerCAmelCase_							)





	def    lowerCamelCase(self							):
		A_		:						Dict	    =   VisionTextDualEncoderProcessor(
		    tokenizer=self.get_tokenizer()     ,     image_processor=self.get_image_processor()							)
		processor.save_pretrained(self.tmpdirname							)

		A_		:						Optional[Any]	    =   self.get_tokenizer(bos_token="""(BOS)"""     ,     eos_token="""(EOS)"""							)
		A_		:						Tuple	    =   self.get_image_processor(do_normalize=lowerCAmelCase_     ,     padding_value=1.0							)

		A_		:						int	    =   VisionTextDualEncoderProcessor.from_pretrained(
		    self.tmpdirname     ,     bos_token="""(BOS)"""     ,     eos_token="""(EOS)"""     ,     do_normalize=lowerCAmelCase_     ,     padding_value=1.0							)

		self.assertEqual(processor.tokenizer.get_vocab()     ,     tokenizer_add_kwargs.get_vocab()							)
		self.assertIsInstance(processor.tokenizer     ,     (BertTokenizer, BertTokenizerFast)							)

		self.assertEqual(processor.image_processor.to_json_string()     ,     image_processor_add_kwargs.to_json_string()							)
		self.assertIsInstance(processor.image_processor     ,     lowerCAmelCase_							)





	def    lowerCamelCase(self							):
		A_		:						Optional[int]	    =   self.get_image_processor()
		A_		:						Optional[Any]	    =   self.get_tokenizer()

		A_		:						str	    =   VisionTextDualEncoderProcessor(tokenizer=lowerCAmelCase_     ,     image_processor=lowerCAmelCase_							)

		A_		:						str	    =   self.prepare_image_inputs()

		A_		:						Union[str, Any]	    =   image_processor(lowerCAmelCase_     ,     return_tensors="""np"""							)
		A_		:						List[str]	    =   processor(images=lowerCAmelCase_     ,     return_tensors="""np"""							)

		for key in input_feat_extract.keys():
			self.assertAlmostEqual(input_feat_extract[key].sum()     ,     input_processor[key].sum()     ,     delta=1e-2							)





	def    lowerCamelCase(self							):
		A_		:						Optional[Any]	    =   self.get_image_processor()
		A_		:						str	    =   self.get_tokenizer()

		A_		:						Union[str, Any]	    =   VisionTextDualEncoderProcessor(tokenizer=lowerCAmelCase_     ,     image_processor=lowerCAmelCase_							)

		A_		:						Optional[int]	    =   """lower newer"""

		A_		:						str	    =   processor(text=lowerCAmelCase_							)

		A_		:						Optional[int]	    =   tokenizer(lowerCAmelCase_							)

		for key in encoded_tok.keys():
			self.assertListEqual(encoded_tok[key]     ,     encoded_processor[key]							)





	def    lowerCamelCase(self							):
		A_		:						int	    =   self.get_image_processor()
		A_		:						int	    =   self.get_tokenizer()

		A_		:						int	    =   VisionTextDualEncoderProcessor(tokenizer=lowerCAmelCase_     ,     image_processor=lowerCAmelCase_							)

		A_		:						Any	    =   """lower newer"""
		A_		:						Optional[int]	    =   self.prepare_image_inputs()

		A_		:						Dict	    =   processor(text=lowerCAmelCase_     ,     images=lowerCAmelCase_							)

		self.assertListEqual(list(inputs.keys()							)     ,     ["""input_ids""", """token_type_ids""", """attention_mask""", """pixel_values"""]							)

		# test if it raises when no input is passed
		with self.assertRaises(lowerCAmelCase_							):
			processor()





	def    lowerCamelCase(self							):
		A_		:						Dict	    =   self.get_image_processor()
		A_		:						Optional[Any]	    =   self.get_tokenizer()

		A_		:						Union[str, Any]	    =   VisionTextDualEncoderProcessor(tokenizer=lowerCAmelCase_     ,     image_processor=lowerCAmelCase_							)

		A_		:						str	    =   [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

		A_		:						Dict	    =   processor.batch_decode(lowerCAmelCase_							)
		A_		:						Any	    =   tokenizer.batch_decode(lowerCAmelCase_							)

		self.assertListEqual(lowerCAmelCase_     ,     lowerCAmelCase_							)





	def    lowerCamelCase(self							):
		A_		:						int	    =   self.get_image_processor()
		A_		:						Union[str, Any]	    =   self.get_tokenizer()

		A_		:						Optional[int]	    =   VisionTextDualEncoderProcessor(tokenizer=lowerCAmelCase_     ,     image_processor=lowerCAmelCase_							)

		A_		:						Union[str, Any]	    =   """lower newer"""
		A_		:						Dict	    =   self.prepare_image_inputs()

		A_		:						Tuple	    =   processor(text=lowerCAmelCase_     ,     images=lowerCAmelCase_							)

		self.assertListEqual(list(inputs.keys()							)     ,     processor.model_input_names							)









"""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      SCREAMING_SNAKE_CASE  :





								"""simple docstring"""




								@staticmethod
								def    lowerCamelCase(*lowerCAmelCase_     ,     **lowerCAmelCase_							):
									pass



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





	"""simple docstring"""




	_A       :				List[Any]    =				MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING





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





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

		A_		:						Optional[int]	    =   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_							)}]							)

		A_		:						Any	    =   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    lowerCamelCase(self							):
		A_		:						int	    =   pipeline("""visual-question-answering"""     ,     model="""dandelin/vilt-b32-finetuned-vqa"""							)
		A_		:						Tuple	    =   """./tests/fixtures/tests_samples/COCO/000000039769.png"""
		A_		:						int	    =   """How many cats are there?"""

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

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

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

