

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


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

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


 def _UpperCamelCase	(						self		: Optional[Any]    )    ->      Union[str, Any]:
   UpperCAmelCase					      =  floats_tensor([self.batch_size, self.max_length, self.num_mel_bins]    )

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

   UpperCAmelCase					      =  self.get_config()

   return config, input_values, labels


 def _UpperCamelCase	(						self		: Any    )    ->      Tuple:
   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 _UpperCamelCase	(						self		: Optional[int]			,						lowerCAmelCase__		: Any			,						lowerCAmelCase__		: str			,						lowerCAmelCase__		: Optional[Any]    )    ->      str:
   UpperCAmelCase					      =  ASTModel(config=lowerCAmelCase_    )
   model.to(lowerCAmelCase_    )
   model.eval()
   UpperCAmelCase					      =  model(lowerCAmelCase_    )
   self.parent.assertEqual(result.last_hidden_state.shape			,						(self.batch_size, self.seq_length, self.hidden_size)    )


 def _UpperCamelCase	(						self		: int    )    ->      Optional[int]:
   UpperCAmelCase					      =  self.prepare_config_and_inputs()
   (
       (
       UpperCAmelCase						
   )      ,	(
       UpperCAmelCase						
   )      ,	(
       UpperCAmelCase						
   )      ,							
   )					      =  config_and_inputs
   UpperCAmelCase					      =  {"input_values": input_values}
   return config, inputs_dict






@require_torch
class 						__magic_name__    (       __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 _UpperCamelCase	(						self		: Any			,						lowerCAmelCase__		: Tuple			,						lowerCAmelCase__		: int			,						lowerCAmelCase__		: int			,						lowerCAmelCase__		: Any			,						lowerCAmelCase__		: Any    )    ->      Union[str, Any]:
   if pipeline_test_casse_name == "AudioClassificationPipelineTests":
     return True

   return False


 def _UpperCamelCase	(						self		: Any    )    ->      Union[str, Any]:
   UpperCAmelCase					      =  ASTModelTester(self    )
   UpperCAmelCase					      =  ConfigTester(self			,						config_class=lowerCAmelCase_			,						has_text_modality=lowerCAmelCase_			,						hidden_size=3_7    )


 def _UpperCamelCase	(						self		: List[str]    )    ->      int:
   self.config_tester.run_common_tests()


 @unittest.skip(reason="AST does not use inputs_embeds"    )
 def _UpperCamelCase	(						self		: Any    )    ->      Union[str, Any]:
   pass


 def _UpperCamelCase	(						self		: Optional[Any]    )    ->      Any:
   UpperCAmelCase      ,	UpperCAmelCase					      =  self.model_tester.prepare_config_and_inputs_for_common()

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


 def _UpperCamelCase	(						self		: Union[str, Any]    )    ->      Any:
   UpperCAmelCase      ,	UpperCAmelCase					      =  self.model_tester.prepare_config_and_inputs_for_common()

   for model_class in self.all_model_classes:
     UpperCAmelCase					      =  model_class(lowerCAmelCase_    )
     UpperCAmelCase					      =  inspect.signature(model.forward    )
     # signature.parameters is an OrderedDict => so arg_names order is deterministic
     UpperCAmelCase					      =  [*signature.parameters.keys()]

     UpperCAmelCase					      =  ["input_values"]
     self.assertListEqual(arg_names[:1]			,						lowerCAmelCase_    )


 def _UpperCamelCase	(						self		: List[Any]    )    ->      List[Any]:
   UpperCAmelCase					      =  self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_model(*lowerCAmelCase_    )


 @slow
 def _UpperCamelCase	(						self		: Tuple    )    ->      Optional[Any]:
   for model_name in AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
     UpperCAmelCase					      =  ASTModel.from_pretrained(lowerCAmelCase_    )
     self.assertIsNotNone(lowerCAmelCase_    )

def      _lowerCAmelCase(	):
  UpperCAmelCase					      =  hf_hub_download(
      repo_id="nielsr/audio-spectogram-transformer-checkpoint"   ,       filename="sample_audio.flac"   ,       repo_type="dataset"			)

  UpperCAmelCase      ,	UpperCAmelCase					      =  torchaudio.load(snake_case__			)

  return audio, sampling_rate






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


 @cached_property
 def _UpperCamelCase	(						self		: Optional[int]    )    ->      List[str]:
   return (
       ASTFeatureExtractor.from_pretrained("MIT/ast-finetuned-audioset-10-10-0.4593"    )
       if is_torchaudio_available()
       else None
   )


 @slow
 def _UpperCamelCase	(						self		: Dict    )    ->      List[str]:
   UpperCAmelCase					      =  self.default_feature_extractor
   UpperCAmelCase					      =  ASTForAudioClassification.from_pretrained("MIT/ast-finetuned-audioset-10-10-0.4593"    ).to(lowerCAmelCase_    )

   UpperCAmelCase					      =  self.default_feature_extractor
   UpperCAmelCase      ,	UpperCAmelCase					      =  prepare_audio()
   UpperCAmelCase					      =  audio.squeeze().numpy()
   UpperCAmelCase					      =  feature_extractor(lowerCAmelCase_			,						sampling_rate=lowerCAmelCase_			,						return_tensors="pt"    ).to(lowerCAmelCase_    )

   # forward pass
   with torch.no_grad():
     UpperCAmelCase					      =  model(**lowerCAmelCase_    )

   # verify the logits
   UpperCAmelCase					      =  torch.Size((1, 5_2_7)    )
   self.assertEqual(outputs.logits.shape			,						lowerCAmelCase_    )

   UpperCAmelCase					      =  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    )    )






import sys
import tempfile
import unittest
import unittest.mock as mock
from pathlib import Path

from huggingface_hub import HfFolder, delete_repo
from requests.exceptions import HTTPError

from transformers import AutoFeatureExtractor, WavaVecaFeatureExtractor
from transformers.testing_utils import TOKEN, USER, get_tests_dir, is_staging_test


sys.path.append(str(Path(__file__).parent.parent / "utils"))

from test_module.custom_feature_extraction import CustomFeatureExtractor  # noqa E402


lowerCAmelCase__       = get_tests_dir("fixtures")






class 						__magic_name__    (       unittest.TestCase    ):


 def _UpperCamelCase	(						self		: Tuple    )    ->      Union[str, Any]:
   # A mock response for an HTTP head request to emulate server down
   UpperCAmelCase					      =  mock.Mock()
   UpperCAmelCase					      =  5_0_0
   UpperCAmelCase					      =  {}
   UpperCAmelCase					      =  HTTPError
   UpperCAmelCase					      =  {}

   # Download this model to make sure it's in the cache.
   UpperCAmelCase					      =  WavaVecaFeatureExtractor.from_pretrained("hf-internal-testing/tiny-random-wav2vec2"    )
   # Under the mock environment we get a 500 error when trying to reach the model.
   with mock.patch("requests.Session.request"			,						return_value=lowerCAmelCase__    ) as mock_head:
     UpperCAmelCase					      =  WavaVecaFeatureExtractor.from_pretrained("hf-internal-testing/tiny-random-wav2vec2"    )
     # This check we did call the fake head request
     mock_head.assert_called()


 def _UpperCamelCase	(						self		: List[Any]    )    ->      Dict:
   # This test is for deprecated behavior and can be removed in v5
   UpperCAmelCase					      =  WavaVecaFeatureExtractor.from_pretrained(
       "https://huggingface.co/hf-internal-testing/tiny-random-wav2vec2/resolve/main/preprocessor_config.json"    )






@is_staging_test
class 						__magic_name__    (       unittest.TestCase    ):


 @classmethod
 def _UpperCamelCase	(						cls		: List[str]    )    ->      List[Any]:
   UpperCAmelCase					      =  TOKEN
   HfFolder.save_token(lowerCAmelCase__    )


 @classmethod
 def _UpperCamelCase	(						cls		: Optional[int]    )    ->      Union[str, Any]:
   try:
     delete_repo(token=cls._token			,						repo_id="test-feature-extractor"    )
   except HTTPError:
     pass

   try:
     delete_repo(token=cls._token			,						repo_id="valid_org/test-feature-extractor-org"    )
   except HTTPError:
     pass

   try:
     delete_repo(token=cls._token			,						repo_id="test-dynamic-feature-extractor"    )
   except HTTPError:
     pass


 def _UpperCamelCase	(						self		: Any    )    ->      Any:
   UpperCAmelCase					      =  WavaVecaFeatureExtractor.from_pretrained(lowerCAmelCase__    )
   feature_extractor.push_to_hub("test-feature-extractor"			,						use_auth_token=self._token    )

   UpperCAmelCase					      =  WavaVecaFeatureExtractor.from_pretrained(f"{USER}/test-feature-extractor"    )
   for k, v in feature_extractor.__dict__.items():
     self.assertEqual(lowerCAmelCase__			,						getattr(lowerCAmelCase__			,						lowerCAmelCase__    )    )

   # Reset repo
   delete_repo(token=self._token			,						repo_id="test-feature-extractor"    )

   # Push to hub via save_pretrained
   with tempfile.TemporaryDirectory() as tmp_dir:
     feature_extractor.save_pretrained(
         lowerCAmelCase__			,						repo_id="test-feature-extractor"			,						push_to_hub=lowerCAmelCase__			,						use_auth_token=self._token    )

   UpperCAmelCase					      =  WavaVecaFeatureExtractor.from_pretrained(f"{USER}/test-feature-extractor"    )
   for k, v in feature_extractor.__dict__.items():
     self.assertEqual(lowerCAmelCase__			,						getattr(lowerCAmelCase__			,						lowerCAmelCase__    )    )


 def _UpperCamelCase	(						self		: List[Any]    )    ->      Tuple:
   UpperCAmelCase					      =  WavaVecaFeatureExtractor.from_pretrained(lowerCAmelCase__    )
   feature_extractor.push_to_hub("valid_org/test-feature-extractor"			,						use_auth_token=self._token    )

   UpperCAmelCase					      =  WavaVecaFeatureExtractor.from_pretrained("valid_org/test-feature-extractor"    )
   for k, v in feature_extractor.__dict__.items():
     self.assertEqual(lowerCAmelCase__			,						getattr(lowerCAmelCase__			,						lowerCAmelCase__    )    )

   # Reset repo
   delete_repo(token=self._token			,						repo_id="valid_org/test-feature-extractor"    )

   # Push to hub via save_pretrained
   with tempfile.TemporaryDirectory() as tmp_dir:
     feature_extractor.save_pretrained(
         lowerCAmelCase__			,						repo_id="valid_org/test-feature-extractor-org"			,						push_to_hub=lowerCAmelCase__			,						use_auth_token=self._token    )

   UpperCAmelCase					      =  WavaVecaFeatureExtractor.from_pretrained("valid_org/test-feature-extractor-org"    )
   for k, v in feature_extractor.__dict__.items():
     self.assertEqual(lowerCAmelCase__			,						getattr(lowerCAmelCase__			,						lowerCAmelCase__    )    )


 def _UpperCamelCase	(						self		: Dict    )    ->      List[str]:
   CustomFeatureExtractor.register_for_auto_class()
   UpperCAmelCase					      =  CustomFeatureExtractor.from_pretrained(lowerCAmelCase__    )

   feature_extractor.push_to_hub("test-dynamic-feature-extractor"			,						use_auth_token=self._token    )

   # This has added the proper auto_map field to the config
   self.assertDictEqual(
       feature_extractor.auto_map			,						{"AutoFeatureExtractor": "custom_feature_extraction.CustomFeatureExtractor"}			,						)

   UpperCAmelCase					      =  AutoFeatureExtractor.from_pretrained(
       f"{USER}/test-dynamic-feature-extractor"			,						trust_remote_code=lowerCAmelCase__    )
   # Can't make an isinstance check because the new_feature_extractor is from the CustomFeatureExtractor class of a dynamic module
   self.assertEqual(new_feature_extractor.__class__.__name__			,						"CustomFeatureExtractor"    )



