






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:  Any      ,	UpperCAmelCase_:  List[Any]      ,	UpperCAmelCase_:  Dict=13      ,	UpperCAmelCase_:  Union[str, Any]=2      ,	UpperCAmelCase_:  str=24      ,	UpperCAmelCase_:  int=16      ,	UpperCAmelCase_:  str=True      ,	UpperCAmelCase_:  int=True      ,	UpperCAmelCase_:  List[str]=32      ,	UpperCAmelCase_:  Union[str, Any]=5      ,	UpperCAmelCase_:  Dict=4      ,	UpperCAmelCase_:  Union[str, Any]=37      ,	UpperCAmelCase_:  List[str]="gelu"      ,	UpperCAmelCase_:  str=0.1      ,	UpperCAmelCase_:  int=0.1      ,	UpperCAmelCase_:  Dict=10      ,	UpperCAmelCase_:  str=0.02      ,	UpperCAmelCase_:  Optional[Any]=None      ,	UpperCAmelCase_:  Optional[Any]=2      ,	UpperCAmelCase_:  List[Any]=2      ,	):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE	      =  parent
									_SCREAMING_SNAKE_CASE	      =  batch_size
									_SCREAMING_SNAKE_CASE	      =  patch_size
									_SCREAMING_SNAKE_CASE	      =  max_length
									_SCREAMING_SNAKE_CASE	      =  num_mel_bins
									_SCREAMING_SNAKE_CASE	      =  is_training
									_SCREAMING_SNAKE_CASE	      =  use_labels
									_SCREAMING_SNAKE_CASE	      =  hidden_size
									_SCREAMING_SNAKE_CASE	      =  num_hidden_layers
									_SCREAMING_SNAKE_CASE	      =  num_attention_heads
									_SCREAMING_SNAKE_CASE	      =  intermediate_size
									_SCREAMING_SNAKE_CASE	      =  hidden_act
									_SCREAMING_SNAKE_CASE	      =  hidden_dropout_prob
									_SCREAMING_SNAKE_CASE	      =  attention_probs_dropout_prob
									_SCREAMING_SNAKE_CASE	      =  type_sequence_label_size
									_SCREAMING_SNAKE_CASE	      =  initializer_range
									_SCREAMING_SNAKE_CASE	      =  scope
									_SCREAMING_SNAKE_CASE	      =  frequency_stride
									_SCREAMING_SNAKE_CASE	      =  time_stride

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







			def 							UpperCamelCase      (	self:  Any     ):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE	      =  floats_tensor([self.batch_size, self.max_length, self.num_mel_bins]     )

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

									_SCREAMING_SNAKE_CASE	      =  self.get_config()

									return config, input_values, labels







			def 							UpperCamelCase      (	self:  Optional[Any]     ):







									'''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=__lowerCamelCase      ,	initializer_range=self.initializer_range      ,	frequency_stride=self.frequency_stride      ,	time_stride=self.time_stride      ,	)







			def 							UpperCamelCase      (	self:  Tuple      ,	UpperCAmelCase_:  Optional[int]      ,	UpperCAmelCase_:  int      ,	UpperCAmelCase_:  List[str]     ):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE	      =  ASTModel(config=__lowerCamelCase     )
									model.to(__lowerCamelCase     )
									model.eval()
									_SCREAMING_SNAKE_CASE	      =  model(__lowerCamelCase     )
									self.parent.assertEqual(result.last_hidden_state.shape      ,	(self.batch_size, self.seq_length, self.hidden_size)     )







			def 							UpperCamelCase      (	self:  Optional[int]     ):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE	      =  self.prepare_config_and_inputs()
									(
									    _SCREAMING_SNAKE_CASE   
									)	      =  config_and_inputs
									_SCREAMING_SNAKE_CASE	      =  {"input_values": input_values}
									return config, inputs_dict



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







			def 							UpperCamelCase      (	self:  List[Any]      ,	UpperCAmelCase_:  Union[str, Any]      ,	UpperCAmelCase_:  str      ,	UpperCAmelCase_:  str      ,	UpperCAmelCase_:  Dict      ,	UpperCAmelCase_:  Tuple     ):







									'''simple docstring'''




									if pipeline_test_casse_name == "AudioClassificationPipelineTests":
															return True

									return False







			def 							UpperCamelCase      (	self:  Optional[Any]     ):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE	      =  ASTModelTester(self     )
									_SCREAMING_SNAKE_CASE	      =  ConfigTester(self      ,	config_class=__lowerCamelCase      ,	has_text_modality=__lowerCamelCase      ,	hidden_size=37     )







			def 							UpperCamelCase      (	self:  Optional[int]     ):







									'''simple docstring'''




									self.config_tester.run_common_tests()







			@unittest.skip(reason="""AST does not use inputs_embeds"""     )
			def 							UpperCamelCase      (	self:  Optional[Any]     ):







									'''simple docstring'''




									pass







			def 							UpperCamelCase      (	self:  Tuple     ):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE	      =  self.model_tester.prepare_config_and_inputs_for_common()

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







			def 							UpperCamelCase      (	self:  Tuple     ):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE	      =  self.model_tester.prepare_config_and_inputs_for_common()

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

															_SCREAMING_SNAKE_CASE	      =  ["input_values"]
															self.assertListEqual(arg_names[:1]      ,	__lowerCamelCase     )







			def 							UpperCamelCase      (	self:  Optional[Any]     ):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE	      =  self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*__lowerCamelCase     )







			@slow
			def 							UpperCamelCase      (	self:  Tuple     ):







									'''simple docstring'''




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

def        __lowerCamelCase			(		)							->  Union[str, Any]:






						"""simple docstring"""





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

						_SCREAMING_SNAKE_CASE	      =  torchaudio.load(_A		)

						return audio, sampling_rate




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







			@cached_property
			def 							UpperCamelCase      (	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 							UpperCamelCase      (	self:  Tuple     ):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE	      =  self.default_feature_extractor
									_SCREAMING_SNAKE_CASE	      =  ASTForAudioClassification.from_pretrained("""MIT/ast-finetuned-audioset-10-10-0.4593"""     ).to(__lowerCamelCase     )

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

									# forward pass
									with torch.no_grad():
															_SCREAMING_SNAKE_CASE	      =  model(**__lowerCamelCase     )

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

									_SCREAMING_SNAKE_CASE	      =  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 datasets

from .evaluate import evaluate


A      :    Dict		=					"\\n@article{hendrycks2021cuad,\n      title={CUAD: An Expert-Annotated NLP Dataset for Legal Contract Review},\n      author={Dan Hendrycks and Collin Burns and Anya Chen and Spencer Ball},\n      journal={arXiv preprint arXiv:2103.06268},\n      year={2021}\n}\n"

A      :    Any		=					"\nThis metric wrap the official scoring script for version 1 of the Contract\nUnderstanding Atticus Dataset (CUAD).\nContract Understanding Atticus Dataset (CUAD) v1 is a corpus of more than 13,000 labels in 510\ncommercial legal contracts that have been manually labeled to identify 41 categories of important\nclauses that lawyers look for when reviewing contracts in connection with corporate transactions.\n"

A      :    str		=					"\nComputes CUAD scores (EM, F1, AUPR, Precision@80%Recall, and Precision@90%Recall).\nArgs:\n    predictions: List of question-answers dictionaries with the following key-values:\n        - 'id': id of the question-answer pair as given in the references (see below)\n        - 'prediction_text': list of possible texts for the answer, as a list of strings\n        depending on a threshold on the confidence probability of each prediction.\n    references: List of question-answers dictionaries with the following key-values:\n        - 'id': id of the question-answer pair (see above),\n        - 'answers': a Dict in the CUAD dataset format\n            {\n                'text': list of possible texts for the answer, as a list of strings\n                'answer_start': list of start positions for the answer, as a list of ints\n            }\n            Note that answer_start values are not taken into account to compute the metric.\nReturns:\n    'exact_match': Exact match (the normalized answer exactly match the gold answer)\n    'f1': The F-score of predicted tokens versus the gold answer\n    'aupr': Area Under the Precision-Recall curve\n    'prec_at_80_recall': Precision at 80% recall\n    'prec_at_90_recall': Precision at 90% recall\nExamples:\n    >>> predictions = [{'prediction_text': ['The seller:', 'The buyer/End-User: Shenzhen LOHAS Supply Chain Management Co., Ltd.'], 'id': 'LohaCompanyltd_20191209_F-1_EX-10.16_11917878_EX-10.16_Supply Agreement__Parties'}]\n    >>> references = [{'answers': {'answer_start': [143, 49], 'text': ['The seller:', 'The buyer/End-User: Shenzhen LOHAS Supply Chain Management Co., Ltd.']}, 'id': 'LohaCompanyltd_20191209_F-1_EX-10.16_11917878_EX-10.16_Supply Agreement__Parties'}]\n    >>> cuad_metric = datasets.load_metric(\"cuad\")\n    >>> results = cuad_metric.compute(predictions=predictions, references=references)\n    >>> print(results)\n    {'exact_match': 100.0, 'f1': 100.0, 'aupr': 0.0, 'prec_at_80_recall': 1.0, 'prec_at_90_recall': 1.0}\n"







@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION							,  _KWARGS_DESCRIPTION)
class 			_lowercase (							datasets.Metric):


       """simple docstring"""




       def 						lowerCAmelCase     (   self							:			Optional[Any]       ):


             '''simple docstring'''



             return datasets.MetricInfo(
                 description=_DESCRIPTION					,      citation=_CITATION					,      inputs_description=_KWARGS_DESCRIPTION					,      features=datasets.Features(
                     {
                         "predictions": {
                             "id": datasets.Value("string"       ),
                             "prediction_text": datasets.features.Sequence(datasets.Value("string"       )       ),
                         },
                         "references": {
                             "id": datasets.Value("string"       ),
                             "answers": datasets.features.Sequence(
                                 {
                                     "text": datasets.Value("string"       ),
                                     "answer_start": datasets.Value("int32"       ),
                                 }       ),
                         },
                     }       )					,      codebase_urls=["https://www.atticusprojectai.org/cuad"]					,      reference_urls=["https://www.atticusprojectai.org/cuad"]					,      )





       def 						lowerCAmelCase     (   self							:			Union[str, Any]					,      __lowerCamelCase							:			List[str]					,      __lowerCamelCase							:			Dict       ):


             '''simple docstring'''



             lowerCamelCase__		:				int    =  {prediction["id"]: prediction["prediction_text"] for prediction in predictions}
             lowerCamelCase__		:				Any    =  [
                 {
                     "paragraphs": [
                         {
                             "qas": [
                                 {
                                     "answers": [{"text": answer_text} for answer_text in ref["answers"]["text"]],
                                     "id": ref["id"],
                                 }
                                 for ref in references
                             ]
                         }
                     ]
                 }
             ]
             lowerCamelCase__		:				Optional[int]    =  evaluate(dataset=__lowerCamelCase					,      predictions=__lowerCamelCase       )
             return score


