



import json
import os
import unittest

from transformers import CLIPTokenizer, CLIPTokenizerFast
from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES
from transformers.testing_utils import require_ftfy, require_tokenizers

from ...test_tokenization_common import TokenizerTesterMixin


@require_tokenizers
class 	__snake_case   ( SCREAMING_SNAKE_CASE							, unittest.TestCase ):
		SCREAMING_SNAKE_CASE__								=	CLIPTokenizer
		SCREAMING_SNAKE_CASE__								=	CLIPTokenizerFast
		SCREAMING_SNAKE_CASE__								=	True
		SCREAMING_SNAKE_CASE__								=	{}
		SCREAMING_SNAKE_CASE__								=	False
		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						super().setUp()

						# fmt: off
						lowerCAmelCase__		  =  ['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
						lowerCAmelCase__		  =  dict(zip(a_    ,range(len(a_						)						)						)						)
						lowerCAmelCase__		  =  ['#version: 0.2', 'l o', 'lo w</w>', 'e r</w>']
						lowerCAmelCase__		  =  {'unk_token': '<unk>'}

						lowerCAmelCase__		  =  os.path.join(self.tmpdirname    ,VOCAB_FILES_NAMES['vocab_file']						)
						lowerCAmelCase__		  =  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(a_						) + '\n'						)
						with open(self.merges_file    ,'w'    ,encoding='utf-8'						) as fp:
										fp.write('\n'.join(a_						)						)
		def 						SCREAMING_SNAKE_CASE_					(							self    ,**a_						):







						"""simple docstring"""
						kwargs.update(self.special_tokens_map						)
						return CLIPTokenizer.from_pretrained(self.tmpdirname    ,**a_						)
		def 						SCREAMING_SNAKE_CASE_					(							self    ,**a_						):







						"""simple docstring"""
						kwargs.update(self.special_tokens_map						)
						return CLIPTokenizerFast.from_pretrained(self.tmpdirname    ,**a_						)
		def 						SCREAMING_SNAKE_CASE_					(							self    ,a_						):







						"""simple docstring"""
						lowerCAmelCase__		  =  'lower newer'
						lowerCAmelCase__		  =  'lower newer'
						return input_text, output_text
		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						lowerCAmelCase__		  =  CLIPTokenizer(self.vocab_file    ,self.merges_file    ,**self.special_tokens_map						)
						lowerCAmelCase__		  =  'lower newer'
						lowerCAmelCase__		  =  ['lo', 'w', 'er</w>', 'n', 'e', 'w', 'er</w>']
						lowerCAmelCase__		  =  tokenizer.tokenize(a_						)
						self.assertListEqual(a_    ,a_						)

						lowerCAmelCase__		  =  tokens + [tokenizer.unk_token]
						lowerCAmelCase__		  =  [10, 2, 16, 9, 3, 2, 16, 20]
						self.assertListEqual(tokenizer.convert_tokens_to_ids(a_						)    ,a_						)
		@require_ftfy
		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
										with self.subTest(f'{tokenizer.__class__.__name__} ({pretrained_name})'						):
														lowerCAmelCase__		  =  self.tokenizer_class.from_pretrained(a_    ,**a_						)
														lowerCAmelCase__		  =  self.rust_tokenizer_class.from_pretrained(a_    ,**a_						)

														lowerCAmelCase__		  =  'A\n\'ll 11p223RF☆ho!!to?\'d\'d\'\'d of a cat to-$\'\'d.'
														lowerCAmelCase__		  =  tokenizer_s.tokenize(a_						)
														lowerCAmelCase__		  =  tokenizer_r.tokenize(a_						)

														self.assertListEqual(a_    ,a_						)

														# Test that the tokenization is identical on an example containing a character (Latin Small Letter A
														# with Tilde) encoded in 2 different ways
														lowerCAmelCase__		  =  'xa\u0303y' + ' ' + 'x\xe3y'
														lowerCAmelCase__		  =  tokenizer_s.tokenize(a_						)
														lowerCAmelCase__		  =  tokenizer_r.tokenize(a_						)

														self.assertListEqual(a_    ,a_						)

														# Test that the tokenization is identical on unicode of space type
														lowerCAmelCase__		  =  [
														    '\u0009',  # (horizontal tab, '\t')
														    '\u000B',  # (vertical tab)
														    '\u000C',  # (form feed)
														    '\u0020',  # (space, ' ')
														    '\u200E',  # (left-to-right mark):w
														    '\u200F',  # (right-to-left mark)
														]
														for unicode_seq in spaces_unicodes:
																		lowerCAmelCase__		  =  tokenizer_s.tokenize(a_						)
																		lowerCAmelCase__		  =  tokenizer_r.tokenize(a_						)

																		self.assertListEqual(a_    ,a_						)

														# Test that the tokenization is identical on unicode of line break type
														lowerCAmelCase__		  =  [
														    '\u000A',  # (line feed, '\n')
														    '\r\n',  # (carriage return and line feed, '\r\n')
														    '\u000D',  # (carriage return, '\r')
														    '\r',  # (carriage return, '\r')
														    '\u000D',  # (carriage return, '\r')
														    '\u2028',  # (line separator)
														    '\u2029',  # (paragraph separator)
														    # "\u0085", # (next line)
														]

														# The tokenization is not identical for the character "\u0085" (next line). The slow version using ftfy transforms
														# it into the Horizontal Ellipsis character "…" ("\u2026") while the fast version transforms it into a
														# space (and thus into an empty list).

														for unicode_seq in line_break_unicodes:
																		lowerCAmelCase__		  =  tokenizer_s.tokenize(a_						)
																		lowerCAmelCase__		  =  tokenizer_r.tokenize(a_						)

																		self.assertListEqual(a_    ,a_						)
		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						# Test which aims to verify that the offsets are well adapted to the argument `add_prefix_space`
						for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
										with self.subTest(f'{tokenizer.__class__.__name__} ({pretrained_name})'						):
														lowerCAmelCase__		  =  'hello'  # `hello` is a token in the vocabulary of `pretrained_name`
														lowerCAmelCase__		  =  f'{text_of_1_token} {text_of_1_token}'

														lowerCAmelCase__		  =  self.rust_tokenizer_class.from_pretrained(
														    a_    ,use_fast=a_    ,)
														lowerCAmelCase__		  =  tokenizer_r(a_    ,return_offsets_mapping=a_    ,add_special_tokens=a_						)
														self.assertEqual(encoding.offset_mapping[0]    ,(0, len(a_						))						)
														self.assertEqual(
														    encoding.offset_mapping[1]    ,(len(a_						) + 1, len(a_						) + 1 + len(a_						))    ,)

														lowerCAmelCase__		  =  f' {text}'

														lowerCAmelCase__		  =  self.rust_tokenizer_class.from_pretrained(
														    a_    ,use_fast=a_    ,)
														lowerCAmelCase__		  =  tokenizer_r(a_    ,return_offsets_mapping=a_    ,add_special_tokens=a_						)
														self.assertEqual(encoding.offset_mapping[0]    ,(1, 1 + len(a_						))						)
														self.assertEqual(
														    encoding.offset_mapping[1]    ,(1 + len(a_						) + 1, 1 + len(a_						) + 1 + len(a_						))    ,)
		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						# Test related to the breaking change introduced in transformers v4.17.0
						# We need to check that an error in raised when the user try to load a previous version of the tokenizer.
						with self.assertRaises(a_						) as context:
										self.rust_tokenizer_class.from_pretrained('robot-test/old-clip-tokenizer'						)

						self.assertTrue(
						    context.exception.args[0].startswith(
						        'The `backend_tokenizer` provided does not match the expected format.'						)						)
		@require_ftfy
		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						super().test_tokenization_python_rust_equals()



		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						# CLIP always lower cases letters
						pass





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 	__snake_case   ( unittest.TestCase ):
		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						lowerCAmelCase__		  =  tempfile.mkdtemp()

						# fmt: off
						lowerCAmelCase__		  =  ['[UNK]', '[CLS]', '[SEP]', '[PAD]', '[MASK]', 'want', '##want', '##ed', 'wa', 'un', 'runn', '##ing', ',', 'low', 'lowest']
						# fmt: on
						lowerCAmelCase__		  =  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__		  =  {
						    '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__		  =  os.path.join(self.tmpdirname    ,a_						)
						with open(self.image_processor_file    ,'w'    ,encoding='utf-8'						) as fp:
										json.dump(a_    ,a_						)
		def 						SCREAMING_SNAKE_CASE_					(							self    ,**a_						):







						"""simple docstring"""
						return BertTokenizer.from_pretrained(self.tmpdirname    ,**a_						)
		def 						SCREAMING_SNAKE_CASE_					(							self    ,**a_						):







						"""simple docstring"""
						return ViTImageProcessor.from_pretrained(self.tmpdirname    ,**a_						)
		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						shutil.rmtree(self.tmpdirname						)
		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						lowerCAmelCase__		  =  [np.random.randint(255    ,size=(3, 30, 400)    ,dtype=np.uinta						)]

						lowerCAmelCase__		  =  [Image.fromarray(np.moveaxis(a_    ,0    ,-1						)						) for x in image_inputs]

						return image_inputs
		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						lowerCAmelCase__		  =  self.get_tokenizer()
						lowerCAmelCase__		  =  self.get_image_processor()

						lowerCAmelCase__		  =  VisionTextDualEncoderProcessor(tokenizer=a_    ,image_processor=a_						)

						processor.save_pretrained(self.tmpdirname						)
						lowerCAmelCase__		  =  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    ,a_						)
		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						lowerCAmelCase__		  =  VisionTextDualEncoderProcessor(
						    tokenizer=self.get_tokenizer()    ,image_processor=self.get_image_processor()						)
						processor.save_pretrained(self.tmpdirname						)

						lowerCAmelCase__		  =  self.get_tokenizer(bos_token='(BOS)'    ,eos_token='(EOS)'						)
						lowerCAmelCase__		  =  self.get_image_processor(do_normalize=a_    ,padding_value=1.0						)

						lowerCAmelCase__		  =  VisionTextDualEncoderProcessor.from_pretrained(
						    self.tmpdirname    ,bos_token='(BOS)'    ,eos_token='(EOS)'    ,do_normalize=a_    ,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    ,a_						)
		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						lowerCAmelCase__		  =  self.get_image_processor()
						lowerCAmelCase__		  =  self.get_tokenizer()

						lowerCAmelCase__		  =  VisionTextDualEncoderProcessor(tokenizer=a_    ,image_processor=a_						)

						lowerCAmelCase__		  =  self.prepare_image_inputs()

						lowerCAmelCase__		  =  image_processor(a_    ,return_tensors='np'						)
						lowerCAmelCase__		  =  processor(images=a_    ,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 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						lowerCAmelCase__		  =  self.get_image_processor()
						lowerCAmelCase__		  =  self.get_tokenizer()

						lowerCAmelCase__		  =  VisionTextDualEncoderProcessor(tokenizer=a_    ,image_processor=a_						)

						lowerCAmelCase__		  =  'lower newer'

						lowerCAmelCase__		  =  processor(text=a_						)

						lowerCAmelCase__		  =  tokenizer(a_						)

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







						"""simple docstring"""
						lowerCAmelCase__		  =  self.get_image_processor()
						lowerCAmelCase__		  =  self.get_tokenizer()

						lowerCAmelCase__		  =  VisionTextDualEncoderProcessor(tokenizer=a_    ,image_processor=a_						)

						lowerCAmelCase__		  =  'lower newer'
						lowerCAmelCase__		  =  self.prepare_image_inputs()

						lowerCAmelCase__		  =  processor(text=a_    ,images=a_						)

						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(a_						):
										processor()
		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						lowerCAmelCase__		  =  self.get_image_processor()
						lowerCAmelCase__		  =  self.get_tokenizer()

						lowerCAmelCase__		  =  VisionTextDualEncoderProcessor(tokenizer=a_    ,image_processor=a_						)

						lowerCAmelCase__		  =  [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

						lowerCAmelCase__		  =  processor.batch_decode(a_						)
						lowerCAmelCase__		  =  tokenizer.batch_decode(a_						)

						self.assertListEqual(a_    ,a_						)



		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						lowerCAmelCase__		  =  self.get_image_processor()
						lowerCAmelCase__		  =  self.get_tokenizer()

						lowerCAmelCase__		  =  VisionTextDualEncoderProcessor(tokenizer=a_    ,image_processor=a_						)

						lowerCAmelCase__		  =  'lower newer'
						lowerCAmelCase__		  =  self.prepare_image_inputs()

						lowerCAmelCase__		  =  processor(text=a_    ,images=a_						)

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