




'''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 EfficientNetImageProcessor






class 						lowerCAmelCase_	(       unittest.TestCase							):







  '''simple docstring'''

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







       '''simple docstring'''



       A:		List[str]          =		size if size is not None else {'''height''': 18, '''width''': 18}
       A:		int          =		parent
       A:		List[str]          =		batch_size
       A:		Dict          =		num_channels
       A:		Optional[int]          =		image_size
       A:		Any          =		min_resolution
       A:		List[Any]          =		max_resolution
       A:		int          =		do_resize
       A:		List[Any]          =		size
       A:		Dict          =		do_normalize
       A:		int          =		image_mean
       A:		Optional[Any]          =		image_std




  def 					_snake_case     ( self				:							str	)			->		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,
       }






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







  '''simple docstring'''

  UpperCamelCase_			:      Union[str, Any]										=				EfficientNetImageProcessor if is_vision_available() else None



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







       '''simple docstring'''



       A:		List[Any]          =		EfficientNetImageProcessorTester(self	)



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







       '''simple docstring'''



       return self.image_processor_tester.prepare_image_processor_dict()



  def 					_snake_case     ( self				:							str	)			->		Any:







       '''simple docstring'''



       A:		Dict          =		self.image_processing_class(**self.image_processor_dict	)
       self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_					,  '''image_mean'''	)	)
       self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_					,  '''image_std'''	)	)
       self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_					,  '''do_normalize'''	)	)
       self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_					,  '''do_resize'''	)	)
       self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_					,  '''size'''	)	)



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







       '''simple docstring'''



       A:		int          =		self.image_processing_class.from_dict(self.image_processor_dict	)
       self.assertEqual(image_processor.size					,  {'''height''': 18, '''width''': 18}	)

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



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







       '''simple docstring'''



       A:		str          =		self.image_processing_class(**self.image_processor_dict	)
       # create random PIL images
       A:		List[str]          =		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:		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.size['''height'''],
               self.image_processor_tester.size['''width'''],
           )					,  )

       # Test batched
       A:		Optional[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.size['''height'''],
               self.image_processor_tester.size['''width'''],
           )					,  )



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







       '''simple docstring'''



       A:		Tuple          =		self.image_processing_class(**self.image_processor_dict	)
       # create random numpy tensors
       A:		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:		List[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.size['''height'''],
               self.image_processor_tester.size['''width'''],
           )					,  )

       # Test batched
       A:		str          =		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.size['''height'''],
               self.image_processor_tester.size['''width'''],
           )					,  )




  def 					_snake_case     ( self				:							int	)			->		str:







       '''simple docstring'''



       A:		Any          =		self.image_processing_class(**self.image_processor_dict	)
       # create random PyTorch tensors
       A:		Tuple          =		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:		Tuple          =		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.size['''height'''],
               self.image_processor_tester.size['''width'''],
           )					,  )

       # Test batched
       A:		Dict          =		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.size['''height'''],
               self.image_processor_tester.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






