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   _lowerCAmelCase		(     unittest.TestCase				):







							def 				snake_case  (				self     :    List[Any]		):
										lowerCamelCase						:int						  =     tempfile.mkdtemp()

										# fmt: off
										lowerCamelCase						:Optional[Any]						  =     ['''[UNK]''', '''[CLS]''', '''[SEP]''', '''[PAD]''', '''[MASK]''', '''want''', '''##want''', '''##ed''', '''wa''', '''un''', '''runn''', '''##ing''', ''',''', '''low''', '''lowest''']
										# fmt: on
										lowerCamelCase						:List[Any]						  =     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]		)		)

										lowerCamelCase						:List[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],
										}
										lowerCamelCase						:Dict						  =     os.path.join(self.tmpdirname	,					__snake_case		)
										with open(self.image_processor_file	,					'''w'''	,					encoding='''utf-8'''		) as fp:
													json.dump(__snake_case	,					__snake_case		)







							def 				snake_case  (				self     :    int	,					**__snake_case     :    List[Any]		):
										return BertTokenizer.from_pretrained(self.tmpdirname	,					**__snake_case		)







							def 				snake_case  (				self     :    Dict	,					**__snake_case     :    Tuple		):
										return ViTImageProcessor.from_pretrained(self.tmpdirname	,					**__snake_case		)







							def 				snake_case  (				self     :    List[Any]		):
										shutil.rmtree(self.tmpdirname		)







							def 				snake_case  (				self     :    Optional[int]		):
										lowerCamelCase						:Optional[int]						  =     [np.random.randint(255	,					size=(3, 30, 400)	,					dtype=np.uinta		)]

										lowerCamelCase						:Optional[int]						  =     [Image.fromarray(np.moveaxis(__snake_case	,					0	,					-1		)		) for x in image_inputs]

										return image_inputs







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

										lowerCamelCase						:Any						  =     VisionTextDualEncoderProcessor(tokenizer=__snake_case	,					image_processor=__snake_case		)

										processor.save_pretrained(self.tmpdirname		)
										lowerCamelCase						:List[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	,					__snake_case		)







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

										lowerCamelCase						:Optional[Any]						  =     self.get_tokenizer(bos_token='''(BOS)'''	,					eos_token='''(EOS)'''		)
										lowerCamelCase						:Tuple						  =     self.get_image_processor(do_normalize=__snake_case	,					padding_value=1.0		)

										lowerCamelCase						:List[str]						  =     VisionTextDualEncoderProcessor.from_pretrained(
										    self.tmpdirname	,					bos_token='''(BOS)'''	,					eos_token='''(EOS)'''	,					do_normalize=__snake_case	,					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	,					__snake_case		)







							def 				snake_case  (				self     :    List[str]		):
										lowerCamelCase						:Dict						  =     self.get_image_processor()
										lowerCamelCase						:int						  =     self.get_tokenizer()

										lowerCamelCase						:Union[str, Any]						  =     VisionTextDualEncoderProcessor(tokenizer=__snake_case	,					image_processor=__snake_case		)

										lowerCamelCase						:Dict						  =     self.prepare_image_inputs()

										lowerCamelCase						:Union[str, Any]						  =     image_processor(__snake_case	,					return_tensors='''np'''		)
										lowerCamelCase						:Union[str, Any]						  =     processor(images=__snake_case	,					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 				snake_case  (				self     :    Optional[int]		):
										lowerCamelCase						:Tuple						  =     self.get_image_processor()
										lowerCamelCase						:Tuple						  =     self.get_tokenizer()

										lowerCamelCase						:Union[str, Any]						  =     VisionTextDualEncoderProcessor(tokenizer=__snake_case	,					image_processor=__snake_case		)

										lowerCamelCase						:List[str]						  =     '''lower newer'''

										lowerCamelCase						:str						  =     processor(text=__snake_case		)

										lowerCamelCase						:Dict						  =     tokenizer(__snake_case		)

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







							def 				snake_case  (				self     :    str		):
										lowerCamelCase						:Tuple						  =     self.get_image_processor()
										lowerCamelCase						:Tuple						  =     self.get_tokenizer()

										lowerCamelCase						:Dict						  =     VisionTextDualEncoderProcessor(tokenizer=__snake_case	,					image_processor=__snake_case		)

										lowerCamelCase						:Optional[Any]						  =     '''lower newer'''
										lowerCamelCase						:Tuple						  =     self.prepare_image_inputs()

										lowerCamelCase						:List[str]						  =     processor(text=__snake_case	,					images=__snake_case		)

										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(__snake_case		):
													processor()







							def 				snake_case  (				self     :    Optional[int]		):
										lowerCamelCase						:int						  =     self.get_image_processor()
										lowerCamelCase						:List[str]						  =     self.get_tokenizer()

										lowerCamelCase						:Optional[int]						  =     VisionTextDualEncoderProcessor(tokenizer=__snake_case	,					image_processor=__snake_case		)

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

										lowerCamelCase						:Dict						  =     processor.batch_decode(__snake_case		)
										lowerCamelCase						:Optional[int]						  =     tokenizer.batch_decode(__snake_case		)

										self.assertListEqual(__snake_case	,					__snake_case		)







							def 				snake_case  (				self     :    Tuple		):
										lowerCamelCase						:Tuple						  =     self.get_image_processor()
										lowerCamelCase						:List[Any]						  =     self.get_tokenizer()

										lowerCamelCase						:Any						  =     VisionTextDualEncoderProcessor(tokenizer=__snake_case	,					image_processor=__snake_case		)

										lowerCamelCase						:Optional[Any]						  =     '''lower newer'''
										lowerCamelCase						:Union[str, Any]						  =     self.prepare_image_inputs()

										lowerCamelCase						:Tuple						  =     processor(text=__snake_case	,					images=__snake_case		)

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




import unittest

from transformers import 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 				snake_case  (				*__snake_case     :    str	,					**__snake_case     :    str		):
													pass
@is_pipeline_test
@require_vision
class   _lowerCAmelCase		(     unittest.TestCase				):







							@require_torch
							def 				snake_case  (				self     :    Union[str, Any]		):
										lowerCamelCase						:Optional[int]						  =     pipeline(
										    model='''hf-internal-testing/tiny-random-clip-zero-shot-image-classification'''	,					)
										lowerCamelCase						:List[str]						  =     Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''		)
										lowerCamelCase						:Dict						  =     image_classifier(__snake_case	,					candidate_labels=['''a''', '''b''', '''c''']		)

										# The floating scores are so close, we enter floating error approximation and the order is not guaranteed across
										# python and torch versions.
										self.assertIn(
										    nested_simplify(__snake_case		)	,					[
										        [{'''score''': 0.3_3_3, '''label''': '''a'''}, {'''score''': 0.3_3_3, '''label''': '''b'''}, {'''score''': 0.3_3_3, '''label''': '''c'''}],
										        [{'''score''': 0.3_3_3, '''label''': '''a'''}, {'''score''': 0.3_3_3, '''label''': '''c'''}, {'''score''': 0.3_3_3, '''label''': '''b'''}],
										    ]	,					)

										lowerCamelCase						:Tuple						  =     image_classifier([image] * 5	,					candidate_labels=['''A''', '''B''', '''C''']	,					batch_size=2		)
										self.assertEqual(
										    nested_simplify(__snake_case		)	,					[
										        [
										            {'''score''': 0.3_3_3, '''label''': ANY(__snake_case		)},
										            {'''score''': 0.3_3_3, '''label''': ANY(__snake_case		)},
										            {'''score''': 0.3_3_3, '''label''': ANY(__snake_case		)},
										        ],
										        [
										            {'''score''': 0.3_3_3, '''label''': ANY(__snake_case		)},
										            {'''score''': 0.3_3_3, '''label''': ANY(__snake_case		)},
										            {'''score''': 0.3_3_3, '''label''': ANY(__snake_case		)},
										        ],
										        [
										            {'''score''': 0.3_3_3, '''label''': ANY(__snake_case		)},
										            {'''score''': 0.3_3_3, '''label''': ANY(__snake_case		)},
										            {'''score''': 0.3_3_3, '''label''': ANY(__snake_case		)},
										        ],
										        [
										            {'''score''': 0.3_3_3, '''label''': ANY(__snake_case		)},
										            {'''score''': 0.3_3_3, '''label''': ANY(__snake_case		)},
										            {'''score''': 0.3_3_3, '''label''': ANY(__snake_case		)},
										        ],
										        [
										            {'''score''': 0.3_3_3, '''label''': ANY(__snake_case		)},
										            {'''score''': 0.3_3_3, '''label''': ANY(__snake_case		)},
										            {'''score''': 0.3_3_3, '''label''': ANY(__snake_case		)},
										        ],
										    ]	,					)







							@require_tf
							def 				snake_case  (				self     :    Tuple		):
										lowerCamelCase						:Tuple						  =     pipeline(
										    model='''hf-internal-testing/tiny-random-clip-zero-shot-image-classification'''	,					framework='''tf'''		)
										lowerCamelCase						:List[str]						  =     Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''		)
										lowerCamelCase						:Optional[Any]						  =     image_classifier(__snake_case	,					candidate_labels=['''a''', '''b''', '''c''']		)

										self.assertEqual(
										    nested_simplify(__snake_case		)	,					[{'''score''': 0.3_3_3, '''label''': '''a'''}, {'''score''': 0.3_3_3, '''label''': '''b'''}, {'''score''': 0.3_3_3, '''label''': '''c'''}]	,					)

										lowerCamelCase						:int						  =     image_classifier([image] * 5	,					candidate_labels=['''A''', '''B''', '''C''']	,					batch_size=2		)
										self.assertEqual(
										    nested_simplify(__snake_case		)	,					[
										        [
										            {'''score''': 0.3_3_3, '''label''': ANY(__snake_case		)},
										            {'''score''': 0.3_3_3, '''label''': ANY(__snake_case		)},
										            {'''score''': 0.3_3_3, '''label''': ANY(__snake_case		)},
										        ],
										        [
										            {'''score''': 0.3_3_3, '''label''': ANY(__snake_case		)},
										            {'''score''': 0.3_3_3, '''label''': ANY(__snake_case		)},
										            {'''score''': 0.3_3_3, '''label''': ANY(__snake_case		)},
										        ],
										        [
										            {'''score''': 0.3_3_3, '''label''': ANY(__snake_case		)},
										            {'''score''': 0.3_3_3, '''label''': ANY(__snake_case		)},
										            {'''score''': 0.3_3_3, '''label''': ANY(__snake_case		)},
										        ],
										        [
										            {'''score''': 0.3_3_3, '''label''': ANY(__snake_case		)},
										            {'''score''': 0.3_3_3, '''label''': ANY(__snake_case		)},
										            {'''score''': 0.3_3_3, '''label''': ANY(__snake_case		)},
										        ],
										        [
										            {'''score''': 0.3_3_3, '''label''': ANY(__snake_case		)},
										            {'''score''': 0.3_3_3, '''label''': ANY(__snake_case		)},
										            {'''score''': 0.3_3_3, '''label''': ANY(__snake_case		)},
										        ],
										    ]	,					)







							@slow
							@require_torch
							def 				snake_case  (				self     :    Any		):
										lowerCamelCase						:str						  =     pipeline(
										    task='''zero-shot-image-classification'''	,					model='''openai/clip-vit-base-patch32'''	,					)
										# This is an image of 2 cats with remotes and no planes
										lowerCamelCase						:List[str]						  =     Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''		)
										lowerCamelCase						:Optional[Any]						  =     image_classifier(__snake_case	,					candidate_labels=['''cat''', '''plane''', '''remote''']		)

										self.assertEqual(
										    nested_simplify(__snake_case		)	,					[
										        {'''score''': 0.5_1_1, '''label''': '''remote'''},
										        {'''score''': 0.4_8_5, '''label''': '''cat'''},
										        {'''score''': 0.0_0_4, '''label''': '''plane'''},
										    ]	,					)

										lowerCamelCase						:Any						  =     image_classifier([image] * 5	,					candidate_labels=['''cat''', '''plane''', '''remote''']	,					batch_size=2		)
										self.assertEqual(
										    nested_simplify(__snake_case		)	,					[
										        [
										            {'''score''': 0.5_1_1, '''label''': '''remote'''},
										            {'''score''': 0.4_8_5, '''label''': '''cat'''},
										            {'''score''': 0.0_0_4, '''label''': '''plane'''},
										        ],
										    ]
										    * 5	,					)







							@slow
							@require_tf
							def 				snake_case  (				self     :    Optional[Any]		):
										lowerCamelCase						:Union[str, Any]						  =     pipeline(
										    task='''zero-shot-image-classification'''	,					model='''openai/clip-vit-base-patch32'''	,					framework='''tf'''		)
										# This is an image of 2 cats with remotes and no planes
										lowerCamelCase						:Dict						  =     Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''		)
										lowerCamelCase						:Dict						  =     image_classifier(__snake_case	,					candidate_labels=['''cat''', '''plane''', '''remote''']		)
										self.assertEqual(
										    nested_simplify(__snake_case		)	,					[
										        {'''score''': 0.5_1_1, '''label''': '''remote'''},
										        {'''score''': 0.4_8_5, '''label''': '''cat'''},
										        {'''score''': 0.0_0_4, '''label''': '''plane'''},
										    ]	,					)

										lowerCamelCase						:Any						  =     image_classifier([image] * 5	,					candidate_labels=['''cat''', '''plane''', '''remote''']	,					batch_size=2		)
										self.assertEqual(
										    nested_simplify(__snake_case		)	,					[
										        [
										            {'''score''': 0.5_1_1, '''label''': '''remote'''},
										            {'''score''': 0.4_8_5, '''label''': '''cat'''},
										            {'''score''': 0.0_0_4, '''label''': '''plane'''},
										        ],
										    ]
										    * 5	,					)



