





"""simple docstring"""
from typing import Dict, List, Optional, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import (
    center_crop,
    get_resize_output_image_size,
    normalize,
    rescale,
    resize,
    to_channel_dimension_format,
)
from ...image_utils import (
    IMAGENET_DEFAULT_MEAN,
    IMAGENET_DEFAULT_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_vision_available, logging


if is_vision_available():
   import PIL


lowerCAmelCase:						List[Any]				   =logging.get_logger(__name__)
class lowerCamelCase__	(     lowerCAmelCase__					):
   __UpperCAmelCase										=       ["""pixel_values"""]



   def __init__(							self   , snake_case = True   , snake_case = None   , snake_case = 0.9   , snake_case = PILImageResampling.BICUBIC   , snake_case = True   , snake_case = None   , snake_case = 1 / 2_5_5   , snake_case = True   , snake_case = True   , snake_case = None   , snake_case = None   , **snake_case   , )			->     Dict:







      """simple docstring"""
      super().__init__(**__a						)
      lowercase     :      Any	=							size if size is not None else {"""shortest_edge""": 2_2_4}
      lowercase     :      Optional[int]	=							get_size_dict(__a   , default_to_square=__a						)
      lowercase     :      List[Any]	=							crop_size if crop_size is not None else {"""height""": 2_2_4, """width""": 2_2_4}
      lowercase     :      int	=							get_size_dict(__a   , param_name="""crop_size"""						)

      lowercase     :      Dict	=							do_resize
      lowercase     :      int	=							size
      lowercase     :      List[str]	=							crop_pct
      lowercase     :      Union[str, Any]	=							resample
      lowercase     :      Dict	=							do_center_crop
      lowercase     :      Any	=							crop_size
      lowercase     :      Union[str, Any]	=							do_rescale
      lowercase     :      Optional[Any]	=							rescale_factor
      lowercase     :      Tuple	=							do_normalize
      lowercase     :      Any	=							image_mean if image_mean is not None else IMAGENET_DEFAULT_MEAN
      lowercase     :      Dict	=							image_std if image_std is not None else IMAGENET_DEFAULT_STD



   def  _UpperCAmelCase						(							self   , snake_case   , snake_case   , snake_case = None   , snake_case = PILImageResampling.BICUBIC   , snake_case = None   , **snake_case   , )			->     Tuple:







      """simple docstring"""
      lowercase     :      List[Any]	=							get_size_dict(__a   , default_to_square=__a						)
      if "shortest_edge" not in size and ("height" not in size or "width" not in size):
         raise ValueError(f'''size must contain \'height\' and \'width\' or \'shortest_edge\' as keys. Got {size.keys()}'''						)
      if crop_pct is not None:
         if "shortest_edge" in size:
            lowercase     :      Optional[Any]	=							int(size["""shortest_edge"""] / crop_pct						)
         elif "height" in size and "width" in size:
            if size["height"] == size["width"]:
               lowercase     :      List[Any]	=							int(size["""height"""] / crop_pct						)
            else:
               lowercase     :      Union[str, Any]	=							(int(size["""height"""] / crop_pct						), int(size["""width"""] / crop_pct						))
         else:
            raise ValueError("""Invalid size for resize: {}""".format(__a						)						)

         lowercase     :      str	=							get_resize_output_image_size(__a   , size=__a   , default_to_square=__a						)
      else:
         if "shortest_edge" in size:
            lowercase     :      Optional[int]	=							get_resize_output_image_size(__a   , size=size["""shortest_edge"""]   , default_to_square=__a						)
         elif "height" in size and "width" in size:
            lowercase     :      Optional[int]	=							(size["""height"""], size["""width"""])
         else:
            raise ValueError("""Invalid size for resize: {}""".format(__a						)						)

      return resize(__a   , size=__a   , resample=__a   , data_format=__a   , **__a						)



   def  _UpperCAmelCase						(							self   , snake_case   , snake_case   , snake_case = None   , **snake_case   , )			->     Tuple:







      """simple docstring"""
      lowercase     :      Union[str, Any]	=							get_size_dict(__a						)
      if "height" not in size or "width" not in size:
         raise ValueError(f'''size must contain \'height\' and \'width\' as keys. Got {size.keys()}'''						)
      return center_crop(__a   , size=(size["""height"""], size["""width"""])   , data_format=__a   , **__a						)



   def  _UpperCAmelCase						(							self   , snake_case   , snake_case   , snake_case = None   , **snake_case   , )			->     Optional[int]:







      """simple docstring"""
      return rescale(__a   , scale=__a   , data_format=__a   , **__a						)



   def  _UpperCAmelCase						(							self   , snake_case   , snake_case   , snake_case   , snake_case = None   , **snake_case   , )			->     Tuple:







      """simple docstring"""
      return normalize(__a   , mean=__a   , std=__a   , data_format=__a   , **__a						)





   def  _UpperCAmelCase						(							self   , snake_case   , snake_case = None   , snake_case = None   , snake_case = None   , snake_case = None   , snake_case = None   , snake_case = None   , snake_case = None   , snake_case = None   , snake_case = None   , snake_case = None   , snake_case = None   , snake_case = None   , snake_case = ChannelDimension.FIRST   , **snake_case   , )			->     List[str]:







      """simple docstring"""
      lowercase     :      Tuple	=							do_resize if do_resize is not None else self.do_resize
      lowercase     :      List[Any]	=							crop_pct if crop_pct is not None else self.crop_pct
      lowercase     :      Union[str, Any]	=							resample if resample is not None else self.resample
      lowercase     :      Dict	=							do_center_crop if do_center_crop is not None else self.do_center_crop
      lowercase     :      Tuple	=							do_rescale if do_rescale is not None else self.do_rescale
      lowercase     :      int	=							rescale_factor if rescale_factor is not None else self.rescale_factor
      lowercase     :      Optional[int]	=							do_normalize if do_normalize is not None else self.do_normalize
      lowercase     :      str	=							image_mean if image_mean is not None else self.image_mean
      lowercase     :      Optional[Any]	=							image_std if image_std is not None else self.image_std

      lowercase     :      Dict	=							size if size is not None else self.size
      lowercase     :      Dict	=							get_size_dict(__a   , default_to_square=__a						)
      lowercase     :      Any	=							crop_size if crop_size is not None else self.crop_size
      lowercase     :      List[str]	=							get_size_dict(__a   , param_name="""crop_size"""						)

      lowercase     :      Any	=							make_list_of_images(__a						)

      if not valid_images(__a						):
         raise ValueError(
             """Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, """
             """torch.Tensor, tf.Tensor or jax.ndarray."""						)

      if do_resize and size is None or resample is None:
         raise ValueError("""Size and resample must be specified if do_resize is True."""						)

      if do_center_crop and crop_pct is None:
         raise ValueError("""Crop_pct must be specified if do_center_crop is True."""						)

      if do_rescale and rescale_factor is None:
         raise ValueError("""Rescale factor must be specified if do_rescale is True."""						)

      if do_normalize and (image_mean is None or image_std is None):
         raise ValueError("""Image mean and std must be specified if do_normalize is True."""						)

      # All transformations expect numpy arrays.
      lowercase     :      List[str]	=							[to_numpy_array(__a						) for image in images]

      if do_resize:
         lowercase     :      int	=							[self.resize(image=__a   , size=__a   , crop_pct=__a   , resample=__a						) for image in images]

      if do_center_crop:
         lowercase     :      Tuple	=							[self.center_crop(image=__a   , size=__a						) for image in images]

      if do_rescale:
         lowercase     :      Dict	=							[self.rescale(image=__a   , scale=__a						) for image in images]

      if do_normalize:
         lowercase     :      Union[str, Any]	=							[self.normalize(image=__a   , mean=__a   , std=__a						) for image in images]

      lowercase     :      Optional[int]	=							[to_channel_dimension_format(__a   , __a						) for image in images]

      lowercase     :      Optional[Any]	=							{"""pixel_values""": images}
      return BatchFeature(data=__a   , tensor_type=__a						)






"""simple docstring"""


import warnings
from contextlib import contextmanager

from ...processing_utils import ProcessorMixin
class     _UpperCamelCase    (  lowerCAmelCase__						):
							'''simple docstring'''







							__UpperCAmelCase		:       List[str]							 ="""Speech2TextFeatureExtractor"""
							__UpperCAmelCase		:       Union[str, Any]							 ="""Speech2TextTokenizer"""

							def __init__(	self       ,  __a       ,  __a      ):
										super().__init__(__a       ,  __a      )
										__lowerCAmelCase							       =     self.feature_extractor
										__lowerCAmelCase							       =     False

							def __call__(	self       ,  *__a       ,  **__a      ):
										# For backward compatibility
										if self._in_target_context_manager:
													return self.current_processor(*__a       ,  **__a      )

										if "raw_speech" in kwargs:
													warnings.warn("Using `raw_speech` as a keyword argument is deprecated. Use `audio` instead."      )
													__lowerCAmelCase							       =     kwargs.pop("raw_speech"      )
										else:
													__lowerCAmelCase							       =     kwargs.pop("audio"       ,  __a      )
										__lowerCAmelCase							       =     kwargs.pop("sampling_rate"       ,  __a      )
										__lowerCAmelCase							       =     kwargs.pop("text"       ,  __a      )
										if len(__a      ) > 0:
													__lowerCAmelCase							       =     args[0]
													__lowerCAmelCase							       =     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:
													__lowerCAmelCase							       =     self.feature_extractor(__a       ,  *__a       ,  sampling_rate=__a       ,  **__a      )
										if text is not None:
													__lowerCAmelCase							       =     self.tokenizer(__a       ,  **__a      )

										if text is None:
													return inputs
										elif audio is None:
													return encodings
										else:
													__lowerCAmelCase							       =     encodings["input_ids"]
													return inputs

							def      snake_case							(	self       ,  *__a       ,  **__a      ):
										return self.tokenizer.batch_decode(*__a       ,  **__a      )

							def      snake_case							(	self       ,  *__a       ,  **__a      ):
										return self.tokenizer.decode(*__a       ,  **__a      )



							@contextmanager
							def      snake_case							(	self      ):
										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."      )
										__lowerCAmelCase							       =     True
										__lowerCAmelCase							       =     self.tokenizer
										yield
										__lowerCAmelCase							       =     self.feature_extractor
										__lowerCAmelCase							       =     False




