




'''simple docstring'''






import itertools
import os
import random
import tempfile
import unittest

import numpy as np
from datasets import load_dataset

from transformers import is_speech_available
from transformers.testing_utils import check_json_file_has_correct_format, require_torch, require_torchaudio
from transformers.utils.import_utils import is_torch_available

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


if is_speech_available():
      from transformers import WhisperFeatureExtractor

if is_torch_available():
      import torch

A_			      =		random.Random()






def 		UpperCamelCase__      (		__SCREAMING_SNAKE_CASE				,      __SCREAMING_SNAKE_CASE=1.0				,      __SCREAMING_SNAKE_CASE=None				,      __SCREAMING_SNAKE_CASE=None		)    ->   Optional[Any]:
      if rng is None:
            snake_case__       :   Union[str, Any]	      =    global_rng

      snake_case__       :   str	      =    []
      for batch_idx in range(shape[0]		):
            values.append([]		)
            for _ in range(shape[1]		):
                  values[-1].append(rng.random() * scale		)

      return values






@require_torch
@require_torchaudio
class        lowercase_      (						unittest.TestCase							):





     def __init__(     self     :					Optional[Any]     ,							__lowerCamelCase     :					Optional[int]     ,							__lowerCamelCase     :					List[str]=7     ,							__lowerCamelCase     :					Tuple=400     ,							__lowerCamelCase     :					Dict=2000     ,							__lowerCamelCase     :					Dict=10     ,							__lowerCamelCase     :					List[Any]=160     ,							__lowerCamelCase     :					Dict=8     ,							__lowerCamelCase     :					List[Any]=0.0     ,							__lowerCamelCase     :					Any=4000     ,							__lowerCamelCase     :					Union[str, Any]=False     ,							__lowerCamelCase     :					Union[str, Any]=True     ,							):
           snake_case__       :   Dict	      =    parent
           snake_case__       :   Tuple	      =    batch_size
           snake_case__       :   List[Any]	      =    min_seq_length
           snake_case__       :   Tuple	      =    max_seq_length
           snake_case__       :   Tuple	      =    (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
           snake_case__       :   str	      =    padding_value
           snake_case__       :   List[Any]	      =    sampling_rate
           snake_case__       :   Union[str, Any]	      =    return_attention_mask
           snake_case__       :   str	      =    do_normalize
           snake_case__       :   Union[str, Any]	      =    feature_size
           snake_case__       :   Optional[Any]	      =    chunk_length
           snake_case__       :   int	      =    hop_length





     def 			_lowerCAmelCase      (     self     :					str			):
           return {
               "feature_size": self.feature_size,
               "hop_length": self.hop_length,
               "chunk_length": self.chunk_length,
               "padding_value": self.padding_value,
               "sampling_rate": self.sampling_rate,
               "return_attention_mask": self.return_attention_mask,
               "do_normalize": self.do_normalize,
           }





     def 			_lowerCAmelCase      (     self     :					List[str]     ,							__lowerCamelCase     :					Union[str, Any]=False     ,							__lowerCamelCase     :					str=False			):
           def _flatten(__lowerCamelCase     :					int			):
                 return list(itertools.chain(*__lowerCamelCase			)			)

           if equal_length:
                 snake_case__       :   List[str]	      =    [floats_list((self.max_seq_length, self.feature_size)			) for _ in range(self.batch_size			)]
           else:
                 # make sure that inputs increase in size
                 snake_case__       :   Union[str, Any]	      =    [
                     floats_list((x, self.feature_size)			)
                     for x in range(self.min_seq_length     ,							self.max_seq_length     ,							self.seq_length_diff			)
                 ]
           if numpify:
                 snake_case__       :   Union[str, Any]	      =    [np.asarray(__lowerCamelCase			) for x in speech_inputs]
           return speech_inputs






@require_torch
@require_torchaudio
class        lowercase_      (						lowerCAmelCase_						,       unittest.TestCase							):
     A_											=			WhisperFeatureExtractor if is_speech_available() else None





     def 			_lowerCAmelCase      (     self     :					Union[str, Any]			):
           snake_case__       :   str	      =    WhisperFeatureExtractionTester(self			)





     def 			_lowerCAmelCase      (     self     :					int			):
           snake_case__       :   Union[str, Any]	      =    self.feature_extraction_class(**self.feat_extract_dict			)

           with tempfile.TemporaryDirectory() as tmpdirname:
                 snake_case__       :   Optional[int]	      =    feat_extract_first.save_pretrained(__lowerCamelCase			)[0]
                 check_json_file_has_correct_format(__lowerCamelCase			)
                 snake_case__       :   Tuple	      =    self.feature_extraction_class.from_pretrained(__lowerCamelCase			)

           snake_case__       :   int	      =    feat_extract_first.to_dict()
           snake_case__       :   int	      =    feat_extract_second.to_dict()
           snake_case__       :   List[str]	      =    feat_extract_first.mel_filters
           snake_case__       :   Dict	      =    feat_extract_second.mel_filters
           self.assertTrue(np.allclose(__lowerCamelCase     ,							__lowerCamelCase			)			)
           self.assertEqual(__lowerCamelCase     ,							__lowerCamelCase			)





     def 			_lowerCAmelCase      (     self     :					str			):
           snake_case__       :   List[str]	      =    self.feature_extraction_class(**self.feat_extract_dict			)

           with tempfile.TemporaryDirectory() as tmpdirname:
                 snake_case__       :   List[Any]	      =    os.path.join(__lowerCamelCase     ,							'feat_extract.json'			)
                 feat_extract_first.to_json_file(__lowerCamelCase			)
                 snake_case__       :   Dict	      =    self.feature_extraction_class.from_json_file(__lowerCamelCase			)

           snake_case__       :   Tuple	      =    feat_extract_first.to_dict()
           snake_case__       :   Optional[Any]	      =    feat_extract_second.to_dict()
           snake_case__       :   int	      =    feat_extract_first.mel_filters
           snake_case__       :   int	      =    feat_extract_second.mel_filters
           self.assertTrue(np.allclose(__lowerCamelCase     ,							__lowerCamelCase			)			)
           self.assertEqual(__lowerCamelCase     ,							__lowerCamelCase			)





     def 			_lowerCAmelCase      (     self     :					Dict			):
           # Tests that all call wrap to encode_plus and batch_encode_plus
           snake_case__       :   List[str]	      =    self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()			)
           # create three inputs of length 800, 1000, and 1200
           snake_case__       :   Optional[int]	      =    [floats_list((1, x)			)[0] for x in range(800     ,							1400     ,							200			)]
           snake_case__       :   Optional[Any]	      =    [np.asarray(__lowerCamelCase			) for speech_input in speech_inputs]

           # Test feature size
           snake_case__       :   Optional[Any]	      =    feature_extractor(__lowerCamelCase     ,							padding='max_length'     ,							return_tensors='np'			).input_features
           self.assertTrue(input_features.ndim == 3			)
           self.assertTrue(input_features.shape[-1] == feature_extractor.nb_max_frames			)
           self.assertTrue(input_features.shape[-2] == feature_extractor.feature_size			)

           # Test not batched input
           snake_case__       :   List[str]	      =    feature_extractor(speech_inputs[0]     ,							return_tensors='np'			).input_features
           snake_case__       :   Dict	      =    feature_extractor(np_speech_inputs[0]     ,							return_tensors='np'			).input_features
           self.assertTrue(np.allclose(__lowerCamelCase     ,							__lowerCamelCase     ,							atol=1E-3			)			)

           # Test batched
           snake_case__       :   Any	      =    feature_extractor(__lowerCamelCase     ,							return_tensors='np'			).input_features
           snake_case__       :   Optional[Any]	      =    feature_extractor(__lowerCamelCase     ,							return_tensors='np'			).input_features
           for enc_seq_a, enc_seq_a in zip(__lowerCamelCase     ,							__lowerCamelCase			):
                 self.assertTrue(np.allclose(__lowerCamelCase     ,							__lowerCamelCase     ,							atol=1E-3			)			)

           # Test 2-D numpy arrays are batched.
           snake_case__       :   Dict	      =    [floats_list((1, x)			)[0] for x in (800, 800, 800)]
           snake_case__       :   List[Any]	      =    np.asarray(__lowerCamelCase			)
           snake_case__       :   Union[str, Any]	      =    feature_extractor(__lowerCamelCase     ,							return_tensors='np'			).input_features
           snake_case__       :   List[str]	      =    feature_extractor(__lowerCamelCase     ,							return_tensors='np'			).input_features
           for enc_seq_a, enc_seq_a in zip(__lowerCamelCase     ,							__lowerCamelCase			):
                 self.assertTrue(np.allclose(__lowerCamelCase     ,							__lowerCamelCase     ,							atol=1E-3			)			)

           # Test truncation required
           snake_case__       :   List[str]	      =    [floats_list((1, x)			)[0] for x in range(200     ,							(feature_extractor.n_samples + 500)     ,							200			)]
           snake_case__       :   List[Any]	      =    [np.asarray(__lowerCamelCase			) for speech_input in speech_inputs]

           snake_case__       :   Any	      =    [x[: feature_extractor.n_samples] for x in speech_inputs]
           snake_case__       :   Optional[int]	      =    [np.asarray(__lowerCamelCase			) for speech_input in speech_inputs_truncated]

           snake_case__       :   Tuple	      =    feature_extractor(__lowerCamelCase     ,							return_tensors='np'			).input_features
           snake_case__       :   Optional[Any]	      =    feature_extractor(__lowerCamelCase     ,							return_tensors='np'			).input_features
           for enc_seq_a, enc_seq_a in zip(__lowerCamelCase     ,							__lowerCamelCase			):
                 self.assertTrue(np.allclose(__lowerCamelCase     ,							__lowerCamelCase     ,							atol=1E-3			)			)





     def 			_lowerCAmelCase      (     self     :					Optional[int]			):
           import torch

           snake_case__       :   List[Any]	      =    self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()			)
           snake_case__       :   Any	      =    np.random.rand(100     ,							32			).astype(np.floataa			)
           snake_case__       :   Union[str, Any]	      =    np_speech_inputs.tolist()

           for inputs in [py_speech_inputs, np_speech_inputs]:
                 snake_case__       :   Union[str, Any]	      =    feature_extractor.pad([{'input_features': inputs}]     ,							return_tensors='np'			)
                 self.assertTrue(np_processed.input_features.dtype == np.floataa			)
                 snake_case__       :   Dict	      =    feature_extractor.pad([{'input_features': inputs}]     ,							return_tensors='pt'			)
                 self.assertTrue(pt_processed.input_features.dtype == torch.floataa			)





     def 			_lowerCAmelCase      (     self     :					Dict     ,							__lowerCamelCase     :					Tuple			):
           snake_case__       :   str	      =    load_dataset('hf-internal-testing/librispeech_asr_dummy'     ,							'clean'     ,							split='validation'			)
           # automatic decoding with librispeech
           snake_case__       :   List[str]	      =    ds.sort('id'			).select(range(__lowerCamelCase			)			)[:num_samples]['audio']

           return [x["array"] for x in speech_samples]





     def 			_lowerCAmelCase      (     self     :					Union[str, Any]			):
           # fmt: off
           snake_case__       :   Union[str, Any]	      =    torch.tensor(
               [
                   0.1_1_9_3, -0.0_9_4_6, -0.1_0_9_8, -0.0_1_9_6, 0.0_2_2_5, -0.0_6_9_0, -0.1_7_3_6, 0.0_9_5_1,
                   0.0_9_7_1, -0.0_8_1_7, -0.0_7_0_2, 0.0_1_6_2, 0.0_2_6_0, 0.0_0_1_7, -0.0_1_9_2, -0.1_6_7_8,
                   0.0_7_0_9, -0.1_8_6_7, -0.0_6_5_5, -0.0_2_7_4, -0.0_2_3_4, -0.1_8_8_4, -0.0_5_1_6, -0.0_5_5_4,
                   -0.0_2_7_4, -0.1_4_2_5, -0.1_4_2_3, 0.0_8_3_7, 0.0_3_7_7, -0.0_8_5_4
               ]			)
           # fmt: on

           snake_case__       :   Any	      =    self._load_datasamples(1			)
           snake_case__       :   List[str]	      =    WhisperFeatureExtractor()
           snake_case__       :   int	      =    feature_extractor(__lowerCamelCase     ,							return_tensors='pt'			).input_features
           self.assertEqual(input_features.shape     ,							(1, 80, 3000)			)
           self.assertTrue(torch.allclose(input_features[0, 0, :30]     ,							__lowerCamelCase     ,							atol=1E-4			)			)





     def 			_lowerCAmelCase      (     self     :					Union[str, Any]			):
           snake_case__       :   str	      =    self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()			)
           snake_case__       :   Optional[int]	      =    self._load_datasamples(1			)[0]
           snake_case__       :   str	      =    ((audio - audio.min()) / (audio.max() - audio.min())) * 65535  # Rescale to [0, 65535] to show issue
           snake_case__       :   Optional[Any]	      =    feat_extract.zero_mean_unit_var_norm([audio]     ,							attention_mask=__lowerCamelCase			)[0]

           self.assertTrue(np.all(np.mean(__lowerCamelCase			) < 1E-3			)			)
           self.assertTrue(np.all(np.abs(np.var(__lowerCamelCase			) - 1			) < 1E-3			)			)






'''simple docstring'''






from ..utils import DummyObject, requires_backends






class        lowercase_      (						metaclass=lowerCAmelCase_							):
     A_											=			["flax"]





     def __init__(     self     :					Optional[Any]     ,							*__lowerCamelCase     :					Tuple     ,							**__lowerCamelCase     :					Optional[int]			):
           requires_backends(self     ,							['flax']			)





     @classmethod
     def 			_lowerCAmelCase      (     cls     :					Tuple     ,							*__lowerCamelCase     :					str     ,							**__lowerCamelCase     :					Optional[int]			):
           requires_backends(cls     ,							['flax']			)





     @classmethod
     def 			_lowerCAmelCase      (     cls     :					Any     ,							*__lowerCamelCase     :					Dict     ,							**__lowerCamelCase     :					List[str]			):
           requires_backends(cls     ,							['flax']			)






class        lowercase_      (						metaclass=lowerCAmelCase_							):
     A_											=			["flax"]





     def __init__(     self     :					int     ,							*__lowerCamelCase     :					str     ,							**__lowerCamelCase     :					Union[str, Any]			):
           requires_backends(self     ,							['flax']			)





     @classmethod
     def 			_lowerCAmelCase      (     cls     :					Optional[Any]     ,							*__lowerCamelCase     :					List[str]     ,							**__lowerCamelCase     :					Optional[int]			):
           requires_backends(cls     ,							['flax']			)





     @classmethod
     def 			_lowerCAmelCase      (     cls     :					Optional[Any]     ,							*__lowerCamelCase     :					Optional[Any]     ,							**__lowerCamelCase     :					Optional[int]			):
           requires_backends(cls     ,							['flax']			)






class        lowercase_      (						metaclass=lowerCAmelCase_							):
     A_											=			["flax"]





     def __init__(     self     :					List[str]     ,							*__lowerCamelCase     :					str     ,							**__lowerCamelCase     :					str			):
           requires_backends(self     ,							['flax']			)





     @classmethod
     def 			_lowerCAmelCase      (     cls     :					Union[str, Any]     ,							*__lowerCamelCase     :					str     ,							**__lowerCamelCase     :					str			):
           requires_backends(cls     ,							['flax']			)





     @classmethod
     def 			_lowerCAmelCase      (     cls     :					Any     ,							*__lowerCamelCase     :					Tuple     ,							**__lowerCamelCase     :					Optional[int]			):
           requires_backends(cls     ,							['flax']			)






class        lowercase_      (						metaclass=lowerCAmelCase_							):
     A_											=			["flax"]





     def __init__(     self     :					Optional[Any]     ,							*__lowerCamelCase     :					Optional[Any]     ,							**__lowerCamelCase     :					str			):
           requires_backends(self     ,							['flax']			)





     @classmethod
     def 			_lowerCAmelCase      (     cls     :					str     ,							*__lowerCamelCase     :					Dict     ,							**__lowerCamelCase     :					int			):
           requires_backends(cls     ,							['flax']			)





     @classmethod
     def 			_lowerCAmelCase      (     cls     :					Optional[Any]     ,							*__lowerCamelCase     :					Union[str, Any]     ,							**__lowerCamelCase     :					Union[str, Any]			):
           requires_backends(cls     ,							['flax']			)






class        lowercase_      (						metaclass=lowerCAmelCase_							):
     A_											=			["flax"]





     def __init__(     self     :					Optional[int]     ,							*__lowerCamelCase     :					Tuple     ,							**__lowerCamelCase     :					Optional[Any]			):
           requires_backends(self     ,							['flax']			)





     @classmethod
     def 			_lowerCAmelCase      (     cls     :					List[Any]     ,							*__lowerCamelCase     :					Dict     ,							**__lowerCamelCase     :					int			):
           requires_backends(cls     ,							['flax']			)





     @classmethod
     def 			_lowerCAmelCase      (     cls     :					Tuple     ,							*__lowerCamelCase     :					List[str]     ,							**__lowerCamelCase     :					str			):
           requires_backends(cls     ,							['flax']			)






class        lowercase_      (						metaclass=lowerCAmelCase_							):
     A_											=			["flax"]





     def __init__(     self     :					List[Any]     ,							*__lowerCamelCase     :					Dict     ,							**__lowerCamelCase     :					Union[str, Any]			):
           requires_backends(self     ,							['flax']			)





     @classmethod
     def 			_lowerCAmelCase      (     cls     :					Any     ,							*__lowerCamelCase     :					Union[str, Any]     ,							**__lowerCamelCase     :					Optional[int]			):
           requires_backends(cls     ,							['flax']			)





     @classmethod
     def 			_lowerCAmelCase      (     cls     :					List[Any]     ,							*__lowerCamelCase     :					Optional[Any]     ,							**__lowerCamelCase     :					Optional[int]			):
           requires_backends(cls     ,							['flax']			)






class        lowercase_      (						metaclass=lowerCAmelCase_							):
     A_											=			["flax"]





     def __init__(     self     :					Dict     ,							*__lowerCamelCase     :					Union[str, Any]     ,							**__lowerCamelCase     :					Any			):
           requires_backends(self     ,							['flax']			)





     @classmethod
     def 			_lowerCAmelCase      (     cls     :					List[Any]     ,							*__lowerCamelCase     :					Optional[int]     ,							**__lowerCamelCase     :					List[str]			):
           requires_backends(cls     ,							['flax']			)





     @classmethod
     def 			_lowerCAmelCase      (     cls     :					Union[str, Any]     ,							*__lowerCamelCase     :					str     ,							**__lowerCamelCase     :					Any			):
           requires_backends(cls     ,							['flax']			)






class        lowercase_      (						metaclass=lowerCAmelCase_							):
     A_											=			["flax"]





     def __init__(     self     :					Tuple     ,							*__lowerCamelCase     :					Union[str, Any]     ,							**__lowerCamelCase     :					Dict			):
           requires_backends(self     ,							['flax']			)





     @classmethod
     def 			_lowerCAmelCase      (     cls     :					List[Any]     ,							*__lowerCamelCase     :					Tuple     ,							**__lowerCamelCase     :					List[Any]			):
           requires_backends(cls     ,							['flax']			)





     @classmethod
     def 			_lowerCAmelCase      (     cls     :					int     ,							*__lowerCamelCase     :					Union[str, Any]     ,							**__lowerCamelCase     :					str			):
           requires_backends(cls     ,							['flax']			)






class        lowercase_      (						metaclass=lowerCAmelCase_							):
     A_											=			["flax"]





     def __init__(     self     :					Union[str, Any]     ,							*__lowerCamelCase     :					Optional[Any]     ,							**__lowerCamelCase     :					int			):
           requires_backends(self     ,							['flax']			)





     @classmethod
     def 			_lowerCAmelCase      (     cls     :					Any     ,							*__lowerCamelCase     :					List[str]     ,							**__lowerCamelCase     :					int			):
           requires_backends(cls     ,							['flax']			)





     @classmethod
     def 			_lowerCAmelCase      (     cls     :					List[str]     ,							*__lowerCamelCase     :					Optional[Any]     ,							**__lowerCamelCase     :					List[str]			):
           requires_backends(cls     ,							['flax']			)






class        lowercase_      (						metaclass=lowerCAmelCase_							):
     A_											=			["flax"]





     def __init__(     self     :					Union[str, Any]     ,							*__lowerCamelCase     :					Optional[Any]     ,							**__lowerCamelCase     :					List[Any]			):
           requires_backends(self     ,							['flax']			)





     @classmethod
     def 			_lowerCAmelCase      (     cls     :					Any     ,							*__lowerCamelCase     :					Optional[int]     ,							**__lowerCamelCase     :					int			):
           requires_backends(cls     ,							['flax']			)





     @classmethod
     def 			_lowerCAmelCase      (     cls     :					str     ,							*__lowerCamelCase     :					Tuple     ,							**__lowerCamelCase     :					List[Any]			):
           requires_backends(cls     ,							['flax']			)






class        lowercase_      (						metaclass=lowerCAmelCase_							):
     A_											=			["flax"]





     def __init__(     self     :					Optional[Any]     ,							*__lowerCamelCase     :					Union[str, Any]     ,							**__lowerCamelCase     :					Union[str, Any]			):
           requires_backends(self     ,							['flax']			)





     @classmethod
     def 			_lowerCAmelCase      (     cls     :					Union[str, Any]     ,							*__lowerCamelCase     :					Tuple     ,							**__lowerCamelCase     :					Optional[Any]			):
           requires_backends(cls     ,							['flax']			)





     @classmethod
     def 			_lowerCAmelCase      (     cls     :					str     ,							*__lowerCamelCase     :					Dict     ,							**__lowerCamelCase     :					Optional[int]			):
           requires_backends(cls     ,							['flax']			)






class        lowercase_      (						metaclass=lowerCAmelCase_							):
     A_											=			["flax"]





     def __init__(     self     :					Union[str, Any]     ,							*__lowerCamelCase     :					List[Any]     ,							**__lowerCamelCase     :					Optional[int]			):
           requires_backends(self     ,							['flax']			)





     @classmethod
     def 			_lowerCAmelCase      (     cls     :					Optional[int]     ,							*__lowerCamelCase     :					Tuple     ,							**__lowerCamelCase     :					Tuple			):
           requires_backends(cls     ,							['flax']			)





     @classmethod
     def 			_lowerCAmelCase      (     cls     :					Dict     ,							*__lowerCamelCase     :					Dict     ,							**__lowerCamelCase     :					Optional[Any]			):
           requires_backends(cls     ,							['flax']			)






class        lowercase_      (						metaclass=lowerCAmelCase_							):
     A_											=			["flax"]





     def __init__(     self     :					str     ,							*__lowerCamelCase     :					int     ,							**__lowerCamelCase     :					Dict			):
           requires_backends(self     ,							['flax']			)





     @classmethod
     def 			_lowerCAmelCase      (     cls     :					List[str]     ,							*__lowerCamelCase     :					Optional[int]     ,							**__lowerCamelCase     :					Tuple			):
           requires_backends(cls     ,							['flax']			)





     @classmethod
     def 			_lowerCAmelCase      (     cls     :					Dict     ,							*__lowerCamelCase     :					Optional[int]     ,							**__lowerCamelCase     :					Optional[Any]			):
           requires_backends(cls     ,							['flax']			)
