



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





							'''simple docstring'''

							def 		_A	(							self				):







											"""simple docstring"""



											a__	:							Union[str, Any]      						= tempfile.mkdtemp()

											# fmt: off
											a__	:							Union[str, 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
											a__	:							Union[str, Any]      						= dict(zip(__UpperCAmelCase ,					range(len(__UpperCAmelCase				)				)				)				)
											a__	:							List[Any]      						= ["#version: 0.2", "l o", "lo w</w>", "e r</w>", ""]
											a__	:							Union[str, Any]      						= {"unk_token": "<unk>"}

											a__	:							Optional[Any]      						= os.path.join(self.tmpdirname ,					VOCAB_FILES_NAMES["vocab_file"]				)
											a__	:							Tuple      						= 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				)				)

											a__	:							Dict      						= {
											    "do_resize": True,
											    "size": 20,
											    "do_center_crop": True,
											    "crop_size": 18,
											    "do_normalize": True,
											    "image_mean": [0.4_8_1_4_5_4_6_6, 0.4_5_7_8_2_7_5, 0.4_0_8_2_1_0_7_3],
											    "image_std": [0.2_6_8_6_2_9_5_4, 0.2_6_1_3_0_2_5_8, 0.2_7_5_7_7_7_1_1],
											}
											a__	:							Optional[int]      						= 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 ,					**__UpperCAmelCase				):







											"""simple docstring"""



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



							def 		_A	(							self ,					**__UpperCAmelCase				):







											"""simple docstring"""



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



							def 		_A	(							self ,					**__UpperCAmelCase				):







											"""simple docstring"""



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



							def 		_A	(							self				):







											"""simple docstring"""



											shutil.rmtree(self.tmpdirname				)



							def 		_A	(							self				):







											"""simple docstring"""



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

											a__	:							Tuple      						= [Image.fromarray(np.moveaxis(__UpperCAmelCase ,					0 ,					-1				)				) for x in image_inputs]

											return image_inputs



							def 		_A	(							self				):







											"""simple docstring"""



											a__	:							int      						= self.get_tokenizer()
											a__	:							List[Any]      						= self.get_rust_tokenizer()
											a__	:							int      						= self.get_image_processor()

											a__	:							Dict      						= CLIPProcessor(tokenizer=__UpperCAmelCase ,					image_processor=__UpperCAmelCase				)
											processor_slow.save_pretrained(self.tmpdirname				)
											a__	:							List[Any]      						= CLIPProcessor.from_pretrained(self.tmpdirname ,					use_fast=__UpperCAmelCase				)

											a__	:							List[str]      						= CLIPProcessor(tokenizer=__UpperCAmelCase ,					image_processor=__UpperCAmelCase				)
											processor_fast.save_pretrained(self.tmpdirname				)
											a__	:							Optional[int]      						= 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				):







											"""simple docstring"""



											a__	:							Dict      						= CLIPProcessor(tokenizer=self.get_tokenizer() ,					image_processor=self.get_image_processor()				)
											processor.save_pretrained(self.tmpdirname				)

											a__	:							Optional[int]      						= self.get_tokenizer(bos_token="(BOS)" ,					eos_token="(EOS)"				)
											a__	:							Any      						= self.get_image_processor(do_normalize=__UpperCAmelCase ,					padding_value=1.0				)

											a__	:							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				):







											"""simple docstring"""



											a__	:							Optional[int]      						= self.get_image_processor()
											a__	:							Dict      						= self.get_tokenizer()

											a__	:							int      						= CLIPProcessor(tokenizer=__UpperCAmelCase ,					image_processor=__UpperCAmelCase				)

											a__	:							Optional[int]      						= self.prepare_image_inputs()

											a__	:							int      						= image_processor(__UpperCAmelCase ,					return_tensors="np"				)
											a__	:							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				):







											"""simple docstring"""



											a__	:							Optional[Any]      						= self.get_image_processor()
											a__	:							Optional[int]      						= self.get_tokenizer()

											a__	:							Optional[Any]      						= CLIPProcessor(tokenizer=__UpperCAmelCase ,					image_processor=__UpperCAmelCase				)

											a__	:							Optional[int]      						= "lower newer"

											a__	:							List[str]      						= processor(text=__UpperCAmelCase				)

											a__	:							Any      						= tokenizer(__UpperCAmelCase				)

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



							def 		_A	(							self				):







											"""simple docstring"""



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

											a__	:							List[str]      						= CLIPProcessor(tokenizer=__UpperCAmelCase ,					image_processor=__UpperCAmelCase				)

											a__	:							Tuple      						= "lower newer"
											a__	:							Optional[Any]      						= self.prepare_image_inputs()

											a__	:							Optional[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				):







											"""simple docstring"""



											a__	:							str      						= self.get_image_processor()
											a__	:							List[Any]      						= self.get_tokenizer()

											a__	:							Optional[Any]      						= CLIPProcessor(tokenizer=__UpperCAmelCase ,					image_processor=__UpperCAmelCase				)

											a__	:							Dict      						= [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

											a__	:							Optional[int]      						= processor.batch_decode(__UpperCAmelCase				)
											a__	:							List[Any]      						= tokenizer.batch_decode(__UpperCAmelCase				)

											self.assertListEqual(__UpperCAmelCase ,					__UpperCAmelCase				)




							def 		_A	(							self				):







											"""simple docstring"""



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

											a__	:							Union[str, Any]      						= CLIPProcessor(tokenizer=__UpperCAmelCase ,					image_processor=__UpperCAmelCase				)

											a__	:							Dict      						= "lower newer"
											a__	:							List[str]      						= self.prepare_image_inputs()

											a__	:							List[str]      						= processor(text=__UpperCAmelCase ,					images=__UpperCAmelCase				)

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







from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available


lowerCamelCase		       =      {}


try:
						if not is_sentencepiece_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						lowerCamelCase		       =      ["""MLukeTokenizer"""]

if TYPE_CHECKING:
						try:
												if not is_sentencepiece_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .tokenization_mluke import MLukeTokenizer


else:
						import sys

						lowerCamelCase		       =      _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)


