'''simple docstring'''
import inspect
import unittest

from huggingface_hub import hf_hub_download

from transformers import ASTConfig
from transformers.testing_utils import require_torch, require_torchaudio, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_torchaudio_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
  import torch
  from torch import nn

  from transformers import ASTForAudioClassification, ASTModel
  from transformers.models.audio_spectrogram_transformer.modeling_audio_spectrogram_transformer import (
      AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
  )


if is_torchaudio_available():
  import torchaudio

  from transformers import ASTFeatureExtractor






class 			__lowercase		:







     """simple docstring"""






     def __init__(		self     ,			_lowerCamelCase     ,			_lowerCamelCase=1_3     ,			_lowerCamelCase=2     ,			_lowerCamelCase=2_4     ,			_lowerCamelCase=1_6     ,			_lowerCamelCase=True     ,			_lowerCamelCase=True     ,			_lowerCamelCase=3_2     ,			_lowerCamelCase=5     ,			_lowerCamelCase=4     ,			_lowerCamelCase=3_7     ,			_lowerCamelCase="gelu"     ,			_lowerCamelCase=0.1     ,			_lowerCamelCase=0.1     ,			_lowerCamelCase=1_0     ,			_lowerCamelCase=0.0_2     ,			_lowerCamelCase=None     ,			_lowerCamelCase=2     ,			_lowerCamelCase=2     ,			):
         __UpperCamelCase	:	List[Any]		    =  parent
         __UpperCamelCase	:	Optional[int]		    =  batch_size
         __UpperCamelCase	:	List[str]		    =  patch_size
         __UpperCamelCase	:	Dict		    =  max_length
         __UpperCamelCase	:	Dict		    =  num_mel_bins
         __UpperCamelCase	:	List[str]		    =  is_training
         __UpperCamelCase	:	Optional[int]		    =  use_labels
         __UpperCamelCase	:	int		    =  hidden_size
         __UpperCamelCase	:	Optional[Any]		    =  num_hidden_layers
         __UpperCamelCase	:	Union[str, Any]		    =  num_attention_heads
         __UpperCamelCase	:	List[Any]		    =  intermediate_size
         __UpperCamelCase	:	Optional[int]		    =  hidden_act
         __UpperCamelCase	:	Optional[int]		    =  hidden_dropout_prob
         __UpperCamelCase	:	List[Any]		    =  attention_probs_dropout_prob
         __UpperCamelCase	:	Tuple		    =  type_sequence_label_size
         __UpperCamelCase	:	Optional[Any]		    =  initializer_range
         __UpperCamelCase	:	List[Any]		    =  scope
         __UpperCamelCase	:	int		    =  frequency_stride
         __UpperCamelCase	:	str		    =  time_stride

         # in AST, the seq length equals the number of patches + 2 (we add 2 for the [CLS] and distillation tokens)
         __UpperCamelCase	:	List[Any]		    =  (self.num_mel_bins - self.patch_size) // self.frequency_stride + 1
         __UpperCamelCase	:	int		    =  (self.max_length - self.patch_size) // self.time_stride + 1
         __UpperCamelCase	:	int		    =  frequency_out_dimension * time_out_dimension
         __UpperCamelCase	:	int		    =  num_patches + 2







     def lowerCAmelCase     (		self    ):
         __UpperCamelCase	:	str		    =  floats_tensor([self.batch_size, self.max_length, self.num_mel_bins]    )

         __UpperCamelCase	:	Tuple		    =  None
         if self.use_labels:
             __UpperCamelCase	:	Tuple		    =  ids_tensor([self.batch_size]     ,			self.type_sequence_label_size    )

         __UpperCamelCase	:	Union[str, Any]		    =  self.get_config()

         return config, input_values, labels







     def lowerCAmelCase     (		self    ):
         return ASTConfig(
             patch_size=self.patch_size     ,			max_length=self.max_length     ,			num_mel_bins=self.num_mel_bins     ,			hidden_size=self.hidden_size     ,			num_hidden_layers=self.num_hidden_layers     ,			num_attention_heads=self.num_attention_heads     ,			intermediate_size=self.intermediate_size     ,			hidden_act=self.hidden_act     ,			hidden_dropout_prob=self.hidden_dropout_prob     ,			attention_probs_dropout_prob=self.attention_probs_dropout_prob     ,			is_decoder=_lowerCamelCase     ,			initializer_range=self.initializer_range     ,			frequency_stride=self.frequency_stride     ,			time_stride=self.time_stride     ,			)







     def lowerCAmelCase     (		self     ,			_lowerCamelCase     ,			_lowerCamelCase     ,			_lowerCamelCase    ):
         __UpperCamelCase	:	str		    =  ASTModel(config=_lowerCamelCase    )
         model.to(_lowerCamelCase    )
         model.eval()
         __UpperCamelCase	:	Optional[Any]		    =  model(_lowerCamelCase    )
         self.parent.assertEqual(result.last_hidden_state.shape     ,			(self.batch_size, self.seq_length, self.hidden_size)    )







     def lowerCAmelCase     (		self    ):
         __UpperCamelCase	:	List[str]		    =  self.prepare_config_and_inputs()
         (
             (
             __UpperCamelCase       
         )    ,					(
             __UpperCamelCase       
         )    ,					(
             __UpperCamelCase       
         )    ,					       
         )	:	int		    =  config_and_inputs
         __UpperCamelCase	:	Tuple		    =  {'input_values': input_values}
         return config, inputs_dict






@require_torch
class 			__lowercase		(						_lowerCamelCase			,							_lowerCamelCase			,							unittest.TestCase						):







     """simple docstring"""






     SCREAMING_SNAKE_CASE__		   =						(
         (
             ASTModel,
             ASTForAudioClassification,
         )
         if is_torch_available()
         else ()
     )
     SCREAMING_SNAKE_CASE__		   =						(
         {"audio-classification": ASTForAudioClassification, "feature-extraction": ASTModel}
         if is_torch_available()
         else {}
     )
     SCREAMING_SNAKE_CASE__		   =						False
     SCREAMING_SNAKE_CASE__		   =						False
     SCREAMING_SNAKE_CASE__		   =						False
     SCREAMING_SNAKE_CASE__		   =						False







     def lowerCAmelCase     (		self     ,			_lowerCamelCase     ,			_lowerCamelCase     ,			_lowerCamelCase     ,			_lowerCamelCase     ,			_lowerCamelCase    ):




         if pipeline_test_casse_name == "AudioClassificationPipelineTests":
             return True

         return False







     def lowerCAmelCase     (		self    ):
         __UpperCamelCase	:	Union[str, Any]		    =  ASTModelTester(self    )
         __UpperCamelCase	:	Tuple		    =  ConfigTester(self     ,			config_class=_lowerCamelCase     ,			has_text_modality=_lowerCamelCase     ,			hidden_size=3_7    )







     def lowerCAmelCase     (		self    ):
         self.config_tester.run_common_tests()







     @unittest.skip(reason='AST does not use inputs_embeds'    )
     def lowerCAmelCase     (		self    ):
         pass







     def lowerCAmelCase     (		self    ):
         __UpperCamelCase    ,					__UpperCamelCase	:	List[Any]		    =  self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
             __UpperCamelCase	:	Union[str, Any]		    =  model_class(_lowerCamelCase    )
             self.assertIsInstance(model.get_input_embeddings()     ,			(nn.Module)    )
             __UpperCamelCase	:	Optional[Any]		    =  model.get_output_embeddings()
             self.assertTrue(x is None or isinstance(_lowerCamelCase     ,			nn.Linear    )    )







     def lowerCAmelCase     (		self    ):
         __UpperCamelCase    ,					__UpperCamelCase	:	Tuple		    =  self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
             __UpperCamelCase	:	Any		    =  model_class(_lowerCamelCase    )
             __UpperCamelCase	:	List[Any]		    =  inspect.signature(model.forward    )
             # signature.parameters is an OrderedDict => so arg_names order is deterministic
             __UpperCamelCase	:	Union[str, Any]		    =  [*signature.parameters.keys()]

             __UpperCamelCase	:	List[Any]		    =  ['input_values']
             self.assertListEqual(arg_names[:1]     ,			_lowerCamelCase    )







     def lowerCAmelCase     (		self    ):
         __UpperCamelCase	:	List[Any]		    =  self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*_lowerCamelCase    )







     @slow
     def lowerCAmelCase     (		self    ):




         for model_name in AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             __UpperCamelCase	:	Optional[int]		    =  ASTModel.from_pretrained(_lowerCamelCase    )
             self.assertIsNotNone(_lowerCamelCase    )







def _UpperCamelCase (     ):




    """simple docstring"""



    __UpperCamelCase	:	Tuple		    =  hf_hub_download(
        repo_id='nielsr/audio-spectogram-transformer-checkpoint'				,     filename='sample_audio.flac'				,     repo_type='dataset'			)

    __UpperCamelCase    ,					__UpperCamelCase	:	List[Any]		    =  torchaudio.load(_a			)

    return audio, sampling_rate






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







     """simple docstring"""






     @cached_property
     def lowerCAmelCase     (		self    ):
         return (
             ASTFeatureExtractor.from_pretrained('MIT/ast-finetuned-audioset-10-10-0.4593'    )
             if is_torchaudio_available()
             else None
         )







     @slow
     def lowerCAmelCase     (		self    ):
         __UpperCamelCase	:	str		    =  self.default_feature_extractor
         __UpperCamelCase	:	Optional[int]		    =  ASTForAudioClassification.from_pretrained('MIT/ast-finetuned-audioset-10-10-0.4593'    ).to(_lowerCamelCase    )

         __UpperCamelCase	:	Union[str, Any]		    =  self.default_feature_extractor
         __UpperCamelCase    ,					__UpperCamelCase	:	str		    =  prepare_audio()
         __UpperCamelCase	:	Union[str, Any]		    =  audio.squeeze().numpy()
         __UpperCamelCase	:	str		    =  feature_extractor(_lowerCamelCase     ,			sampling_rate=_lowerCamelCase     ,			return_tensors='pt'    ).to(_lowerCamelCase    )

         # forward pass
         with torch.no_grad():
             __UpperCamelCase	:	Tuple		    =  model(**_lowerCamelCase    )

         # verify the logits
         __UpperCamelCase	:	str		    =  torch.Size((1, 5_2_7)    )
         self.assertEqual(outputs.logits.shape     ,			_lowerCamelCase    )

         __UpperCamelCase	:	Tuple		    =  torch.tensor([-0.8_7_6_0, -7.0_0_4_2, -8.6_6_0_2]    ).to(_lowerCamelCase    )

         self.assertTrue(torch.allclose(outputs.logits[0, :3]     ,			_lowerCamelCase     ,			atol=1E-4    )    )




'''simple docstring'''
from ...processing_utils import ProcessorMixin






class 			__lowercase		(						_lowerCamelCase						):







     """simple docstring"""






     SCREAMING_SNAKE_CASE__		   =						'''SpeechT5FeatureExtractor'''
     SCREAMING_SNAKE_CASE__		   =						'''SpeechT5Tokenizer'''







     def __init__(		self     ,			_lowerCamelCase     ,			_lowerCamelCase    ):
         super().__init__(_lowerCamelCase     ,			_lowerCamelCase    )







     def __call__(		self     ,			*_lowerCamelCase     ,			**_lowerCamelCase    ):
         __UpperCamelCase	:	Union[str, Any]		    =  kwargs.pop('audio'     ,			_lowerCamelCase    )
         __UpperCamelCase	:	Tuple		    =  kwargs.pop('text'     ,			_lowerCamelCase    )
         __UpperCamelCase	:	Union[str, Any]		    =  kwargs.pop('text_target'     ,			_lowerCamelCase    )
         __UpperCamelCase	:	List[Any]		    =  kwargs.pop('audio_target'     ,			_lowerCamelCase    )
         __UpperCamelCase	:	Any		    =  kwargs.pop('sampling_rate'     ,			_lowerCamelCase    )

         if audio is not None and text is not None:
             raise ValueError(
                 'Cannot process both `audio` and `text` inputs. Did you mean `audio_target` or `text_target`?'    )
         if audio_target is not None and text_target is not None:
             raise ValueError(
                 'Cannot process both `audio_target` and `text_target` inputs. Did you mean `audio` or `text`?'    )
         if audio is None and audio_target is None and text is None and text_target is None:
             raise ValueError(
                 'You need to specify either an `audio`, `audio_target`, `text`, or `text_target` input to process.'    )

         if audio is not None:
             __UpperCamelCase	:	Dict		    =  self.feature_extractor(_lowerCamelCase     ,			*_lowerCamelCase     ,			sampling_rate=_lowerCamelCase     ,			**_lowerCamelCase    )
         elif text is not None:
             __UpperCamelCase	:	int		    =  self.tokenizer(_lowerCamelCase     ,			**_lowerCamelCase    )
         else:
             __UpperCamelCase	:	List[str]		    =  None

         if audio_target is not None:
             __UpperCamelCase	:	Dict		    =  self.feature_extractor(audio_target=_lowerCamelCase     ,			*_lowerCamelCase     ,			sampling_rate=_lowerCamelCase     ,			**_lowerCamelCase    )
             __UpperCamelCase	:	Optional[int]		    =  targets['input_values']
         elif text_target is not None:
             __UpperCamelCase	:	Any		    =  self.tokenizer(_lowerCamelCase     ,			**_lowerCamelCase    )
             __UpperCamelCase	:	Optional[int]		    =  targets['input_ids']
         else:
             __UpperCamelCase	:	List[str]		    =  None

         if inputs is None:
             return targets

         if targets is not None:
             __UpperCamelCase	:	List[Any]		    =  labels

             __UpperCamelCase	:	Any		    =  targets.get('attention_mask'    )
             if decoder_attention_mask is not None:
                 __UpperCamelCase	:	int		    =  decoder_attention_mask

         return inputs







     def lowerCAmelCase     (		self     ,			*_lowerCamelCase     ,			**_lowerCamelCase    ):
         __UpperCamelCase	:	Optional[Any]		    =  kwargs.pop('input_values'     ,			_lowerCamelCase    )
         __UpperCamelCase	:	Union[str, Any]		    =  kwargs.pop('input_ids'     ,			_lowerCamelCase    )
         __UpperCamelCase	:	List[Any]		    =  kwargs.pop('labels'     ,			_lowerCamelCase    )

         if input_values is not None and input_ids is not None:
             raise ValueError('Cannot process both `input_values` and `input_ids` inputs.'    )
         if input_values is None and input_ids is None and labels is None:
             raise ValueError(
                 'You need to specify either an `input_values`, `input_ids`, or `labels` input to be padded.'    )

         if input_values is not None:
             __UpperCamelCase	:	Optional[int]		    =  self.feature_extractor.pad(_lowerCamelCase     ,			*_lowerCamelCase     ,			**_lowerCamelCase    )
         elif input_ids is not None:
             __UpperCamelCase	:	int		    =  self.tokenizer.pad(_lowerCamelCase     ,			**_lowerCamelCase    )
         else:
             __UpperCamelCase	:	str		    =  None

         if labels is not None:
             if "input_ids" in labels or (isinstance(_lowerCamelCase     ,			_lowerCamelCase    ) and "input_ids" in labels[0]):
                 __UpperCamelCase	:	Dict		    =  self.tokenizer.pad(_lowerCamelCase     ,			**_lowerCamelCase    )
                 __UpperCamelCase	:	str		    =  targets['input_ids']
             else:
                 __UpperCamelCase	:	int		    =  self.feature_extractor.feature_size
                 __UpperCamelCase	:	Any		    =  self.feature_extractor.num_mel_bins
                 __UpperCamelCase	:	Dict		    =  self.feature_extractor.pad(_lowerCamelCase     ,			*_lowerCamelCase     ,			**_lowerCamelCase    )
                 __UpperCamelCase	:	List[str]		    =  feature_size_hack
                 __UpperCamelCase	:	Tuple		    =  targets['input_values']
         else:
             __UpperCamelCase	:	List[str]		    =  None

         if inputs is None:
             return targets

         if targets is not None:
             __UpperCamelCase	:	Optional[int]		    =  labels

             __UpperCamelCase	:	Union[str, Any]		    =  targets.get('attention_mask'    )
             if decoder_attention_mask is not None:
                 __UpperCamelCase	:	Optional[Any]		    =  decoder_attention_mask

         return inputs







     def lowerCAmelCase     (		self     ,			*_lowerCamelCase     ,			**_lowerCamelCase    ):
         return self.tokenizer.batch_decode(*_lowerCamelCase     ,			**_lowerCamelCase    )







     def lowerCAmelCase     (		self     ,			*_lowerCamelCase     ,			**_lowerCamelCase    ):
         return self.tokenizer.decode(*_lowerCamelCase     ,			**_lowerCamelCase    )



