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


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

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

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


 def    _A	(    self  : List[str]    ):
  '''simple docstring'''

  lowerCAmelCase__     :   List[Any]     		=	floats_tensor([self.batch_size, self.max_length, self.num_mel_bins]    )

  lowerCAmelCase__     :   Any     		=	None
  if self.use_labels:
   lowerCAmelCase__     :   Tuple     		=	ids_tensor([self.batch_size]			,							self.type_sequence_label_size    )

  lowerCAmelCase__     :   Optional[Any]     		=	self.get_config()

  return config, input_values, labels


 def    _A	(    self  : List[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=a__			,							initializer_range=self.initializer_range			,							frequency_stride=self.frequency_stride			,							time_stride=self.time_stride			,							)


 def    _A	(    self  : Tuple			,							a__  : str			,							a__  : Optional[Any]			,							a__  : Dict    ):
  '''simple docstring'''

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


 def    _A	(    self  : str    ):
  '''simple docstring'''

  lowerCAmelCase__     :   Tuple     		=	self.prepare_config_and_inputs()
  (
      (
      lowerCAmelCase__
  )		,     (
      lowerCAmelCase__
  )		,     (
      lowerCAmelCase__
  )		,     
  )     :   Dict     		=	config_and_inputs
  lowerCAmelCase__     :   List[Any]     		=	{"input_values": input_values}
  return config, inputs_dict




@require_torch
class    lowerCAmelCase       (     UpperCamelCase_    ,  UpperCamelCase_    ,  unittest.TestCase  ):
 A_				:				Union[str, Any]	=			(
     (
         ASTModel,
         ASTForAudioClassification,
     )
     if is_torch_available()
     else ()
 )
 A_				:				Any	=			(
     {"""audio-classification""": ASTForAudioClassification, """feature-extraction""": ASTModel}
     if is_torch_available()
     else {}
 )
 A_				:				List[Any]	=			False
 A_				:				Union[str, Any]	=			False
 A_				:				Optional[int]	=			False
 A_				:				Union[str, Any]	=			False


 def    _A	(    self  : Optional[int]			,							a__  : Dict			,							a__  : Any			,							a__  : Optional[int]			,							a__  : Dict			,							a__  : Any    ):
  '''simple docstring'''

  if pipeline_test_casse_name == "AudioClassificationPipelineTests":
   return True

  return False


 def    _A	(    self  : int    ):
  '''simple docstring'''

  lowerCAmelCase__     :   Any     		=	ASTModelTester(self    )
  lowerCAmelCase__     :   List[str]     		=	ConfigTester(self			,							config_class=a__			,							has_text_modality=a__			,							hidden_size=37    )


 def    _A	(    self  : Optional[Any]    ):
  '''simple docstring'''

  self.config_tester.run_common_tests()


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

  pass


 def    _A	(    self  : Any    ):
  '''simple docstring'''

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

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


 def    _A	(    self  : str    ):
  '''simple docstring'''

  lowerCAmelCase__		,     lowerCAmelCase__     :   List[Any]     		=	self.model_tester.prepare_config_and_inputs_for_common()

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

   lowerCAmelCase__     :   List[Any]     		=	["input_values"]
   self.assertListEqual(arg_names[:1]			,							a__    )


 def    _A	(    self  : List[str]    ):
  '''simple docstring'''

  lowerCAmelCase__     :   Tuple     		=	self.model_tester.prepare_config_and_inputs()
  self.model_tester.create_and_check_model(*a__    )


 @slow
 def    _A	(    self  : Optional[int]    ):
  '''simple docstring'''

  for model_name in AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
   lowerCAmelCase__     :   List[Any]     		=	ASTModel.from_pretrained(a__    )
   self.assertIsNotNone(a__    )



def        UpperCAmelCase_ (  ):







 """simple docstring"""


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

 lowerCAmelCase__		,     lowerCAmelCase__     :   Optional[Any]     		=	torchaudio.load(lowerCamelCase_			)

 return audio, sampling_rate







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


 @cached_property
 def    _A	(    self  : List[str]    ):
  '''simple docstring'''

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


 @slow
 def    _A	(    self  : List[Any]    ):
  '''simple docstring'''

  lowerCAmelCase__     :   List[Any]     		=	self.default_feature_extractor
  lowerCAmelCase__     :   Optional[Any]     		=	ASTForAudioClassification.from_pretrained("MIT/ast-finetuned-audioset-10-10-0.4593"    ).to(a__    )

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

  # forward pass
  with torch.no_grad():
   lowerCAmelCase__     :   Union[str, Any]     		=	model(**a__    )

  # verify the logits
  lowerCAmelCase__     :   List[str]     		=	torch.Size((1, 527)    )
  self.assertEqual(outputs.logits.shape			,							a__    )

  lowerCAmelCase__     :   Tuple     		=	torch.tensor([-0.8760, -7.0042, -8.6602]    ).to(a__    )

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







'''simple docstring'''

# Copyright 2023 The HuggingFace Inc. team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from ..models.whisper import WhisperForConditionalGeneration, WhisperProcessor
from .base import PipelineTool




class    lowerCAmelCase       (     UpperCamelCase_  ):
 A_				:				int	=			"""openai/whisper-base"""
 A_				:				str	=			(
     """This is a tool that transcribes an audio into text. It takes an input named `audio` and returns the """
     """transcribed text."""
 )
 A_				:				Union[str, Any]	=			"""transcriber"""
 A_				:				Tuple	=			WhisperProcessor
 A_				:				Dict	=			WhisperForConditionalGeneration

 A_				:				Tuple	=			["""audio"""]
 A_				:				Optional[int]	=			["""text"""]


 def    _A	(    self  : Optional[int]			,							a__  : Optional[int]    ):
  '''simple docstring'''

  return self.pre_processor(a__			,							return_tensors="pt"    ).input_features


 def    _A	(    self  : List[str]			,							a__  : Union[str, Any]    ):
  '''simple docstring'''

  return self.model.generate(inputs=a__    )


 def    _A	(    self  : Optional[Any]			,							a__  : Dict    ):
  '''simple docstring'''

  return self.pre_processor.batch_decode(a__			,							skip_special_tokens=a__    )[0]





