
"""simple docstring"""


from typing import Dict, List, Optional, Union

import numpy as np

from .feature_extraction_utils import BatchFeature, FeatureExtractionMixin
from .utils import PaddingStrategy, TensorType, is_tf_tensor, is_torch_tensor, logging, to_numpy


_lowercase							   =				logging.get_logger(__name__)







class 				__lowercase				(      __a    ):



						'''simple docstring'''


						def __init__( self     :      List[str]  ,A_     :      Optional[int]  ,A_     :      Optional[Any]  ,A_     :      List[Any]  ,**A_     :      Any      )     ->   Union[str, Any]:
							A						   =   feature_size
							A						   =   sampling_rate
							A						   =   padding_value

							A						   =   kwargs.pop('padding_side'  ,'right'      )
							A						   =   kwargs.pop('return_attention_mask'  ,A__      )

							super().__init__(**A__      )


						def 						_SCREAMING_SNAKE_CASE      ( self     :      Optional[int]  ,A_     :      Union[str, Any]  ,A_     :      List[str] = True  ,A_     :      Optional[Any] = None  ,A_     :      Union[str, Any] = False  ,A_     :      List[Any] = None  ,A_     :      Union[str, Any] = None  ,A_     :      str = None  ,)     ->   BatchFeature:
							# If we have a list of dicts, let's convert it in a dict of lists
							# We do this to allow using this method as a collate_fn function in PyTorch Dataloader
							if isinstance(A__  ,(list, tuple)      ) and isinstance(processed_features[0]  ,(dict, BatchFeature)      ):
								A						   =   {
								    key: [example[key] for example in processed_features] for key in processed_features[0].keys()
								}

							# The model's main input name, usually `input_values`, has be passed for padding
							if self.model_input_names[0] not in processed_features:
								raise ValueError(
								    'You should supply an instance of `transformers.BatchFeature` or list of `transformers.BatchFeature`'
								    F' to this method that includes {self.model_input_names[0]}, but you provided'
								    F' {list(processed_features.keys()      )}'      )

							A						   =   processed_features[self.model_input_names[0]]
							A						   =   (
							    return_attention_mask if return_attention_mask is not None else self.return_attention_mask
							)

							if len(A__      ) == 0:
								if return_attention_mask:
									A						   =   []
								return processed_features

							# If we have PyTorch/TF tensors or lists as inputs, we cast them as Numpy arrays
							# and rebuild them afterwards if no return_tensors is specified
							# Note that we lose the specific device the tensor may be on for PyTorch

							A						   =   required_input[0]
							if isinstance(A__  ,(list, tuple)      ):
								# first_element might be an empty list/tuple in some edge cases so we grab the first non empty element.
								A						   =   0
								while len(required_input[index]      ) == 0:
									index += 1
								if index < len(A__      ):
									A						   =   required_input[index][0]

							if return_tensors is None:
								if is_tf_tensor(A__      ):
									A						   =   'tf'
								elif is_torch_tensor(A__      ):
									A						   =   'pt'
								elif isinstance(A__  ,(int, float, list, tuple, np.ndarray)      ):
									A						   =   'np'
								else:
									raise ValueError(
									    F'type of {first_element} unknown: {type(A__      )}. '
									    'Should be one of a python, numpy, pytorch or tensorflow object.'      )

							for key, value in processed_features.items():
								if isinstance(value[0]  ,(int, float)      ):
									A						   =   to_numpy(A__      )
								else:
									A						   =   [to_numpy(A__      ) for v in value]

									# Convert padding_strategy in PaddingStrategy
							A						   =   self._get_padding_strategies(padding=A__  ,max_length=A__      )

							A						   =   processed_features[self.model_input_names[0]]

							A						   =   len(A__      )
							if not all(len(A__      ) == batch_size for v in processed_features.values()      ):
								raise ValueError('Some items in the output dictionary have a different batch size than others.'      )

							A						   =   []
							for i in range(A__      ):
								A						   =   {k: v[i] for k, v in processed_features.items()}
								# truncation
								A						   =   self._truncate(
								    A__  ,max_length=A__  ,pad_to_multiple_of=A__  ,truncation=A__  ,)
								truncated_inputs.append(A__      )

							if padding_strategy == PaddingStrategy.LONGEST:
								# make sure that `max_length` cannot be longer than the longest truncated length
								A						   =   max(len(input_slice[self.model_input_names[0]]      ) for input_slice in truncated_inputs      )
								A						   =   PaddingStrategy.MAX_LENGTH

							A						   =   {}
							for i in range(A__      ):
								# padding
								A						   =   self._pad(
								    truncated_inputs[i]  ,max_length=A__  ,padding_strategy=A__  ,pad_to_multiple_of=A__  ,return_attention_mask=A__  ,)

								for key, value in outputs.items():
									if key not in batch_outputs:
										A						   =   []
									if value.dtype is np.dtype(np.floataa      ):
										A						   =   value.astype(np.floataa      )
									batch_outputs[key].append(A__      )

							return BatchFeature(A__  ,tensor_type=A__      )


						def 						_SCREAMING_SNAKE_CASE      ( self     :      int  ,A_     :      int  ,A_     :      Union[str, Any] = None  ,A_     :      str = PaddingStrategy.DO_NOT_PAD  ,A_     :      Dict = None  ,A_     :      List[str] = None  ,)     ->   dict:
							A						   =   processed_features[self.model_input_names[0]]

							if padding_strategy == PaddingStrategy.LONGEST:
								A						   =   len(A__      )

							if max_length is not None and pad_to_multiple_of is not None and (max_length % pad_to_multiple_of != 0):
								A						   =   ((max_length // pad_to_multiple_of) + 1) * pad_to_multiple_of

							A						   =   padding_strategy != PaddingStrategy.DO_NOT_PAD and len(A__      ) < max_length

							if return_attention_mask and "attention_mask" not in processed_features:
								A						   =   np.ones(len(A__      )  ,dtype=np.intaa      )

							if needs_to_be_padded:
								A						   =   max_length - len(A__      )
								if self.padding_side == "right":
									if return_attention_mask:
										A						   =   np.pad(
										    processed_features['attention_mask']  ,(0, difference)      )
									A						   =   ((0, difference), (0, 0)) if self.feature_size > 1 else (0, difference)
									A						   =   np.pad(
									    A__  ,A__  ,'constant'  ,constant_values=self.padding_value      )
								elif self.padding_side == "left":
									if return_attention_mask:
										A						   =   np.pad(
										    processed_features['attention_mask']  ,(difference, 0)      )
									A						   =   ((difference, 0), (0, 0)) if self.feature_size > 1 else (difference, 0)
									A						   =   np.pad(
									    A__  ,A__  ,'constant'  ,constant_values=self.padding_value      )
								else:
									raise ValueError('Invalid padding strategy:' + str(self.padding_side      )      )

							return processed_features


						def 						_SCREAMING_SNAKE_CASE      ( self     :      List[Any]  ,A_     :      Optional[Any]  ,A_     :      Union[str, Any] = None  ,A_     :      Tuple = None  ,A_     :      List[Any] = None  ,)     ->   Union[str, Any]:
							if not truncation:
								return processed_features
							elif truncation and max_length is None:
								raise ValueError('When setting ``truncation=True``, make sure that ``max_length`` is defined.'      )

							A						   =   processed_features[self.model_input_names[0]]

							# find `max_length` that fits `pad_to_multiple_of`
							if max_length is not None and pad_to_multiple_of is not None and (max_length % pad_to_multiple_of != 0):
								A						   =   ((max_length // pad_to_multiple_of) + 1) * pad_to_multiple_of

							A						   =   len(A__      ) > max_length

							if needs_to_be_truncated:
								A						   =   processed_features[self.model_input_names[0]][:max_length]
								if "attention_mask" in processed_features:
									A						   =   processed_features['attention_mask'][:max_length]

							return processed_features




						def 						_SCREAMING_SNAKE_CASE      ( self     :      Optional[int]  ,A_     :      Optional[int]=False  ,A_     :      Tuple=None      )     ->   Union[str, Any]:

							# Get padding strategy
							if padding is not False:
								if padding is True:
									A						   =   PaddingStrategy.LONGEST  # Default to pad to the longest sequence in the batch
								elif not isinstance(A__  ,A__      ):
									A						   =   PaddingStrategy(A__      )
								elif isinstance(A__  ,A__      ):
									A						   =   padding
							else:
								A						   =   PaddingStrategy.DO_NOT_PAD

							# Set max length if needed
							if max_length is None:
								if padding_strategy == PaddingStrategy.MAX_LENGTH:
									raise ValueError(
									    F'When setting ``padding={PaddingStrategy.MAX_LENGTH}``, make sure that max_length is defined'      )

									# Test if we have a padding value
							if padding_strategy != PaddingStrategy.DO_NOT_PAD and (self.padding_value is None):
								raise ValueError(
								    'Asking to pad but the feature_extractor does not have a padding value. Please select a value to use'
								    ' as `padding_value`. For example: `feature_extractor.padding_value = 0.0`.'      )

							return padding_strategy

"""simple docstring"""


from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
    is_vision_available,
)


_lowercase							   =				{
    '''configuration_clip''': [
        '''CLIP_PRETRAINED_CONFIG_ARCHIVE_MAP''',
        '''CLIPConfig''',
        '''CLIPOnnxConfig''',
        '''CLIPTextConfig''',
        '''CLIPVisionConfig''',
    ],
    '''processing_clip''': ['''CLIPProcessor'''],
    '''tokenization_clip''': ['''CLIPTokenizer'''],
}

try:
		if not is_tokenizers_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		_lowercase							   =				['''CLIPTokenizerFast''']

try:
		if not is_vision_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		_lowercase							   =				['''CLIPFeatureExtractor''']
		_lowercase							   =				['''CLIPImageProcessor''']

try:
		if not is_torch_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		_lowercase							   =				[
		    '''CLIP_PRETRAINED_MODEL_ARCHIVE_LIST''',
		    '''CLIPModel''',
		    '''CLIPPreTrainedModel''',
		    '''CLIPTextModel''',
		    '''CLIPTextModelWithProjection''',
		    '''CLIPVisionModel''',
		    '''CLIPVisionModelWithProjection''',
		]

try:
		if not is_tf_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		_lowercase							   =				[
		    '''TF_CLIP_PRETRAINED_MODEL_ARCHIVE_LIST''',
		    '''TFCLIPModel''',
		    '''TFCLIPPreTrainedModel''',
		    '''TFCLIPTextModel''',
		    '''TFCLIPVisionModel''',
		]

try:
		if not is_flax_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		_lowercase							   =				[
		    '''FlaxCLIPModel''',
		    '''FlaxCLIPPreTrainedModel''',
		    '''FlaxCLIPTextModel''',
		    '''FlaxCLIPTextPreTrainedModel''',
		    '''FlaxCLIPVisionModel''',
		    '''FlaxCLIPVisionPreTrainedModel''',
		]


if TYPE_CHECKING:
		from .configuration_clip import (
		    CLIP_PRETRAINED_CONFIG_ARCHIVE_MAP,
		    CLIPConfig,
		    CLIPOnnxConfig,
		    CLIPTextConfig,
		    CLIPVisionConfig,
		)
		from .processing_clip import CLIPProcessor
		from .tokenization_clip import CLIPTokenizer

		try:
				if not is_tokenizers_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .tokenization_clip_fast import CLIPTokenizerFast

		try:
				if not is_vision_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .feature_extraction_clip import CLIPFeatureExtractor
				from .image_processing_clip import CLIPImageProcessor

		try:
				if not is_torch_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_clip import (
				    CLIP_PRETRAINED_MODEL_ARCHIVE_LIST,
				    CLIPModel,
				    CLIPPreTrainedModel,
				    CLIPTextModel,
				    CLIPTextModelWithProjection,
				    CLIPVisionModel,
				    CLIPVisionModelWithProjection,
				)

		try:
				if not is_tf_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_tf_clip import (
				    TF_CLIP_PRETRAINED_MODEL_ARCHIVE_LIST,
				    TFCLIPModel,
				    TFCLIPPreTrainedModel,
				    TFCLIPTextModel,
				    TFCLIPVisionModel,
				)

		try:
				if not is_flax_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_flax_clip import (
				    FlaxCLIPModel,
				    FlaxCLIPPreTrainedModel,
				    FlaxCLIPTextModel,
				    FlaxCLIPTextPreTrainedModel,
				    FlaxCLIPVisionModel,
				    FlaxCLIPVisionPreTrainedModel,
				)


else:
		import sys

		_lowercase							   =				_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)