


import itertools
import random
import unittest

import numpy as np

from transformers import BatchFeature, SpeechTaFeatureExtractor
from transformers.testing_utils import require_torch
from transformers.utils.import_utils import is_torch_available

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


if is_torch_available():
    import torch


__snake_case									=		random.Random()






def 						_A						(						_lowercase ,   _lowercase=1.0 ,   _lowercase=None ,   _lowercase=None      ) ->      Tuple:


     """simple docstring"""


     if rng is None:
          __UpperCamelCase      =       global_rng

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

     return values



@require_torch
class      __lowerCamelCase       (unittest.TestCase		):







       def __init__(       self:      Optional[int],A_:      Union[str, Any],A_:      Optional[int]=7,A_:      int=400,A_:      Union[str, Any]=2000,A_:      Union[str, Any]=1,A_:      Tuple=0.0,A_:      Any=1_6000,A_:      List[Any]=True,A_:      Any=80,A_:      str=16,A_:      List[str]=64,A_:      Tuple="hann_window",A_:      Optional[Any]=80,A_:      Tuple=7600,A_:      str=1E-10,A_:      str=True,):






            '''simple docstring'''




            __UpperCamelCase      =       parent
            __UpperCamelCase      =       batch_size
            __UpperCamelCase      =       min_seq_length
            __UpperCamelCase      =       max_seq_length
            __UpperCamelCase      =       (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
            __UpperCamelCase      =       feature_size
            __UpperCamelCase      =       padding_value
            __UpperCamelCase      =       sampling_rate
            __UpperCamelCase      =       do_normalize
            __UpperCamelCase      =       num_mel_bins
            __UpperCamelCase      =       hop_length
            __UpperCamelCase      =       win_length
            __UpperCamelCase      =       win_function
            __UpperCamelCase      =       fmin
            __UpperCamelCase      =       fmax
            __UpperCamelCase      =       mel_floor
            __UpperCamelCase      =       return_attention_mask







       def 						snake_case_     (       self:      List[str] ):






            '''simple docstring'''




            return {
                "feature_size": self.feature_size,
                "padding_value": self.padding_value,
                "sampling_rate": self.sampling_rate,
                "do_normalize": self.do_normalize,
                "num_mel_bins": self.num_mel_bins,
                "hop_length": self.hop_length,
                "win_length": self.win_length,
                "win_function": self.win_function,
                "fmin": self.fmin,
                "fmax": self.fmax,
                "mel_floor": self.mel_floor,
                "return_attention_mask": self.return_attention_mask,
            }







       def 						snake_case_     (       self:      Dict,A_:      str=False,A_:      Optional[Any]=False ):






            '''simple docstring'''




            def _flatten(A_:      str ):
                 return list(itertools.chain(*_A ) )

            if equal_length:
                 __UpperCamelCase      =       floats_list((self.batch_size, self.max_seq_length) )
            else:
                 # make sure that inputs increase in size
                 __UpperCamelCase      =       [
                     _flatten(floats_list((x, self.feature_size) ) )
                     for x in range(self.min_seq_length,self.max_seq_length,self.seq_length_diff )
                 ]

            if numpify:
                 __UpperCamelCase      =       [np.asarray(_A ) for x in speech_inputs]

            return speech_inputs







       def 						snake_case_     (       self:      Optional[Any],A_:      Optional[Any]=False,A_:      Tuple=False ):






            '''simple docstring'''




            if equal_length:
                 __UpperCamelCase      =       [floats_list((self.max_seq_length, self.num_mel_bins) ) for _ in range(self.batch_size )]
            else:
                 # make sure that inputs increase in size
                 __UpperCamelCase      =       [
                     floats_list((x, self.num_mel_bins) )
                     for x in range(self.min_seq_length,self.max_seq_length,self.seq_length_diff )
                 ]

            if numpify:
                 __UpperCamelCase      =       [np.asarray(_A ) for x in speech_inputs]

            return speech_inputs



@require_torch
class      __lowerCamelCase       (a_							,	unittest.TestCase		):
       _lowercase											=			SpeechTaFeatureExtractor







       def 						snake_case_     (       self:      Optional[int] ):






            '''simple docstring'''




            __UpperCamelCase      =       SpeechTaFeatureExtractionTester(self )







       def 						snake_case_     (       self:      Optional[int],A_:      Optional[int] ):






            '''simple docstring'''




            self.assertTrue(np.all(np.mean(_A,axis=0 ) < 1E-3 ) )
            self.assertTrue(np.all(np.abs(np.var(_A,axis=0 ) - 1 ) < 1E-3 ) )







       def 						snake_case_     (       self:      List[str] ):






            '''simple docstring'''




            __UpperCamelCase      =       self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() )
            # create three inputs of length 800, 1000, and 1200
            __UpperCamelCase      =       [floats_list((1, x) )[0] for x in range(800,1400,200 )]
            __UpperCamelCase      =       [np.asarray(_A ) for speech_input in speech_inputs]

            # Test not batched input
            __UpperCamelCase      =       feat_extract(speech_inputs[0],return_tensors='np' ).input_values
            __UpperCamelCase      =       feat_extract(np_speech_inputs[0],return_tensors='np' ).input_values
            self.assertTrue(np.allclose(_A,_A,atol=1E-3 ) )

            # Test batched
            __UpperCamelCase      =       feat_extract(_A,return_tensors='np' ).input_values
            __UpperCamelCase      =       feat_extract(_A,return_tensors='np' ).input_values
            for enc_seq_a, enc_seq_a in zip(_A,_A ):
                 self.assertTrue(np.allclose(_A,_A,atol=1E-3 ) )







       def 						snake_case_     (       self:      Tuple ):






            '''simple docstring'''




            __UpperCamelCase      =       self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() )
            __UpperCamelCase      =       [floats_list((1, x) )[0] for x in range(800,1400,200 )]

            __UpperCamelCase      =       ['longest', 'max_length', 'do_not_pad']
            __UpperCamelCase      =       [None, 1600, None]
            for max_length, padding in zip(_A,_A ):
                 __UpperCamelCase      =       feat_extract(_A,padding=_A,max_length=_A,return_tensors='np' )
                 __UpperCamelCase      =       processed.input_values

                 self._check_zero_mean_unit_variance(input_values[0][:800] )
                 self.assertTrue(input_values[0][800:].sum() < 1E-6 )
                 self._check_zero_mean_unit_variance(input_values[1][:1000] )
                 self.assertTrue(input_values[0][1000:].sum() < 1E-6 )
                 self._check_zero_mean_unit_variance(input_values[2][:1200] )







       def 						snake_case_     (       self:      Optional[int] ):






            '''simple docstring'''




            __UpperCamelCase      =       self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() )
            __UpperCamelCase      =       range(800,1400,200 )
            __UpperCamelCase      =       [floats_list((1, x) )[0] for x in lengths]

            __UpperCamelCase      =       ['longest', 'max_length', 'do_not_pad']
            __UpperCamelCase      =       [None, 1600, None]

            for max_length, padding in zip(_A,_A ):
                 __UpperCamelCase      =       feat_extract(_A,max_length=_A,padding=_A )
                 __UpperCamelCase      =       processed.input_values

                 self._check_zero_mean_unit_variance(input_values[0][:800] )
                 self._check_zero_mean_unit_variance(input_values[1][:1000] )
                 self._check_zero_mean_unit_variance(input_values[2][:1200] )







       def 						snake_case_     (       self:      str ):






            '''simple docstring'''




            __UpperCamelCase      =       self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() )
            __UpperCamelCase      =       [floats_list((1, x) )[0] for x in range(800,1400,200 )]
            __UpperCamelCase      =       feat_extract(
                _A,truncation=_A,max_length=1000,padding='max_length',return_tensors='np' )
            __UpperCamelCase      =       processed.input_values

            self._check_zero_mean_unit_variance(input_values[0, :800] )
            self._check_zero_mean_unit_variance(input_values[1] )
            self._check_zero_mean_unit_variance(input_values[2] )







       def 						snake_case_     (       self:      int ):






            '''simple docstring'''




            __UpperCamelCase      =       self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() )
            __UpperCamelCase      =       [floats_list((1, x) )[0] for x in range(800,1400,200 )]
            __UpperCamelCase      =       feat_extract(
                _A,truncation=_A,max_length=1000,padding='longest',return_tensors='np' )
            __UpperCamelCase      =       processed.input_values

            self._check_zero_mean_unit_variance(input_values[0, :800] )
            self._check_zero_mean_unit_variance(input_values[1, :1000] )
            self._check_zero_mean_unit_variance(input_values[2] )

            # make sure that if max_length < longest -> then pad to max_length
            self.assertTrue(input_values.shape == (3, 1000) )

            __UpperCamelCase      =       [floats_list((1, x) )[0] for x in range(800,1400,200 )]
            __UpperCamelCase      =       feat_extract(
                _A,truncation=_A,max_length=2000,padding='longest',return_tensors='np' )
            __UpperCamelCase      =       processed.input_values

            self._check_zero_mean_unit_variance(input_values[0, :800] )
            self._check_zero_mean_unit_variance(input_values[1, :1000] )
            self._check_zero_mean_unit_variance(input_values[2] )

            # make sure that if max_length > longest -> then pad to longest
            self.assertTrue(input_values.shape == (3, 1200) )







       def 						snake_case_     (       self:      Dict ):






            '''simple docstring'''




            __UpperCamelCase      =       self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() )
            __UpperCamelCase      =       np.random.rand(100 ).astype(np.floataa )
            __UpperCamelCase      =       np_speech_inputs.tolist()

            for inputs in [py_speech_inputs, np_speech_inputs]:
                 __UpperCamelCase      =       feature_extractor.pad([{'input_values': inputs}],return_tensors='np' )
                 self.assertTrue(np_processed.input_values.dtype == np.floataa )
                 __UpperCamelCase      =       feature_extractor.pad([{'input_values': inputs}],return_tensors='pt' )
                 self.assertTrue(pt_processed.input_values.dtype == torch.floataa )







       def 						snake_case_     (       self:      Dict ):






            '''simple docstring'''




            __UpperCamelCase      =       self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() )
            # create three inputs of length 800, 1000, and 1200
            __UpperCamelCase      =       [floats_list((1, x) )[0] for x in range(800,1400,200 )]
            __UpperCamelCase      =       [np.asarray(_A ) for speech_input in speech_inputs]

            # Test feature size
            __UpperCamelCase      =       feature_extractor(audio_target=_A,padding=_A,return_tensors='np' ).input_values
            self.assertTrue(input_values.ndim == 3 )
            self.assertTrue(input_values.shape[-1] == feature_extractor.num_mel_bins )

            # Test not batched input
            __UpperCamelCase      =       feature_extractor(speech_inputs[0],return_tensors='np' ).input_values
            __UpperCamelCase      =       feature_extractor(np_speech_inputs[0],return_tensors='np' ).input_values
            self.assertTrue(np.allclose(_A,_A,atol=1E-3 ) )

            # Test batched
            __UpperCamelCase      =       feature_extractor(_A,return_tensors='np' ).input_values
            __UpperCamelCase      =       feature_extractor(_A,return_tensors='np' ).input_values
            for enc_seq_a, enc_seq_a in zip(_A,_A ):
                 self.assertTrue(np.allclose(_A,_A,atol=1E-3 ) )

            # Test 2-D numpy arrays are batched.
            __UpperCamelCase      =       [floats_list((1, x) )[0] for x in (800, 800, 800)]
            __UpperCamelCase      =       np.asarray(_A )
            __UpperCamelCase      =       feature_extractor(_A,return_tensors='np' ).input_values
            __UpperCamelCase      =       feature_extractor(_A,return_tensors='np' ).input_values
            for enc_seq_a, enc_seq_a in zip(_A,_A ):
                 self.assertTrue(np.allclose(_A,_A,atol=1E-3 ) )







       def 						snake_case_     (       self:      Union[str, Any] ):






            '''simple docstring'''




            __UpperCamelCase      =       self.feat_extract_tester.prepare_inputs_for_target()
            __UpperCamelCase      =       self.feature_extraction_class(**self.feat_extract_dict )
            __UpperCamelCase      =       feat_extract.model_input_names[0]

            __UpperCamelCase      =       BatchFeature({input_name: speech_inputs} )

            self.assertTrue(all(len(_A ) == len(_A ) for x, y in zip(_A,processed_features[input_name] ) ) )

            __UpperCamelCase      =       self.feat_extract_tester.prepare_inputs_for_target(equal_length=_A )
            __UpperCamelCase      =       BatchFeature({input_name: speech_inputs},tensor_type='np' )

            __UpperCamelCase      =       processed_features[input_name]

            if len(batch_features_input.shape ) < 3:
                 __UpperCamelCase      =       batch_features_input[:, :, None]

            self.assertTrue(
                batch_features_input.shape
                == (self.feat_extract_tester.batch_size, len(speech_inputs[0] ), self.feat_extract_tester.num_mel_bins) )







       @require_torch
       def 						snake_case_     (       self:      Optional[Any] ):






            '''simple docstring'''




            __UpperCamelCase      =       self.feat_extract_tester.prepare_inputs_for_target(equal_length=_A )
            __UpperCamelCase      =       self.feature_extraction_class(**self.feat_extract_dict )
            __UpperCamelCase      =       feat_extract.model_input_names[0]

            __UpperCamelCase      =       BatchFeature({input_name: speech_inputs},tensor_type='pt' )

            __UpperCamelCase      =       processed_features[input_name]

            if len(batch_features_input.shape ) < 3:
                 __UpperCamelCase      =       batch_features_input[:, :, None]

            self.assertTrue(
                batch_features_input.shape
                == (self.feat_extract_tester.batch_size, len(speech_inputs[0] ), self.feat_extract_tester.num_mel_bins) )







       @require_torch
       def 						snake_case_     (       self:      Optional[Any] ):






            '''simple docstring'''




            __UpperCamelCase      =       self.feature_extraction_class(**self.feat_extract_dict )
            __UpperCamelCase      =       self.feat_extract_tester.prepare_inputs_for_target()
            __UpperCamelCase      =       feat_extract.model_input_names[0]

            __UpperCamelCase      =       BatchFeature({input_name: speech_inputs} )

            __UpperCamelCase      =       feat_extract.num_mel_bins  # hack!

            __UpperCamelCase      =       feat_extract.pad(_A,padding='longest',return_tensors='np' )[input_name]
            __UpperCamelCase      =       feat_extract.pad(_A,padding='longest',return_tensors='pt' )[input_name]

            self.assertTrue(abs(input_np.astype(np.floataa ).sum() - input_pt.numpy().astype(np.floataa ).sum() ) < 1E-2 )







       def 						snake_case_     (       self:      List[Any] ):






            '''simple docstring'''




            __UpperCamelCase      =       self.feat_extract_dict
            __UpperCamelCase      =       True
            __UpperCamelCase      =       self.feature_extraction_class(**_A )
            __UpperCamelCase      =       self.feat_extract_tester.prepare_inputs_for_target()
            __UpperCamelCase      =       [len(_A ) for x in speech_inputs]
            __UpperCamelCase      =       feat_extract.model_input_names[0]

            __UpperCamelCase      =       BatchFeature({input_name: speech_inputs} )

            __UpperCamelCase      =       feat_extract.num_mel_bins  # hack!

            __UpperCamelCase      =       feat_extract.pad(_A,padding='longest',return_tensors='np' )
            self.assertIn('attention_mask',_A )
            self.assertListEqual(list(processed.attention_mask.shape ),list(processed[input_name].shape[:2] ) )
            self.assertListEqual(processed.attention_mask.sum(-1 ).tolist(),_A )







       def 						snake_case_     (       self:      Optional[Any] ):






            '''simple docstring'''




            __UpperCamelCase      =       self.feat_extract_dict
            __UpperCamelCase      =       True
            __UpperCamelCase      =       self.feature_extraction_class(**_A )
            __UpperCamelCase      =       self.feat_extract_tester.prepare_inputs_for_target()
            __UpperCamelCase      =       [len(_A ) for x in speech_inputs]
            __UpperCamelCase      =       feat_extract.model_input_names[0]

            __UpperCamelCase      =       BatchFeature({input_name: speech_inputs} )
            __UpperCamelCase      =       min(_A )

            __UpperCamelCase      =       feat_extract.num_mel_bins  # hack!

            __UpperCamelCase      =       feat_extract.pad(
                _A,padding='max_length',max_length=_A,truncation=_A,return_tensors='np' )
            self.assertIn('attention_mask',_A )
            self.assertListEqual(
                list(processed_pad.attention_mask.shape ),[processed_pad[input_name].shape[0], max_length] )
            self.assertListEqual(
                processed_pad.attention_mask[:, :max_length].sum(-1 ).tolist(),[max_length for x in speech_inputs] )







       def 						snake_case_     (       self:      int,A_:      List[str] ):






            '''simple docstring'''




            from datasets import load_dataset

            __UpperCamelCase      =       load_dataset('hf-internal-testing/librispeech_asr_dummy','clean',split='validation' )
            # automatic decoding with librispeech
            __UpperCamelCase      =       ds.sort('id' ).select(range(_A ) )[:num_samples]['audio']

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







       def 						snake_case_     (       self:      Union[str, Any] ):






            '''simple docstring'''




            __UpperCamelCase      =       torch.tensor(
                [2.3_804E-03, 2.0_752E-03, 1.9_836E-03, 2.1_057E-03, 1.6_174E-03,
                 3.0_518E-04, 9.1_553E-05, 3.3_569E-04, 9.7_656E-04, 1.8_311E-03,
                 2.0_142E-03, 2.1_057E-03, 1.7_395E-03, 4.5_776E-04, -3.9_673E-04,
                 4.5_776E-04, 1.0_071E-03, 9.1_553E-05, 4.8_828E-04, 1.1_597E-03,
                 7.3_242E-04, 9.4_604E-04, 1.8_005E-03, 1.8_311E-03, 8.8_501E-04,
                 4.2_725E-04, 4.8_828E-04, 7.3_242E-04, 1.0_986E-03, 2.1_057E-03] )
            # fmt: on

            __UpperCamelCase      =       self._load_datasamples(1 )
            __UpperCamelCase      =       SpeechTaFeatureExtractor()
            __UpperCamelCase      =       feature_extractor(_A,return_tensors='pt' ).input_values
            self.assertEquals(input_values.shape,(1, 9_3680) )
            self.assertTrue(torch.allclose(input_values[0, :30],_A,atol=1E-6 ) )







       def 						snake_case_     (       self:      str ):






            '''simple docstring'''




            __UpperCamelCase      =       torch.tensor(
                [-2.6_8_7_0, -3.0_1_0_4, -3.1_3_5_6, -3.5_3_5_2, -3.0_0_4_4, -3.0_3_5_3, -3.4_7_1_9, -3.6_7_7_7,
                 -3.1_5_2_0, -2.9_4_3_5, -2.6_5_5_3, -2.8_7_9_5, -2.9_9_4_4, -2.5_9_2_1, -3.0_2_7_9, -3.0_3_8_6,
                 -3.0_8_6_4, -3.1_2_9_1, -3.2_3_5_3, -2.7_4_4_4, -2.6_8_3_1, -2.7_2_8_7, -3.1_7_6_1, -3.1_5_7_1,
                 -3.2_7_2_6, -3.0_5_8_2, -3.1_0_0_7, -3.4_5_3_3, -3.4_6_9_5, -3.0_9_9_8] )
            # fmt: on

            __UpperCamelCase      =       self._load_datasamples(1 )
            __UpperCamelCase      =       SpeechTaFeatureExtractor()
            __UpperCamelCase      =       feature_extractor(audio_target=_A,return_tensors='pt' ).input_values
            self.assertEquals(input_values.shape,(1, 366, 80) )
            self.assertTrue(torch.allclose(input_values[0, 0, :30],_A,atol=1E-4 ) )









from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_sentencepiece_available,
    is_speech_available,
    is_tf_available,
    is_torch_available,
)


__A				:    Union[str, Any]       =   {
    "configuration_speech_to_text": ["SPEECH_TO_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP", "Speech2TextConfig"],
    "processing_speech_to_text": ["Speech2TextProcessor"],
}

try:
				if not is_sentencepiece_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				__A				:    List[Any]       =   ["Speech2TextTokenizer"]

try:
				if not is_speech_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				__A				:    List[Any]       =   ["Speech2TextFeatureExtractor"]

try:
				if not is_tf_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				__A				:    int       =   [
				    "TF_SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST",
				    "TFSpeech2TextForConditionalGeneration",
				    "TFSpeech2TextModel",
				    "TFSpeech2TextPreTrainedModel",
				]

try:
				if not is_torch_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				__A				:    Tuple       =   [
				    "SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST",
				    "Speech2TextForConditionalGeneration",
				    "Speech2TextModel",
				    "Speech2TextPreTrainedModel",
				]


if TYPE_CHECKING:
				from .configuration_speech_to_text import SPEECH_TO_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP, SpeechaTextConfig
				from .processing_speech_to_text import SpeechaTextProcessor

				try:
								if not is_sentencepiece_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .tokenization_speech_to_text import SpeechaTextTokenizer

				try:
								if not is_speech_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .feature_extraction_speech_to_text import SpeechaTextFeatureExtractor

				try:
								if not is_tf_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .modeling_tf_speech_to_text import (
								    TF_SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST,
								    TFSpeechaTextForConditionalGeneration,
								    TFSpeechaTextModel,
								    TFSpeechaTextPreTrainedModel,
								)

				try:
								if not is_torch_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .modeling_speech_to_text import (
								    SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST,
								    SpeechaTextForConditionalGeneration,
								    SpeechaTextModel,
								    SpeechaTextPreTrainedModel,
								)

else:
				import sys

				__A				:    Dict       =   _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)



