"""simple docstring"""



import uuid
from typing import Any, Dict, List, Optional, Union

from ..utils import add_end_docstrings, is_tf_available, is_torch_available, logging
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_tf_available():
				import tensorflow as tf

if is_torch_available():
				import torch


_UpperCamelCase								=		logging.get_logger(__name__)
class 						__UpperCAmelCase    :







			'''simple docstring'''





			def __init__(			self   ,      snake_case_ = None   ,      snake_case_ = None   ,      snake_case_=None   ,      snake_case_=None							):







										'''simple docstring'''


										if not conversation_id:
																	A__       :     Optional[int]								=			uuid.uuida()
										if past_user_inputs is None:
																	A__       :     Tuple								=			[]
										if generated_responses is None:
																	A__       :     Any								=			[]

										A__       :     uuid.UUID								=			conversation_id
										A__       :     List[str]								=			past_user_inputs
										A__       :     List[str]								=			generated_responses
										A__       :     Optional[str]								=			text





			def __eq__(			self   ,      snake_case_							):







										'''simple docstring'''


										if not isinstance(snake_case_   ,      snake_case_							):
																	return False
										if self.uuid == other.uuid:
																	return True
										return (
										    self.new_user_input == other.new_user_input
										    and self.past_user_inputs == other.past_user_inputs
										    and self.generated_responses == other.generated_responses
										)





			def 		lowerCamelCase					(			self   ,      snake_case_   ,      snake_case_ = False							):







										'''simple docstring'''


										if self.new_user_input:
																	if overwrite:
																								logger.warning(
																								    F'''User input added while unprocessed input was existing: "{self.new_user_input}" was overwritten '''
																								    F'''with: "{text}".'''							)
																								A__       :     Union[str, Any]								=			text
																	else:
																								logger.warning(
																								    F'''User input added while unprocessed input was existing: "{self.new_user_input}" new input '''
																								    F'''ignored: "{text}". Set `overwrite` to True to overwrite unprocessed user input'''							)
										else:
																	A__       :     Dict								=			text





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										if self.new_user_input:
																	self.past_user_inputs.append(self.new_user_input							)
										A__       :     List[Any]								=			None





			def 		lowerCamelCase					(			self   ,      snake_case_							):







										'''simple docstring'''


										self.generated_responses.append(snake_case_							)





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										for user_input, generated_response in zip(self.past_user_inputs   ,      self.generated_responses							):
																	yield True, user_input
																	yield False, generated_response
										if self.new_user_input:
																	yield True, self.new_user_input





			def __repr__(			self							):







										'''simple docstring'''


										A__       :     str								=			F'''Conversation id: {self.uuid} \n'''
										for is_user, text in self.iter_texts():
																	A__       :     int								=			"""user""" if is_user else """bot"""
																	output += F'''{name} >> {text} \n'''
										return output





@add_end_docstrings(
    __A							,   R'\n        min_length_for_response (`int`, *optional*, defaults to 32):\n            The minimum length (in number of tokens) for a response.\n        minimum_tokens (`int`, *optional*, defaults to 10):\n            The minimum length of tokens to leave for a response.\n    '							,   )
class 						__UpperCAmelCase    (__A     ):







			'''simple docstring'''





			def __init__(			self   ,      *snake_case_   ,      **snake_case_							):







										'''simple docstring'''


										super().__init__(*snake_case_   ,      **snake_case_							)
										if self.tokenizer.pad_token_id is None:
																	A__       :     Union[str, Any]								=			self.tokenizer.eos_token





			def 		lowerCamelCase					(			self   ,      snake_case_=None   ,      snake_case_=None   ,      snake_case_=None   ,      **snake_case_							):







										'''simple docstring'''


										A__       :     List[Any]								=			{}
										A__       :     Tuple								=			{}
										A__       :     int								=			{}

										if min_length_for_response is not None:
																	A__       :     Union[str, Any]								=			min_length_for_response
										if minimum_tokens is not None:
																	A__       :     str								=			minimum_tokens

										if "max_length" in generate_kwargs:
																	A__       :     Optional[int]								=			generate_kwargs["""max_length"""]
																	# self.max_length = generate_kwargs.get("max_length", self.model.config.max_length)
										if clean_up_tokenization_spaces is not None:
																	A__       :     Any								=			clean_up_tokenization_spaces

										if generate_kwargs:
																	forward_params.update(snake_case_							)
										return preprocess_params, forward_params, postprocess_params





			def __call__(			self   ,      snake_case_   ,      snake_case_=0   ,      **snake_case_							):







										'''simple docstring'''


										A__       :     Union[str, Any]								=			super().__call__(snake_case_   ,      num_workers=snake_case_   ,      **snake_case_							)
										if isinstance(snake_case_   ,      snake_case_							) and len(snake_case_							) == 1:
																	return outputs[0]
										return outputs





			def 		lowerCamelCase					(			self   ,      snake_case_   ,      snake_case_=32							):







										'''simple docstring'''


										if not isinstance(snake_case_   ,      snake_case_							):
																	raise ValueError("""ConversationalPipeline, expects Conversation as inputs"""							)
										if conversation.new_user_input is None:
																	raise ValueError(
																	    F'''Conversation with UUID {type(conversation.uuid							)} does not contain new user input to process. '''
																	    """Add user inputs with the conversation's `add_user_input` method"""							)
										if hasattr(self.tokenizer   ,      """_build_conversation_input_ids"""							):
																	A__       :     Tuple								=			self.tokenizer._build_conversation_input_ids(snake_case_							)
										else:
																	# If the tokenizer cannot handle conversations, we default to only the old version
																	A__       :     Union[str, Any]								=			self._legacy_parse_and_tokenize(snake_case_							)

										if self.framework == "pt":
																	A__       :     int								=			torch.LongTensor([input_ids]							)
										elif self.framework == "tf":
																	A__       :     Optional[int]								=			tf.constant([input_ids]							)
										return {"input_ids": input_ids, "conversation": conversation}





			def 		lowerCamelCase					(			self   ,      snake_case_   ,      snake_case_=10   ,      **snake_case_							):







										'''simple docstring'''


										A__       :     Optional[Any]								=			generate_kwargs.get("""max_length"""   ,      self.model.config.max_length							)

										A__       :     Dict								=			model_inputs["""input_ids"""].shape[1]
										if max_length - minimum_tokens < n:
																	logger.warning(F'''Conversation input is to long ({n}), trimming it to ({max_length} - {minimum_tokens})'''							)
																	A__       :     Optional[Any]								=			max_length - minimum_tokens
																	A__       :     Optional[Any]								=			model_inputs["""input_ids"""][:, -trim:]
																	if "attention_mask" in model_inputs:
																								A__       :     int								=			model_inputs["""attention_mask"""][:, -trim:]
										A__       :     Optional[Any]								=			model_inputs.pop("""conversation"""							)
										A__       :     Dict								=			max_length
										A__       :     int								=			self.model.generate(**snake_case_   ,      **snake_case_							)
										if self.model.config.is_encoder_decoder:
																	A__       :     Tuple								=			1
										else:
																	A__       :     str								=			n
										return {"output_ids": output_ids[:, start_position:], "conversation": conversation}





			def 		lowerCamelCase					(			self   ,      snake_case_   ,      snake_case_=True							):







										'''simple docstring'''


										A__       :     List[Any]								=			model_outputs["""output_ids"""]
										A__       :     Any								=			self.tokenizer.decode(
										    output_ids[0]   ,      skip_special_tokens=snake_case_   ,      clean_up_tokenization_spaces=snake_case_   ,      )
										A__       :     List[str]								=			model_outputs["""conversation"""]
										conversation.mark_processed()
										conversation.append_response(snake_case_							)
										return conversation





			def 		lowerCamelCase					(			self   ,      snake_case_							):







										'''simple docstring'''


										A__       :     List[Any]								=			self.tokenizer.eos_token_id
										A__       :     int								=			[]
										for is_user, text in conversation.iter_texts():
																	if eos_token_id is not None:
																								input_ids.extend(self.tokenizer.encode(snake_case_   ,      add_special_tokens=snake_case_							) + [eos_token_id]							)
																	else:
																								input_ids.extend(self.tokenizer.encode(snake_case_   ,      add_special_tokens=snake_case_							)							)

										if len(snake_case_							) > self.tokenizer.model_max_length:
																	A__       :     List[str]								=			input_ids[-self.tokenizer.model_max_length :]
										return input_ids






"""simple docstring"""



import datasets

from .evaluate import evaluate


_UpperCamelCase								=		"\\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"

_UpperCamelCase								=		"\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"

_UpperCamelCase								=		"\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 						__UpperCAmelCase    (datasets.Metric     ):







			'''simple docstring'''





			def 		lowerCamelCase					(			self							):







										'''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   ,      snake_case_   ,      snake_case_							):







										'''simple docstring'''


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





