




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 			_UpperCamelCase      :
      def __init__(			self   :Dict ,      lowerCamelCase   :List[str] ,      lowerCamelCase   :Union[str, Any]=13 ,      lowerCamelCase   :Optional[int]=2 ,      lowerCamelCase   :Tuple=24 ,      lowerCamelCase   :Union[str, Any]=16 ,      lowerCamelCase   :List[Any]=True ,      lowerCamelCase   :int=True ,      lowerCamelCase   :int=32 ,      lowerCamelCase   :Optional[Any]=5 ,      lowerCamelCase   :List[str]=4 ,      lowerCamelCase   :Optional[Any]=37 ,      lowerCamelCase   :Any="gelu" ,      lowerCamelCase   :Dict=0.1 ,      lowerCamelCase   :int=0.1 ,      lowerCamelCase   :List[str]=10 ,      lowerCamelCase   :List[str]=0.02 ,      lowerCamelCase   :List[str]=None ,      lowerCamelCase   :Union[str, Any]=2 ,      lowerCamelCase   :int=2 ,      )     ->			Optional[Any]:
       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   :List[Any]      )     ->			List[str]:
       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   :int      )     ->			List[str]:
       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   :int ,      lowerCamelCase   :Union[str, Any] ,      lowerCamelCase   :Optional[int] ,      lowerCamelCase   :int      )     ->			Dict:
       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   :Optional[Any]      )     ->			Union[str, Any]:
       UpperCAmelCase__							 =						self.prepare_config_and_inputs()
       (
           (
           UpperCAmelCase__       
       ) , (
           UpperCAmelCase__       
       ) , (
           UpperCAmelCase__       
       ) ,        
       )							 =						config_and_inputs
       UpperCAmelCase__							 =						{"input_values": input_values}
       return config, inputs_dict



@require_torch
class 			_UpperCamelCase      (				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   :List[str] ,      lowerCamelCase   :Optional[int] ,      lowerCamelCase   :int ,      lowerCamelCase   :Union[str, Any] ,      lowerCamelCase   :Union[str, Any] ,      lowerCamelCase   :Dict      )     ->			Optional[int]:




       if pipeline_test_casse_name == "AudioClassificationPipelineTests":
        return True

       return False
      def 	UpperCAmelCase_						(			self   :Any      )     ->			Optional[Any]:
       UpperCAmelCase__							 =						ASTModelTester(self      )
       UpperCAmelCase__							 =						ConfigTester(self ,      config_class=lowerCamelCase ,      has_text_modality=lowerCamelCase ,      hidden_size=37      )
      def 	UpperCAmelCase_						(			self   :Optional[int]      )     ->			Any:
       self.config_tester.run_common_tests()
      @unittest.skip(reason="AST does not use inputs_embeds"      )
      def 	UpperCAmelCase_						(			self   :Optional[int]      )     ->			int:
       pass
      def 	UpperCAmelCase_						(			self   :str      )     ->			Union[str, 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   :str      )     ->			int:
       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   :Tuple      )     ->			List[str]:
       UpperCAmelCase__							 =						self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*lowerCamelCase      )







      @slow
      def 	UpperCAmelCase_						(			self   :Optional[Any]      )     ->			Dict:




       for model_name in AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        UpperCAmelCase__							 =						ASTModel.from_pretrained(lowerCamelCase      )
        self.assertIsNotNone(lowerCamelCase      )



def 						lowerCAmelCase		(		):




 """simple docstring"""







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

 UpperCAmelCase__ , UpperCAmelCase__							 =						torchaudio.load(_lowerCAmelCase	)

 return audio, sampling_rate


@require_torch
@require_torchaudio
class 			_UpperCamelCase      (				unittest.TestCase		):
      @cached_property
      def 	UpperCAmelCase_						(			self   :Any      )     ->			Tuple:
       return (
           ASTFeatureExtractor.from_pretrained("MIT/ast-finetuned-audioset-10-10-0.4593"      )
           if is_torchaudio_available()
           else None
       )







      @slow
      def 	UpperCAmelCase_						(			self   :List[str]      )     ->			Optional[Any]:
       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, 527)      )
       self.assertEqual(outputs.logits.shape ,      lowerCamelCase      )

       UpperCAmelCase__							 =						torch.tensor([-0.87_60, -7.00_42, -8.66_02]      ).to(lowerCamelCase      )

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











import glob
import os
import random
from string import ascii_lowercase, digits

import cva
import numpy as np

# Parrameters
_lowerCAmelCase						:   Union[str, Any]		=   (7_2_0, 1_2_8_0)  # Height, Width
_lowerCAmelCase						:   Any		=   (0.4, 0.6)  # if height or width lower than this scale, drop it.
_lowerCAmelCase						:   List[str]		=   1 / 1_0_0
_lowerCAmelCase						:   Union[str, Any]		=   ""
_lowerCAmelCase						:   Any		=   ""
_lowerCAmelCase						:   List[str]		=   ""
_lowerCAmelCase						:   Union[str, Any]		=   2_5_0



def 						lowerCAmelCase		(		):




 """simple docstring"""







 UpperCAmelCase__ , UpperCAmelCase__							 =						get_dataset(_lowerCAmelCase						,							_lowerCAmelCase	)
 for index in range(_lowerCAmelCase	):
  UpperCAmelCase__							 =						random.sample(range(len(_lowerCAmelCase	)	)						,							4	)
  UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__							 =						update_image_and_anno(
      _lowerCAmelCase						,							_lowerCAmelCase						,							_lowerCAmelCase						,							_lowerCAmelCase						,							_lowerCAmelCase						,							filter_scale=_lowerCAmelCase						,							)

  # Get random string code: '7b7ad245cdff75241935e4dd860f3bad'
  UpperCAmelCase__							 =						random_chars(32	)
  UpperCAmelCase__							 =						path.split(os.sep	)[-1].rsplit("."						,							1	)[0]
  UpperCAmelCase__							 =						F'''{OUTPUT_DIR}/{file_name}_MOSAIC_{letter_code}'''
  cva.imwrite(F'''{file_root}.jpg'''						,							_lowerCAmelCase						,							[cva.IMWRITE_JPEG_QUALITY, 85]	)
  print(F'''Succeeded {index+1}/{NUMBER_IMAGES} with {file_name}'''	)
  UpperCAmelCase__							 =						[]
  for anno in new_annos:
   UpperCAmelCase__							 =						anno[3] - anno[1]
   UpperCAmelCase__							 =						anno[4] - anno[2]
   UpperCAmelCase__							 =						anno[1] + width / 2
   UpperCAmelCase__							 =						anno[2] + height / 2
   UpperCAmelCase__							 =						F'''{anno[0]} {x_center} {y_center} {width} {height}'''
   annos_list.append(_lowerCAmelCase	)
  with open(F'''{file_root}.txt'''						,							"w"	) as outfile:
   outfile.write("\n".join(line for line in annos_list	)	)



def 						lowerCAmelCase		(		_lowerCAmelCase							:   str						,							_lowerCAmelCase							:   str	):




 """simple docstring"""







 UpperCAmelCase__							 =						[]
 UpperCAmelCase__							 =						[]
 for label_file in glob.glob(os.path.join(_lowerCAmelCase						,							"*.txt"	)	):
  UpperCAmelCase__							 =						label_file.split(os.sep	)[-1].rsplit("."						,							1	)[0]
  with open(_lowerCAmelCase	) as in_file:
   UpperCAmelCase__							 =						in_file.readlines()
  UpperCAmelCase__							 =						os.path.join(_lowerCAmelCase						,							F'''{label_name}.jpg'''	)

  UpperCAmelCase__							 =						[]
  for obj_list in obj_lists:
   UpperCAmelCase__							 =						obj_list.rstrip("\n"	).split(" "	)
   UpperCAmelCase__							 =						float(obj[1]	) - float(obj[3]	) / 2
   UpperCAmelCase__							 =						float(obj[2]	) - float(obj[4]	) / 2
   UpperCAmelCase__							 =						float(obj[1]	) + float(obj[3]	) / 2
   UpperCAmelCase__							 =						float(obj[2]	) + float(obj[4]	) / 2

   boxes.append([int(obj[0]	), xmin, ymin, xmax, ymax]	)
  if not boxes:
   continue
  img_paths.append(_lowerCAmelCase	)
  labels.append(_lowerCAmelCase	)
 return img_paths, labels



def 						lowerCAmelCase		(		_lowerCAmelCase							:   list						,							_lowerCAmelCase							:   list						,							_lowerCAmelCase							:   list[int]						,							_lowerCAmelCase							:   tuple[int, int]						,							_lowerCAmelCase							:   tuple[float, float]						,							_lowerCAmelCase							:   float = 0.0						,							):




 """simple docstring"""







 UpperCAmelCase__							 =						np.zeros([output_size[0], output_size[1], 3]						,							dtype=np.uinta	)
 UpperCAmelCase__							 =						scale_range[0] + random.random() * (scale_range[1] - scale_range[0])
 UpperCAmelCase__							 =						scale_range[0] + random.random() * (scale_range[1] - scale_range[0])
 UpperCAmelCase__							 =						int(scale_x * output_size[1]	)
 UpperCAmelCase__							 =						int(scale_y * output_size[0]	)

 UpperCAmelCase__							 =						[]
 UpperCAmelCase__							 =						[]
 for i, index in enumerate(_lowerCAmelCase	):
  UpperCAmelCase__							 =						all_img_list[index]
  path_list.append(_lowerCAmelCase	)
  UpperCAmelCase__							 =						all_annos[index]
  UpperCAmelCase__							 =						cva.imread(_lowerCAmelCase	)
  if i == 0:  # top-left
   UpperCAmelCase__							 =						cva.resize(_lowerCAmelCase						,							(divid_point_x, divid_point_y)	)
   UpperCAmelCase__							 =						img
   for bbox in img_annos:
    UpperCAmelCase__							 =						bbox[1] * scale_x
    UpperCAmelCase__							 =						bbox[2] * scale_y
    UpperCAmelCase__							 =						bbox[3] * scale_x
    UpperCAmelCase__							 =						bbox[4] * scale_y
    new_anno.append([bbox[0], xmin, ymin, xmax, ymax]	)
  elif i == 1:  # top-right
   UpperCAmelCase__							 =						cva.resize(_lowerCAmelCase						,							(output_size[1] - divid_point_x, divid_point_y)	)
   UpperCAmelCase__							 =						img
   for bbox in img_annos:
    UpperCAmelCase__							 =						scale_x + bbox[1] * (1 - scale_x)
    UpperCAmelCase__							 =						bbox[2] * scale_y
    UpperCAmelCase__							 =						scale_x + bbox[3] * (1 - scale_x)
    UpperCAmelCase__							 =						bbox[4] * scale_y
    new_anno.append([bbox[0], xmin, ymin, xmax, ymax]	)
  elif i == 2:  # bottom-left
   UpperCAmelCase__							 =						cva.resize(_lowerCAmelCase						,							(divid_point_x, output_size[0] - divid_point_y)	)
   UpperCAmelCase__							 =						img
   for bbox in img_annos:
    UpperCAmelCase__							 =						bbox[1] * scale_x
    UpperCAmelCase__							 =						scale_y + bbox[2] * (1 - scale_y)
    UpperCAmelCase__							 =						bbox[3] * scale_x
    UpperCAmelCase__							 =						scale_y + bbox[4] * (1 - scale_y)
    new_anno.append([bbox[0], xmin, ymin, xmax, ymax]	)
  else:  # bottom-right
   UpperCAmelCase__							 =						cva.resize(
       _lowerCAmelCase						,							(output_size[1] - divid_point_x, output_size[0] - divid_point_y)	)
   UpperCAmelCase__							 =						img
   for bbox in img_annos:
    UpperCAmelCase__							 =						scale_x + bbox[1] * (1 - scale_x)
    UpperCAmelCase__							 =						scale_y + bbox[2] * (1 - scale_y)
    UpperCAmelCase__							 =						scale_x + bbox[3] * (1 - scale_x)
    UpperCAmelCase__							 =						scale_y + bbox[4] * (1 - scale_y)
    new_anno.append([bbox[0], xmin, ymin, xmax, ymax]	)

    # Remove bounding box small than scale of filter
 if filter_scale > 0:
  UpperCAmelCase__							 =						[
      anno
      for anno in new_anno
      if filter_scale < (anno[3] - anno[1]) and filter_scale < (anno[4] - anno[2])
  ]

 return output_img, new_anno, path_list[0]



def 						lowerCAmelCase		(		_lowerCAmelCase							:   int	):




 """simple docstring"""







 assert number_char > 1, "The number of character should greater than 1"
 UpperCAmelCase__							 =						ascii_lowercase + digits
 return "".join(random.choice(_lowerCAmelCase	) for _ in range(_lowerCAmelCase	)	)


if __name__ == "__main__":
  main()
  print("DONE ✅")





