






import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_video_inputs


if is_torch_available():
							import torch

if is_vision_available():
							from PIL import Image

							from transformers import VivitImageProcessor


class      __SCREAMING_SNAKE_CASE(		unittest.TestCase		):
							def __init__(   self:					Tuple				,		UpperCamelCase:					List[Any]				,		UpperCamelCase:					Tuple=7				,		UpperCamelCase:					Tuple=3				,		UpperCamelCase:					str=10				,		UpperCamelCase:					int=18				,		UpperCamelCase:					Optional[int]=30				,		UpperCamelCase:					List[Any]=4_00				,		UpperCamelCase:					List[str]=True				,		UpperCamelCase:					Optional[Any]=None				,		UpperCamelCase:					Optional[Any]=True				,		UpperCamelCase:					Tuple=[0.5, 0.5, 0.5]				,		UpperCamelCase:					Optional[int]=[0.5, 0.5, 0.5]				,		UpperCamelCase:					int=None				,		)						->						Tuple:
											snake_case__													=   size if size is not None else {'shortest_edge': 18}
											snake_case__													=   crop_size if crop_size is not None else {'height': 18, 'width': 18}

											snake_case__													=   parent
											snake_case__													=   batch_size
											snake_case__													=   num_channels
											snake_case__													=   num_frames
											snake_case__													=   image_size
											snake_case__													=   min_resolution
											snake_case__													=   max_resolution
											snake_case__													=   do_resize
											snake_case__													=   size
											snake_case__													=   do_normalize
											snake_case__													=   image_mean
											snake_case__													=   image_std
											snake_case__													=   crop_size


							def lowerCAmelCase_       (   self:					List[Any]  )						->						Optional[int]:
											return {
											    "image_mean": self.image_mean,
											    "image_std": self.image_std,
											    "do_normalize": self.do_normalize,
											    "do_resize": self.do_resize,
											    "size": self.size,
											    "crop_size": self.crop_size,
											}


@require_torch
@require_vision
class      __SCREAMING_SNAKE_CASE(		UpperCAmelCase__			,     unittest.TestCase		):
							_UpperCAmelCase       			=				VivitImageProcessor if is_vision_available() else None
							def lowerCAmelCase_       (   self:					Union[str, Any]  )						->						Optional[int]:
											snake_case__													=   VivitImageProcessingTester(self  )
							@property
							def lowerCAmelCase_       (   self:					Dict  )						->						Union[str, Any]:
											return self.image_processor_tester.prepare_image_processor_dict()
							def lowerCAmelCase_       (   self:					Any  )						->						Tuple:
											snake_case__													=   self.image_processing_class(**self.image_processor_dict  )
											self.assertTrue(hasattr(snake_case_				,		'image_mean'  )  )
											self.assertTrue(hasattr(snake_case_				,		'image_std'  )  )
											self.assertTrue(hasattr(snake_case_				,		'do_normalize'  )  )
											self.assertTrue(hasattr(snake_case_				,		'do_resize'  )  )
											self.assertTrue(hasattr(snake_case_				,		'do_center_crop'  )  )
											self.assertTrue(hasattr(snake_case_				,		'size'  )  )
							def lowerCAmelCase_       (   self:					Union[str, Any]  )						->						Union[str, Any]:
											snake_case__													=   self.image_processing_class.from_dict(self.image_processor_dict  )
											self.assertEqual(image_processor.size				,		{'shortest_edge': 18}  )
											self.assertEqual(image_processor.crop_size				,		{'height': 18, 'width': 18}  )

											snake_case__													=   self.image_processing_class.from_dict(self.image_processor_dict				,		size=42				,		crop_size=84  )
											self.assertEqual(image_processor.size				,		{'shortest_edge': 42}  )
											self.assertEqual(image_processor.crop_size				,		{'height': 84, 'width': 84}  )
							def lowerCAmelCase_       (   self:					str  )						->						int:
											snake_case__													=   self.image_processing_class(**self.image_processor_dict  )
											# create random PIL videos
											snake_case__													=   prepare_video_inputs(self.image_processor_tester				,		equal_resolution=snake_case_  )
											for video in video_inputs:
															self.assertIsInstance(snake_case_				,		snake_case_  )
															self.assertIsInstance(video[0]				,		Image.Image  )

											# Test not batched input
											snake_case__													=   image_processing(video_inputs[0]				,		return_tensors='pt'  ).pixel_values
											self.assertEqual(
											    encoded_videos.shape				,		(
											        1,
											        self.image_processor_tester.num_frames,
											        self.image_processor_tester.num_channels,
											        self.image_processor_tester.crop_size['height'],
											        self.image_processor_tester.crop_size['width'],
											    )				,		)

											# Test batched
											snake_case__													=   image_processing(snake_case_				,		return_tensors='pt'  ).pixel_values
											self.assertEqual(
											    encoded_videos.shape				,		(
											        self.image_processor_tester.batch_size,
											        self.image_processor_tester.num_frames,
											        self.image_processor_tester.num_channels,
											        self.image_processor_tester.crop_size['height'],
											        self.image_processor_tester.crop_size['width'],
											    )				,		)
							def lowerCAmelCase_       (   self:					Union[str, Any]  )						->						Any:
											snake_case__													=   self.image_processing_class(**self.image_processor_dict  )
											# create random numpy tensors
											snake_case__													=   prepare_video_inputs(self.image_processor_tester				,		equal_resolution=snake_case_				,		numpify=snake_case_  )
											for video in video_inputs:
															self.assertIsInstance(snake_case_				,		snake_case_  )
															self.assertIsInstance(video[0]				,		np.ndarray  )

											# Test not batched input
											snake_case__													=   image_processing(video_inputs[0]				,		return_tensors='pt'  ).pixel_values
											self.assertEqual(
											    encoded_videos.shape				,		(
											        1,
											        self.image_processor_tester.num_frames,
											        self.image_processor_tester.num_channels,
											        self.image_processor_tester.crop_size['height'],
											        self.image_processor_tester.crop_size['width'],
											    )				,		)

											# Test batched
											snake_case__													=   image_processing(snake_case_				,		return_tensors='pt'  ).pixel_values
											self.assertEqual(
											    encoded_videos.shape				,		(
											        self.image_processor_tester.batch_size,
											        self.image_processor_tester.num_frames,
											        self.image_processor_tester.num_channels,
											        self.image_processor_tester.crop_size['height'],
											        self.image_processor_tester.crop_size['width'],
											    )				,		)


							def lowerCAmelCase_       (   self:					Dict  )						->						str:
											snake_case__													=   self.image_processing_class(**self.image_processor_dict  )
											# create random PyTorch tensors
											snake_case__													=   prepare_video_inputs(self.image_processor_tester				,		equal_resolution=snake_case_				,		torchify=snake_case_  )
											for video in video_inputs:
															self.assertIsInstance(snake_case_				,		snake_case_  )
															self.assertIsInstance(video[0]				,		torch.Tensor  )

											# Test not batched input
											snake_case__													=   image_processing(video_inputs[0]				,		return_tensors='pt'  ).pixel_values
											self.assertEqual(
											    encoded_videos.shape				,		(
											        1,
											        self.image_processor_tester.num_frames,
											        self.image_processor_tester.num_channels,
											        self.image_processor_tester.crop_size['height'],
											        self.image_processor_tester.crop_size['width'],
											    )				,		)

											# Test batched
											snake_case__													=   image_processing(snake_case_				,		return_tensors='pt'  ).pixel_values
											self.assertEqual(
											    encoded_videos.shape				,		(
											        self.image_processor_tester.batch_size,
											        self.image_processor_tester.num_frames,
											        self.image_processor_tester.num_channels,
											        self.image_processor_tester.crop_size['height'],
											        self.image_processor_tester.crop_size['width'],
											    )				,		)











"""simple docstring"""




import copy
import random

from transformers import CLIPTokenizer

class  _UpperCAmelCase  (   UpperCAmelCase__		):



      '''simple docstring'''

      def __init__(     self					,      *snake_case_					,      **snake_case_  ):

        """simple docstring"""

        super().__init__(*snake_case_					,      **snake_case_  )
        A_  :       Tuple										=  {}

      def 					lowerCamelCase_			(     self					,      snake_case_					,      *snake_case_					,      **snake_case_  ):

        """simple docstring"""

        A_  :       str										=  super().add_tokens(snake_case_					,      *snake_case_					,      **snake_case_  )
        if num_added_tokens == 0:
          raise ValueError(
              F"""The tokenizer already contains the token {placeholder_token}. Please pass a different"""
              ' `placeholder_token` that is not already in the tokenizer.'  )

      def 					lowerCamelCase_			(     self					,      snake_case_					,      *snake_case_					,      snake_case_=1					,      **snake_case_  ):

        """simple docstring"""

        A_  :       Tuple										=  []
        if num_vec_per_token == 1:
          self.try_adding_tokens(snake_case_					,      *snake_case_					,      **snake_case_  )
          output.append(snake_case_  )
        else:
          A_  :       Tuple										=  []
          for i in range(snake_case_  ):
            A_  :       List[str]										=  placeholder_token + F"""_{i}"""
            self.try_adding_tokens(snake_case_					,      *snake_case_					,      **snake_case_  )
            output.append(snake_case_  )
        # handle cases where there is a new placeholder token that contains the current placeholder token but is larger
        for token in self.token_map:
          if token in placeholder_token:
            raise ValueError(
                F"""The tokenizer already has placeholder token {token} that can get confused with"""
                F""" {placeholder_token}keep placeholder tokens independent"""  )
        A_  :       Any										=  output

      def 					lowerCamelCase_			(     self					,      snake_case_					,      snake_case_=False					,      snake_case_=1.0  ):

        """simple docstring"""

        if isinstance(snake_case_					,      snake_case_  ):
          A_  :       Optional[Any]										=  []
          for i in range(len(snake_case_  )  ):
            output.append(self.replace_placeholder_tokens_in_text(text[i]					,      vector_shuffle=snake_case_  )  )
          return output
        for placeholder_token in self.token_map:
          if placeholder_token in text:
            A_  :       List[Any]										=  self.token_map[placeholder_token]
            A_  :       Optional[int]										=  tokens[: 1 + int(len(snake_case_  ) * prop_tokens_to_load  )]
            if vector_shuffle:
              A_  :       Optional[Any]										=  copy.copy(snake_case_  )
              random.shuffle(snake_case_  )
            A_  :       List[str]										=  text.replace(snake_case_					,      ' '.join(snake_case_  )  )
        return text

      def __call__(     self					,      snake_case_					,      *snake_case_					,      snake_case_=False					,      snake_case_=1.0					,      **snake_case_  ):

        """simple docstring"""

        return super().__call__(
            self.replace_placeholder_tokens_in_text(
                snake_case_					,      vector_shuffle=snake_case_					,      prop_tokens_to_load=snake_case_  )					,      *snake_case_					,      **snake_case_					,      )

      def 					lowerCamelCase_			(     self					,      snake_case_					,      *snake_case_					,      snake_case_=False					,      snake_case_=1.0					,      **snake_case_  ):

        """simple docstring"""

        return super().encode(
            self.replace_placeholder_tokens_in_text(
                snake_case_					,      vector_shuffle=snake_case_					,      prop_tokens_to_load=snake_case_  )					,      *snake_case_					,      **snake_case_					,      )