






"""simple docstring"""
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        UpperCamelCase__ (				unittest.TestCase):







    """simple docstring"""







    def 				a__			(		self :	Dict ):





           '''simple docstring'''

           __magic_name__      				=    tempfile.mkdtemp()

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

           __magic_name__      				=    os.path.join(self.tmpdirname				,    VOCAB_FILES_NAMES['vocab_file'] )
           __magic_name__      				=    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_ ) )

           __magic_name__      				=    {
               'do_resize': True,
               'size': 2_0,
               'do_center_crop': True,
               'crop_size': 1_8,
               'do_normalize': True,
               'image_mean': [0.48_145_466, 0.4_578_275, 0.40_821_073],
               'image_std': [0.26_862_954, 0.26_130_258, 0.27_577_711],
           }
           __magic_name__      				=    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 :	str				,    **UpperCamelCase_ :	List[str] ):





           '''simple docstring'''

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







    def 				a__			(		self :	Optional[Any]				,    **UpperCamelCase_ :	Any ):





           '''simple docstring'''

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







    def 				a__			(		self :	Any				,    **UpperCamelCase_ :	Optional[Any] ):





           '''simple docstring'''

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







    def 				a__			(		self :	List[str] ):





           '''simple docstring'''

           shutil.rmtree(self.tmpdirname )







    def 				a__			(		self :	List[str] ):





           '''simple docstring'''

           __magic_name__      				=    [np.random.randint(2_5_5				,    size=(3, 3_0, 4_0_0)				,    dtype=np.uinta )]

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

           return image_inputs







    def 				a__			(		self :	List[Any] ):





           '''simple docstring'''

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

           __magic_name__      				=    CLIPProcessor(tokenizer=UpperCamelCase_				,    image_processor=UpperCamelCase_ )
           processor_slow.save_pretrained(self.tmpdirname )
           __magic_name__      				=    CLIPProcessor.from_pretrained(self.tmpdirname				,    use_fast=UpperCamelCase_ )

           __magic_name__      				=    CLIPProcessor(tokenizer=UpperCamelCase_				,    image_processor=UpperCamelCase_ )
           processor_fast.save_pretrained(self.tmpdirname )
           __magic_name__      				=    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 :	Dict ):





           '''simple docstring'''

           __magic_name__      				=    CLIPProcessor(tokenizer=self.get_tokenizer()				,    image_processor=self.get_image_processor() )
           processor.save_pretrained(self.tmpdirname )

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

           __magic_name__      				=    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'''

           __magic_name__      				=    self.get_image_processor()
           __magic_name__      				=    self.get_tokenizer()

           __magic_name__      				=    CLIPProcessor(tokenizer=UpperCamelCase_				,    image_processor=UpperCamelCase_ )

           __magic_name__      				=    self.prepare_image_inputs()

           __magic_name__      				=    image_processor(UpperCamelCase_				,    return_tensors='np' )
           __magic_name__      				=    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 :	Union[str, Any] ):





           '''simple docstring'''

           __magic_name__      				=    self.get_image_processor()
           __magic_name__      				=    self.get_tokenizer()

           __magic_name__      				=    CLIPProcessor(tokenizer=UpperCamelCase_				,    image_processor=UpperCamelCase_ )

           __magic_name__      				=    'lower newer'

           __magic_name__      				=    processor(text=UpperCamelCase_ )

           __magic_name__      				=    tokenizer(UpperCamelCase_ )

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







    def 				a__			(		self :	Optional[int] ):





           '''simple docstring'''

           __magic_name__      				=    self.get_image_processor()
           __magic_name__      				=    self.get_tokenizer()

           __magic_name__      				=    CLIPProcessor(tokenizer=UpperCamelCase_				,    image_processor=UpperCamelCase_ )

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

           __magic_name__      				=    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 :	str ):





           '''simple docstring'''

           __magic_name__      				=    self.get_image_processor()
           __magic_name__      				=    self.get_tokenizer()

           __magic_name__      				=    CLIPProcessor(tokenizer=UpperCamelCase_				,    image_processor=UpperCamelCase_ )

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

           __magic_name__      				=    processor.batch_decode(UpperCamelCase_ )
           __magic_name__      				=    tokenizer.batch_decode(UpperCamelCase_ )

           self.assertListEqual(UpperCamelCase_				,    UpperCamelCase_ )







    def 				a__			(		self :	Dict ):





           '''simple docstring'''

           __magic_name__      				=    self.get_image_processor()
           __magic_name__      				=    self.get_tokenizer()

           __magic_name__      				=    CLIPProcessor(tokenizer=UpperCamelCase_				,    image_processor=UpperCamelCase_ )

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

           __magic_name__      				=    processor(text=UpperCamelCase_				,    images=UpperCamelCase_ )

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







"""simple docstring"""
def 							A						(				__snake_case: str  )    ->		list:

       """simple docstring"""





       return [
           txt[:a] + txt[a].upper() + txt[a + 1 :]
           for a in range(len(__snake_case  )  )
           if txt[a].isalpha()
       ]


if __name__ == "__main__":
 __import__("""doctest""").testmod()