



"""simple docstring"""
import itertools
import random
import unittest

import numpy as np

from transformers import ASTFeatureExtractor
from transformers.testing_utils import require_torch, require_torchaudio
from transformers.utils.import_utils import is_torch_available

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


A:   Union[str, Any]								=		random.Random()

if is_torch_available():
      import torch

def       _snake_case						(						UpperCamelCase						:		Optional[int]			,		UpperCamelCase						:		int=1.0			,		UpperCamelCase						:		Tuple=None			,		UpperCamelCase						:		Tuple=None						):
 if rng is None:
  UpperCAmelCase	:					List[str]	=	global_rng

 UpperCAmelCase	:					str	=	[]
 for batch_idx in range(shape[0]						):
  values.append([]						)
  for _ in range(shape[1]						):
   values[-1].append(rng.random() * scale						)

 return values



class       SCREAMING_SNAKE_CASE__  (					unittest.TestCase ):

   def __init__(   self      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE=7      ,     _SCREAMING_SNAKE_CASE=400      ,     _SCREAMING_SNAKE_CASE=2000      ,     _SCREAMING_SNAKE_CASE=1      ,     _SCREAMING_SNAKE_CASE=0.0      ,     _SCREAMING_SNAKE_CASE=16000      ,     _SCREAMING_SNAKE_CASE=True      ,     _SCREAMING_SNAKE_CASE=True      ,     )  ->      List[Any]:





    '''simple docstring'''




    UpperCAmelCase	:					int	=	parent
    UpperCAmelCase	:					Tuple	=	batch_size
    UpperCAmelCase	:					Any	=	min_seq_length
    UpperCAmelCase	:					Dict	=	max_seq_length
    UpperCAmelCase	:					Tuple	=	(self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
    UpperCAmelCase	:					Optional[Any]	=	feature_size
    UpperCAmelCase	:					Union[str, Any]	=	padding_value
    UpperCAmelCase	:					List[Any]	=	sampling_rate
    UpperCAmelCase	:					Optional[Any]	=	return_attention_mask
    UpperCAmelCase	:					List[str]	=	do_normalize

   def 	SCREAMING_SNAKE_CASE     (   self						)  ->      int:





    '''simple docstring'''




    return {
        "feature_size": self.feature_size,
        "padding_value": self.padding_value,
        "sampling_rate": self.sampling_rate,
        "return_attention_mask": self.return_attention_mask,
        "do_normalize": self.do_normalize,
    }




   def 	SCREAMING_SNAKE_CASE     (   self      ,     _SCREAMING_SNAKE_CASE=False      ,     _SCREAMING_SNAKE_CASE=False						)  ->      List[str]:





    '''simple docstring'''




    def _flatten(_SCREAMING_SNAKE_CASE						):
     return list(itertools.chain(*_SCREAMING_SNAKE_CASE						)						)

    if equal_length:
     UpperCAmelCase	:					Optional[int]	=	floats_list((self.batch_size, self.max_seq_length)						)
    else:
     # make sure that inputs increase in size
     UpperCAmelCase	:					Union[str, Any]	=	[
         _flatten(floats_list((x, self.feature_size)						)						)
         for x in range(self.min_seq_length      ,     self.max_seq_length      ,     self.seq_length_diff						)
     ]

    if numpify:
     UpperCAmelCase	:					int	=	[np.asarray(_SCREAMING_SNAKE_CASE						) for x in speech_inputs]

    return speech_inputs



@require_torch
@require_torchaudio
class       SCREAMING_SNAKE_CASE__  (					UpperCAmelCase__      ,			unittest.TestCase ):
   __lowerCAmelCase  :   Dict						=						ASTFeatureExtractor

   def 	SCREAMING_SNAKE_CASE     (   self						)  ->      Optional[Any]:





    '''simple docstring'''




    UpperCAmelCase	:					Tuple	=	ASTFeatureExtractionTester(self						)

   def 	SCREAMING_SNAKE_CASE     (   self						)  ->      Any:





    '''simple docstring'''




    UpperCAmelCase	:					Tuple	=	self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()						)
    # create three inputs of length 800, 1000, and 1200
    UpperCAmelCase	:					List[Any]	=	[floats_list((1, x)						)[0] for x in range(800      ,     1400      ,     200						)]
    UpperCAmelCase	:					Dict	=	[np.asarray(_SCREAMING_SNAKE_CASE						) for speech_input in speech_inputs]

    # Test not batched input
    UpperCAmelCase	:					Any	=	feat_extract(speech_inputs[0]      ,     return_tensors="""np"""						).input_values
    UpperCAmelCase	:					Any	=	feat_extract(np_speech_inputs[0]      ,     return_tensors="""np"""						).input_values
    self.assertTrue(np.allclose(_SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE      ,     atol=1E-3						)						)

    # Test batched
    UpperCAmelCase	:					Any	=	feat_extract(_SCREAMING_SNAKE_CASE      ,     padding=_SCREAMING_SNAKE_CASE      ,     return_tensors="""np"""						).input_values
    UpperCAmelCase	:					Union[str, Any]	=	feat_extract(_SCREAMING_SNAKE_CASE      ,     padding=_SCREAMING_SNAKE_CASE      ,     return_tensors="""np"""						).input_values
    for enc_seq_a, enc_seq_a in zip(_SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE						):
     self.assertTrue(np.allclose(_SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE      ,     atol=1E-3						)						)

    # Test 2-D numpy arrays are batched.
    UpperCAmelCase	:					List[Any]	=	[floats_list((1, x)						)[0] for x in (800, 800, 800)]
    UpperCAmelCase	:					Tuple	=	np.asarray(_SCREAMING_SNAKE_CASE						)
    UpperCAmelCase	:					Dict	=	feat_extract(_SCREAMING_SNAKE_CASE      ,     return_tensors="""np"""						).input_values
    UpperCAmelCase	:					str	=	feat_extract(_SCREAMING_SNAKE_CASE      ,     return_tensors="""np"""						).input_values
    for enc_seq_a, enc_seq_a in zip(_SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE						):
     self.assertTrue(np.allclose(_SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE      ,     atol=1E-3						)						)

   @require_torch
   def 	SCREAMING_SNAKE_CASE     (   self						)  ->      Tuple:





    '''simple docstring'''




    import torch

    UpperCAmelCase	:					Tuple	=	self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()						)
    UpperCAmelCase	:					List[Any]	=	np.random.rand(100						).astype(np.floataa						)
    UpperCAmelCase	:					Optional[Any]	=	np_speech_inputs.tolist()

    for inputs in [py_speech_inputs, np_speech_inputs]:
     UpperCAmelCase	:					Union[str, Any]	=	feature_extractor.pad([{"""input_values""": inputs}]      ,     return_tensors="""np"""						)
     self.assertTrue(np_processed.input_values.dtype == np.floataa						)
     UpperCAmelCase	:					Dict	=	feature_extractor.pad([{"""input_values""": inputs}]      ,     return_tensors="""pt"""						)
     self.assertTrue(pt_processed.input_values.dtype == torch.floataa						)

   def 	SCREAMING_SNAKE_CASE     (   self      ,     _SCREAMING_SNAKE_CASE						)  ->      List[str]:





    '''simple docstring'''




    from datasets import load_dataset

    UpperCAmelCase	:					Tuple	=	load_dataset("""hf-internal-testing/librispeech_asr_dummy"""      ,     """clean"""      ,     split="""validation"""						)
    # automatic decoding with librispeech
    UpperCAmelCase	:					List[str]	=	ds.sort("""id"""						).select(range(_SCREAMING_SNAKE_CASE						)						)[:num_samples]["""audio"""]

    return [x["array"] for x in speech_samples]




   @require_torch
   def 	SCREAMING_SNAKE_CASE     (   self						)  ->      Any:





    '''simple docstring'''




    UpperCAmelCase	:					Optional[Any]	=	torch.tensor(
        [-0.9894, -1.2776, -0.9066, -1.2776, -0.9349, -1.2609, -1.0386, -1.2776,
         -1.1561, -1.2776, -1.2052, -1.2723, -1.2190, -1.2132, -1.2776, -1.1133,
         -1.1953, -1.1343, -1.1584, -1.2203, -1.1770, -1.2474, -1.2381, -1.1936,
         -0.9270, -0.8317, -0.8049, -0.7706, -0.7565, -0.7869]						)
    # fmt: on

    UpperCAmelCase	:					int	=	self._load_datasamples(1						)
    UpperCAmelCase	:					Any	=	ASTFeatureExtractor()
    UpperCAmelCase	:					Union[str, Any]	=	feature_extractor(_SCREAMING_SNAKE_CASE      ,     return_tensors="""pt"""						).input_values
    self.assertEquals(input_values.shape      ,     (1, 1024, 128)						)
    self.assertTrue(torch.allclose(input_values[0, 0, :30]      ,     _SCREAMING_SNAKE_CASE      ,     atol=1E-4						)						)





"""simple docstring"""
import datasets

from .evaluate import evaluate


A:   Optional[Any]								=		"\\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:   Optional[int]								=		"\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:   int								=		"\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       SCREAMING_SNAKE_CASE__  (					datasets.Metric ):

   def 	SCREAMING_SNAKE_CASE     (   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 	SCREAMING_SNAKE_CASE     (   self      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE						)  ->      Dict:





    '''simple docstring'''




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