




'''simple docstring'''


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_image_inputs


if is_torch_available():
     import torch

if is_vision_available():
     from PIL import Image

     from transformers import DeiTImageProcessor






class 						lowerCAmelCase_	(       unittest.TestCase							):







  '''simple docstring'''

  def __init__( self				:							Optional[Any]					,  SCREAMING_SNAKE_CASE_				:							str					,  SCREAMING_SNAKE_CASE_				:							Optional[int]=7					,  SCREAMING_SNAKE_CASE_				:							str=3					,  SCREAMING_SNAKE_CASE_				:							Tuple=18					,  SCREAMING_SNAKE_CASE_				:							str=30					,  SCREAMING_SNAKE_CASE_				:							Optional[int]=4_00					,  SCREAMING_SNAKE_CASE_				:							Any=True					,  SCREAMING_SNAKE_CASE_				:							Optional[Any]=None					,  SCREAMING_SNAKE_CASE_				:							Tuple=True					,  SCREAMING_SNAKE_CASE_				:							Optional[Any]=None					,  SCREAMING_SNAKE_CASE_				:							Any=True					,  SCREAMING_SNAKE_CASE_				:							Optional[Any]=[0.5, 0.5, 0.5]					,  SCREAMING_SNAKE_CASE_				:							Optional[int]=[0.5, 0.5, 0.5]					,  )			->		Any:







       '''simple docstring'''



       A:		List[str]          =		size if size is not None else {'''height''': 20, '''width''': 20}
       A:		Dict          =		crop_size if crop_size is not None else {'''height''': 18, '''width''': 18}

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




  def 					_snake_case     ( self				:							Dict	)			->		str:







       '''simple docstring'''



       return {
           "do_resize": self.do_resize,
           "size": self.size,
           "do_center_crop": self.do_center_crop,
           "crop_size": self.crop_size,
           "do_normalize": self.do_normalize,
           "image_mean": self.image_mean,
           "image_std": self.image_std,
       }






@require_torch
@require_vision
class 						lowerCAmelCase_	(       UpperCAmelCase_       ,					unittest.TestCase							):







  '''simple docstring'''

  UpperCamelCase_			:      int										=				DeiTImageProcessor if is_vision_available() else None
  UpperCamelCase_			:      Optional[Any]										=				True



  def 					_snake_case     ( self				:							List[Any]	)			->		List[str]:







       '''simple docstring'''



       A:		str          =		DeiTImageProcessingTester(self	)



  @property
  def 					_snake_case     ( self				:							str	)			->		Optional[Any]:







       '''simple docstring'''



       return self.image_processor_tester.prepare_image_processor_dict()



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







       '''simple docstring'''



       A:		Optional[Any]          =		self.image_processing_class(**self.image_processor_dict	)
       self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_					,  '''do_resize'''	)	)
       self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_					,  '''size'''	)	)
       self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_					,  '''do_center_crop'''	)	)
       self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_					,  '''center_crop'''	)	)
       self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_					,  '''do_normalize'''	)	)
       self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_					,  '''image_mean'''	)	)
       self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_					,  '''image_std'''	)	)



  def 					_snake_case     ( self				:							str	)			->		Optional[int]:







       '''simple docstring'''



       A:		Optional[Any]          =		self.image_processing_class.from_dict(self.image_processor_dict	)
       self.assertEqual(image_processor.size					,  {'''height''': 20, '''width''': 20}	)
       self.assertEqual(image_processor.crop_size					,  {'''height''': 18, '''width''': 18}	)

       A:		Dict          =		self.image_processing_class.from_dict(self.image_processor_dict					,  size=42					,  crop_size=84	)
       self.assertEqual(image_processor.size					,  {'''height''': 42, '''width''': 42}	)
       self.assertEqual(image_processor.crop_size					,  {'''height''': 84, '''width''': 84}	)



  def 					_snake_case     ( self				:							Any	)			->		Optional[Any]:







       '''simple docstring'''



       pass



  def 					_snake_case     ( self				:							Optional[Any]	)			->		Any:







       '''simple docstring'''



       A:		Any          =		self.image_processing_class(**self.image_processor_dict	)
       # create random PIL images
       A:		Any          =		prepare_image_inputs(self.image_processor_tester					,  equal_resolution=SCREAMING_SNAKE_CASE_	)
       for image in image_inputs:
            self.assertIsInstance(SCREAMING_SNAKE_CASE_					,  Image.Image	)

       # Test not batched input
       A:		int          =		image_processing(image_inputs[0]					,  return_tensors='''pt'''	).pixel_values
       self.assertEqual(
           encoded_images.shape					,  (
               1,
               self.image_processor_tester.num_channels,
               self.image_processor_tester.crop_size['''height'''],
               self.image_processor_tester.crop_size['''width'''],
           )					,  )

       # Test batched
       A:		Any          =		image_processing(SCREAMING_SNAKE_CASE_					,  return_tensors='''pt'''	).pixel_values
       self.assertEqual(
           encoded_images.shape					,  (
               self.image_processor_tester.batch_size,
               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]	)			->		Tuple:







       '''simple docstring'''



       A:		List[str]          =		self.image_processing_class(**self.image_processor_dict	)
       # create random numpy tensors
       A:		Optional[int]          =		prepare_image_inputs(self.image_processor_tester					,  equal_resolution=SCREAMING_SNAKE_CASE_					,  numpify=SCREAMING_SNAKE_CASE_	)
       for image in image_inputs:
            self.assertIsInstance(SCREAMING_SNAKE_CASE_					,  np.ndarray	)

       # Test not batched input
       A:		Any          =		image_processing(image_inputs[0]					,  return_tensors='''pt'''	).pixel_values
       self.assertEqual(
           encoded_images.shape					,  (
               1,
               self.image_processor_tester.num_channels,
               self.image_processor_tester.crop_size['''height'''],
               self.image_processor_tester.crop_size['''width'''],
           )					,  )

       # Test batched
       A:		int          =		image_processing(SCREAMING_SNAKE_CASE_					,  return_tensors='''pt'''	).pixel_values
       self.assertEqual(
           encoded_images.shape					,  (
               self.image_processor_tester.batch_size,
               self.image_processor_tester.num_channels,
               self.image_processor_tester.crop_size['''height'''],
               self.image_processor_tester.crop_size['''width'''],
           )					,  )




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







       '''simple docstring'''



       A:		Tuple          =		self.image_processing_class(**self.image_processor_dict	)
       # create random PyTorch tensors
       A:		Optional[Any]          =		prepare_image_inputs(self.image_processor_tester					,  equal_resolution=SCREAMING_SNAKE_CASE_					,  torchify=SCREAMING_SNAKE_CASE_	)
       for image in image_inputs:
            self.assertIsInstance(SCREAMING_SNAKE_CASE_					,  torch.Tensor	)

       # Test not batched input
       A:		Any          =		image_processing(image_inputs[0]					,  return_tensors='''pt'''	).pixel_values
       self.assertEqual(
           encoded_images.shape					,  (
               1,
               self.image_processor_tester.num_channels,
               self.image_processor_tester.crop_size['''height'''],
               self.image_processor_tester.crop_size['''width'''],
           )					,  )

       # Test batched
       A:		int          =		image_processing(SCREAMING_SNAKE_CASE_					,  return_tensors='''pt'''	).pixel_values
       self.assertEqual(
           encoded_images.shape					,  (
               self.image_processor_tester.batch_size,
               self.image_processor_tester.num_channels,
               self.image_processor_tester.crop_size['''height'''],
               self.image_processor_tester.crop_size['''width'''],
           )					,  )












'''simple docstring'''


import warnings
from contextlib import contextmanager

from ...processing_utils import ProcessorMixin






class 						lowerCAmelCase_	(       UpperCAmelCase_							):







  '''simple docstring'''

  UpperCamelCase_			:      Optional[int]										=				"""AutoFeatureExtractor"""
  UpperCamelCase_			:      List[Any]										=				"""Speech2Text2Tokenizer"""



  def __init__( self				:							str					,  SCREAMING_SNAKE_CASE_				:							str					,  SCREAMING_SNAKE_CASE_				:							int	)			->		Any:







       '''simple docstring'''



       super().__init__(SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_	)
       A:		Union[str, Any]          =		self.feature_extractor
       A:		Dict          =		False



  def __call__( self				:							Any					,  *SCREAMING_SNAKE_CASE_				:							Any					,  **SCREAMING_SNAKE_CASE_				:							Optional[int]	)			->		Optional[Any]:







       '''simple docstring'''



       if self._in_target_context_manager:
            return self.current_processor(*SCREAMING_SNAKE_CASE_					,  **SCREAMING_SNAKE_CASE_	)

       if "raw_speech" in kwargs:
            warnings.warn('''Using `raw_speech` as a keyword argument is deprecated. Use `audio` instead.'''	)
            A:		List[str]          =		kwargs.pop('''raw_speech'''	)
       else:
            A:		Any          =		kwargs.pop('''audio'''					,  SCREAMING_SNAKE_CASE_	)
       A:		List[Any]          =		kwargs.pop('''sampling_rate'''					,  SCREAMING_SNAKE_CASE_	)
       A:		Optional[int]          =		kwargs.pop('''text'''					,  SCREAMING_SNAKE_CASE_	)
       if len(SCREAMING_SNAKE_CASE_	) > 0:
            A:		str          =		args[0]
            A:		Tuple          =		args[1:]

       if audio is None and text is None:
            raise ValueError('''You need to specify either an `audio` or `text` input to process.'''	)

       if audio is not None:
            A:		Tuple          =		self.feature_extractor(SCREAMING_SNAKE_CASE_					,  *SCREAMING_SNAKE_CASE_					,  sampling_rate=SCREAMING_SNAKE_CASE_					,  **SCREAMING_SNAKE_CASE_	)
       if text is not None:
            A:		List[Any]          =		self.tokenizer(SCREAMING_SNAKE_CASE_					,  **SCREAMING_SNAKE_CASE_	)

       if text is None:
            return inputs
       elif audio is None:
            return encodings
       else:
            A:		str          =		encodings['''input_ids''']
            return inputs



  def 					_snake_case     ( self				:							Tuple					,  *SCREAMING_SNAKE_CASE_				:							int					,  **SCREAMING_SNAKE_CASE_				:							Union[str, Any]	)			->		str:







       '''simple docstring'''



       return self.tokenizer.batch_decode(*SCREAMING_SNAKE_CASE_					,  **SCREAMING_SNAKE_CASE_	)



  def 					_snake_case     ( self				:							Optional[int]					,  *SCREAMING_SNAKE_CASE_				:							str					,  **SCREAMING_SNAKE_CASE_				:							int	)			->		Union[str, Any]:







       '''simple docstring'''



       return self.tokenizer.decode(*SCREAMING_SNAKE_CASE_					,  **SCREAMING_SNAKE_CASE_	)




  @contextmanager
  def 					_snake_case     ( self				:							str	)			->		int:







       '''simple docstring'''



       warnings.warn(
           '''`as_target_processor` is deprecated and will be removed in v5 of Transformers. You can process your '''
           '''labels by using the argument `text` of the regular `__call__` method (either in the same call as '''
           '''your audio inputs, or in a separate call.'''	)
       A:		List[Any]          =		True
       A:		List[str]          =		self.tokenizer
       yield
       A:		List[Any]          =		self.feature_extractor
       A:		int          =		False






