




"""simple docstring"""





import os
from dataclasses import dataclass, field
from io import BytesIO
from typing import TYPE_CHECKING, Any, ClassVar, Dict, Optional, Union

import numpy as np
import pyarrow as pa

from .. import config
from ..download.streaming_download_manager import xopen, xsplitext
from ..table import array_cast
from ..utils.py_utils import no_op_if_value_is_null, string_to_dict


if TYPE_CHECKING:
	from .features import FeatureType


A ,						A ,						A   =       False, False, False



@dataclass
class      a__				:
			lowercase_    =	None
			lowercase_    =	True
			lowercase_    =	True
			lowercase_    =	None
			# Automatically constructed
			lowercase_    =	"dict"
			lowercase_    =	pa.struct({"bytes": pa.binary(), "path": pa.string()}   )
			lowercase_    =	field(default="Audio"							,    init=lowercase__							,    repr=lowercase__   )


			def __call__(      self      :    Tuple):


						"""simple docstring"""




						return self.pa_type


			def       a_     (      self      :    Tuple      ,  UpperCamelCase_      :    Union[str, bytes, dict]):


						"""simple docstring"""




						try:
									import soundfile as sf  # soundfile is a dependency of librosa, needed to decode audio files.
						except ImportError as err:
									raise ImportError("To support encoding audio data, please install \'soundfile\'.") from err
						if isinstance(UpperCAmelCase__      ,  UpperCAmelCase__):
									return {"bytes": None, "path": value}
						elif isinstance(UpperCAmelCase__      ,  UpperCAmelCase__):
									return {"bytes": value, "path": None}
						elif "array" in value:
									# convert the audio array to wav bytes
									__UpperCAmelCase	: Optional[int]		=					BytesIO()
									sf.write(UpperCAmelCase__      ,  value["array"]      ,  value["sampling_rate"]      ,  format="wav")
									return {"bytes": buffer.getvalue(), "path": None}
						elif value.get("path") is not None and os.path.isfile(value["path"]):
									# we set "bytes": None to not duplicate the data if they're already available locally
									if value["path"].endswith("pcm"):
												# "PCM" only has raw audio bytes
												if value.get("sampling_rate") is None:
															# At least, If you want to convert "PCM-byte" to "WAV-byte", you have to know sampling rate
															raise KeyError("To use PCM files, please specify a \'sampling_rate\' in Audio object")
												if value.get("bytes"):
															# If we already had PCM-byte, we don`t have to make "read file, make bytes" (just use it!)
															__UpperCAmelCase	: Optional[int]		=					np.frombuffer(value["bytes"]      ,  dtype=np.intaa).astype(np.floataa) / 32767
												else:
															__UpperCAmelCase	: Union[str, Any]		=					np.memmap(value["path"]      ,  dtype="h"      ,  mode="r").astype(np.floataa) / 32767

												__UpperCAmelCase	: Tuple		=					BytesIO(bytes())
												sf.write(UpperCAmelCase__      ,  UpperCAmelCase__      ,  value["sampling_rate"]      ,  format="wav")
												return {"bytes": buffer.getvalue(), "path": None}
									else:
												return {"bytes": None, "path": value.get("path")}
						elif value.get("bytes") is not None or value.get("path") is not None:
									# store the audio bytes, and path is used to infer the audio format using the file extension
									return {"bytes": value.get("bytes"), "path": value.get("path")}
						else:
									raise ValueError(
									    F"An audio sample should have one of \'path\' or \'bytes\' but they are missing or None in {value}.")


			def       a_     (      self      :    int      ,  UpperCamelCase_      :    dict      ,  UpperCamelCase_      :    Optional[Dict[str, Union[str, bool, None]]] = None):


						"""simple docstring"""




						if not self.decode:
									raise RuntimeError("Decoding is disabled for this feature. Please use Audio(decode=True) instead.")

						__UpperCAmelCase	: str		=					(value['''path'''], BytesIO(value["bytes"])) if value['''bytes'''] is not None else (value['''path'''], None)
						if path is None and file is None:
									raise ValueError(F"An audio sample should have one of \'path\' or \'bytes\' but both are None in {value}.")

						try:
									import librosa
									import soundfile as sf
						except ImportError as err:
									raise ImportError("To support decoding audio files, please install \'librosa\' and \'soundfile\'.") from err

						__UpperCAmelCase	: Union[str, Any]		=					xsplitext(UpperCAmelCase__)[1][1:].lower() if path is not None else None
						if not config.IS_OPUS_SUPPORTED and audio_format == "opus":
									raise RuntimeError(
									    "Decoding \'opus\' files requires system library \'libsndfile\'>=1.0.31, "
									    "You can try to update `soundfile` python library: `pip install \"soundfile>=0.12.1\"`. ")
						elif not config.IS_MP3_SUPPORTED and audio_format == "mp3":
									raise RuntimeError(
									    "Decoding \'mp3\' files requires system library \'libsndfile\'>=1.1.0, "
									    "You can try to update `soundfile` python library: `pip install \"soundfile>=0.12.1\"`. ")

						if file is None:
									__UpperCAmelCase	: List[str]		=					token_per_repo_id or {}
									__UpperCAmelCase	: Dict		=					path.split("::")[-1]
									try:
												__UpperCAmelCase	: Optional[Any]		=					string_to_dict(UpperCAmelCase__      ,  config.HUB_DATASETS_URL)['''repo_id''']
												__UpperCAmelCase	: Optional[int]		=					token_per_repo_id[repo_id]
									except (ValueError, KeyError):
												__UpperCAmelCase	: List[Any]		=					None

									with xopen(UpperCAmelCase__      ,  "rb"      ,  use_auth_token=UpperCAmelCase__) as f:
												__UpperCAmelCase	: Any		=					sf.read(UpperCAmelCase__)

						else:
									__UpperCAmelCase	: Optional[Any]		=					sf.read(UpperCAmelCase__)

						__UpperCAmelCase	: int		=					array.T
						if self.mono:
									__UpperCAmelCase	: Tuple		=					librosa.to_mono(UpperCAmelCase__)
						if self.sampling_rate and self.sampling_rate != sampling_rate:
									__UpperCAmelCase	: Union[str, Any]		=					librosa.resample(UpperCAmelCase__      ,  orig_sr=UpperCAmelCase__      ,  target_sr=self.sampling_rate)
									__UpperCAmelCase	: str		=					self.sampling_rate

						return {"path": path, "array": array, "sampling_rate": sampling_rate}


			def       a_     (      self      :    str):


						"""simple docstring"""




						from .features import Value

						if self.decode:
									raise ValueError("Cannot flatten a decoded Audio feature.")
						return {
						    "bytes": Value("binary"),
						    "path": Value("string"),
						}


			def       a_     (      self      :    Any      ,  UpperCamelCase_      :    Union[pa.StringArray, pa.StructArray]):


						"""simple docstring"""




						if pa.types.is_string(storage.type):
									__UpperCAmelCase	: List[Any]		=					pa.array([None] * len(UpperCAmelCase__)      ,  type=pa.binary())
									__UpperCAmelCase	: Tuple		=					pa.StructArray.from_arrays([bytes_array, storage]      ,  ["bytes", "path"]      ,  mask=storage.is_null())
						elif pa.types.is_binary(storage.type):
									__UpperCAmelCase	: List[Any]		=					pa.array([None] * len(UpperCAmelCase__)      ,  type=pa.string())
									__UpperCAmelCase	: List[str]		=					pa.StructArray.from_arrays([storage, path_array]      ,  ["bytes", "path"]      ,  mask=storage.is_null())
						elif pa.types.is_struct(storage.type) and storage.type.get_all_field_indices("array"):
									__UpperCAmelCase	: int		=					pa.array([Audio().encode_example(UpperCAmelCase__) if x is not None else None for x in storage.to_pylist()])
						elif pa.types.is_struct(storage.type):
									if storage.type.get_field_index("bytes") >= 0:
												__UpperCAmelCase	: Any		=					storage.field("bytes")
									else:
												__UpperCAmelCase	: Optional[int]		=					pa.array([None] * len(UpperCAmelCase__)      ,  type=pa.binary())
									if storage.type.get_field_index("path") >= 0:
												__UpperCAmelCase	: Optional[Any]		=					storage.field("path")
									else:
												__UpperCAmelCase	: Dict		=					pa.array([None] * len(UpperCAmelCase__)      ,  type=pa.string())
									__UpperCAmelCase	: Optional[Any]		=					pa.StructArray.from_arrays([bytes_array, path_array]      ,  ["bytes", "path"]      ,  mask=storage.is_null())
						return array_cast(UpperCAmelCase__      ,  self.pa_type)


			def       a_     (      self      :    Tuple      ,  UpperCamelCase_      :    pa.StructArray):


						"""simple docstring"""




						@no_op_if_value_is_null
						def path_to_bytes(UpperCamelCase_      :    Tuple):
									with xopen(UpperCAmelCase__      ,  "rb") as f:
												__UpperCAmelCase	: str		=					f.read()
									return bytes_

						__UpperCAmelCase	: List[Any]		=					pa.array(
						    [
						        (path_to_bytes(x["path"]) if x["bytes"] is None else x["bytes"]) if x is not None else None
						        for x in storage.to_pylist()
						    ]      ,  type=pa.binary()      ,  )
						__UpperCAmelCase	: Optional[Any]		=					pa.array(
						    [os.path.basename(UpperCAmelCase__) if path is not None else None for path in storage.field("path").to_pylist()]      ,  type=pa.string()      ,  )
						__UpperCAmelCase	: List[str]		=					pa.StructArray.from_arrays([bytes_array, path_array]      ,  ["bytes", "path"]      ,  mask=bytes_array.is_null())
						return array_cast(UpperCAmelCase__      ,  self.pa_type)








"""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_STANDARD_MEAN,
    IMAGENET_STANDARD_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    is_valid_image,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_vision_available, logging


if is_vision_available():
					import PIL


A   =       logging.get_logger(__name__)

def 					_UpperCamelCase    (			UpperCamelCase							)			->			List[List[ImageInput]]:


			"""simple docstring"""
			if isinstance(UpperCamelCase				,       (list, tuple)							) and isinstance(videos[0]				,       (list, tuple)							) and is_valid_image(videos[0][0]							):
						return videos

			elif isinstance(UpperCamelCase				,       (list, tuple)							) and is_valid_image(videos[0]							):
						return [videos]

			elif is_valid_image(UpperCamelCase							):
						return [[videos]]

			raise ValueError(f"Could not make batched video from {videos}"							)





class      a__				(	__magic_name__   ):
			lowercase_    =	["pixel_values"]


			def __init__(      self      :    Tuple      ,  UpperCamelCase_      :    bool = True      ,  UpperCamelCase_      :    Dict[str, int] = None      ,  UpperCamelCase_      :    PILImageResampling = PILImageResampling.BILINEAR      ,  UpperCamelCase_      :    bool = True      ,  UpperCamelCase_      :    Dict[str, int] = None      ,  UpperCamelCase_      :    bool = True      ,  UpperCamelCase_      :    Union[int, float] = 1 / 255      ,  UpperCamelCase_      :    bool = True      ,  UpperCamelCase_      :    Optional[Union[float, List[float]]] = None      ,  UpperCamelCase_      :    Optional[Union[float, List[float]]] = None      ,  **UpperCamelCase_      :    List[str]      ,  ):


						"""simple docstring"""




						super().__init__(**UpperCamelCase_)
						__UpperCAmelCase	: Dict		=					size if size is not None else {"shortest_edge": 224}
						__UpperCAmelCase	: Union[str, Any]		=					get_size_dict(UpperCamelCase_      ,  default_to_square=UpperCamelCase_)
						__UpperCAmelCase	: Optional[int]		=					crop_size if crop_size is not None else {"height": 224, "width": 224}
						__UpperCAmelCase	: List[str]		=					get_size_dict(UpperCamelCase_      ,  param_name="crop_size")

						__UpperCAmelCase	: Optional[Any]		=					do_resize
						__UpperCAmelCase	: Union[str, Any]		=					size
						__UpperCAmelCase	: str		=					do_center_crop
						__UpperCAmelCase	: str		=					crop_size
						__UpperCAmelCase	: Any		=					resample
						__UpperCAmelCase	: List[Any]		=					do_rescale
						__UpperCAmelCase	: Union[str, Any]		=					rescale_factor
						__UpperCAmelCase	: List[Any]		=					do_normalize
						__UpperCAmelCase	: Dict		=					image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
						__UpperCAmelCase	: Tuple		=					image_std if image_std is not None else IMAGENET_STANDARD_STD


			def       a_     (      self      :    int      ,  UpperCamelCase_      :    np.ndarray      ,  UpperCamelCase_      :    Dict[str, int]      ,  UpperCamelCase_      :    PILImageResampling = PILImageResampling.BILINEAR      ,  UpperCamelCase_      :    Optional[Union[str, ChannelDimension]] = None      ,  **UpperCamelCase_      :    List[Any]      ,  ):


						"""simple docstring"""




						__UpperCAmelCase	: Optional[Any]		=					get_size_dict(UpperCamelCase_      ,  default_to_square=UpperCamelCase_)
						if "shortest_edge" in size:
									__UpperCAmelCase	: Dict		=					get_resize_output_image_size(UpperCamelCase_      ,  size["shortest_edge"]      ,  default_to_square=UpperCamelCase_)
						elif "height" in size and "width" in size:
									__UpperCAmelCase	: Dict		=					(size["height"], size["width"])
						else:
									raise ValueError(F"Size must have 'height' and 'width' or 'shortest_edge' as keys. Got {size.keys()}")
						return resize(UpperCamelCase_      ,  size=UpperCamelCase_      ,  resample=UpperCamelCase_      ,  data_format=UpperCamelCase_      ,  **UpperCamelCase_)


			def       a_     (      self      :    List[str]      ,  UpperCamelCase_      :    np.ndarray      ,  UpperCamelCase_      :    Dict[str, int]      ,  UpperCamelCase_      :    Optional[Union[str, ChannelDimension]] = None      ,  **UpperCamelCase_      :    Tuple      ,  ):


						"""simple docstring"""




						__UpperCAmelCase	: int		=					get_size_dict(UpperCamelCase_)
						if "height" not in size or "width" not in size:
									raise ValueError(F"Size must have 'height' and 'width' as keys. Got {size.keys()}")
						return center_crop(UpperCamelCase_      ,  size=(size["height"], size["width"])      ,  data_format=UpperCamelCase_      ,  **UpperCamelCase_)


			def       a_     (      self      :    Optional[int]      ,  UpperCamelCase_      :    np.ndarray      ,  UpperCamelCase_      :    Union[int, float]      ,  UpperCamelCase_      :    Optional[Union[str, ChannelDimension]] = None      ,  **UpperCamelCase_      :    Optional[int]      ,  ):


						"""simple docstring"""




						return rescale(UpperCamelCase_      ,  scale=UpperCamelCase_      ,  data_format=UpperCamelCase_      ,  **UpperCamelCase_)


			def       a_     (      self      :    List[Any]      ,  UpperCamelCase_      :    np.ndarray      ,  UpperCamelCase_      :    Union[float, List[float]]      ,  UpperCamelCase_      :    Union[float, List[float]]      ,  UpperCamelCase_      :    Optional[Union[str, ChannelDimension]] = None      ,  **UpperCamelCase_      :    Union[str, Any]      ,  ):


						"""simple docstring"""




						return normalize(UpperCamelCase_      ,  mean=UpperCamelCase_      ,  std=UpperCamelCase_      ,  data_format=UpperCamelCase_      ,  **UpperCamelCase_)


			def       a_     (      self      :    List[str]      ,  UpperCamelCase_      :    ImageInput      ,  UpperCamelCase_      :    bool = None      ,  UpperCamelCase_      :    Dict[str, int] = None      ,  UpperCamelCase_      :    PILImageResampling = None      ,  UpperCamelCase_      :    bool = None      ,  UpperCamelCase_      :    Dict[str, int] = None      ,  UpperCamelCase_      :    bool = None      ,  UpperCamelCase_      :    float = None      ,  UpperCamelCase_      :    bool = None      ,  UpperCamelCase_      :    Optional[Union[float, List[float]]] = None      ,  UpperCamelCase_      :    Optional[Union[float, List[float]]] = None      ,  UpperCamelCase_      :    Optional[ChannelDimension] = ChannelDimension.FIRST      ,  ):


						"""simple docstring"""




						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_size is None:
									raise ValueError("Crop size 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.
						__UpperCAmelCase	: List[str]		=					to_numpy_array(UpperCamelCase_)

						if do_resize:
									__UpperCAmelCase	: Tuple		=					self.resize(image=UpperCamelCase_      ,  size=UpperCamelCase_      ,  resample=UpperCamelCase_)

						if do_center_crop:
									__UpperCAmelCase	: str		=					self.center_crop(UpperCamelCase_      ,  size=UpperCamelCase_)

						if do_rescale:
									__UpperCAmelCase	: Union[str, Any]		=					self.rescale(image=UpperCamelCase_      ,  scale=UpperCamelCase_)

						if do_normalize:
									__UpperCAmelCase	: Optional[int]		=					self.normalize(image=UpperCamelCase_      ,  mean=UpperCamelCase_      ,  std=UpperCamelCase_)

						__UpperCAmelCase	: Any		=					to_channel_dimension_format(UpperCamelCase_      ,  UpperCamelCase_)
						return image


			def       a_     (      self      :    List[Any]      ,  UpperCamelCase_      :    ImageInput      ,  UpperCamelCase_      :    bool = None      ,  UpperCamelCase_      :    Dict[str, int] = None      ,  UpperCamelCase_      :    PILImageResampling = None      ,  UpperCamelCase_      :    bool = None      ,  UpperCamelCase_      :    Dict[str, int] = None      ,  UpperCamelCase_      :    bool = None      ,  UpperCamelCase_      :    float = None      ,  UpperCamelCase_      :    bool = None      ,  UpperCamelCase_      :    Optional[Union[float, List[float]]] = None      ,  UpperCamelCase_      :    Optional[Union[float, List[float]]] = None      ,  UpperCamelCase_      :    Optional[Union[str, TensorType]] = None      ,  UpperCamelCase_      :    ChannelDimension = ChannelDimension.FIRST      ,  **UpperCamelCase_      :    Optional[int]      ,  ):


						"""simple docstring"""




						__UpperCAmelCase	: Optional[Any]		=					do_resize if do_resize is not None else self.do_resize
						__UpperCAmelCase	: Optional[Any]		=					resample if resample is not None else self.resample
						__UpperCAmelCase	: Any		=					do_center_crop if do_center_crop is not None else self.do_center_crop
						__UpperCAmelCase	: str		=					do_rescale if do_rescale is not None else self.do_rescale
						__UpperCAmelCase	: str		=					rescale_factor if rescale_factor is not None else self.rescale_factor
						__UpperCAmelCase	: Tuple		=					do_normalize if do_normalize is not None else self.do_normalize
						__UpperCAmelCase	: str		=					image_mean if image_mean is not None else self.image_mean
						__UpperCAmelCase	: Optional[Any]		=					image_std if image_std is not None else self.image_std

						__UpperCAmelCase	: Tuple		=					size if size is not None else self.size
						__UpperCAmelCase	: str		=					get_size_dict(UpperCamelCase_      ,  default_to_square=UpperCamelCase_)
						__UpperCAmelCase	: Any		=					crop_size if crop_size is not None else self.crop_size
						__UpperCAmelCase	: int		=					get_size_dict(UpperCamelCase_      ,  param_name="crop_size")

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

						__UpperCAmelCase	: Dict		=					make_batched(UpperCamelCase_)

						__UpperCAmelCase	: List[str]		=					[
						    [
						        self._preprocess_image(
						            image=UpperCamelCase_      ,  do_resize=UpperCamelCase_      ,  size=UpperCamelCase_      ,  resample=UpperCamelCase_      ,  do_center_crop=UpperCamelCase_      ,  crop_size=UpperCamelCase_      ,  do_rescale=UpperCamelCase_      ,  rescale_factor=UpperCamelCase_      ,  do_normalize=UpperCamelCase_      ,  image_mean=UpperCamelCase_      ,  image_std=UpperCamelCase_      ,  data_format=UpperCamelCase_      ,  )
						        for img in video
						    ]
						    for video in videos
						]

						__UpperCAmelCase	: int		=					{"pixel_values": videos}
						return BatchFeature(data=UpperCamelCase_      ,  tensor_type=UpperCamelCase_)


