


'''simple docstring'''

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

import datasets
import numpy as np
import tensorflow as tf

from transformers import (
    AutoConfig,
    AutoTokenizer,
    EvalPrediction,
    HfArgumentParser,
    PreTrainedTokenizer,
    TFAutoModelForSequenceClassification,
    TFTrainer,
    TFTrainingArguments,
)
from transformers.utils import logging as hf_logging


hf_logging.set_verbosity_info()
hf_logging.enable_default_handler()
hf_logging.enable_explicit_format()






def __UpperCamelCase   (			__lowerCamelCase		: str      ,  __lowerCamelCase		: str      ,  __lowerCamelCase		: str      ,  __lowerCamelCase		: PreTrainedTokenizer      ,  __lowerCamelCase		: int      ,  __lowerCamelCase		: Optional[int] = None      ,  )      ->     Tuple:







     '''simple docstring'''






     _a						=		{}

     if train_file is not None:
          _a						=		[train_file]
     if eval_file is not None:
          _a						=		[eval_file]
     if test_file is not None:
          _a						=		[test_file]

     _a						=		datasets.load_dataset("csv"      ,  data_files=__lowerCamelCase				)
     _a						=		list(ds[list(files.keys()				)[0]].features.keys()				)
     _a						=		features_name.pop(__lowerCamelCase				)
     _a						=		list(set(ds[list(files.keys()				)[0]][label_name]				)				)
     _a						=		{label: i for i, label in enumerate(__lowerCamelCase				)}
     _a						=		tokenizer.model_input_names
     _a						=		{}

     if len(__lowerCamelCase				) == 1:
          for k in files.keys():
               _a						=		ds[k].map(
                   lambda __lowerCamelCase				: tokenizer.batch_encode_plus(
                       example[features_name[0]]      ,  truncation=__lowerCamelCase      ,  max_length=__lowerCamelCase      ,  padding="max_length"				)      ,  batched=__lowerCamelCase      ,  )
     elif len(__lowerCamelCase				) == 2:
          for k in files.keys():
               _a						=		ds[k].map(
                   lambda __lowerCamelCase				: tokenizer.batch_encode_plus(
                       (example[features_name[0]], example[features_name[1]])      ,  truncation=__lowerCamelCase      ,  max_length=__lowerCamelCase      ,  padding="max_length"      ,  )      ,  batched=__lowerCamelCase      ,  )

     def gen_train():
          for ex in transformed_ds[datasets.Split.TRAIN]:
               _a						=		{k: v for k, v in ex.items() if k in input_names}
               _a						=		labelaid[ex[label_name]]
               yield (d, label)

     def gen_val():
          for ex in transformed_ds[datasets.Split.VALIDATION]:
               _a						=		{k: v for k, v in ex.items() if k in input_names}
               _a						=		labelaid[ex[label_name]]
               yield (d, label)

     def gen_test():
          for ex in transformed_ds[datasets.Split.TEST]:
               _a						=		{k: v for k, v in ex.items() if k in input_names}
               _a						=		labelaid[ex[label_name]]
               yield (d, label)

     _a						=		(
         tf.data.Dataset.from_generator(
             __lowerCamelCase      ,  ({k: tf.intaa for k in input_names}, tf.intaa)      ,  ({k: tf.TensorShape([None]				) for k in input_names}, tf.TensorShape([]				))      ,  )
         if datasets.Split.TRAIN in transformed_ds
         else None
     )

     if train_ds is not None:
          _a						=		train_ds.apply(tf.data.experimental.assert_cardinality(len(ds[datasets.Split.TRAIN]				)				)				)

     _a						=		(
         tf.data.Dataset.from_generator(
             __lowerCamelCase      ,  ({k: tf.intaa for k in input_names}, tf.intaa)      ,  ({k: tf.TensorShape([None]				) for k in input_names}, tf.TensorShape([]				))      ,  )
         if datasets.Split.VALIDATION in transformed_ds
         else None
     )

     if val_ds is not None:
          _a						=		val_ds.apply(tf.data.experimental.assert_cardinality(len(ds[datasets.Split.VALIDATION]				)				)				)

     _a						=		(
         tf.data.Dataset.from_generator(
             __lowerCamelCase      ,  ({k: tf.intaa for k in input_names}, tf.intaa)      ,  ({k: tf.TensorShape([None]				) for k in input_names}, tf.TensorShape([]				))      ,  )
         if datasets.Split.TEST in transformed_ds
         else None
     )

     if test_ds is not None:
          _a						=		test_ds.apply(tf.data.experimental.assert_cardinality(len(ds[datasets.Split.TEST]				)				)				)

     return train_ds, val_ds, test_ds, labelaid


lowercase__				     =		logging.getLogger(__name__)





@dataclass
class 						__SCREAMING_SNAKE_CASE					:
     UpperCAmelCase 					=				field(metadata={'''help''': '''Which column contains the label'''} )
     UpperCAmelCase 					=				field(default=lowerCamelCase__ ,			metadata={'''help''': '''The path of the training file'''} )
     UpperCAmelCase 					=				field(default=lowerCamelCase__ ,			metadata={'''help''': '''The path of the development file'''} )
     UpperCAmelCase 					=				field(default=lowerCamelCase__ ,			metadata={'''help''': '''The path of the test file'''} )
     UpperCAmelCase 					=				field(
         default=128 ,			metadata={
             '''help''': (
                 '''The maximum total input sequence length after tokenization. Sequences longer '''
                 '''than this will be truncated, sequences shorter will be padded.'''
             )
         } ,			)
     UpperCAmelCase 					=				field(
         default=lowerCamelCase__ ,			metadata={'''help''': '''Overwrite the cached training and evaluation sets'''} )







@dataclass
class 						__SCREAMING_SNAKE_CASE					:
     UpperCAmelCase 					=				field(
         metadata={'''help''': '''Path to pretrained model or model identifier from huggingface.co/models'''} )
     UpperCAmelCase 					=				field(
         default=lowerCamelCase__ ,			metadata={'''help''': '''Pretrained config name or path if not the same as model_name'''} )
     UpperCAmelCase 					=				field(
         default=lowerCamelCase__ ,			metadata={'''help''': '''Pretrained tokenizer name or path if not the same as model_name'''} )
     UpperCAmelCase 					=				field(default=lowerCamelCase__ ,			metadata={'''help''': '''Set this flag to use fast tokenization.'''} )
     # If you want to tweak more attributes on your tokenizer, you should do it in a distinct script,
     # or just modify its tokenizer_config.json.
     UpperCAmelCase 					=				field(
         default=lowerCamelCase__ ,			metadata={'''help''': '''Where do you want to store the pretrained models downloaded from huggingface.co'''} ,			)







def __UpperCamelCase   (			)      ->     str:







     '''simple docstring'''






     _a						=		HfArgumentParser((ModelArguments, DataTrainingArguments, TFTrainingArguments)				)
     _a			,			_a			,			_a						=		parser.parse_args_into_dataclasses()

     if (
         os.path.exists(training_args.output_dir				)
         and os.listdir(training_args.output_dir				)
         and training_args.do_train
         and not training_args.overwrite_output_dir
     ):
          raise ValueError(
              F"Output directory ({training_args.output_dir}) already exists and is not empty. Use"
              " --overwrite_output_dir to overcome."				)

     # Setup logging
     logging.basicConfig(
         format="%(asctime)s - %(levelname)s - %(name)s - %(message)s"      ,  datefmt="%m/%d/%Y %H:%M:%S"      ,  level=logging.INFO      ,  )
     logger.info(
         F"n_replicas: {training_args.n_replicas}, distributed training: {bool(training_args.n_replicas > 1				)}, "
         F"16-bits training: {training_args.fpaa}"				)
     logger.info(F"Training/evaluation parameters {training_args}"				)

     # Load pretrained model and tokenizer
     #
     # Distributed training:
     # The .from_pretrained methods guarantee that only one local process can concurrently
     # download model & vocab.

     _a						=		AutoTokenizer.from_pretrained(
         model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path      ,  cache_dir=model_args.cache_dir      ,  )

     _a			,			_a			,			_a			,			_a						=		get_tfds(
         train_file=data_args.train_file      ,  eval_file=data_args.dev_file      ,  test_file=data_args.test_file      ,  tokenizer=__lowerCamelCase      ,  label_column_id=data_args.label_column_id      ,  max_seq_length=data_args.max_seq_length      ,  )

     _a						=		AutoConfig.from_pretrained(
         model_args.config_name if model_args.config_name else model_args.model_name_or_path      ,  num_labels=len(__lowerCamelCase				)      ,  labelaid=__lowerCamelCase      ,  idalabel={id: label for label, id in labelaid.items()}      ,  finetuning_task="text-classification"      ,  cache_dir=model_args.cache_dir      ,  )

     with training_args.strategy.scope():
          _a						=		TFAutoModelForSequenceClassification.from_pretrained(
              model_args.model_name_or_path      ,  from_pt=bool(".bin" in model_args.model_name_or_path				)      ,  config=__lowerCamelCase      ,  cache_dir=model_args.cache_dir      ,  )

     def compute_metrics(__lowerCamelCase		: EvalPrediction				) -> Dict:
          _a						=		np.argmax(p.predictions      ,  axis=1				)

          return {"acc": (preds == p.label_ids).mean()}

     # Initialize our Trainer
     _a						=		TFTrainer(
         model=__lowerCamelCase      ,  args=__lowerCamelCase      ,  train_dataset=__lowerCamelCase      ,  eval_dataset=__lowerCamelCase      ,  compute_metrics=__lowerCamelCase      ,  )

     # Training
     if training_args.do_train:
          trainer.train()
          trainer.save_model()
          tokenizer.save_pretrained(training_args.output_dir				)

     # Evaluation
     _a						=		{}
     if training_args.do_eval:
          logger.info("*** Evaluate ***"				)
          _a						=		trainer.evaluate()
          _a						=		os.path.join(training_args.output_dir      ,  "eval_results.txt"				)

          with open(__lowerCamelCase      ,  "w"				) as writer:
               logger.info("***** Eval results *****"				)

               for key, value in result.items():
                    logger.info(F"  {key} = {value}"				)
                    writer.write(F"{key} = {value}\n"				)

               results.update(__lowerCamelCase				)

     return results


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






     def __init__(			self     ,							__UpperCamelCase     ,							__UpperCamelCase=13     ,							__UpperCamelCase=2     ,							__UpperCamelCase=24     ,							__UpperCamelCase=16     ,							__UpperCamelCase=True     ,							__UpperCamelCase=True     ,							__UpperCamelCase=32     ,							__UpperCamelCase=5     ,							__UpperCamelCase=4     ,							__UpperCamelCase=37     ,							__UpperCamelCase="gelu"     ,							__UpperCamelCase=0.1     ,							__UpperCamelCase=0.1     ,							__UpperCamelCase=10     ,							__UpperCamelCase=0.02     ,							__UpperCamelCase=None     ,							__UpperCamelCase=2     ,							__UpperCamelCase=2     ,							)       ->    Optional[int]:
          _a						=		parent
          _a						=		batch_size
          _a						=		patch_size
          _a						=		max_length
          _a						=		num_mel_bins
          _a						=		is_training
          _a						=		use_labels
          _a						=		hidden_size
          _a						=		num_hidden_layers
          _a						=		num_attention_heads
          _a						=		intermediate_size
          _a						=		hidden_act
          _a						=		hidden_dropout_prob
          _a						=		attention_probs_dropout_prob
          _a						=		type_sequence_label_size
          _a						=		initializer_range
          _a						=		scope
          _a						=		frequency_stride
          _a						=		time_stride

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






     def  a_					(			self		)       ->    List[str]:
          _a						=		floats_tensor([self.batch_size, self.max_length, self.num_mel_bins]		)

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

          _a						=		self.get_config()

          return config, input_values, labels






     def  a_					(			self		)       ->    Optional[int]:
          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=__UpperCamelCase     ,							initializer_range=self.initializer_range     ,							frequency_stride=self.frequency_stride     ,							time_stride=self.time_stride     ,							)






     def  a_					(			self     ,							__UpperCamelCase     ,							__UpperCamelCase     ,							__UpperCamelCase		)       ->    Optional[Any]:
          _a						=		ASTModel(config=__UpperCamelCase		)
          model.to(__UpperCamelCase		)
          model.eval()
          _a						=		model(__UpperCamelCase		)
          self.parent.assertEqual(result.last_hidden_state.shape     ,							(self.batch_size, self.seq_length, self.hidden_size)		)






     def  a_					(			self		)       ->    str:
          _a						=		self.prepare_config_and_inputs()
          (
              (
              _a
          )			,			(
              _a
          )			,			(
              _a
          )			,			
          )						=		config_and_inputs
          _a						=		{"input_values": input_values}
          return config, inputs_dict







@require_torch
class 						__SCREAMING_SNAKE_CASE					(      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  a_					(			self     ,							__UpperCamelCase     ,							__UpperCamelCase     ,							__UpperCamelCase     ,							__UpperCamelCase     ,							__UpperCamelCase		)       ->    List[str]:
          if pipeline_test_casse_name == "AudioClassificationPipelineTests":
               return True

          return False






     def  a_					(			self		)       ->    int:
          _a						=		ASTModelTester(self		)
          _a						=		ConfigTester(self     ,							config_class=__UpperCamelCase     ,							has_text_modality=__UpperCamelCase     ,							hidden_size=37		)






     def  a_					(			self		)       ->    str:
          self.config_tester.run_common_tests()






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






     def  a_					(			self		)       ->    List[Any]:
          _a			,			_a						=		self.model_tester.prepare_config_and_inputs_for_common()

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






     def  a_					(			self		)       ->    Any:
          _a			,			_a						=		self.model_tester.prepare_config_and_inputs_for_common()

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

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






     def  a_					(			self		)       ->    int:
          _a						=		self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*__UpperCamelCase		)






     @slow
     def  a_					(			self		)       ->    Optional[Any]:
          for model_name in AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               _a						=		ASTModel.from_pretrained(__UpperCamelCase		)
               self.assertIsNotNone(__UpperCamelCase		)







def __UpperCamelCase   (			)      ->     Dict:







     '''simple docstring'''






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

     _a			,			_a						=		torchaudio.load(__lowerCamelCase				)

     return audio, sampling_rate






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






     @cached_property
     def  a_					(			self		)       ->    int:
          return (
              ASTFeatureExtractor.from_pretrained("MIT/ast-finetuned-audioset-10-10-0.4593"		)
              if is_torchaudio_available()
              else None
          )






     @slow
     def  a_					(			self		)       ->    Dict:
          _a						=		self.default_feature_extractor
          _a						=		ASTForAudioClassification.from_pretrained("MIT/ast-finetuned-audioset-10-10-0.4593"		).to(__UpperCamelCase		)

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

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

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

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

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

