



import datasets

from .evaluate import evaluate


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

lowercase_								=     """
This metric wrap the official scoring script for version 1 of the Contract
Understanding Atticus Dataset (CUAD).
Contract Understanding Atticus Dataset (CUAD) v1 is a corpus of more than 13,000 labels in 510
commercial legal contracts that have been manually labeled to identify 41 categories of important
clauses that lawyers look for when reviewing contracts in connection with corporate transactions.
"""

lowercase_								=     """
Computes CUAD scores (EM, F1, AUPR, Precision@80%Recall, and Precision@90%Recall).
Args:
    predictions: List of question-answers dictionaries with the following key-values:
        - \'id\': id of the question-answer pair as given in the references (see below)
        - \'prediction_text\': list of possible texts for the answer, as a list of strings
        depending on a threshold on the confidence probability of each prediction.
    references: List of question-answers dictionaries with the following key-values:
        - \'id\': id of the question-answer pair (see above),
        - \'answers\': a Dict in the CUAD dataset format
            {
                \'text\': list of possible texts for the answer, as a list of strings
                \'answer_start\': list of start positions for the answer, as a list of ints
            }
            Note that answer_start values are not taken into account to compute the metric.
Returns:
    \'exact_match\': Exact match (the normalized answer exactly match the gold answer)
    \'f1\': The F-score of predicted tokens versus the gold answer
    \'aupr\': Area Under the Precision-Recall curve
    \'prec_at_80_recall\': Precision at 80% recall
    \'prec_at_90_recall\': Precision at 90% recall
Examples:
    >>> 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\'}]
    >>> 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\'}]
    >>> cuad_metric = datasets.load_metric(\"cuad\")
    >>> results = cuad_metric.compute(predictions=predictions, references=references)
    >>> print(results)
    {\'exact_match\': 100.0, \'f1\': 100.0, \'aupr\': 0.0, \'prec_at_80_recall\': 1.0, \'prec_at_90_recall\': 1.0}
"""
@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION    ,	_KWARGS_DESCRIPTION)
class 				_snake_case			(					datasets.Metric):




				def 							A__      (						self					:						int						):
							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 							A__      (						self					:						List[Any],					__lowercase					:						Tuple,					__lowercase					:						str						):
							lowercase__					  =				{prediction["id"]: prediction["prediction_text"] for prediction in predictions}
							lowercase__					  =				[
							    {
							        "paragraphs": [
							            {
							                "qas": [
							                    {
							                        "answers": [{"text": answer_text} for answer_text in ref["answers"]["text"]],
							                        "id": ref["id"],
							                    }
							                    for ref in references
							                ]
							            }
							        ]
							    }
							]
							lowercase__					  =				evaluate(dataset=__UpperCamelCase,					predictions=__UpperCamelCase						)
							return score









import json
from typing import Dict, List, Optional, Tuple, Union

from tokenizers import pre_tokenizers, processors

from ...tokenization_utils_base import AddedToken, BatchEncoding, EncodedInput
from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import PaddingStrategy, logging
from .tokenization_led import LEDTokenizer


lowercase_								=     logging.get_logger(__name__)


lowercase_								=     {"""vocab_file""": """vocab.json""", """merges_file""": """merges.txt""", """tokenizer_file""": """tokenizer.json"""}

lowercase_								=     {
    """vocab_file""": {
        """allenai/led-base-16384""": """https://huggingface.co/allenai/led-base-16384/resolve/main/vocab.json""",
    },
    """merges_file""": {
        """allenai/led-base-16384""": """https://huggingface.co/allenai/led-base-16384/resolve/main/merges.txt""",
    },
    """tokenizer_file""": {
        """allenai/led-base-16384""": """https://huggingface.co/allenai/led-base-16384/resolve/main/tokenizer.json""",
    },
}

lowercase_								=     {
    """allenai/led-base-16384""": 1_6384,
}
class 				_snake_case			(					lowercase__):
				UpperCamelCase__				:					int     		=VOCAB_FILES_NAMES
				UpperCamelCase__				:					Any     		=PRETRAINED_VOCAB_FILES_MAP
				UpperCamelCase__				:					Dict     		=PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
				UpperCamelCase__				:					List[Any]     		=LEDTokenizer
				UpperCamelCase__				:					Tuple     		=["""input_ids""", """attention_mask"""]




				def __init__(						self					:						Optional[Any],					__lowercase					:						Optional[Any]=None,					__lowercase					:						Dict=None,					__lowercase					:						Tuple=None,					__lowercase					:						Union[str, Any]="replace",					__lowercase					:						Tuple="<s>",					__lowercase					:						Optional[Any]="</s>",					__lowercase					:						Tuple="</s>",					__lowercase					:						List[str]="<s>",					__lowercase					:						Tuple="<unk>",					__lowercase					:						Dict="<pad>",					__lowercase					:						Dict="<mask>",					__lowercase					:						Any=False,					__lowercase					:						Any=True,					**__lowercase					:						List[Any],					):
							super().__init__(
							    __lowercase,					__lowercase,					tokenizer_file=__lowercase,					errors=__lowercase,					bos_token=__lowercase,					eos_token=__lowercase,					sep_token=__lowercase,					cls_token=__lowercase,					unk_token=__lowercase,					pad_token=__lowercase,					mask_token=__lowercase,					add_prefix_space=__lowercase,					trim_offsets=__lowercase,					**__lowercase,					)

							lowercase__					  =				json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__()						)
							if pre_tok_state.get("add_prefix_space",					__lowercase						) != add_prefix_space:
										lowercase__					  =				getattr(__lowercase,					pre_tok_state.pop("type"						)						)
										lowercase__					  =				add_prefix_space
										lowercase__					  =				pre_tok_class(**__lowercase						)

							lowercase__					  =				add_prefix_space

							# the pre_tokenizer is already updated in the GPT2TokenizerFast `__init__`
							lowercase__					  =				"post_processor"
							lowercase__					  =				getattr(self.backend_tokenizer,					__lowercase,					__lowercase						)
							if tokenizer_component_instance:
										lowercase__					  =				json.loads(tokenizer_component_instance.__getstate__()						)

										# The lists 'sep' and 'cls' must be cased in tuples for the object `post_processor_class`
										if "sep" in state:
													lowercase__					  =				tuple(state["sep"]						)
										if "cls" in state:
													lowercase__					  =				tuple(state["cls"]						)

										lowercase__					  =				False

										if state.get("add_prefix_space",					__lowercase						) != add_prefix_space:
													lowercase__					  =				add_prefix_space
													lowercase__					  =				True

										if state.get("trim_offsets",					__lowercase						) != trim_offsets:
													lowercase__					  =				trim_offsets
													lowercase__					  =				True

										if changes_to_apply:
													lowercase__					  =				getattr(__lowercase,					state.pop("type"						)						)
													lowercase__					  =				component_class(**__lowercase						)
													setattr(self.backend_tokenizer,					__lowercase,					__lowercase						)




				@property
				# Copied from transformers.models.bart.tokenization_bart_fast.BartTokenizerFast.mask_token with BART->LED
				def 							A__      (						self					:						str						):
							if self._mask_token is None:
										if self.verbose:
													logger.error("Using mask_token, but it is not set yet."						)
										return None
							return str(self._mask_token						)




				@mask_token.setter
				def 							A__      (						self					:						Optional[int],					__lowercase					:						Dict						):
							lowercase__					  =				AddedToken(__lowercase,					lstrip=__lowercase,					rstrip=__lowercase						) if isinstance(__lowercase,					__lowercase						) else value
							lowercase__					  =				value




				def 							A__      (						self					:						Any,					*__lowercase					:						List[Any],					**__lowercase					:						Optional[Any]						):
							lowercase__					  =				kwargs.get("is_split_into_words",					__lowercase						)

							if is_split_into_words and not self.add_prefix_space:
										raise ValueError(
										    F'''You need to instantiate {self.__class__.__name__} with add_prefix_space=True '''
										    "to use it with pretokenized inputs."						)

							return super()._batch_encode_plus(*__lowercase,					**__lowercase						)




				def 							A__      (						self					:						int,					*__lowercase					:						Union[str, Any],					**__lowercase					:						List[str]						):
							lowercase__					  =				kwargs.get("is_split_into_words",					__lowercase						)

							if is_split_into_words and not self.add_prefix_space:
										raise ValueError(
										    F'''You need to instantiate {self.__class__.__name__} with add_prefix_space=True '''
										    "to use it with pretokenized inputs."						)

							return super()._encode_plus(*__lowercase,					**__lowercase						)




				def 							A__      (						self					:						Optional[Any],					__lowercase					:						str,					__lowercase					:						Optional[str] = None						):
							lowercase__					  =				self._tokenizer.model.save(__lowercase,					name=__lowercase						)
							return tuple(__lowercase						)




				def 							A__      (						self					:						List[str],					__lowercase					:						int,					__lowercase					:						Optional[int]=None						):
							lowercase__					  =				[self.bos_token_id] + token_ids_a + [self.eos_token_id]
							if token_ids_a is None:
										return output

							return output + [self.eos_token_id] + token_ids_a + [self.eos_token_id]




				def 							A__      (						self					:						int,					__lowercase					:						List[int],					__lowercase					:						Optional[List[int]] = None						):
							lowercase__					  =				[self.sep_token_id]
							lowercase__					  =				[self.cls_token_id]

							if token_ids_a is None:
										return len(cls + token_ids_a + sep						) * [0]
							return len(cls + token_ids_a + sep + sep + token_ids_a + sep						) * [0]




				def 							A__      (						self					:						Union[str, Any],					__lowercase					:						Union[Dict[str, EncodedInput], BatchEncoding],					__lowercase					:						Optional[int] = None,					__lowercase					:						PaddingStrategy = PaddingStrategy.DO_NOT_PAD,					__lowercase					:						Optional[int] = None,					__lowercase					:						Optional[bool] = None,					):
							lowercase__					  =				super()._pad(
							    encoded_inputs=__lowercase,					max_length=__lowercase,					padding_strategy=__lowercase,					pad_to_multiple_of=__lowercase,					return_attention_mask=__lowercase,					)

							# Load from model defaults
							if return_attention_mask is None:
										lowercase__					  =				"attention_mask" in self.model_input_names

							if return_attention_mask and "global_attention_mask" in encoded_inputs:
										lowercase__					  =				encoded_inputs[self.model_input_names[0]]
										# `global_attention_mask` need to have the same length as other (sequential) inputs.
										lowercase__					  =				len(encoded_inputs["global_attention_mask"]						) != len(__lowercase						)

										if needs_to_be_padded:
													lowercase__					  =				len(__lowercase						) - len(encoded_inputs["global_attention_mask"]						)

													if self.padding_side == "right":
																# Use `-1` since `0` in `global_attention_mask` means `local attention` instead of `not to attend`
																lowercase__					  =				(
																    encoded_inputs["global_attention_mask"] + [-1] * difference
																)
													elif self.padding_side == "left":
																lowercase__					  =				[-1] * difference + encoded_inputs[
																    "global_attention_mask"
																]
													else:
																raise ValueError("Invalid padding strategy:" + str(self.padding_side						)						)

							return encoded_inputs




