





"""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 			_a						:






				def __init__(			self		:   Tuple,			lowerCAmelCase__		:   Optional[int],			lowerCAmelCase__		:   int=1_3,			lowerCAmelCase__		:   Tuple=2,			lowerCAmelCase__		:   Tuple=2_4,			lowerCAmelCase__		:   Optional[int]=1_6,			lowerCAmelCase__		:   Dict=True,			lowerCAmelCase__		:   Tuple=True,			lowerCAmelCase__		:   Dict=3_2,			lowerCAmelCase__		:   List[Any]=5,			lowerCAmelCase__		:   Optional[Any]=4,			lowerCAmelCase__		:   Optional[Any]=3_7,			lowerCAmelCase__		:   Union[str, Any]="gelu",			lowerCAmelCase__		:   Tuple=0.1,			lowerCAmelCase__		:   List[str]=0.1,			lowerCAmelCase__		:   int=1_0,			lowerCAmelCase__		:   Tuple=0.02,			lowerCAmelCase__		:   Optional[int]=None,			lowerCAmelCase__		:   int=2,			lowerCAmelCase__		:   Optional[Any]=2,			)     ->				str:




										'''simple docstring'''



										_UpperCamelCase      : Tuple         =   parent
										_UpperCamelCase      : Optional[int]         =   batch_size
										_UpperCamelCase      : List[Any]         =   patch_size
										_UpperCamelCase      : Tuple         =   max_length
										_UpperCamelCase      : Any         =   num_mel_bins
										_UpperCamelCase      : Tuple         =   is_training
										_UpperCamelCase      : List[Any]         =   use_labels
										_UpperCamelCase      : Union[str, Any]         =   hidden_size
										_UpperCamelCase      : Dict         =   num_hidden_layers
										_UpperCamelCase      : str         =   num_attention_heads
										_UpperCamelCase      : Tuple         =   intermediate_size
										_UpperCamelCase      : Tuple         =   hidden_act
										_UpperCamelCase      : List[Any]         =   hidden_dropout_prob
										_UpperCamelCase      : Dict         =   attention_probs_dropout_prob
										_UpperCamelCase      : Tuple         =   type_sequence_label_size
										_UpperCamelCase      : Union[str, Any]         =   initializer_range
										_UpperCamelCase      : List[Any]         =   scope
										_UpperCamelCase      : List[str]         =   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      : str         =   (self.num_mel_bins - self.patch_size) // self.frequency_stride + 1
										_UpperCamelCase      : Optional[int]         =   (self.max_length - self.patch_size) // self.time_stride + 1
										_UpperCamelCase      : str         =   frequency_out_dimension * time_out_dimension
										_UpperCamelCase      : Union[str, Any]         =   num_patches + 2






				def     snake_case     (			self		:   List[Any]							)     ->				Optional[Any]:




										'''simple docstring'''



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

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

										_UpperCamelCase      : List[Any]         =   self.get_config()

										return config, input_values, labels






				def     snake_case     (			self		:   Union[str, Any]							)     ->				List[str]:




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






				def     snake_case     (			self		:   List[Any],			lowerCAmelCase__		:   Union[str, Any],			lowerCAmelCase__		:   List[Any],			lowerCAmelCase__		:   Union[str, Any]							)     ->				int:




										'''simple docstring'''



										_UpperCamelCase      : Optional[int]         =   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     snake_case     (			self		:   List[str]							)     ->				int:




										'''simple docstring'''



										_UpperCamelCase      : Optional[int]         =   self.prepare_config_and_inputs()
										(
										    _UpperCamelCase							
										)      : Union[str, Any]         =   config_and_inputs
										_UpperCamelCase      : str         =   {'''input_values''': input_values}
										return config, inputs_dict




@require_torch
class 			_a						(				_lowerCAmelCase     ,    _lowerCAmelCase     ,    unittest.TestCase    ):
				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     snake_case     (			self		:   Optional[Any],			lowerCAmelCase__		:   List[Any],			lowerCAmelCase__		:   str,			lowerCAmelCase__		:   Optional[Any],			lowerCAmelCase__		:   Optional[Any],			lowerCAmelCase__		:   str							)     ->				Union[str, Any]:




										'''simple docstring'''



										if pipeline_test_casse_name == "AudioClassificationPipelineTests":
																return True

										return False






				def     snake_case     (			self		:   Dict							)     ->				int:




										'''simple docstring'''



										_UpperCamelCase      : Dict         =   ASTModelTester(self							)
										_UpperCamelCase      : Dict         =   ConfigTester(self,			config_class=lowerCAmelCase__,			has_text_modality=lowerCAmelCase__,			hidden_size=3_7							)






				def     snake_case     (			self		:   Optional[int]							)     ->				List[str]:




										'''simple docstring'''



										self.config_tester.run_common_tests()






				@unittest.skip(reason='''AST does not use inputs_embeds'''							)
				def     snake_case     (			self		:   Optional[int]							)     ->				Dict:




										'''simple docstring'''



										pass






				def     snake_case     (			self		:   Optional[Any]							)     ->				List[Any]:




										'''simple docstring'''



										_UpperCamelCase      : List[str]         =   self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes:
																_UpperCamelCase      : int         =   model_class(lowerCAmelCase__							)
																self.assertIsInstance(model.get_input_embeddings(),			(nn.Module)							)
																_UpperCamelCase      : Any         =   model.get_output_embeddings()
																self.assertTrue(x is None or isinstance(lowerCAmelCase__,			nn.Linear							)							)






				def     snake_case     (			self		:   Optional[Any]							)     ->				Tuple:




										'''simple docstring'''



										_UpperCamelCase      : Union[str, Any]         =   self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes:
																_UpperCamelCase      : Tuple         =   model_class(lowerCAmelCase__							)
																_UpperCamelCase      : Dict         =   inspect.signature(model.forward							)
																# signature.parameters is an OrderedDict => so arg_names order is deterministic
																_UpperCamelCase      : Optional[int]         =   [*signature.parameters.keys()]

																_UpperCamelCase      : Tuple         =   ['''input_values''']
																self.assertListEqual(arg_names[:1],			lowerCAmelCase__							)






				def     snake_case     (			self		:   str							)     ->				Optional[int]:




										'''simple docstring'''



										_UpperCamelCase      : List[str]         =   self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*lowerCAmelCase__							)






				@slow
				def     snake_case     (			self		:   Dict							)     ->				Dict:




										'''simple docstring'''



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





def 	a_    (							):
						_UpperCamelCase      : Union[str, Any]         =   hf_hub_download(
						    repo_id='''nielsr/audio-spectogram-transformer-checkpoint'''	,						filename='''sample_audio.flac'''	,						repo_type='''dataset'''       )

						_UpperCamelCase      : Union[str, Any]         =   torchaudio.load(_lowercase       )

						return audio, sampling_rate






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






				@cached_property
				def     snake_case     (			self		:   Optional[Any]							)     ->				List[Any]:




										'''simple docstring'''



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






				@slow
				def     snake_case     (			self		:   List[str]							)     ->				int:




										'''simple docstring'''



										_UpperCamelCase      : int         =   self.default_feature_extractor
										_UpperCamelCase      : Optional[int]         =   ASTForAudioClassification.from_pretrained('''MIT/ast-finetuned-audioset-10-10-0.4593'''							).to(lowerCAmelCase__							)

										_UpperCamelCase      : Optional[int]         =   self.default_feature_extractor
										_UpperCamelCase      : Optional[int]         =   prepare_audio()
										_UpperCamelCase      : List[str]         =   audio.squeeze().numpy()
										_UpperCamelCase      : Tuple         =   feature_extractor(lowerCAmelCase__,			sampling_rate=lowerCAmelCase__,			return_tensors='''pt'''							).to(lowerCAmelCase__							)

										# forward pass
										with torch.no_grad():
																_UpperCamelCase      : Union[str, Any]         =   model(**lowerCAmelCase__							)

										# verify the logits
										_UpperCamelCase      : Tuple         =   torch.Size((1, 5_2_7)							)
										self.assertEqual(outputs.logits.shape,			lowerCAmelCase__							)

										_UpperCamelCase      : Any         =   torch.tensor([-0.8_760, -7.0_042, -8.6_602]							).to(lowerCAmelCase__							)

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










"""simple docstring"""




from typing import List, Optional, Union

from ...image_utils import ImageInput
from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy
from ...utils import TensorType




class 			_a						(				_lowerCAmelCase    ):
				UpperCamelCase								=   ['''image_processor''', '''tokenizer''']
				UpperCamelCase								=   '''BlipImageProcessor'''
				UpperCamelCase								=   '''AutoTokenizer'''






				def __init__(			self		:   List[str],			lowerCAmelCase__		:   Optional[int],			lowerCAmelCase__		:   Optional[int]							)     ->				int:




										'''simple docstring'''



										_UpperCamelCase      : Any         =   False
										super().__init__(lowerCAmelCase__,			lowerCAmelCase__							)
										_UpperCamelCase      : Tuple         =   self.image_processor






				def __call__(			self		:   str,			lowerCAmelCase__		:   ImageInput = None,			lowerCAmelCase__		:   Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None,			lowerCAmelCase__		:   bool = True,			lowerCAmelCase__		:   Union[bool, str, PaddingStrategy] = False,			lowerCAmelCase__		:   Union[bool, str, TruncationStrategy] = None,			lowerCAmelCase__		:   Optional[int] = None,			lowerCAmelCase__		:   int = 0,			lowerCAmelCase__		:   Optional[int] = None,			lowerCAmelCase__		:   Optional[bool] = None,			lowerCAmelCase__		:   bool = False,			lowerCAmelCase__		:   bool = False,			lowerCAmelCase__		:   bool = False,			lowerCAmelCase__		:   bool = False,			lowerCAmelCase__		:   bool = False,			lowerCAmelCase__		:   bool = True,			lowerCAmelCase__		:   Optional[Union[str, TensorType]] = None,			**lowerCAmelCase__		:   Optional[Any],			)     ->				BatchEncoding:




										'''simple docstring'''



										if images is None and text is None:
																raise ValueError('''You have to specify either images or text.'''							)

										# Get only text
										if images is None:
																_UpperCamelCase      : int         =   self.tokenizer
																_UpperCamelCase      : List[str]         =   self.tokenizer(
																    text=lowerCAmelCase__,			add_special_tokens=lowerCAmelCase__,			padding=lowerCAmelCase__,			truncation=lowerCAmelCase__,			max_length=lowerCAmelCase__,			stride=lowerCAmelCase__,			pad_to_multiple_of=lowerCAmelCase__,			return_attention_mask=lowerCAmelCase__,			return_overflowing_tokens=lowerCAmelCase__,			return_special_tokens_mask=lowerCAmelCase__,			return_offsets_mapping=lowerCAmelCase__,			return_token_type_ids=lowerCAmelCase__,			return_length=lowerCAmelCase__,			verbose=lowerCAmelCase__,			return_tensors=lowerCAmelCase__,			**lowerCAmelCase__,			)
																return text_encoding

										# add pixel_values
										_UpperCamelCase      : List[str]         =   self.image_processor(lowerCAmelCase__,			return_tensors=lowerCAmelCase__							)

										if text is not None:
																_UpperCamelCase      : Any         =   self.tokenizer(
																    text=lowerCAmelCase__,			add_special_tokens=lowerCAmelCase__,			padding=lowerCAmelCase__,			truncation=lowerCAmelCase__,			max_length=lowerCAmelCase__,			stride=lowerCAmelCase__,			pad_to_multiple_of=lowerCAmelCase__,			return_attention_mask=lowerCAmelCase__,			return_overflowing_tokens=lowerCAmelCase__,			return_special_tokens_mask=lowerCAmelCase__,			return_offsets_mapping=lowerCAmelCase__,			return_token_type_ids=lowerCAmelCase__,			return_length=lowerCAmelCase__,			verbose=lowerCAmelCase__,			return_tensors=lowerCAmelCase__,			**lowerCAmelCase__,			)
										else:
																_UpperCamelCase      : List[Any]         =   None

										if text_encoding is not None:
																encoding_image_processor.update(lowerCAmelCase__							)

										return encoding_image_processor






				def     snake_case     (			self		:   List[Any],			*lowerCAmelCase__		:   List[str],			**lowerCAmelCase__		:   str							)     ->				Optional[int]:




										'''simple docstring'''



										return self.tokenizer.batch_decode(*lowerCAmelCase__,			**lowerCAmelCase__							)






				def     snake_case     (			self		:   List[Any],			*lowerCAmelCase__		:   Dict,			**lowerCAmelCase__		:   Any							)     ->				List[str]:




										'''simple docstring'''



										return self.tokenizer.decode(*lowerCAmelCase__,			**lowerCAmelCase__							)






				@property
				# Copied from transformers.models.blip.processing_blip.BlipProcessor.model_input_names
				def     snake_case     (			self		:   Any							)     ->				str:




										'''simple docstring'''



										_UpperCamelCase      : List[str]         =   self.tokenizer.model_input_names
										_UpperCamelCase      : Union[str, Any]         =   self.image_processor.model_input_names
										return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names							)							)



