


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

   '''simple docstring'''







   def __init__(	self	:			Tuple							,	_lowerCamelCase	:			str							,	_lowerCamelCase	:			Dict=7							,	_lowerCamelCase	:			Tuple=3							,	_lowerCamelCase	:			Dict=1_0							,	_lowerCamelCase	:			Dict=1_8							,	_lowerCamelCase	:			str=3_0							,	_lowerCamelCase	:			List[str]=4_0_0							,	_lowerCamelCase	:			Optional[int]=True							,	_lowerCamelCase	:			List[str]=None							,	_lowerCamelCase	:			Any=True							,	_lowerCamelCase	:			List[str]=[0.5, 0.5, 0.5]							,	_lowerCamelCase	:			Dict=[0.5, 0.5, 0.5]							,	_lowerCamelCase	:			Optional[int]=None							,	):






         '''simple docstring'''



         __lowerCamelCase  :   Tuple							    =    size if size is not None else {"""shortest_edge""": 1_8}
         __lowerCamelCase  :   Any							    =    crop_size if crop_size is not None else {"""height""": 1_8, """width""": 1_8}

         __lowerCamelCase  :   Dict							    =    parent
         __lowerCamelCase  :   str							    =    batch_size
         __lowerCamelCase  :   List[Any]							    =    num_channels
         __lowerCamelCase  :   Optional[int]							    =    num_frames
         __lowerCamelCase  :   Dict							    =    image_size
         __lowerCamelCase  :   Tuple							    =    min_resolution
         __lowerCamelCase  :   Union[str, Any]							    =    max_resolution
         __lowerCamelCase  :   Any							    =    do_resize
         __lowerCamelCase  :   Tuple							    =    size
         __lowerCamelCase  :   Optional[Any]							    =    do_normalize
         __lowerCamelCase  :   Tuple							    =    image_mean
         __lowerCamelCase  :   List[str]							    =    image_std
         __lowerCamelCase  :   Optional[Any]							    =    crop_size


   def 	_snake_case     (	self	:			Optional[Any]				):






         '''simple docstring'''



         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 				_UpperCamelCase			(    _a,unittest.TestCase    ):

   '''simple docstring'''







   a_					:				str     =			VivitImageProcessor if is_vision_available() else None

   def 	_snake_case     (	self	:			Tuple				):






         '''simple docstring'''



         __lowerCamelCase  :   str							    =    VivitImageProcessingTester(self				)

   @property
   def 	_snake_case     (	self	:			Any				):






         '''simple docstring'''



         return self.image_processor_tester.prepare_image_processor_dict()

   def 	_snake_case     (	self	:			Any				):






         '''simple docstring'''



         __lowerCamelCase  :   int							    =    self.image_processing_class(**self.image_processor_dict				)
         self.assertTrue(hasattr(__UpperCamelCase							,	"""image_mean"""				)				)
         self.assertTrue(hasattr(__UpperCamelCase							,	"""image_std"""				)				)
         self.assertTrue(hasattr(__UpperCamelCase							,	"""do_normalize"""				)				)
         self.assertTrue(hasattr(__UpperCamelCase							,	"""do_resize"""				)				)
         self.assertTrue(hasattr(__UpperCamelCase							,	"""do_center_crop"""				)				)
         self.assertTrue(hasattr(__UpperCamelCase							,	"""size"""				)				)

   def 	_snake_case     (	self	:			Any				):






         '''simple docstring'''



         __lowerCamelCase  :   Any							    =    self.image_processing_class.from_dict(self.image_processor_dict				)
         self.assertEqual(image_processor.size							,	{"""shortest_edge""": 1_8}				)
         self.assertEqual(image_processor.crop_size							,	{"""height""": 1_8, """width""": 1_8}				)

         __lowerCamelCase  :   int							    =    self.image_processing_class.from_dict(self.image_processor_dict							,	size=4_2							,	crop_size=8_4				)
         self.assertEqual(image_processor.size							,	{"""shortest_edge""": 4_2}				)
         self.assertEqual(image_processor.crop_size							,	{"""height""": 8_4, """width""": 8_4}				)

   def 	_snake_case     (	self	:			List[str]				):






         '''simple docstring'''



         __lowerCamelCase  :   Tuple							    =    self.image_processing_class(**self.image_processor_dict				)
         # create random PIL videos
         __lowerCamelCase  :   Any							    =    prepare_video_inputs(self.image_processor_tester							,	equal_resolution=__UpperCamelCase				)
         for video in video_inputs:
               self.assertIsInstance(__UpperCamelCase							,	__UpperCamelCase				)
               self.assertIsInstance(video[0]							,	Image.Image				)

         # Test not batched input
         __lowerCamelCase  :   Dict							    =    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
         __lowerCamelCase  :   Optional[int]							    =    image_processing(__UpperCamelCase							,	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 	_snake_case     (	self	:			List[str]				):






         '''simple docstring'''



         __lowerCamelCase  :   Optional[Any]							    =    self.image_processing_class(**self.image_processor_dict				)
         # create random numpy tensors
         __lowerCamelCase  :   List[str]							    =    prepare_video_inputs(self.image_processor_tester							,	equal_resolution=__UpperCamelCase							,	numpify=__UpperCamelCase				)
         for video in video_inputs:
               self.assertIsInstance(__UpperCamelCase							,	__UpperCamelCase				)
               self.assertIsInstance(video[0]							,	np.ndarray				)

         # Test not batched input
         __lowerCamelCase  :   Union[str, Any]							    =    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
         __lowerCamelCase  :   Tuple							    =    image_processing(__UpperCamelCase							,	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 	_snake_case     (	self	:			int				):






         '''simple docstring'''



         __lowerCamelCase  :   Tuple							    =    self.image_processing_class(**self.image_processor_dict				)
         # create random PyTorch tensors
         __lowerCamelCase  :   Union[str, Any]							    =    prepare_video_inputs(self.image_processor_tester							,	equal_resolution=__UpperCamelCase							,	torchify=__UpperCamelCase				)
         for video in video_inputs:
               self.assertIsInstance(__UpperCamelCase							,	__UpperCamelCase				)
               self.assertIsInstance(video[0]							,	torch.Tensor				)

         # Test not batched input
         __lowerCamelCase  :   List[Any]							    =    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
         __lowerCamelCase  :   Union[str, Any]							    =    image_processing(__UpperCamelCase							,	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"""],
             )							,	)





from ...processing_utils import ProcessorMixin



class    a_ (			_a    ):
			a	:			Optional[int]					   =							'''SpeechT5FeatureExtractor'''
			a	:			List[Any]					   =							'''SpeechT5Tokenizer'''






			def __init__(      self				,			__UpperCamelCase				,			__UpperCamelCase  ):
									super().__init__(__UpperCamelCase				,			__UpperCamelCase  )






			def __call__(      self				,			*__UpperCamelCase				,			**__UpperCamelCase  ):
									_lowercase					       =					kwargs.pop("""audio"""				,			__UpperCamelCase  )
									_lowercase					       =					kwargs.pop("""text"""				,			__UpperCamelCase  )
									_lowercase					       =					kwargs.pop("""text_target"""				,			__UpperCamelCase  )
									_lowercase					       =					kwargs.pop("""audio_target"""				,			__UpperCamelCase  )
									_lowercase					       =					kwargs.pop("""sampling_rate"""				,			__UpperCamelCase  )

									if audio is not None and text is not None:
															raise ValueError(
															    """Cannot process both `audio` and `text` inputs. Did you mean `audio_target` or `text_target`?"""  )
									if audio_target is not None and text_target is not None:
															raise ValueError(
															    """Cannot process both `audio_target` and `text_target` inputs. Did you mean `audio` or `text`?"""  )
									if audio is None and audio_target is None and text is None and text_target is None:
															raise ValueError(
															    """You need to specify either an `audio`, `audio_target`, `text`, or `text_target` input to process."""  )

									if audio is not None:
															_lowercase					       =					self.feature_extractor(__UpperCamelCase				,			*__UpperCamelCase				,			sampling_rate=__UpperCamelCase				,			**__UpperCamelCase  )
									elif text is not None:
															_lowercase					       =					self.tokenizer(__UpperCamelCase				,			**__UpperCamelCase  )
									else:
															_lowercase					       =					None

									if audio_target is not None:
															_lowercase					       =					self.feature_extractor(audio_target=__UpperCamelCase				,			*__UpperCamelCase				,			sampling_rate=__UpperCamelCase				,			**__UpperCamelCase  )
															_lowercase					       =					targets["""input_values"""]
									elif text_target is not None:
															_lowercase					       =					self.tokenizer(__UpperCamelCase				,			**__UpperCamelCase  )
															_lowercase					       =					targets["""input_ids"""]
									else:
															_lowercase					       =					None

									if inputs is None:
															return targets

									if targets is not None:
															_lowercase					       =					labels

															_lowercase					       =					targets.get("""attention_mask"""  )
															if decoder_attention_mask is not None:
																					_lowercase					       =					decoder_attention_mask

									return inputs






			def    UpperCamelCase_				(      self				,			*__UpperCamelCase				,			**__UpperCamelCase  ):
									_lowercase					       =					kwargs.pop("""input_values"""				,			__UpperCamelCase  )
									_lowercase					       =					kwargs.pop("""input_ids"""				,			__UpperCamelCase  )
									_lowercase					       =					kwargs.pop("""labels"""				,			__UpperCamelCase  )

									if input_values is not None and input_ids is not None:
															raise ValueError("""Cannot process both `input_values` and `input_ids` inputs."""  )
									if input_values is None and input_ids is None and labels is None:
															raise ValueError(
															    """You need to specify either an `input_values`, `input_ids`, or `labels` input to be padded."""  )

									if input_values is not None:
															_lowercase					       =					self.feature_extractor.pad(__UpperCamelCase				,			*__UpperCamelCase				,			**__UpperCamelCase  )
									elif input_ids is not None:
															_lowercase					       =					self.tokenizer.pad(__UpperCamelCase				,			**__UpperCamelCase  )
									else:
															_lowercase					       =					None

									if labels is not None:
															if "input_ids" in labels or (isinstance(__UpperCamelCase				,			__UpperCamelCase  ) and "input_ids" in labels[0]):
																					_lowercase					       =					self.tokenizer.pad(__UpperCamelCase				,			**__UpperCamelCase  )
																					_lowercase					       =					targets["""input_ids"""]
															else:
																					_lowercase					       =					self.feature_extractor.feature_size
																					_lowercase					       =					self.feature_extractor.num_mel_bins
																					_lowercase					       =					self.feature_extractor.pad(__UpperCamelCase				,			*__UpperCamelCase				,			**__UpperCamelCase  )
																					_lowercase					       =					feature_size_hack
																					_lowercase					       =					targets["""input_values"""]
									else:
															_lowercase					       =					None

									if inputs is None:
															return targets

									if targets is not None:
															_lowercase					       =					labels

															_lowercase					       =					targets.get("""attention_mask"""  )
															if decoder_attention_mask is not None:
																					_lowercase					       =					decoder_attention_mask

									return inputs






			def    UpperCamelCase_				(      self				,			*__UpperCamelCase				,			**__UpperCamelCase  ):
									return self.tokenizer.batch_decode(*__UpperCamelCase				,			**__UpperCamelCase  )






			def    UpperCamelCase_				(      self				,			*__UpperCamelCase				,			**__UpperCamelCase  ):
									return self.tokenizer.decode(*__UpperCamelCase				,			**__UpperCamelCase  )