



import json
import os
import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers import BertTokenizer, BertTokenizerFast
from transformers.models.bert.tokenization_bert 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 AlignProcessor, EfficientNetImageProcessor

@require_vision
class 					_A (			unittest.TestCase					):



       def 	_lowerCamelCase    (	self							:  Any):


           '''simple docstring'''

           __a		 =       tempfile.mkdtemp()

           __a		 =       [
               '''[UNK]''',
               '''[CLS]''',
               '''[SEP]''',
               '''[PAD]''',
               '''[MASK]''',
               '''want''',
               '''##want''',
               '''##ed''',
               '''wa''',
               '''un''',
               '''runn''',
               '''##ing''',
               ''',''',
               '''low''',
               '''lowest''',
           ]
           __a		 =       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		 =       {
               '''do_resize''': True,
               '''size''': 20,
               '''do_center_crop''': True,
               '''crop_size''': 18,
               '''do_normalize''': True,
               '''image_mean''': [0.48_14_54_66, 0.4_57_82_75, 0.40_82_10_73],
               '''image_std''': [0.26_86_29_54, 0.26_13_02_58, 0.27_57_77_11],
           }
           __a		 =       os.path.join(self.tmpdirname  ,	__SCREAMING_SNAKE_CASE)
           with open(self.image_processor_file  ,	'''w'''  ,	encoding='''utf-8''') as fp:
               json.dump(__SCREAMING_SNAKE_CASE  ,	__SCREAMING_SNAKE_CASE)



       def 	_lowerCamelCase    (	self							:  str  ,	**__SCREAMING_SNAKE_CASE							:  int):


           '''simple docstring'''

           return BertTokenizer.from_pretrained(self.tmpdirname  ,	**__SCREAMING_SNAKE_CASE)



       def 	_lowerCamelCase    (	self							:  List[Any]  ,	**__SCREAMING_SNAKE_CASE							:  Union[str, Any]):


           '''simple docstring'''

           return BertTokenizerFast.from_pretrained(self.tmpdirname  ,	**__SCREAMING_SNAKE_CASE)



       def 	_lowerCamelCase    (	self							:  List[str]  ,	**__SCREAMING_SNAKE_CASE							:  Optional[int]):


           '''simple docstring'''

           return EfficientNetImageProcessor.from_pretrained(self.tmpdirname  ,	**__SCREAMING_SNAKE_CASE)



       def 	_lowerCamelCase    (	self							:  List[Any]):


           '''simple docstring'''

           shutil.rmtree(self.tmpdirname)



       def 	_lowerCamelCase    (	self							:  str):


           '''simple docstring'''

           __a		 =       [np.random.randint(255  ,	size=(3, 30, 400)  ,	dtype=np.uinta)]
           __a		 =       [Image.fromarray(np.moveaxis(__SCREAMING_SNAKE_CASE  ,	0  ,	-1)) for x in image_inputs]
           return image_inputs



       def 	_lowerCamelCase    (	self							:  Union[str, Any]):


           '''simple docstring'''

           __a		 =       self.get_tokenizer()
           __a		 =       self.get_rust_tokenizer()
           __a		 =       self.get_image_processor()

           __a		 =       AlignProcessor(tokenizer=__SCREAMING_SNAKE_CASE  ,	image_processor=__SCREAMING_SNAKE_CASE)
           processor_slow.save_pretrained(self.tmpdirname)
           __a		 =       AlignProcessor.from_pretrained(self.tmpdirname  ,	use_fast=__SCREAMING_SNAKE_CASE)

           __a		 =       AlignProcessor(tokenizer=__SCREAMING_SNAKE_CASE  ,	image_processor=__SCREAMING_SNAKE_CASE)
           processor_fast.save_pretrained(self.tmpdirname)
           __a		 =       AlignProcessor.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  ,	__SCREAMING_SNAKE_CASE)
           self.assertIsInstance(processor_fast.tokenizer  ,	__SCREAMING_SNAKE_CASE)

           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  ,	__SCREAMING_SNAKE_CASE)
           self.assertIsInstance(processor_fast.image_processor  ,	__SCREAMING_SNAKE_CASE)



       def 	_lowerCamelCase    (	self							:  Optional[Any]):


           '''simple docstring'''

           __a		 =       AlignProcessor(tokenizer=self.get_tokenizer()  ,	image_processor=self.get_image_processor())
           processor.save_pretrained(self.tmpdirname)

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

           __a		 =       AlignProcessor.from_pretrained(
               self.tmpdirname  ,	bos_token='''(BOS)'''  ,	eos_token='''(EOS)'''  ,	do_normalize=__SCREAMING_SNAKE_CASE  ,	padding_value=1.0)

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

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



       def 	_lowerCamelCase    (	self							:  Union[str, Any]):


           '''simple docstring'''

           __a		 =       self.get_image_processor()
           __a		 =       self.get_tokenizer()

           __a		 =       AlignProcessor(tokenizer=__SCREAMING_SNAKE_CASE  ,	image_processor=__SCREAMING_SNAKE_CASE)

           __a		 =       self.prepare_image_inputs()

           __a		 =       image_processor(__SCREAMING_SNAKE_CASE  ,	return_tensors='''np''')
           __a		 =       processor(images=__SCREAMING_SNAKE_CASE  ,	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 	_lowerCamelCase    (	self							:  Tuple):


           '''simple docstring'''

           __a		 =       self.get_image_processor()
           __a		 =       self.get_tokenizer()

           __a		 =       AlignProcessor(tokenizer=__SCREAMING_SNAKE_CASE  ,	image_processor=__SCREAMING_SNAKE_CASE)

           __a		 =       '''lower newer'''

           __a		 =       processor(text=__SCREAMING_SNAKE_CASE)

           __a		 =       tokenizer(__SCREAMING_SNAKE_CASE  ,	padding='''max_length'''  ,	max_length=64)

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



       def 	_lowerCamelCase    (	self							:  Dict):


           '''simple docstring'''

           __a		 =       self.get_image_processor()
           __a		 =       self.get_tokenizer()

           __a		 =       AlignProcessor(tokenizer=__SCREAMING_SNAKE_CASE  ,	image_processor=__SCREAMING_SNAKE_CASE)

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

           __a		 =       processor(text=__SCREAMING_SNAKE_CASE  ,	images=__SCREAMING_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 pytest.raises(__SCREAMING_SNAKE_CASE):
               processor()



       def 	_lowerCamelCase    (	self							:  Optional[Any]):


           '''simple docstring'''

           __a		 =       self.get_image_processor()
           __a		 =       self.get_tokenizer()

           __a		 =       AlignProcessor(tokenizer=__SCREAMING_SNAKE_CASE  ,	image_processor=__SCREAMING_SNAKE_CASE)

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

           __a		 =       processor.batch_decode(__SCREAMING_SNAKE_CASE)
           __a		 =       tokenizer.batch_decode(__SCREAMING_SNAKE_CASE)

           self.assertListEqual(__SCREAMING_SNAKE_CASE  ,	__SCREAMING_SNAKE_CASE)





       def 	_lowerCamelCase    (	self							:  Dict):


           '''simple docstring'''

           __a		 =       self.get_image_processor()
           __a		 =       self.get_tokenizer()

           __a		 =       AlignProcessor(tokenizer=__SCREAMING_SNAKE_CASE  ,	image_processor=__SCREAMING_SNAKE_CASE)

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

           __a		 =       processor(text=__SCREAMING_SNAKE_CASE  ,	images=__SCREAMING_SNAKE_CASE)

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










import os
import sys
import unittest


__snake_case		:Union[str, Any]   	=  os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
sys.path.append(os.path.join(git_repo_path, '''utils'''))

import check_dummies  # noqa: E402
from check_dummies import create_dummy_files, create_dummy_object, find_backend, read_init  # noqa: E402


# Align TRANSFORMERS_PATH in check_dummies with the current path
__snake_case		:List[str]   	=  os.path.join(git_repo_path, '''src''', '''transformers''')

__snake_case		:Any   	=  '''
{0} = None
'''

__snake_case		:Dict   	=  '''
class {0}(metaclass=DummyObject):
    _backends = {1}

    def __init__(self, *args, **kwargs):
        requires_backends(self, {1})
'''


__snake_case		:str   	=  '''
def {0}(*args, **kwargs):
    requires_backends({0}, {1})
'''

class 					_A (			unittest.TestCase					):



       def 	_lowerCamelCase    (	self							:  List[Any]):


           '''simple docstring'''

           __a		 =       find_backend('''    _import_structure["models.albert"].append("AlbertTokenizerFast")''')
           self.assertIsNone(__SCREAMING_SNAKE_CASE)

           __a		 =       find_backend('''    if not is_tokenizers_available():''')
           self.assertEqual(__SCREAMING_SNAKE_CASE  ,	'''tokenizers''')

           __a		 =       find_backend('''    if not is_tensorflow_text_available():''')
           self.assertEqual(__SCREAMING_SNAKE_CASE  ,	'''tensorflow_text''')

           __a		 =       find_backend('''    if not (is_sentencepiece_available() and is_tokenizers_available()):''')
           self.assertEqual(__SCREAMING_SNAKE_CASE  ,	'''sentencepiece_and_tokenizers''')

           __a		 =       find_backend(
               '''    if not (is_sentencepiece_available() and is_tensorflow_text_available()):''')
           self.assertEqual(__SCREAMING_SNAKE_CASE  ,	'''sentencepiece_and_tensorflow_text''')

           __a		 =       find_backend(
               '''    if not (is_sentencepiece_available() and is_tokenizers_available() and is_vision_available()):''')
           self.assertEqual(__SCREAMING_SNAKE_CASE  ,	'''sentencepiece_and_tokenizers_and_vision''')



       def 	_lowerCamelCase    (	self							:  Optional[Any]):


           '''simple docstring'''

           __a		 =       read_init()
           # We don't assert on the exact list of keys to allow for smooth grow of backend-specific objects
           self.assertIn('''torch'''  ,	__SCREAMING_SNAKE_CASE)
           self.assertIn('''tensorflow_text'''  ,	__SCREAMING_SNAKE_CASE)
           self.assertIn('''sentencepiece_and_tokenizers'''  ,	__SCREAMING_SNAKE_CASE)

           # Likewise, we can't assert on the exact content of a key
           self.assertIn('''BertModel'''  ,	objects['''torch'''])
           self.assertIn('''TFBertModel'''  ,	objects['''tf'''])
           self.assertIn('''FlaxBertModel'''  ,	objects['''flax'''])
           self.assertIn('''BertModel'''  ,	objects['''torch'''])
           self.assertIn('''TFBertTokenizer'''  ,	objects['''tensorflow_text'''])
           self.assertIn('''convert_slow_tokenizer'''  ,	objects['''sentencepiece_and_tokenizers'''])



       def 	_lowerCamelCase    (	self							:  Any):


           '''simple docstring'''

           __a		 =       create_dummy_object('''CONSTANT'''  ,	'''\'torch\'''')
           self.assertEqual(__SCREAMING_SNAKE_CASE  ,	'''\nCONSTANT = None\n''')

           __a		 =       create_dummy_object('''function'''  ,	'''\'torch\'''')
           self.assertEqual(
               __SCREAMING_SNAKE_CASE  ,	'''\ndef function(*args, **kwargs):\n    requires_backends(function, \'torch\')\n''')

           __a		 =       '''
class FakeClass(metaclass=DummyObject):
    _backends = \'torch\'

    def __init__(self, *args, **kwargs):
        requires_backends(self, \'torch\')
'''
           __a		 =       create_dummy_object('''FakeClass'''  ,	'''\'torch\'''')
           self.assertEqual(__SCREAMING_SNAKE_CASE  ,	__SCREAMING_SNAKE_CASE)





       def 	_lowerCamelCase    (	self							:  Optional[Any]):


           '''simple docstring'''

           __a		 =       '''# This file is autogenerated by the command `make fix-copies`, do not edit.
from ..utils import DummyObject, requires_backends


CONSTANT = None


def function(*args, **kwargs):
    requires_backends(function, ["torch"])


class FakeClass(metaclass=DummyObject):
    _backends = ["torch"]

    def __init__(self, *args, **kwargs):
        requires_backends(self, ["torch"])
'''
           __a		 =       create_dummy_files({'''torch''': ['''CONSTANT''', '''function''', '''FakeClass''']})
           self.assertEqual(dummy_files['''torch''']  ,	__SCREAMING_SNAKE_CASE)





