

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


UpperCAmelCase__ :     int				    =    logging.get_logger(__name__)







class        a__       (   __A       ):







  """simple docstring"""




  def __init__(			self    : List[str]				,				UpperCAmelCase__    : List[Any]				,				UpperCAmelCase__    : int				,				UpperCAmelCase__    : List[Any]				,				**UpperCAmelCase__    : Optional[int]		)					->List[Any]:
        """simple docstring"""







        SCREAMING_SNAKE_CASE :      int       =   feature_size
        SCREAMING_SNAKE_CASE :      List[Any]       =   sampling_rate
        SCREAMING_SNAKE_CASE :      Dict       =   padding_value

        SCREAMING_SNAKE_CASE :      List[Any]       =   kwargs.pop("""padding_side"""				,				"""right"""		)
        SCREAMING_SNAKE_CASE :      str       =   kwargs.pop("""return_attention_mask"""				,				UpperCAmelCase__		)

        super().__init__(**UpperCAmelCase__		)


  def  _lowercase      (			self    : int				,				UpperCAmelCase__    : Union[str, Any]				,				UpperCAmelCase__    : List[Any] = True				,				UpperCAmelCase__    : int = None				,				UpperCAmelCase__    : List[Any] = False				,				UpperCAmelCase__    : str = None				,				UpperCAmelCase__    : Dict = None				,				UpperCAmelCase__    : Any = None				,				)					->str:
        """simple docstring"""







        if isinstance(UpperCAmelCase__				,				(list, tuple)		) and isinstance(processed_features[0]				,				(dict, BatchFeature)		):
              SCREAMING_SNAKE_CASE :      int       =   {
                  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()		)}"		)

        SCREAMING_SNAKE_CASE :      Union[str, Any]       =   processed_features[self.model_input_names[0]]
        SCREAMING_SNAKE_CASE :      str       =   (
            return_attention_mask if return_attention_mask is not None else self.return_attention_mask
        )

        if len(UpperCAmelCase__		) == 0:
              if return_attention_mask:
                    SCREAMING_SNAKE_CASE :      Optional[Any]       =   []
              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

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

        if return_tensors is None:
              if is_tf_tensor(UpperCAmelCase__		):
                    SCREAMING_SNAKE_CASE :      List[str]       =   'tf'
              elif is_torch_tensor(UpperCAmelCase__		):
                    SCREAMING_SNAKE_CASE :      Tuple       =   'pt'
              elif isinstance(UpperCAmelCase__				,				(int, float, list, tuple, np.ndarray)		):
                    SCREAMING_SNAKE_CASE :      str       =   'np'
              else:
                    raise ValueError(
                        f"type of {first_element} unknown: {type(UpperCAmelCase__		)}. "
                        """Should be one of a python, numpy, pytorch or tensorflow object."""		)

        for key, value in processed_features.items():
              if isinstance(value[0]				,				(int, float)		):
                    SCREAMING_SNAKE_CASE :      Dict       =   to_numpy(UpperCAmelCase__		)
              else:
                    SCREAMING_SNAKE_CASE :      str       =   [to_numpy(UpperCAmelCase__		) for v in value]

        # Convert padding_strategy in PaddingStrategy
        SCREAMING_SNAKE_CASE :      List[Any]       =   self._get_padding_strategies(padding=UpperCAmelCase__				,				max_length=UpperCAmelCase__		)

        SCREAMING_SNAKE_CASE :      Union[str, Any]       =   processed_features[self.model_input_names[0]]

        SCREAMING_SNAKE_CASE :      List[str]       =   len(UpperCAmelCase__		)
        if not all(len(UpperCAmelCase__		) == batch_size for v in processed_features.values()		):
              raise ValueError("""Some items in the output dictionary have a different batch size than others."""		)

        SCREAMING_SNAKE_CASE :      List[str]       =   []
        for i in range(UpperCAmelCase__		):
              SCREAMING_SNAKE_CASE :      Optional[int]       =   {k: v[i] for k, v in processed_features.items()}
              # truncation
              SCREAMING_SNAKE_CASE :      Tuple       =   self._truncate(
                  UpperCAmelCase__				,				max_length=UpperCAmelCase__				,				pad_to_multiple_of=UpperCAmelCase__				,				truncation=UpperCAmelCase__				,				)
              truncated_inputs.append(UpperCAmelCase__		)

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

        SCREAMING_SNAKE_CASE :      List[str]       =   {}
        for i in range(UpperCAmelCase__		):
              # padding
              SCREAMING_SNAKE_CASE :      int       =   self._pad(
                  truncated_inputs[i]				,				max_length=UpperCAmelCase__				,				padding_strategy=UpperCAmelCase__				,				pad_to_multiple_of=UpperCAmelCase__				,				return_attention_mask=UpperCAmelCase__				,				)

              for key, value in outputs.items():
                    if key not in batch_outputs:
                          SCREAMING_SNAKE_CASE :      Optional[int]       =   []
                    if value.dtype is np.dtype(np.floataa		):
                          SCREAMING_SNAKE_CASE :      Any       =   value.astype(np.floataa		)
                    batch_outputs[key].append(UpperCAmelCase__		)

        return BatchFeature(UpperCAmelCase__				,				tensor_type=UpperCAmelCase__		)


  def  _lowercase      (			self    : Dict				,				UpperCAmelCase__    : Optional[int]				,				UpperCAmelCase__    : Union[str, Any] = None				,				UpperCAmelCase__    : Optional[Any] = PaddingStrategy.DO_NOT_PAD				,				UpperCAmelCase__    : Any = None				,				UpperCAmelCase__    : Tuple = None				,				)					->Optional[int]:
        """simple docstring"""







        SCREAMING_SNAKE_CASE :      str       =   processed_features[self.model_input_names[0]]

        if padding_strategy == PaddingStrategy.LONGEST:
              SCREAMING_SNAKE_CASE :      List[Any]       =   len(UpperCAmelCase__		)

        if max_length is not None and pad_to_multiple_of is not None and (max_length % pad_to_multiple_of != 0):
              SCREAMING_SNAKE_CASE :      Optional[Any]       =   ((max_length // pad_to_multiple_of) + 1) * pad_to_multiple_of

        SCREAMING_SNAKE_CASE :      Dict       =   padding_strategy != PaddingStrategy.DO_NOT_PAD and len(UpperCAmelCase__		) < max_length

        if return_attention_mask and "attention_mask" not in processed_features:
              SCREAMING_SNAKE_CASE :      List[Any]       =   np.ones(len(UpperCAmelCase__		)				,				dtype=np.intaa		)

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

        return processed_features


  def  _lowercase      (			self    : str				,				UpperCAmelCase__    : Any				,				UpperCAmelCase__    : int = None				,				UpperCAmelCase__    : int = None				,				UpperCAmelCase__    : Union[str, Any] = None				,				)					->str:
        """simple docstring"""







        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."""		)

        SCREAMING_SNAKE_CASE :      Any       =   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):
              SCREAMING_SNAKE_CASE :      int       =   ((max_length // pad_to_multiple_of) + 1) * pad_to_multiple_of

        SCREAMING_SNAKE_CASE :      Optional[int]       =   len(UpperCAmelCase__		) > max_length

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

        return processed_features




  def  _lowercase      (			self    : Optional[int]				,				UpperCAmelCase__    : List[str]=False				,				UpperCAmelCase__    : str=None		)					->Any:
        """simple docstring"""







        if padding is not False:
              if padding is True:
                    SCREAMING_SNAKE_CASE :      Union[str, Any]       =   PaddingStrategy.LONGEST  # Default to pad to the longest sequence in the batch
              elif not isinstance(UpperCAmelCase__				,				UpperCAmelCase__		):
                    SCREAMING_SNAKE_CASE :      Optional[int]       =   PaddingStrategy(UpperCAmelCase__		)
              elif isinstance(UpperCAmelCase__				,				UpperCAmelCase__		):
                    SCREAMING_SNAKE_CASE :      str       =   padding
        else:
              SCREAMING_SNAKE_CASE :      str       =   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







import sys
import tempfile
import unittest
import unittest.mock as mock
from pathlib import Path

from huggingface_hub import HfFolder, delete_repo
from requests.exceptions import HTTPError

from transformers import AutoFeatureExtractor, WavaVecaFeatureExtractor
from transformers.testing_utils import TOKEN, USER, get_tests_dir, is_staging_test


sys.path.append(str(Path(__file__).parent.parent / """utils"""))

from test_module.custom_feature_extraction import CustomFeatureExtractor  # noqa E402


UpperCAmelCase__ :     int				    =    get_tests_dir("""fixtures""")







class        a__       (   unittest.TestCase       ):







  """simple docstring"""




  def  _lowercase      (			self    : Dict		)					->Any:
        """simple docstring"""







        SCREAMING_SNAKE_CASE :      Dict       =   mock.Mock()
        SCREAMING_SNAKE_CASE :      Optional[int]       =   5_0_0
        SCREAMING_SNAKE_CASE :      List[str]       =   {}
        SCREAMING_SNAKE_CASE :      List[Any]       =   HTTPError
        SCREAMING_SNAKE_CASE :      int       =   {}

        # Download this model to make sure it's in the cache.
        SCREAMING_SNAKE_CASE :      List[Any]       =   WavaVecaFeatureExtractor.from_pretrained("""hf-internal-testing/tiny-random-wav2vec2"""		)
        # Under the mock environment we get a 500 error when trying to reach the model.
        with mock.patch("""requests.Session.request"""				,				return_value=UpperCAmelCase__		) as mock_head:
              SCREAMING_SNAKE_CASE :      Optional[int]       =   WavaVecaFeatureExtractor.from_pretrained("""hf-internal-testing/tiny-random-wav2vec2"""		)
              # This check we did call the fake head request
              mock_head.assert_called()




  def  _lowercase      (			self    : Optional[Any]		)					->int:
        """simple docstring"""







        SCREAMING_SNAKE_CASE :      str       =   WavaVecaFeatureExtractor.from_pretrained(
            """https://huggingface.co/hf-internal-testing/tiny-random-wav2vec2/resolve/main/preprocessor_config.json"""		)







@is_staging_test
class        a__       (   unittest.TestCase       ):







  """simple docstring"""




  @classmethod
  def  _lowercase      (			cls    : List[str]		)					->Dict:
        """simple docstring"""







        SCREAMING_SNAKE_CASE :      int       =   TOKEN
        HfFolder.save_token(UpperCAmelCase__		)


  @classmethod
  def  _lowercase      (			cls    : List[str]		)					->Optional[int]:
        """simple docstring"""







        try:
              delete_repo(token=cls._token				,				repo_id="""test-feature-extractor"""		)
        except HTTPError:
              pass

        try:
              delete_repo(token=cls._token				,				repo_id="""valid_org/test-feature-extractor-org"""		)
        except HTTPError:
              pass

        try:
              delete_repo(token=cls._token				,				repo_id="""test-dynamic-feature-extractor"""		)
        except HTTPError:
              pass


  def  _lowercase      (			self    : int		)					->Optional[int]:
        """simple docstring"""







        SCREAMING_SNAKE_CASE :      int       =   WavaVecaFeatureExtractor.from_pretrained(UpperCAmelCase__		)
        feature_extractor.push_to_hub("""test-feature-extractor"""				,				use_auth_token=self._token		)

        SCREAMING_SNAKE_CASE :      Any       =   WavaVecaFeatureExtractor.from_pretrained(f"{USER}/test-feature-extractor"		)
        for k, v in feature_extractor.__dict__.items():
              self.assertEqual(UpperCAmelCase__				,				getattr(UpperCAmelCase__				,				UpperCAmelCase__		)		)

        # Reset repo
        delete_repo(token=self._token				,				repo_id="""test-feature-extractor"""		)

        # Push to hub via save_pretrained
        with tempfile.TemporaryDirectory() as tmp_dir:
              feature_extractor.save_pretrained(
                  UpperCAmelCase__				,				repo_id="""test-feature-extractor"""				,				push_to_hub=UpperCAmelCase__				,				use_auth_token=self._token		)

        SCREAMING_SNAKE_CASE :      List[str]       =   WavaVecaFeatureExtractor.from_pretrained(f"{USER}/test-feature-extractor"		)
        for k, v in feature_extractor.__dict__.items():
              self.assertEqual(UpperCAmelCase__				,				getattr(UpperCAmelCase__				,				UpperCAmelCase__		)		)


  def  _lowercase      (			self    : List[Any]		)					->List[Any]:
        """simple docstring"""







        SCREAMING_SNAKE_CASE :      List[str]       =   WavaVecaFeatureExtractor.from_pretrained(UpperCAmelCase__		)
        feature_extractor.push_to_hub("""valid_org/test-feature-extractor"""				,				use_auth_token=self._token		)

        SCREAMING_SNAKE_CASE :      Optional[Any]       =   WavaVecaFeatureExtractor.from_pretrained("""valid_org/test-feature-extractor"""		)
        for k, v in feature_extractor.__dict__.items():
              self.assertEqual(UpperCAmelCase__				,				getattr(UpperCAmelCase__				,				UpperCAmelCase__		)		)

        # Reset repo
        delete_repo(token=self._token				,				repo_id="""valid_org/test-feature-extractor"""		)

        # Push to hub via save_pretrained
        with tempfile.TemporaryDirectory() as tmp_dir:
              feature_extractor.save_pretrained(
                  UpperCAmelCase__				,				repo_id="""valid_org/test-feature-extractor-org"""				,				push_to_hub=UpperCAmelCase__				,				use_auth_token=self._token		)

        SCREAMING_SNAKE_CASE :      List[Any]       =   WavaVecaFeatureExtractor.from_pretrained("""valid_org/test-feature-extractor-org"""		)
        for k, v in feature_extractor.__dict__.items():
              self.assertEqual(UpperCAmelCase__				,				getattr(UpperCAmelCase__				,				UpperCAmelCase__		)		)




  def  _lowercase      (			self    : Optional[Any]		)					->Optional[int]:
        """simple docstring"""







        CustomFeatureExtractor.register_for_auto_class()
        SCREAMING_SNAKE_CASE :      Any       =   CustomFeatureExtractor.from_pretrained(UpperCAmelCase__		)

        feature_extractor.push_to_hub("""test-dynamic-feature-extractor"""				,				use_auth_token=self._token		)

        # This has added the proper auto_map field to the config
        self.assertDictEqual(
            feature_extractor.auto_map				,				{"""AutoFeatureExtractor""": """custom_feature_extraction.CustomFeatureExtractor"""}				,				)

        SCREAMING_SNAKE_CASE :      Any       =   AutoFeatureExtractor.from_pretrained(
            f"{USER}/test-dynamic-feature-extractor"				,				trust_remote_code=UpperCAmelCase__		)
        # Can't make an isinstance check because the new_feature_extractor is from the CustomFeatureExtractor class of a dynamic module
        self.assertEqual(new_feature_extractor.__class__.__name__				,				"""CustomFeatureExtractor"""		)




