





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     __magic_name__     :





     """simple docstring"""



     def __init__(							self				:int	,					snake_case				:Dict	,					snake_case				:Optional[Any]=13	,					snake_case				:Any=2	,					snake_case				:int=24	,					snake_case				:Optional[int]=16	,					snake_case				:Tuple=True	,					snake_case				:int=True	,					snake_case				:str=32	,					snake_case				:Any=5	,					snake_case				:Tuple=4	,					snake_case				:int=37	,					snake_case				:List[str]="gelu"	,					snake_case				:Union[str, Any]=0.1	,					snake_case				:Optional[int]=0.1	,					snake_case				:List[str]=10	,					snake_case				:Optional[int]=0.02	,					snake_case				:Optional[int]=None	,					snake_case				:str=2	,					snake_case				:str=2	,					):





        '''simple docstring'''




        A_    : Dict								=	parent
        A_    : Optional[Any]								=	batch_size
        A_    : Any								=	patch_size
        A_    : int								=	max_length
        A_    : List[str]								=	num_mel_bins
        A_    : Tuple								=	is_training
        A_    : Tuple								=	use_labels
        A_    : Optional[Any]								=	hidden_size
        A_    : Optional[int]								=	num_hidden_layers
        A_    : Any								=	num_attention_heads
        A_    : List[str]								=	intermediate_size
        A_    : Dict								=	hidden_act
        A_    : str								=	hidden_dropout_prob
        A_    : Union[str, Any]								=	attention_probs_dropout_prob
        A_    : Tuple								=	type_sequence_label_size
        A_    : List[str]								=	initializer_range
        A_    : Any								=	scope
        A_    : Dict								=	frequency_stride
        A_    : str								=	time_stride

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



     def   SCREAMING_SNAKE_CASE						(							self				:Optional[Any]     ):





        '''simple docstring'''




        A_    : Optional[Any]								=	floats_tensor([self.batch_size, self.max_length, self.num_mel_bins]     )

        A_    : Optional[Any]								=	None
        if self.use_labels:
           A_    : List[Any]								=	ids_tensor([self.batch_size]	,					self.type_sequence_label_size     )

        A_    : Optional[int]								=	self.get_config()

        return config, input_values, labels



     def   SCREAMING_SNAKE_CASE						(							self				:int     ):





        '''simple docstring'''




        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=_a	,					initializer_range=self.initializer_range	,					frequency_stride=self.frequency_stride	,					time_stride=self.time_stride	,					)



     def   SCREAMING_SNAKE_CASE						(							self				:Dict	,					snake_case				:Optional[int]	,					snake_case				:List[str]	,					snake_case				:Any     ):





        '''simple docstring'''




        A_    : Union[str, Any]								=	ASTModel(config=_a     )
        model.to(_a     )
        model.eval()
        A_    : Optional[int]								=	model(_a     )
        self.parent.assertEqual(result.last_hidden_state.shape	,					(self.batch_size, self.seq_length, self.hidden_size)     )





     def   SCREAMING_SNAKE_CASE						(							self				:Optional[int]     ):





        '''simple docstring'''




        A_    : List[Any]								=	self.prepare_config_and_inputs()
        (
            (
            A_     
        )					,  (
            A_     
        )					,  (
            A_     
        )					,       
        )    : int								=	config_and_inputs
        A_    : Any								=	{"input_values": input_values}
        return config, inputs_dict




@require_torch
class     __magic_name__     (							__SCREAMING_SNAKE_CASE			, __SCREAMING_SNAKE_CASE			, unittest.TestCase							):





     """simple docstring"""



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



     def   SCREAMING_SNAKE_CASE						(							self				:Any	,					snake_case				:str	,					snake_case				:List[Any]	,					snake_case				:Optional[int]	,					snake_case				:Tuple	,					snake_case				:Optional[int]     ):





        '''simple docstring'''




        if pipeline_test_casse_name == "AudioClassificationPipelineTests":
           return True

        return False



     def   SCREAMING_SNAKE_CASE						(							self				:str     ):





        '''simple docstring'''




        A_    : Any								=	ASTModelTester(self     )
        A_    : Any								=	ConfigTester(self	,					config_class=_a	,					has_text_modality=_a	,					hidden_size=37     )



     def   SCREAMING_SNAKE_CASE						(							self				:str     ):





        '''simple docstring'''




        self.config_tester.run_common_tests()



     @unittest.skip(reason="AST does not use inputs_embeds"     )
     def   SCREAMING_SNAKE_CASE						(							self				:List[Any]     ):





        '''simple docstring'''




        pass



     def   SCREAMING_SNAKE_CASE						(							self				:str     ):





        '''simple docstring'''




        A_					,  A_    : List[Any]								=	self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
           A_    : List[str]								=	model_class(_a     )
           self.assertIsInstance(model.get_input_embeddings()	,					(nn.Module)     )
           A_    : str								=	model.get_output_embeddings()
           self.assertTrue(x is None or isinstance(_a	,					nn.Linear     )     )



     def   SCREAMING_SNAKE_CASE						(							self				:Dict     ):





        '''simple docstring'''




        A_					,  A_    : Optional[Any]								=	self.model_tester.prepare_config_and_inputs_for_common()

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

           A_    : Any								=	["input_values"]
           self.assertListEqual(arg_names[:1]	,					_a     )



     def   SCREAMING_SNAKE_CASE						(							self				:int     ):





        '''simple docstring'''




        A_    : Any								=	self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*_a     )





     @slow
     def   SCREAMING_SNAKE_CASE						(							self				:List[Any]     ):





        '''simple docstring'''




        for model_name in AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           A_    : Optional[Any]								=	ASTModel.from_pretrained(_a     )
           self.assertIsNotNone(_a     )




def    __snake_case  (    )       ->  int:
   A_    : str								=	hf_hub_download(
       repo_id="nielsr/audio-spectogram-transformer-checkpoint" , filename="sample_audio.flac" , repo_type="dataset"		)

   A_					,  A_    : str								=	torchaudio.load(lowerCAmelCase__		)

   return audio, sampling_rate





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





     """simple docstring"""



     @cached_property
     def   SCREAMING_SNAKE_CASE						(							self				:int     ):





        '''simple docstring'''




        return (
            ASTFeatureExtractor.from_pretrained("MIT/ast-finetuned-audioset-10-10-0.4593"     )
            if is_torchaudio_available()
            else None
        )





     @slow
     def   SCREAMING_SNAKE_CASE						(							self				:str     ):





        '''simple docstring'''




        A_    : Union[str, Any]								=	self.default_feature_extractor
        A_    : List[Any]								=	ASTForAudioClassification.from_pretrained("MIT/ast-finetuned-audioset-10-10-0.4593"     ).to(_a     )

        A_    : List[str]								=	self.default_feature_extractor
        A_					,  A_    : Any								=	prepare_audio()
        A_    : int								=	audio.squeeze().numpy()
        A_    : Dict								=	feature_extractor(_a	,					sampling_rate=_a	,					return_tensors="pt"     ).to(_a     )

        # forward pass
        with torch.no_grad():
           A_    : Tuple								=	model(**_a     )

        # verify the logits
        A_    : int								=	torch.Size((1, 527)     )
        self.assertEqual(outputs.logits.shape	,					_a     )

        A_    : int								=	torch.tensor([-0.8760, -7.0042, -8.6602]     ).to(_a     )

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




"""simple docstring"""




def        lowercase							(		lowerCAmelCase__ :     str    ,					lowerCAmelCase__ :     list[str]			) ->      str:
		__a           =			''''''
		for word_or_phrase in separated:
				if not isinstance(lowerCAmelCase__    ,					lowerCAmelCase__			):
						raise Exception('''join() accepts only strings to be joined'''			)
				joined += word_or_phrase + separator
		return joined.strip(lowerCAmelCase__			)


if __name__ == "__main__":
				from doctest import testmod

				testmod()






