import json
import os
import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers import CLIPTokenizer, CLIPTokenizerFast
from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES
from transformers.testing_utils import require_vision
from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available


if is_vision_available():
					from PIL import Image

					from transformers import CLIPImageProcessor, CLIPProcessor





@require_vision
class 					lowercase__						(   unittest.TestCase):


						def        __A       (       self	:				int						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    Optional[Any]							=							tempfile.mkdtemp()

												# fmt: off
												SCREAMING_SNAKE_CASE							:    Any							=							['''l''', '''o''', '''w''', '''e''', '''r''', '''s''', '''t''', '''i''', '''d''', '''n''', '''lo''', '''l</w>''', '''w</w>''', '''r</w>''', '''t</w>''', '''low</w>''', '''er</w>''', '''lowest</w>''', '''newer</w>''', '''wider''', '''<unk>''', '''<|startoftext|>''', '''<|endoftext|>''']
												# fmt: on
												SCREAMING_SNAKE_CASE							:    List[str]							=							dict(zip(UpperCamelCase__    ,		range(len(UpperCamelCase__						)						)						)						)
												SCREAMING_SNAKE_CASE							:    Optional[Any]							=							['''#version: 0.2''', '''l o''', '''lo w</w>''', '''e r</w>''', '''''']
												SCREAMING_SNAKE_CASE							:    Optional[Any]							=							{'''unk_token''': '''<unk>'''}

												SCREAMING_SNAKE_CASE							:    Optional[Any]							=							os.path.join(self.tmpdirname    ,		VOCAB_FILES_NAMES['''vocab_file''']						)
												SCREAMING_SNAKE_CASE							:    str							=							os.path.join(self.tmpdirname    ,		VOCAB_FILES_NAMES['''merges_file''']						)
												with open(self.vocab_file    ,		'''w'''    ,		encoding='''utf-8'''						) as fp:
																		fp.write(json.dumps(UpperCamelCase__						) + '''\n'''						)
												with open(self.merges_file    ,		'''w'''    ,		encoding='''utf-8'''						) as fp:
																		fp.write('''\n'''.join(UpperCamelCase__						)						)

												SCREAMING_SNAKE_CASE							:    str							=							{
												    '''do_resize''': True,
												    '''size''': 20,
												    '''do_center_crop''': True,
												    '''crop_size''': 18,
												    '''do_normalize''': True,
												    '''image_mean''': [0.4814_5466, 0.457_8275, 0.4082_1073],
												    '''image_std''': [0.2686_2954, 0.2613_0258, 0.2757_7711],
												}
												SCREAMING_SNAKE_CASE							:    Tuple							=							os.path.join(self.tmpdirname    ,		UpperCamelCase__						)
												with open(self.image_processor_file    ,		'''w'''    ,		encoding='''utf-8'''						) as fp:
																		json.dump(UpperCamelCase__    ,		UpperCamelCase__						)


						def        __A       (       self	:				Dict    ,		**UpperCamelCase__	:				List[Any]						):





												'''simple docstring'''





												return CLIPTokenizer.from_pretrained(self.tmpdirname    ,		**UpperCamelCase__						)


						def        __A       (       self	:				Dict    ,		**UpperCamelCase__	:				Union[str, Any]						):





												'''simple docstring'''





												return CLIPTokenizerFast.from_pretrained(self.tmpdirname    ,		**UpperCamelCase__						)


						def        __A       (       self	:				Any    ,		**UpperCamelCase__	:				Union[str, Any]						):





												'''simple docstring'''





												return CLIPImageProcessor.from_pretrained(self.tmpdirname    ,		**UpperCamelCase__						)


						def        __A       (       self	:				Optional[Any]						):





												'''simple docstring'''





												shutil.rmtree(self.tmpdirname						)


						def        __A       (       self	:				Optional[Any]						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    Dict							=							[np.random.randint(255    ,		size=(3, 30, 400)    ,		dtype=np.uinta						)]

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

												return image_inputs


						def        __A       (       self	:				Tuple						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    Dict							=							self.get_tokenizer()
												SCREAMING_SNAKE_CASE							:    Optional[int]							=							self.get_rust_tokenizer()
												SCREAMING_SNAKE_CASE							:    Any							=							self.get_image_processor()

												SCREAMING_SNAKE_CASE							:    List[Any]							=							CLIPProcessor(tokenizer=UpperCamelCase__    ,		image_processor=UpperCamelCase__						)
												processor_slow.save_pretrained(self.tmpdirname						)
												SCREAMING_SNAKE_CASE							:    str							=							CLIPProcessor.from_pretrained(self.tmpdirname    ,		use_fast=UpperCamelCase__						)

												SCREAMING_SNAKE_CASE							:    List[str]							=							CLIPProcessor(tokenizer=UpperCamelCase__    ,		image_processor=UpperCamelCase__						)
												processor_fast.save_pretrained(self.tmpdirname						)
												SCREAMING_SNAKE_CASE							:    Dict							=							CLIPProcessor.from_pretrained(self.tmpdirname						)

												self.assertEqual(processor_slow.tokenizer.get_vocab()    ,		tokenizer_slow.get_vocab()						)
												self.assertEqual(processor_fast.tokenizer.get_vocab()    ,		tokenizer_fast.get_vocab()						)
												self.assertEqual(tokenizer_slow.get_vocab()    ,		tokenizer_fast.get_vocab()						)
												self.assertIsInstance(processor_slow.tokenizer    ,		UpperCamelCase__						)
												self.assertIsInstance(processor_fast.tokenizer    ,		UpperCamelCase__						)

												self.assertEqual(processor_slow.image_processor.to_json_string()    ,		image_processor.to_json_string()						)
												self.assertEqual(processor_fast.image_processor.to_json_string()    ,		image_processor.to_json_string()						)
												self.assertIsInstance(processor_slow.image_processor    ,		UpperCamelCase__						)
												self.assertIsInstance(processor_fast.image_processor    ,		UpperCamelCase__						)


						def        __A       (       self	:				int						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    List[str]							=							CLIPProcessor(tokenizer=self.get_tokenizer()    ,		image_processor=self.get_image_processor()						)
												processor.save_pretrained(self.tmpdirname						)

												SCREAMING_SNAKE_CASE							:    List[Any]							=							self.get_tokenizer(bos_token='''(BOS)'''    ,		eos_token='''(EOS)'''						)
												SCREAMING_SNAKE_CASE							:    List[Any]							=							self.get_image_processor(do_normalize=UpperCamelCase__    ,		padding_value=1.0						)

												SCREAMING_SNAKE_CASE							:    List[Any]							=							CLIPProcessor.from_pretrained(
												    self.tmpdirname    ,		bos_token='''(BOS)'''    ,		eos_token='''(EOS)'''    ,		do_normalize=UpperCamelCase__    ,		padding_value=1.0						)

												self.assertEqual(processor.tokenizer.get_vocab()    ,		tokenizer_add_kwargs.get_vocab()						)
												self.assertIsInstance(processor.tokenizer    ,		UpperCamelCase__						)

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


						def        __A       (       self	:				Dict						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    Union[str, Any]							=							self.get_image_processor()
												SCREAMING_SNAKE_CASE							:    Tuple							=							self.get_tokenizer()

												SCREAMING_SNAKE_CASE							:    str							=							CLIPProcessor(tokenizer=UpperCamelCase__    ,		image_processor=UpperCamelCase__						)

												SCREAMING_SNAKE_CASE							:    int							=							self.prepare_image_inputs()

												SCREAMING_SNAKE_CASE							:    Optional[Any]							=							image_processor(UpperCamelCase__    ,		return_tensors='''np'''						)
												SCREAMING_SNAKE_CASE							:    str							=							processor(images=UpperCamelCase__    ,		return_tensors='''np'''						)

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


						def        __A       (       self	:				Any						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    str							=							self.get_image_processor()
												SCREAMING_SNAKE_CASE							:    Union[str, Any]							=							self.get_tokenizer()

												SCREAMING_SNAKE_CASE							:    int							=							CLIPProcessor(tokenizer=UpperCamelCase__    ,		image_processor=UpperCamelCase__						)

												SCREAMING_SNAKE_CASE							:    Any							=							'''lower newer'''

												SCREAMING_SNAKE_CASE							:    List[Any]							=							processor(text=UpperCamelCase__						)

												SCREAMING_SNAKE_CASE							:    Any							=							tokenizer(UpperCamelCase__						)

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


						def        __A       (       self	:				Any						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    Union[str, Any]							=							self.get_image_processor()
												SCREAMING_SNAKE_CASE							:    Tuple							=							self.get_tokenizer()

												SCREAMING_SNAKE_CASE							:    Union[str, Any]							=							CLIPProcessor(tokenizer=UpperCamelCase__    ,		image_processor=UpperCamelCase__						)

												SCREAMING_SNAKE_CASE							:    Optional[Any]							=							'''lower newer'''
												SCREAMING_SNAKE_CASE							:    Optional[int]							=							self.prepare_image_inputs()

												SCREAMING_SNAKE_CASE							:    int							=							processor(text=UpperCamelCase__    ,		images=UpperCamelCase__						)

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

												# test if it raises when no input is passed
												with pytest.raises(UpperCamelCase__						):
																		processor()


						def        __A       (       self	:				Optional[int]						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    str							=							self.get_image_processor()
												SCREAMING_SNAKE_CASE							:    Dict							=							self.get_tokenizer()

												SCREAMING_SNAKE_CASE							:    int							=							CLIPProcessor(tokenizer=UpperCamelCase__    ,		image_processor=UpperCamelCase__						)

												SCREAMING_SNAKE_CASE							:    List[Any]							=							[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

												SCREAMING_SNAKE_CASE							:    List[str]							=							processor.batch_decode(UpperCamelCase__						)
												SCREAMING_SNAKE_CASE							:    List[str]							=							tokenizer.batch_decode(UpperCamelCase__						)

												self.assertListEqual(UpperCamelCase__    ,		UpperCamelCase__						)



						def        __A       (       self	:				Optional[int]						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    Optional[int]							=							self.get_image_processor()
												SCREAMING_SNAKE_CASE							:    List[str]							=							self.get_tokenizer()

												SCREAMING_SNAKE_CASE							:    Optional[Any]							=							CLIPProcessor(tokenizer=UpperCamelCase__    ,		image_processor=UpperCamelCase__						)

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

												SCREAMING_SNAKE_CASE							:    Optional[Any]							=							processor(text=UpperCamelCase__    ,		images=UpperCamelCase__						)

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






import json
import sys
import tempfile
import unittest
from pathlib import Path

import transformers
from transformers import (
    CONFIG_MAPPING,
    IMAGE_PROCESSOR_MAPPING,
    AutoConfig,
    AutoImageProcessor,
    CLIPConfig,
    CLIPImageProcessor,
)
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER


sys.path.append(str(Path(__file__).parent.parent.parent.parent / 'utils'))

from test_module.custom_configuration import CustomConfig  # noqa E402
from test_module.custom_image_processing import CustomImageProcessor  # noqa E402





class 					lowercase__						(   unittest.TestCase):


						def        __A       (       self	:				List[Any]						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    Dict							=							0


						def        __A       (       self	:				Dict						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    str							=							AutoImageProcessor.from_pretrained('''openai/clip-vit-base-patch32'''						)
												self.assertIsInstance(UpperCamelCase__    ,		UpperCamelCase__						)


						def        __A       (       self	:				Dict						):





												'''simple docstring'''





												with tempfile.TemporaryDirectory() as tmpdirname:
																		SCREAMING_SNAKE_CASE							:    str							=							Path(UpperCamelCase__						) / '''preprocessor_config.json'''
																		SCREAMING_SNAKE_CASE							:    Dict							=							Path(UpperCamelCase__						) / '''config.json'''
																		json.dump(
																		    {'''image_processor_type''': '''CLIPImageProcessor''', '''processor_class''': '''CLIPProcessor'''}    ,		open(UpperCamelCase__    ,		'''w'''						)    ,		)
																		json.dump({'''model_type''': '''clip'''}    ,		open(UpperCamelCase__    ,		'''w'''						)						)

																		SCREAMING_SNAKE_CASE							:    Any							=							AutoImageProcessor.from_pretrained(UpperCamelCase__						)
																		self.assertIsInstance(UpperCamelCase__    ,		UpperCamelCase__						)


						def        __A       (       self	:				int						):





												'''simple docstring'''





												with tempfile.TemporaryDirectory() as tmpdirname:
																		SCREAMING_SNAKE_CASE							:    Tuple							=							Path(UpperCamelCase__						) / '''preprocessor_config.json'''
																		SCREAMING_SNAKE_CASE							:    Dict							=							Path(UpperCamelCase__						) / '''config.json'''
																		json.dump(
																		    {'''feature_extractor_type''': '''CLIPFeatureExtractor''', '''processor_class''': '''CLIPProcessor'''}    ,		open(UpperCamelCase__    ,		'''w'''						)    ,		)
																		json.dump({'''model_type''': '''clip'''}    ,		open(UpperCamelCase__    ,		'''w'''						)						)

																		SCREAMING_SNAKE_CASE							:    Optional[Any]							=							AutoImageProcessor.from_pretrained(UpperCamelCase__						)
																		self.assertIsInstance(UpperCamelCase__    ,		UpperCamelCase__						)


						def        __A       (       self	:				List[str]						):





												'''simple docstring'''





												with tempfile.TemporaryDirectory() as tmpdirname:
																		SCREAMING_SNAKE_CASE							:    Dict							=							CLIPConfig()

																		# Create a dummy config file with image_proceesor_type
																		SCREAMING_SNAKE_CASE							:    Dict							=							Path(UpperCamelCase__						) / '''preprocessor_config.json'''
																		SCREAMING_SNAKE_CASE							:    List[str]							=							Path(UpperCamelCase__						) / '''config.json'''
																		json.dump(
																		    {'''image_processor_type''': '''CLIPImageProcessor''', '''processor_class''': '''CLIPProcessor'''}    ,		open(UpperCamelCase__    ,		'''w'''						)    ,		)
																		json.dump({'''model_type''': '''clip'''}    ,		open(UpperCamelCase__    ,		'''w'''						)						)

																		# remove image_processor_type to make sure config.json alone is enough to load image processor locally
																		SCREAMING_SNAKE_CASE							:    List[Any]							=							AutoImageProcessor.from_pretrained(UpperCamelCase__						).to_dict()

																		config_dict.pop('''image_processor_type'''						)
																		SCREAMING_SNAKE_CASE							:    str							=							CLIPImageProcessor(**UpperCamelCase__						)

																		# save in new folder
																		model_config.save_pretrained(UpperCamelCase__						)
																		config.save_pretrained(UpperCamelCase__						)

																		SCREAMING_SNAKE_CASE							:    Union[str, Any]							=							AutoImageProcessor.from_pretrained(UpperCamelCase__						)

																		# make sure private variable is not incorrectly saved
																		SCREAMING_SNAKE_CASE							:    List[Any]							=							json.loads(config.to_json_string()						)
																		self.assertTrue('''_processor_class''' not in dict_as_saved						)

												self.assertIsInstance(UpperCamelCase__    ,		UpperCamelCase__						)


						def        __A       (       self	:				Tuple						):





												'''simple docstring'''





												with tempfile.TemporaryDirectory() as tmpdirname:
																		SCREAMING_SNAKE_CASE							:    Optional[int]							=							Path(UpperCamelCase__						) / '''preprocessor_config.json'''
																		json.dump(
																		    {'''image_processor_type''': '''CLIPImageProcessor''', '''processor_class''': '''CLIPProcessor'''}    ,		open(UpperCamelCase__    ,		'''w'''						)    ,		)

																		SCREAMING_SNAKE_CASE							:    Dict							=							AutoImageProcessor.from_pretrained(UpperCamelCase__						)
																		self.assertIsInstance(UpperCamelCase__    ,		UpperCamelCase__						)


						def        __A       (       self	:				int						):





												'''simple docstring'''





												with self.assertRaisesRegex(
												    UpperCamelCase__    ,		'''clip-base is not a local folder and is not a valid model identifier'''						):
																		SCREAMING_SNAKE_CASE							:    List[str]							=							AutoImageProcessor.from_pretrained('''clip-base'''						)


						def        __A       (       self	:				List[str]						):





												'''simple docstring'''





												with self.assertRaisesRegex(
												    UpperCamelCase__    ,		r'''aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)'''						):
																		SCREAMING_SNAKE_CASE							:    Tuple							=							AutoImageProcessor.from_pretrained(UpperCamelCase__    ,		revision='''aaaaaa'''						)


						def        __A       (       self	:				Dict						):





												'''simple docstring'''





												with self.assertRaisesRegex(
												    UpperCamelCase__    ,		'''hf-internal-testing/config-no-model does not appear to have a file named preprocessor_config.json.'''    ,		):
																		SCREAMING_SNAKE_CASE							:    Dict							=							AutoImageProcessor.from_pretrained('''hf-internal-testing/config-no-model'''						)


						def        __A       (       self	:				List[Any]						):





												'''simple docstring'''





												with self.assertRaises(UpperCamelCase__						):
																		SCREAMING_SNAKE_CASE							:    str							=							AutoImageProcessor.from_pretrained('''hf-internal-testing/test_dynamic_image_processor'''						)
												# If remote code is disabled, we can't load this config.
												with self.assertRaises(UpperCamelCase__						):
																		SCREAMING_SNAKE_CASE							:    List[Any]							=							AutoImageProcessor.from_pretrained(
																		    '''hf-internal-testing/test_dynamic_image_processor'''    ,		trust_remote_code=UpperCamelCase__						)

												SCREAMING_SNAKE_CASE							:    List[Any]							=							AutoImageProcessor.from_pretrained(
												    '''hf-internal-testing/test_dynamic_image_processor'''    ,		trust_remote_code=UpperCamelCase__						)
												self.assertEqual(image_processor.__class__.__name__    ,		'''NewImageProcessor'''						)

												# Test image processor can be reloaded.
												with tempfile.TemporaryDirectory() as tmp_dir:
																		image_processor.save_pretrained(UpperCamelCase__						)
																		SCREAMING_SNAKE_CASE							:    Optional[int]							=							AutoImageProcessor.from_pretrained(UpperCamelCase__    ,		trust_remote_code=UpperCamelCase__						)
												self.assertEqual(reloaded_image_processor.__class__.__name__    ,		'''NewImageProcessor'''						)


						def        __A       (       self	:				Optional[Any]						):





												'''simple docstring'''





												try:
																		AutoConfig.register('''custom'''    ,		UpperCamelCase__						)
																		AutoImageProcessor.register(UpperCamelCase__    ,		UpperCamelCase__						)
																		# Trying to register something existing in the Transformers library will raise an error
																		with self.assertRaises(UpperCamelCase__						):
																								AutoImageProcessor.register(UpperCamelCase__    ,		UpperCamelCase__						)

																		with tempfile.TemporaryDirectory() as tmpdirname:
																								SCREAMING_SNAKE_CASE							:    Union[str, Any]							=							Path(UpperCamelCase__						) / '''preprocessor_config.json'''
																								SCREAMING_SNAKE_CASE							:    Any							=							Path(UpperCamelCase__						) / '''config.json'''
																								json.dump(
																								    {'''feature_extractor_type''': '''CLIPFeatureExtractor''', '''processor_class''': '''CLIPProcessor'''}    ,		open(UpperCamelCase__    ,		'''w'''						)    ,		)
																								json.dump({'''model_type''': '''clip'''}    ,		open(UpperCamelCase__    ,		'''w'''						)						)

																								SCREAMING_SNAKE_CASE							:    Tuple							=							CustomImageProcessor.from_pretrained(UpperCamelCase__						)

																		# Now that the config is registered, it can be used as any other config with the auto-API
																		with tempfile.TemporaryDirectory() as tmp_dir:
																								image_processor.save_pretrained(UpperCamelCase__						)
																								SCREAMING_SNAKE_CASE							:    Any							=							AutoImageProcessor.from_pretrained(UpperCamelCase__						)
																								self.assertIsInstance(UpperCamelCase__    ,		UpperCamelCase__						)

												finally:
																		if "custom" in CONFIG_MAPPING._extra_content:
																								del CONFIG_MAPPING._extra_content["custom"]
																		if CustomConfig in IMAGE_PROCESSOR_MAPPING._extra_content:
																								del IMAGE_PROCESSOR_MAPPING._extra_content[CustomConfig]



						def        __A       (       self	:				Any						):





												'''simple docstring'''





												class 					lowercase__						(   UpperCamelCase_):
																		UpperCamelCase_     =		True

												try:
																		AutoConfig.register('''custom'''    ,		UpperCamelCase__						)
																		AutoImageProcessor.register(UpperCamelCase__    ,		UpperCamelCase__						)
																		# If remote code is not set, the default is to use local
																		SCREAMING_SNAKE_CASE							:    str							=							AutoImageProcessor.from_pretrained('''hf-internal-testing/test_dynamic_image_processor'''						)
																		self.assertEqual(image_processor.__class__.__name__    ,		'''NewImageProcessor'''						)
																		self.assertTrue(image_processor.is_local						)

																		# If remote code is disabled, we load the local one.
																		SCREAMING_SNAKE_CASE							:    Union[str, Any]							=							AutoImageProcessor.from_pretrained(
																		    '''hf-internal-testing/test_dynamic_image_processor'''    ,		trust_remote_code=UpperCamelCase__						)
																		self.assertEqual(image_processor.__class__.__name__    ,		'''NewImageProcessor'''						)
																		self.assertTrue(image_processor.is_local						)

																		# If remote is enabled, we load from the Hub
																		SCREAMING_SNAKE_CASE							:    Optional[Any]							=							AutoImageProcessor.from_pretrained(
																		    '''hf-internal-testing/test_dynamic_image_processor'''    ,		trust_remote_code=UpperCamelCase__						)
																		self.assertEqual(image_processor.__class__.__name__    ,		'''NewImageProcessor'''						)
																		self.assertTrue(not hasattr(UpperCamelCase__    ,		'''is_local'''						)						)

												finally:
																		if "custom" in CONFIG_MAPPING._extra_content:
																								del CONFIG_MAPPING._extra_content["custom"]
																		if CustomConfig in IMAGE_PROCESSOR_MAPPING._extra_content:
																								del IMAGE_PROCESSOR_MAPPING._extra_content[CustomConfig]





