





'''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 		UpperCAmelCase   :


  """simple docstring"""







  def __init__(				self    :					List[str]							,	UpperCamelCase__    :					Tuple							,	UpperCamelCase__    :					Union[str, Any]=13							,	UpperCamelCase__    :					List[Any]=2							,	UpperCamelCase__    :					List[Any]=24							,	UpperCamelCase__    :					List[str]=16							,	UpperCamelCase__    :					str=True							,	UpperCamelCase__    :					Dict=True							,	UpperCamelCase__    :					Union[str, Any]=32							,	UpperCamelCase__    :					str=5							,	UpperCamelCase__    :					Union[str, Any]=4							,	UpperCamelCase__    :					Optional[int]=37							,	UpperCamelCase__    :					Optional[Any]="gelu"							,	UpperCamelCase__    :					Optional[int]=0.1							,	UpperCamelCase__    :					Union[str, Any]=0.1							,	UpperCamelCase__    :					Optional[int]=10							,	UpperCamelCase__    :					Any=0.02							,	UpperCamelCase__    :					Any=None							,	UpperCamelCase__    :					Optional[Any]=2							,	UpperCamelCase__    :					List[str]=2							,	)     ->						Union[str, 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    :					Union[str, Any]  )     ->						List[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    :					List[str]  )     ->						Dict:
    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   UpperCamelCase__      (				self    :					List[str]							,	UpperCamelCase__    :					Union[str, Any]							,	UpperCamelCase__    :					Any							,	UpperCamelCase__    :					Union[str, Any]  )     ->						Optional[Any]:
    _UpperCamelCase       			=ASTModel(config=UpperCamelCase__  )
    model.to(UpperCamelCase__  )
    model.eval()
    _UpperCamelCase       			=model(UpperCamelCase__  )
    self.parent.assertEqual(result.last_hidden_state.shape							,	(self.batch_size, self.seq_length, self.hidden_size)  )




  def   UpperCamelCase__      (				self    :					Any  )     ->						Dict:
    _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   (				lowercase_   ,     lowercase_   ,     unittest.TestCase):


  """simple docstring"""







  lowerCAmelCase_					=				(
      (
          ASTModel,
          ASTForAudioClassification,
      )
      if is_torch_available()
      else ()
  )
  lowerCAmelCase_					=				(
      {"""audio-classification""": ASTForAudioClassification, """feature-extraction""": ASTModel}
      if is_torch_available()
      else {}
  )
  lowerCAmelCase_					=				False
  lowerCAmelCase_					=				False
  lowerCAmelCase_					=				False
  lowerCAmelCase_					=				False


  def   UpperCamelCase__      (				self    :					int							,	UpperCamelCase__    :					Optional[Any]							,	UpperCamelCase__    :					int							,	UpperCamelCase__    :					List[Any]							,	UpperCamelCase__    :					Optional[int]							,	UpperCamelCase__    :					List[Any]  )     ->						str:
    if pipeline_test_casse_name == "AudioClassificationPipelineTests":
      return True

    return False


  def   UpperCamelCase__      (				self    :					List[Any]  )     ->						Optional[Any]:
    _UpperCamelCase       			=ASTModelTester(self  )
    _UpperCamelCase       			=ConfigTester(self							,	config_class=UpperCamelCase__							,	has_text_modality=UpperCamelCase__							,	hidden_size=37  )


  def   UpperCamelCase__      (				self    :					Tuple  )     ->						Any:
    self.config_tester.run_common_tests()


  @unittest.skip(reason='''AST does not use inputs_embeds'''  )
  def   UpperCamelCase__      (				self    :					Tuple  )     ->						List[Any]:
    pass


  def   UpperCamelCase__      (				self    :					List[Any]  )     ->						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(UpperCamelCase__  )
      self.assertIsInstance(model.get_input_embeddings()							,	(nn.Module)  )
      _UpperCamelCase       			=model.get_output_embeddings()
      self.assertTrue(x is None or isinstance(UpperCamelCase__							,	nn.Linear  )  )


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

    for model_class in self.all_model_classes:
      _UpperCamelCase       			=model_class(UpperCamelCase__  )
      _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]							,	UpperCamelCase__  )


  def   UpperCamelCase__      (				self    :					Optional[Any]  )     ->						str:
    _UpperCamelCase       			=self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_model(*UpperCamelCase__  )




  @slow
  def   UpperCamelCase__      (				self    :					int  )     ->						int:
    for model_name in AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
      _UpperCamelCase       			=ASTModel.from_pretrained(UpperCamelCase__  )
      self.assertIsNotNone(UpperCamelCase__  )




def 					_a   ():


  """simple docstring"""

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

  _UpperCamelCase			,     _UpperCamelCase       			=torchaudio.load(__SCREAMING_SNAKE_CASE   )

  return audio, sampling_rate







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


  """simple docstring"""







  @cached_property
  def   UpperCamelCase__      (				self    :					int  )     ->						Union[str, Any]:
    return (
        ASTFeatureExtractor.from_pretrained('''MIT/ast-finetuned-audioset-10-10-0.4593'''  )
        if is_torchaudio_available()
        else None
    )




  @slow
  def   UpperCamelCase__      (				self    :					str  )     ->						Any:
    _UpperCamelCase       			=self.default_feature_extractor
    _UpperCamelCase       			=ASTForAudioClassification.from_pretrained('''MIT/ast-finetuned-audioset-10-10-0.4593'''  ).to(UpperCamelCase__  )

    _UpperCamelCase       			=self.default_feature_extractor
    _UpperCamelCase			,     _UpperCamelCase       			=prepare_audio()
    _UpperCamelCase       			=audio.squeeze().numpy()
    _UpperCamelCase       			=feature_extractor(UpperCamelCase__							,	sampling_rate=UpperCamelCase__							,	return_tensors='''pt'''  ).to(UpperCamelCase__  )

    # forward pass
    with torch.no_grad():
      _UpperCamelCase       			=model(**UpperCamelCase__  )

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

    _UpperCamelCase       			=torch.tensor([-0.8760, -7.0042, -8.6602]  ).to(UpperCamelCase__  )

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













'''simple docstring'''






def 					_a   (__SCREAMING_SNAKE_CASE   ):


  """simple docstring"""

  return 1 if digit in (0, 1) else (digit * factorial(digit - 1   ))


def 					_a   (__SCREAMING_SNAKE_CASE   ):


  """simple docstring"""

  _UpperCamelCase       			=0
  _UpperCamelCase       			=number
  while duplicate > 0:
    _UpperCamelCase			,     _UpperCamelCase       			=divmod(__SCREAMING_SNAKE_CASE  ,      10   )
    fact_sum += factorial(__SCREAMING_SNAKE_CASE   )
  return fact_sum == number


if __name__ == "__main__":
      print('Program to check whether a number is a Krisnamurthy Number or not.')
      __lowerCamelCase						: Optional[Any]		=   int(input('Enter number: ').strip())
      print(
          F"""{number} is {'' if krishnamurthy(number) else 'not '}a Krishnamurthy Number."""
      )






