


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






       def       lowerCAmelCase		(      self)						->  str:




          '''simple docstring'''
          _UpperCAmelCase       :       str							   =tempfile.mkdtemp()

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

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

          _UpperCAmelCase       :       Optional[Any]							   ={
              'do_resize': True,
              'size': 2_0,
              'do_center_crop': True,
              'crop_size': 1_8,
              '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],
          }
          _UpperCAmelCase       :       Union[str, Any]							   =os.path.join(self.tmpdirname  ,							snake_case)
          with open(self.image_processor_file  ,							'w'  ,							encoding='utf-8') as fp:
             json.dump(snake_case  ,							snake_case)






       def       lowerCAmelCase		(      self  ,							**snake_case)						->  Tuple:




          '''simple docstring'''
          return CLIPTokenizer.from_pretrained(self.tmpdirname  ,							**snake_case)






       def       lowerCAmelCase		(      self  ,							**snake_case)						->  int:




          '''simple docstring'''
          return CLIPTokenizerFast.from_pretrained(self.tmpdirname  ,							**snake_case)






       def       lowerCAmelCase		(      self  ,							**snake_case)						->  Any:




          '''simple docstring'''
          return CLIPImageProcessor.from_pretrained(self.tmpdirname  ,							**snake_case)






       def       lowerCAmelCase		(      self)						->  List[Any]:




          '''simple docstring'''
          shutil.rmtree(self.tmpdirname)






       def       lowerCAmelCase		(      self)						->  Any:




          '''simple docstring'''
          _UpperCAmelCase       :       Dict							   =[np.random.randint(2_5_5  ,							size=(3, 3_0, 4_0_0)  ,							dtype=np.uinta)]

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

          return image_inputs






       def       lowerCAmelCase		(      self)						->  Optional[int]:




          '''simple docstring'''
          _UpperCAmelCase       :       Any							   =self.get_tokenizer()
          _UpperCAmelCase       :       int							   =self.get_rust_tokenizer()
          _UpperCAmelCase       :       Optional[int]							   =self.get_image_processor()

          _UpperCAmelCase       :       List[str]							   =CLIPProcessor(tokenizer=snake_case  ,							image_processor=snake_case)
          processor_slow.save_pretrained(self.tmpdirname)
          _UpperCAmelCase       :       Optional[int]							   =CLIPProcessor.from_pretrained(self.tmpdirname  ,							use_fast=snake_case)

          _UpperCAmelCase       :       Tuple							   =CLIPProcessor(tokenizer=snake_case  ,							image_processor=snake_case)
          processor_fast.save_pretrained(self.tmpdirname)
          _UpperCAmelCase       :       List[Any]							   =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  ,							snake_case)
          self.assertIsInstance(processor_fast.tokenizer  ,							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  ,							snake_case)
          self.assertIsInstance(processor_fast.image_processor  ,							snake_case)






       def       lowerCAmelCase		(      self)						->  int:




          '''simple docstring'''
          _UpperCAmelCase       :       Optional[int]							   =CLIPProcessor(tokenizer=self.get_tokenizer()  ,							image_processor=self.get_image_processor())
          processor.save_pretrained(self.tmpdirname)

          _UpperCAmelCase       :       Dict							   =self.get_tokenizer(bos_token='(BOS)'  ,							eos_token='(EOS)')
          _UpperCAmelCase       :       Optional[Any]							   =self.get_image_processor(do_normalize=snake_case  ,							padding_value=1.0)

          _UpperCAmelCase       :       Union[str, Any]							   =CLIPProcessor.from_pretrained(
              self.tmpdirname  ,							bos_token='(BOS)'  ,							eos_token='(EOS)'  ,							do_normalize=snake_case  ,							padding_value=1.0)

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

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






       def       lowerCAmelCase		(      self)						->  Optional[int]:




          '''simple docstring'''
          _UpperCAmelCase       :       Any							   =self.get_image_processor()
          _UpperCAmelCase       :       Optional[Any]							   =self.get_tokenizer()

          _UpperCAmelCase       :       Dict							   =CLIPProcessor(tokenizer=snake_case  ,							image_processor=snake_case)

          _UpperCAmelCase       :       Dict							   =self.prepare_image_inputs()

          _UpperCAmelCase       :       Dict							   =image_processor(snake_case  ,							return_tensors='np')
          _UpperCAmelCase       :       Optional[int]							   =processor(images=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)						->  List[str]:




          '''simple docstring'''
          _UpperCAmelCase       :       Union[str, Any]							   =self.get_image_processor()
          _UpperCAmelCase       :       Tuple							   =self.get_tokenizer()

          _UpperCAmelCase       :       List[Any]							   =CLIPProcessor(tokenizer=snake_case  ,							image_processor=snake_case)

          _UpperCAmelCase       :       Union[str, Any]							   ='lower newer'

          _UpperCAmelCase       :       Optional[Any]							   =processor(text=snake_case)

          _UpperCAmelCase       :       int							   =tokenizer(snake_case)

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






       def       lowerCAmelCase		(      self)						->  int:




          '''simple docstring'''
          _UpperCAmelCase       :       Optional[int]							   =self.get_image_processor()
          _UpperCAmelCase       :       str							   =self.get_tokenizer()

          _UpperCAmelCase       :       List[str]							   =CLIPProcessor(tokenizer=snake_case  ,							image_processor=snake_case)

          _UpperCAmelCase       :       str							   ='lower newer'
          _UpperCAmelCase       :       Any							   =self.prepare_image_inputs()

          _UpperCAmelCase       :       Dict							   =processor(text=snake_case  ,							images=snake_case)

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

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






       def       lowerCAmelCase		(      self)						->  Dict:




          '''simple docstring'''
          _UpperCAmelCase       :       Optional[Any]							   =self.get_image_processor()
          _UpperCAmelCase       :       Optional[int]							   =self.get_tokenizer()

          _UpperCAmelCase       :       str							   =CLIPProcessor(tokenizer=snake_case  ,							image_processor=snake_case)

          _UpperCAmelCase       :       Union[str, Any]							   =[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

          _UpperCAmelCase       :       Optional[Any]							   =processor.batch_decode(snake_case)
          _UpperCAmelCase       :       Dict							   =tokenizer.batch_decode(snake_case)

          self.assertListEqual(snake_case  ,							snake_case)






       def       lowerCAmelCase		(      self)						->  str:




          '''simple docstring'''
          _UpperCAmelCase       :       Optional[int]							   =self.get_image_processor()
          _UpperCAmelCase       :       Tuple							   =self.get_tokenizer()

          _UpperCAmelCase       :       List[str]							   =CLIPProcessor(tokenizer=snake_case  ,							image_processor=snake_case)

          _UpperCAmelCase       :       List[Any]							   ='lower newer'
          _UpperCAmelCase       :       Optional[Any]							   =self.prepare_image_inputs()

          _UpperCAmelCase       :       List[str]							   =processor(text=snake_case  ,							images=snake_case)

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





'''simple docstring'''






import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers import is_speech_available, is_vision_available
from transformers.testing_utils import require_torch


if is_vision_available():
 from transformers import TvltImageProcessor

if is_speech_available():
 from transformers import TvltFeatureExtractor

from transformers import TvltProcessor






@require_torch
class  __magic_name__							(		unittest.TestCase    ):






       def       lowerCAmelCase		(      self)						->  str:




          '''simple docstring'''
          _UpperCAmelCase       :       Dict							   ='ZinengTang/tvlt-base'
          _UpperCAmelCase       :       Dict							   =tempfile.mkdtemp()






       def       lowerCAmelCase		(      self  ,							**snake_case)						->  Union[str, Any]:




          '''simple docstring'''
          return TvltImageProcessor.from_pretrained(self.checkpoint  ,							**snake_case)






       def       lowerCAmelCase		(      self  ,							**snake_case)						->  Dict:




          '''simple docstring'''
          return TvltFeatureExtractor.from_pretrained(self.checkpoint  ,							**snake_case)






       def       lowerCAmelCase		(      self)						->  Any:




          '''simple docstring'''
          shutil.rmtree(self.tmpdirname)






       def       lowerCAmelCase		(      self)						->  Optional[int]:




          '''simple docstring'''
          _UpperCAmelCase       :       Any							   =self.get_image_processor()
          _UpperCAmelCase       :       Optional[Any]							   =self.get_feature_extractor()

          _UpperCAmelCase       :       str							   =TvltProcessor(image_processor=snake_case  ,							feature_extractor=snake_case)
          processor.save_pretrained(self.tmpdirname)
          _UpperCAmelCase       :       str							   =TvltProcessor.from_pretrained(self.tmpdirname)

          self.assertIsInstance(processor.feature_extractor  ,							snake_case)
          self.assertIsInstance(processor.image_processor  ,							snake_case)






       def       lowerCAmelCase		(      self)						->  Union[str, Any]:




          '''simple docstring'''
          _UpperCAmelCase       :       str							   =self.get_image_processor()
          _UpperCAmelCase       :       List[Any]							   =self.get_feature_extractor()

          _UpperCAmelCase       :       str							   =TvltProcessor(image_processor=snake_case  ,							feature_extractor=snake_case)

          _UpperCAmelCase       :       Optional[int]							   =np.ones([1_2_0_0_0])

          _UpperCAmelCase       :       str							   =feature_extractor(snake_case  ,							return_tensors='np')
          _UpperCAmelCase       :       Dict							   =processor(audio=snake_case  ,							return_tensors='np')

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






       def       lowerCAmelCase		(      self)						->  int:




          '''simple docstring'''
          _UpperCAmelCase       :       Dict							   =self.get_image_processor()
          _UpperCAmelCase       :       int							   =self.get_feature_extractor()

          _UpperCAmelCase       :       int							   =TvltProcessor(image_processor=snake_case  ,							feature_extractor=snake_case)

          _UpperCAmelCase       :       Union[str, Any]							   =np.ones([3, 2_2_4, 2_2_4])

          _UpperCAmelCase       :       Optional[Any]							   =image_processor(snake_case  ,							return_tensors='np')
          _UpperCAmelCase       :       List[Any]							   =processor(images=snake_case  ,							return_tensors='np')

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






       def       lowerCAmelCase		(      self)						->  List[str]:




          '''simple docstring'''
          _UpperCAmelCase       :       List[str]							   =self.get_image_processor()
          _UpperCAmelCase       :       Dict							   =self.get_feature_extractor()

          _UpperCAmelCase       :       Optional[int]							   =TvltProcessor(image_processor=snake_case  ,							feature_extractor=snake_case)

          _UpperCAmelCase       :       Optional[int]							   =np.ones([1_2_0_0_0])
          _UpperCAmelCase       :       str							   =np.ones([3, 2_2_4, 2_2_4])

          _UpperCAmelCase       :       Optional[int]							   =processor(audio=snake_case  ,							images=snake_case)

          self.assertListEqual(list(inputs.keys())  ,							['audio_values', 'audio_mask', 'pixel_values', 'pixel_mask'])

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






       def       lowerCAmelCase		(      self)						->  Optional[int]:




          '''simple docstring'''
          _UpperCAmelCase       :       Optional[int]							   =self.get_image_processor()
          _UpperCAmelCase       :       Tuple							   =self.get_feature_extractor()

          _UpperCAmelCase       :       Dict							   =TvltProcessor(image_processor=snake_case  ,							feature_extractor=snake_case)

          self.assertListEqual(
              processor.model_input_names  ,							image_processor.model_input_names + feature_extractor.model_input_names  ,							msg='`processor` and `image_processor`+`feature_extractor` model input names do not match'  ,							)

