

"""simple docstring"""






import logging
import os
import sys
from dataclasses import dataclass, field
from typing import Optional

import evaluate
import numpy as np
import torch
from datasets import load_dataset
from PIL import Image
from torchvision.transforms import (
    CenterCrop,
    Compose,
    Normalize,
    RandomHorizontalFlip,
    RandomResizedCrop,
    Resize,
    ToTensor,
)

import transformers
from transformers import (
    MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING,
    AutoConfig,
    AutoImageProcessor,
    AutoModelForImageClassification,
    HfArgumentParser,
    Trainer,
    TrainingArguments,
    set_seed,
)
from transformers.trainer_utils import get_last_checkpoint
from transformers.utils import check_min_version, send_example_telemetry
from transformers.utils.versions import require_version
snake_case_   :   Optional[int]        =       logging.getLogger(__name__)

# Will error if the minimal version of Transformers is not installed. Remove at your own risks.
check_min_version("""4.31.0""")

require_version("""datasets>=1.8.0""", """To fix: pip install -r examples/pytorch/image-classification/requirements.txt""")

snake_case_   :   str        =       list(MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING.keys())
snake_case_   :   Optional[int]        =       tuple(conf.model_type for conf in MODEL_CONFIG_CLASSES)



def   lowercase_	(    _lowercase       :		str	):




					'''simple docstring'''






					with open(_lowercase							,      "rb"	) as f:
										UpperCAmelCase			:   Tuple      =				Image.open(_lowercase	)
										return im.convert("RGB"	)







@dataclass
class       snake_case__  :
			SCREAMING_SNAKE_CASE__     				=						field(
			    default=lowerCAmelCase_					,  metadata={
			        '''help''': '''Name of a dataset from the hub (could be your own, possibly private dataset hosted on the hub).'''
			    }					,  )
			SCREAMING_SNAKE_CASE__     				=						field(
			    default=lowerCAmelCase_					,  metadata={'''help''': '''The configuration name of the dataset to use (via the datasets library).'''}     )
			SCREAMING_SNAKE_CASE__     				=						field(default=lowerCAmelCase_					,  metadata={'''help''': '''A folder containing the training data.'''}     )
			SCREAMING_SNAKE_CASE__     				=						field(default=lowerCAmelCase_					,  metadata={'''help''': '''A folder containing the validation data.'''}     )
			SCREAMING_SNAKE_CASE__     				=						field(
			    default=0.1_5					,  metadata={'''help''': '''Percent to split off of train for validation.'''}     )
			SCREAMING_SNAKE_CASE__     				=						field(
			    default=lowerCAmelCase_					,  metadata={
			        '''help''': (
			            '''For debugging purposes or quicker training, truncate the number of training examples to this '''
			            '''value if set.'''
			        )
			    }					,  )
			SCREAMING_SNAKE_CASE__     				=						field(
			    default=lowerCAmelCase_					,  metadata={
			        '''help''': (
			            '''For debugging purposes or quicker training, truncate the number of evaluation examples to this '''
			            '''value if set.'''
			        )
			    }					,  )







			def 			__lowerCAmelCase				(			self			: Dict  ):





								'''simple docstring'''



								if self.dataset_name is None and (self.train_dir is None and self.validation_dir is None):
													raise ValueError(
													    "You must specify either a dataset name from the hub or a train and/or validation directory."  )







@dataclass
class       snake_case__  :
			SCREAMING_SNAKE_CASE__     				=						field(
			    default='''google/vit-base-patch16-224-in21k'''					,  metadata={'''help''': '''Path to pretrained model or model identifier from huggingface.co/models'''}					,  )
			SCREAMING_SNAKE_CASE__     				=						field(
			    default=lowerCAmelCase_					,  metadata={'''help''': '''If training from scratch, pass a model type from the list: ''' + ''', '''.join(lowerCAmelCase_     )}					,  )
			SCREAMING_SNAKE_CASE__     				=						field(
			    default=lowerCAmelCase_					,  metadata={'''help''': '''Pretrained config name or path if not the same as model_name'''}     )
			SCREAMING_SNAKE_CASE__     				=						field(
			    default=lowerCAmelCase_					,  metadata={'''help''': '''Where do you want to store the pretrained models downloaded from s3'''}     )
			SCREAMING_SNAKE_CASE__     				=						field(
			    default='''main'''					,  metadata={'''help''': '''The specific model version to use (can be a branch name, tag name or commit id).'''}					,  )
			SCREAMING_SNAKE_CASE__     				=						field(default=lowerCAmelCase_					,  metadata={'''help''': '''Name or path of preprocessor config.'''}     )
			SCREAMING_SNAKE_CASE__     				=						field(
			    default=lowerCAmelCase_					,  metadata={
			        '''help''': (
			            '''Will use the token generated when running `huggingface-cli login` (necessary to use this script '''
			            '''with private models).'''
			        )
			    }					,  )
			SCREAMING_SNAKE_CASE__     				=						field(
			    default=lowerCAmelCase_					,  metadata={'''help''': '''Will enable to load a pretrained model whose head dimensions are different.'''}					,  )



def   lowercase_	(    _lowercase       :		Any	):




					'''simple docstring'''






					UpperCAmelCase			:   int      =				torch.stack([example["pixel_values"] for example in examples]	)
					UpperCAmelCase			:   Optional[int]      =				torch.tensor([example["labels"] for example in examples]	)
					return {"pixel_values": pixel_values, "labels": labels}



def   lowercase_	(    ):




					'''simple docstring'''






					UpperCAmelCase			:   Any      =				HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments)	)
					if len(sys.argv	) == 2 and sys.argv[1].endswith(".json"	):
										# If we pass only one argument to the script and it's the path to a json file,
										# let's parse it to get our arguments.
										UpperCAmelCase			:   Tuple      =				parser.parse_json_file(json_file=os.path.abspath(sys.argv[1]	)	)
					else:
										UpperCAmelCase			:   int      =				parser.parse_args_into_dataclasses()

					# Sending telemetry. Tracking the example usage helps us better allocate resources to maintain them. The
					# information sent is the one passed as arguments along with your Python/PyTorch versions.
					send_example_telemetry("run_image_classification"							,      _lowercase							,      _lowercase	)

					# Setup logging
					logging.basicConfig(
					    format="%(asctime)s - %(levelname)s - %(name)s - %(message)s"							,      datefmt="%m/%d/%Y %H:%M:%S"							,      handlers=[logging.StreamHandler(sys.stdout	)]							,      )

					if training_args.should_log:
										# The default of training_args.log_level is passive, so we set log level at info here to have that default.
										transformers.utils.logging.set_verbosity_info()

					UpperCAmelCase			:   Optional[Any]      =				training_args.get_process_log_level()
					logger.setLevel(_lowercase	)
					transformers.utils.logging.set_verbosity(_lowercase	)
					transformers.utils.logging.enable_default_handler()
					transformers.utils.logging.enable_explicit_format()

					# Log on each process the small summary:
					logger.warning(
					    F"""Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}"""
					    + F"""distributed training: {bool(training_args.local_rank != -1	)}, 16-bits training: {training_args.fpaa}"""	)
					logger.info(F"""Training/evaluation parameters {training_args}"""	)

					# Detecting last checkpoint.
					UpperCAmelCase			:   Tuple      =				None
					if os.path.isdir(training_args.output_dir	) and training_args.do_train and not training_args.overwrite_output_dir:
										UpperCAmelCase			:   Optional[int]      =				get_last_checkpoint(training_args.output_dir	)
										if last_checkpoint is None and len(os.listdir(training_args.output_dir	)	) > 0:
															raise ValueError(
															    F"""Output directory ({training_args.output_dir}) already exists and is not empty. """
															    "Use --overwrite_output_dir to overcome."	)
										elif last_checkpoint is not None and training_args.resume_from_checkpoint is None:
															logger.info(
															    F"""Checkpoint detected, resuming training at {last_checkpoint}. To avoid this behavior, change """
															    "the `--output_dir` or add `--overwrite_output_dir` to train from scratch."	)

    # Set seed before initializing model.
					set_seed(training_args.seed	)

					# Initialize our dataset and prepare it for the 'image-classification' task.
					if data_args.dataset_name is not None:
										UpperCAmelCase			:   Dict      =				load_dataset(
										    data_args.dataset_name							,      data_args.dataset_config_name							,      cache_dir=model_args.cache_dir							,      task="image-classification"							,      use_auth_token=True if model_args.use_auth_token else None							,      )
					else:
										UpperCAmelCase			:   Optional[Any]      =				{}
										if data_args.train_dir is not None:
															UpperCAmelCase			:   List[Any]      =				os.path.join(data_args.train_dir							,      "**"	)
										if data_args.validation_dir is not None:
															UpperCAmelCase			:   Optional[Any]      =				os.path.join(data_args.validation_dir							,      "**"	)
										UpperCAmelCase			:   Any      =				load_dataset(
										    "imagefolder"							,      data_files=_lowercase							,      cache_dir=model_args.cache_dir							,      task="image-classification"							,      )

					# If we don't have a validation split, split off a percentage of train as validation.
					UpperCAmelCase			:   Optional[int]      =				None if "validation" in dataset.keys() else data_args.train_val_split
					if isinstance(data_args.train_val_split							,      _lowercase	) and data_args.train_val_split > 0.0:
										UpperCAmelCase			:   Union[str, Any]      =				dataset["train"].train_test_split(data_args.train_val_split	)
										UpperCAmelCase			:   Optional[Any]      =				split["train"]
										UpperCAmelCase			:   Dict      =				split["test"]

					# Prepare label mappings.
					# We'll include these in the model's config to get human readable labels in the Inference API.
					UpperCAmelCase			:   Dict      =				dataset["train"].features["labels"].names
					UpperCAmelCase			:   List[str]      =				{}, {}
					for i, label in enumerate(_lowercase	):
										UpperCAmelCase			:   Dict      =				str(_lowercase	)
										UpperCAmelCase			:   Dict      =				label

					# Load the accuracy metric from the datasets package
					UpperCAmelCase			:   int      =				evaluate.load("accuracy"	)

					# Define our compute_metrics function. It takes an `EvalPrediction` object (a namedtuple with a
					# predictions and label_ids field) and has to return a dictionary string to float.
					def compute_metrics(_lowercase       :		List[Any]	):
										return metric.compute(predictions=np.argmax(p.predictions							,      axis=1	)							,      references=p.label_ids	)

					UpperCAmelCase			:   List[str]      =				AutoConfig.from_pretrained(
					    model_args.config_name or model_args.model_name_or_path							,      num_labels=len(_lowercase	)							,      labelaid=_lowercase							,      idalabel=_lowercase							,      finetuning_task="image-classification"							,      cache_dir=model_args.cache_dir							,      revision=model_args.model_revision							,      use_auth_token=True if model_args.use_auth_token else None							,      )
					UpperCAmelCase			:   Optional[int]      =				AutoModelForImageClassification.from_pretrained(
					    model_args.model_name_or_path							,      from_tf=bool(".ckpt" in model_args.model_name_or_path	)							,      config=_lowercase							,      cache_dir=model_args.cache_dir							,      revision=model_args.model_revision							,      use_auth_token=True if model_args.use_auth_token else None							,      ignore_mismatched_sizes=model_args.ignore_mismatched_sizes							,      )
					UpperCAmelCase			:   Any      =				AutoImageProcessor.from_pretrained(
					    model_args.image_processor_name or model_args.model_name_or_path							,      cache_dir=model_args.cache_dir							,      revision=model_args.model_revision							,      use_auth_token=True if model_args.use_auth_token else None							,      )

					# Define torchvision transforms to be applied to each image.
					if "shortest_edge" in image_processor.size:
										UpperCAmelCase			:   Optional[Any]      =				image_processor.size["shortest_edge"]
					else:
										UpperCAmelCase			:   Any      =				(image_processor.size["height"], image_processor.size["width"])
					UpperCAmelCase			:   Union[str, Any]      =				Normalize(mean=image_processor.image_mean							,      std=image_processor.image_std	)
					UpperCAmelCase			:   List[str]      =				Compose(
					    [
					        RandomResizedCrop(_lowercase	),
					        RandomHorizontalFlip(),
					        ToTensor(),
					        normalize,
					    ]	)
					UpperCAmelCase			:   Any      =				Compose(
					    [
					        Resize(_lowercase	),
					        CenterCrop(_lowercase	),
					        ToTensor(),
					        normalize,
					    ]	)

					def train_transforms(_lowercase       :		int	):
										UpperCAmelCase			:   int      =				[
										    _train_transforms(pil_img.convert("RGB"	)	) for pil_img in example_batch["image"]
										]
										return example_batch

					def val_transforms(_lowercase       :		List[str]	):
										UpperCAmelCase			:   str      =				[_val_transforms(pil_img.convert("RGB"	)	) for pil_img in example_batch["image"]]
										return example_batch

					if training_args.do_train:
										if "train" not in dataset:
															raise ValueError("--do_train requires a train dataset"	)
										if data_args.max_train_samples is not None:
															UpperCAmelCase			:   Tuple      =				(
															    dataset["train"].shuffle(seed=training_args.seed	).select(range(data_args.max_train_samples	)	)
															)
										# Set the training transforms
										dataset["train"].set_transform(_lowercase	)

					if training_args.do_eval:
										if "validation" not in dataset:
															raise ValueError("--do_eval requires a validation dataset"	)
										if data_args.max_eval_samples is not None:
															UpperCAmelCase			:   str      =				(
															    dataset["validation"].shuffle(seed=training_args.seed	).select(range(data_args.max_eval_samples	)	)
															)
										# Set the validation transforms
										dataset["validation"].set_transform(_lowercase	)

					# Initalize our trainer
					UpperCAmelCase			:   List[str]      =				Trainer(
					    model=_lowercase							,      args=_lowercase							,      train_dataset=dataset["train"] if training_args.do_train else None							,      eval_dataset=dataset["validation"] if training_args.do_eval else None							,      compute_metrics=_lowercase							,      tokenizer=_lowercase							,      data_collator=_lowercase							,      )

					# Training
					if training_args.do_train:
										UpperCAmelCase			:   Optional[int]      =				None
										if training_args.resume_from_checkpoint is not None:
															UpperCAmelCase			:   int      =				training_args.resume_from_checkpoint
										elif last_checkpoint is not None:
															UpperCAmelCase			:   List[str]      =				last_checkpoint
										UpperCAmelCase			:   Optional[Any]      =				trainer.train(resume_from_checkpoint=_lowercase	)
										trainer.save_model()
										trainer.log_metrics("train"							,      train_result.metrics	)
										trainer.save_metrics("train"							,      train_result.metrics	)
										trainer.save_state()

					# Evaluation
					if training_args.do_eval:
										UpperCAmelCase			:   Optional[int]      =				trainer.evaluate()
										trainer.log_metrics("eval"							,      _lowercase	)
										trainer.save_metrics("eval"							,      _lowercase	)

					# Write model card and (optionally) push to hub
					UpperCAmelCase			:   str      =				{
					    "finetuned_from": model_args.model_name_or_path,
					    "tasks": "image-classification",
					    "dataset": data_args.dataset_name,
					    "tags": ["image-classification", "vision"],
					}
					if training_args.push_to_hub:
										trainer.push_to_hub(**_lowercase	)
					else:
										trainer.create_model_card(**_lowercase	)


if __name__ == "__main__":
	main()


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







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





								'''simple docstring'''



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

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







			def 			__lowerCAmelCase				(			self			: Optional[Any]  ):





								'''simple docstring'''



								UpperCAmelCase			:   List[str]      =				floats_tensor([self.batch_size, self.max_length, self.num_mel_bins]  )

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

								UpperCAmelCase			:   List[str]      =				self.get_config()

								return config, input_values, labels







			def 			__lowerCAmelCase				(			self			: 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=lowercase			,  initializer_range=self.initializer_range			,  frequency_stride=self.frequency_stride			,  time_stride=self.time_stride			,  )







			def 			__lowerCAmelCase				(			self			: List[Any]			,  lowercase			: Optional[int]			,  lowercase			: Any			,  lowercase			: List[str]  ):





								'''simple docstring'''



								UpperCAmelCase			:   Tuple      =				ASTModel(config=lowercase  )
								model.to(lowercase  )
								model.eval()
								UpperCAmelCase			:   Optional[int]      =				model(lowercase  )
								self.parent.assertEqual(result.last_hidden_state.shape			,  (self.batch_size, self.seq_length, self.hidden_size)  )







			def 			__lowerCAmelCase				(			self			: Dict  ):





								'''simple docstring'''



								UpperCAmelCase			:   Union[str, Any]      =				self.prepare_config_and_inputs()
								(
								    (
								    UpperCAmelCase
								)      ,							(
								    UpperCAmelCase
								)      ,							(
								    UpperCAmelCase
								)      ,							
								)			:   Tuple      =				config_and_inputs
								UpperCAmelCase			:   int      =				{"input_values": input_values}
								return config, inputs_dict







@require_torch
class       snake_case__  ( lowerCAmelCase_					,  lowerCAmelCase_					,  unittest.TestCase     ):
			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			: Optional[Any]			,  lowercase			: Optional[Any]			,  lowercase			: Union[str, Any]			,  lowercase			: List[str]			,  lowercase			: Any			,  lowercase			: str  ):





								'''simple docstring'''



								if pipeline_test_casse_name == "AudioClassificationPipelineTests":
													return True

								return False







			def 			__lowerCAmelCase				(			self			: str  ):





								'''simple docstring'''



								UpperCAmelCase			:   int      =				ASTModelTester(self  )
								UpperCAmelCase			:   Union[str, Any]      =				ConfigTester(self			,  config_class=lowercase			,  has_text_modality=lowercase			,  hidden_size=37  )







			def 			__lowerCAmelCase				(			self			: str  ):





								'''simple docstring'''



								self.config_tester.run_common_tests()







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





								'''simple docstring'''



								pass







			def 			__lowerCAmelCase				(			self			: Dict  ):





								'''simple docstring'''



								UpperCAmelCase      ,							UpperCAmelCase			:   str      =				self.model_tester.prepare_config_and_inputs_for_common()

								for model_class in self.all_model_classes:
													UpperCAmelCase			:   Dict      =				model_class(lowercase  )
													self.assertIsInstance(model.get_input_embeddings()			,  (nn.Module)  )
													UpperCAmelCase			:   List[Any]      =				model.get_output_embeddings()
													self.assertTrue(x is None or isinstance(lowercase			,  nn.Linear  )  )







			def 			__lowerCAmelCase				(			self			: List[Any]  ):





								'''simple docstring'''



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

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

													UpperCAmelCase			:   List[str]      =				["input_values"]
													self.assertListEqual(arg_names[:1]			,  lowercase  )







			def 			__lowerCAmelCase				(			self			: Optional[int]  ):





								'''simple docstring'''



								UpperCAmelCase			:   List[str]      =				self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*lowercase  )







			@slow
			def 			__lowerCAmelCase				(			self			: str  ):





								'''simple docstring'''



								for model_name in AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													UpperCAmelCase			:   Dict      =				ASTModel.from_pretrained(lowercase  )
													self.assertIsNotNone(lowercase  )



def   lowercase_	(    ):




					'''simple docstring'''






					UpperCAmelCase			:   List[str]      =				hf_hub_download(
					    repo_id="nielsr/audio-spectogram-transformer-checkpoint"							,      filename="sample_audio.flac"							,      repo_type="dataset"	)

					UpperCAmelCase      ,							UpperCAmelCase			:   str      =				torchaudio.load(_lowercase	)

					return audio, sampling_rate







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







			@cached_property
			def 			__lowerCAmelCase				(			self			: List[Any]  ):





								'''simple docstring'''



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







			@slow
			def 			__lowerCAmelCase				(			self			: Union[str, Any]  ):





								'''simple docstring'''



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

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

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

								# verify the logits
								UpperCAmelCase			:   Dict      =				torch.Size((1, 5_27)  )
								self.assertEqual(outputs.logits.shape			,  lowercase  )

								UpperCAmelCase			:   Optional[Any]      =				torch.tensor([-0.8_7_6_0, -7.0_0_4_2, -8.6_6_0_2]  ).to(lowercase  )

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


