




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 ConvNextImageProcessor

class      UpperCAmelCase						(			unittest.TestCase   ):






   def __init__(	self   :       int						,					UpperCamelCase_   :       List[Any]						,					UpperCamelCase_   :       int=7						,					UpperCamelCase_   :       Dict=3						,					UpperCamelCase_   :       List[str]=18						,					UpperCamelCase_   :       List[Any]=30						,					UpperCamelCase_   :       Any=4_00						,					UpperCamelCase_   :       List[Any]=True						,					UpperCamelCase_   :       Dict=None						,					UpperCamelCase_   :       str=0.875						,					UpperCamelCase_   :       int=True						,					UpperCamelCase_   :       str=[0.5, 0.5, 0.5]						,					UpperCamelCase_   :       Tuple=[0.5, 0.5, 0.5]						,					)->			Optional[int]:
     _lowercase: Tuple						     =    size if size is not None else {'shortest_edge': 20}
     _lowercase: Tuple						     =    parent
     _lowercase: Optional[int]						     =    batch_size
     _lowercase: Dict						     =    num_channels
     _lowercase: Union[str, Any]						     =    image_size
     _lowercase: str						     =    min_resolution
     _lowercase: int						     =    max_resolution
     _lowercase: List[Any]						     =    do_resize
     _lowercase: Tuple						     =    size
     _lowercase: str						     =    crop_pct
     _lowercase: List[str]						     =    do_normalize
     _lowercase: List[str]						     =    image_mean
     _lowercase: Any						     =    image_std






   def  _a						(	self   :       int     )->			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_pct": self.crop_pct,
     }

@require_torch
@require_vision
class      UpperCAmelCase						(			snake_case__   ,					unittest.TestCase   ):
   __SCREAMING_SNAKE_CASE		:							Any							=					ConvNextImageProcessor if is_vision_available() else None






   def  _a						(	self   :       Dict     )->			str:
     _lowercase: Union[str, Any]						     =    ConvNextImageProcessingTester(self     )






   @property
   def  _a						(	self   :       Union[str, Any]     )->			Union[str, Any]:
     return self.image_processor_tester.prepare_image_processor_dict()






   def  _a						(	self   :       List[Any]     )->			List[str]:
     _lowercase: Optional[int]						     =    self.image_processing_class(**self.image_processor_dict     )
     self.assertTrue(hasattr(UpperCamelCase_						,					'do_resize'     )     )
     self.assertTrue(hasattr(UpperCamelCase_						,					'size'     )     )
     self.assertTrue(hasattr(UpperCamelCase_						,					'crop_pct'     )     )
     self.assertTrue(hasattr(UpperCamelCase_						,					'do_normalize'     )     )
     self.assertTrue(hasattr(UpperCamelCase_						,					'image_mean'     )     )
     self.assertTrue(hasattr(UpperCamelCase_						,					'image_std'     )     )






   def  _a						(	self   :       Union[str, Any]     )->			List[str]:
     _lowercase: List[Any]						     =    self.image_processing_class.from_dict(self.image_processor_dict     )
     self.assertEqual(image_processor.size						,					{'shortest_edge': 20}     )

     _lowercase: List[str]						     =    self.image_processing_class.from_dict(self.image_processor_dict						,					size=42     )
     self.assertEqual(image_processor.size						,					{'shortest_edge': 42}     )






   def  _a						(	self   :       Tuple     )->			Tuple:
     pass






   def  _a						(	self   :       Optional[Any]     )->			List[Any]:
     # Initialize image_processing
     _lowercase: Tuple						     =    self.image_processing_class(**self.image_processor_dict     )
     # create random PIL images
     _lowercase: List[Any]						     =    prepare_image_inputs(self.image_processor_tester						,					equal_resolution=UpperCamelCase_     )
     for image in image_inputs:
       self.assertIsInstance(UpperCamelCase_						,					Image.Image     )

     # Test not batched input
     _lowercase: List[str]						     =    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['shortest_edge'],
             self.image_processor_tester.size['shortest_edge'],
         )						,					)

     # Test batched
     _lowercase: Optional[Any]						     =    image_processing(UpperCamelCase_						,					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['shortest_edge'],
             self.image_processor_tester.size['shortest_edge'],
         )						,					)






   def  _a						(	self   :       Any     )->			Optional[Any]:
     # Initialize image_processing
     _lowercase: Optional[int]						     =    self.image_processing_class(**self.image_processor_dict     )
     # create random numpy tensors
     _lowercase: str						     =    prepare_image_inputs(self.image_processor_tester						,					equal_resolution=UpperCamelCase_						,					numpify=UpperCamelCase_     )
     for image in image_inputs:
       self.assertIsInstance(UpperCamelCase_						,					np.ndarray     )

     # Test not batched input
     _lowercase: Union[str, 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['shortest_edge'],
             self.image_processor_tester.size['shortest_edge'],
         )						,					)

     # Test batched
     _lowercase: Tuple						     =    image_processing(UpperCamelCase_						,					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['shortest_edge'],
             self.image_processor_tester.size['shortest_edge'],
         )						,					)






   def  _a						(	self   :       Optional[int]     )->			Optional[int]:
     # Initialize image_processing
     _lowercase: int						     =    self.image_processing_class(**self.image_processor_dict     )
     # create random PyTorch tensors
     _lowercase: Optional[int]						     =    prepare_image_inputs(self.image_processor_tester						,					equal_resolution=UpperCamelCase_						,					torchify=UpperCamelCase_     )
     for image in image_inputs:
       self.assertIsInstance(UpperCamelCase_						,					torch.Tensor     )

     # Test not batched input
     _lowercase: 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['shortest_edge'],
             self.image_processor_tester.size['shortest_edge'],
         )						,					)

     # Test batched
     _lowercase: Any						     =    image_processing(UpperCamelCase_						,					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['shortest_edge'],
             self.image_processor_tester.size['shortest_edge'],
         )						,					)






import warnings
from contextlib import contextmanager

from ...processing_utils import ProcessorMixin

class      UpperCAmelCase						(			A   ):
   __SCREAMING_SNAKE_CASE		:							Optional[Any]							=					"AutoFeatureExtractor"
   __SCREAMING_SNAKE_CASE		:							Optional[Any]							=					"Speech2Text2Tokenizer"






   def __init__(	self   :       Optional[Any]						,					UpperCamelCase_   :       Any						,					UpperCamelCase_   :       List[Any]     )->			Dict:
     super().__init__(UpperCamelCase_						,					UpperCamelCase_     )
     _lowercase: Optional[int]						     =    self.feature_extractor
     _lowercase: int						     =    False






   def __call__(	self   :       List[Any]						,					*UpperCamelCase_   :       Dict						,					**UpperCamelCase_   :       Optional[int]     )->			str:
     # For backward compatibility
     if self._in_target_context_manager:
       return self.current_processor(*UpperCamelCase_						,					**UpperCamelCase_     )

     if "raw_speech" in kwargs:
       warnings.warn('Using `raw_speech` as a keyword argument is deprecated. Use `audio` instead.'     )
       _lowercase: Tuple						     =    kwargs.pop('raw_speech'     )
     else:
       _lowercase: Optional[Any]						     =    kwargs.pop('audio'						,					UpperCamelCase_     )
     _lowercase: int						     =    kwargs.pop('sampling_rate'						,					UpperCamelCase_     )
     _lowercase: Dict						     =    kwargs.pop('text'						,					UpperCamelCase_     )
     if len(UpperCamelCase_     ) > 0:
       _lowercase: List[Any]						     =    args[0]
       _lowercase: List[Any]						     =    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:
       _lowercase: Optional[int]						     =    self.feature_extractor(UpperCamelCase_						,					*UpperCamelCase_						,					sampling_rate=UpperCamelCase_						,					**UpperCamelCase_     )
     if text is not None:
       _lowercase: str						     =    self.tokenizer(UpperCamelCase_						,					**UpperCamelCase_     )

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






   def  _a						(	self   :       Optional[int]						,					*UpperCamelCase_   :       Tuple						,					**UpperCamelCase_   :       Dict     )->			List[str]:
     return self.tokenizer.batch_decode(*UpperCamelCase_						,					**UpperCamelCase_     )






   def  _a						(	self   :       Tuple						,					*UpperCamelCase_   :       List[Any]						,					**UpperCamelCase_   :       Union[str, Any]     )->			Optional[int]:
     return self.tokenizer.decode(*UpperCamelCase_						,					**UpperCamelCase_     )






   @contextmanager
   def  _a						(	self   :       Tuple     )->			Union[str, Any]:
     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.'     )
     _lowercase: Any						     =    True
     _lowercase: Optional[Any]						     =    self.tokenizer
     yield
     _lowercase: Union[str, Any]						     =    self.feature_extractor
     _lowercase: str						     =    False
