





"""simple docstring"""



from scipy.stats import pearsonr, spearmanr
from sklearn.metrics import fa_score, matthews_corrcoef

import datasets


a_ =			"""\
@inproceedings{wang2019glue,
  title={{GLUE}: A Multi-Task Benchmark and Analysis Platform for Natural Language Understanding},
  author={Wang, Alex and Singh, Amanpreet and Michael, Julian and Hill, Felix and Levy, Omer and Bowman, Samuel R.},
  note={In the Proceedings of ICLR.},
  year={2019}
}
"""

a_ =			"""\
GLUE, the General Language Understanding Evaluation benchmark
(https://gluebenchmark.com/) is a collection of resources for training,
evaluating, and analyzing natural language understanding systems.
"""

a_ =			"""
Compute GLUE evaluation metric associated to each GLUE dataset.
Args:
    predictions: list of predictions to score.
        Each translation should be tokenized into a list of tokens.
    references: list of lists of references for each translation.
        Each reference should be tokenized into a list of tokens.
Returns: depending on the GLUE subset, one or several of:
    \"accuracy\": Accuracy
    \"f1\": F1 score
    \"pearson\": Pearson Correlation
    \"spearmanr\": Spearman Correlation
    \"matthews_correlation\": Matthew Correlation
Examples:

    >>> glue_metric = datasets.load_metric('glue', 'sst2')  # 'sst2' or any of [\"mnli\", \"mnli_mismatched\", \"mnli_matched\", \"qnli\", \"rte\", \"wnli\", \"hans\"]
    >>> references = [0, 1]
    >>> predictions = [0, 1]
    >>> results = glue_metric.compute(predictions=predictions, references=references)
    >>> print(results)
    {'accuracy': 1.0}

    >>> glue_metric = datasets.load_metric('glue', 'mrpc')  # 'mrpc' or 'qqp'
    >>> references = [0, 1]
    >>> predictions = [0, 1]
    >>> results = glue_metric.compute(predictions=predictions, references=references)
    >>> print(results)
    {'accuracy': 1.0, 'f1': 1.0}

    >>> glue_metric = datasets.load_metric('glue', 'stsb')
    >>> references = [0., 1., 2., 3., 4., 5.]
    >>> predictions = [0., 1., 2., 3., 4., 5.]
    >>> results = glue_metric.compute(predictions=predictions, references=references)
    >>> print({\"pearson\": round(results[\"pearson\"], 2), \"spearmanr\": round(results[\"spearmanr\"], 2)})
    {'pearson': 1.0, 'spearmanr': 1.0}

    >>> glue_metric = datasets.load_metric('glue', 'cola')
    >>> references = [0, 1]
    >>> predictions = [0, 1]
    >>> results = glue_metric.compute(predictions=predictions, references=references)
    >>> print(results)
    {'matthews_correlation': 1.0}
"""







def 		__lowercase     (   snake_case_  :  Union[str, Any]				,snake_case_  :  List[str]						)   ->str:



    '''simple docstring'''







    return float((preds == labels).mean()						)







def 		__lowercase     (   snake_case_  :  Any				,snake_case_  :  List[str]						)   ->List[str]:



    '''simple docstring'''







    __A    :						Union[str, Any]						 =     simple_accuracy(snake_case_				,snake_case_						)
    __A    :						Optional[int]						 =     float(fa_score(y_true=snake_case_				,y_pred=snake_case_						)						)
    return {
        "accuracy": acc,
        "f1": fa,
    }







def 		__lowercase     (   snake_case_  :  List[str]				,snake_case_  :  str						)   ->Optional[Any]:



    '''simple docstring'''







    __A    :						int						 =     float(pearsonr(snake_case_				,snake_case_						)[0]						)
    __A    :						Dict						 =     float(spearmanr(snake_case_				,snake_case_						)[0]						)
    return {
        "pearson": pearson_corr,
        "spearmanr": spearman_corr,
    }






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





 """simple docstring"""


 def 					UpperCamelCase__(    self  ):



     '''simple docstring'''




     if self.config_name not in [
         "sst2",
         "mnli",
         "mnli_mismatched",
         "mnli_matched",
         "cola",
         "stsb",
         "mrpc",
         "qqp",
         "qnli",
         "rte",
         "wnli",
         "hans",
     ]:
         raise KeyError(
             '''You should supply a configuration name selected in '''
             '''["sst2", "mnli", "mnli_mismatched", "mnli_matched", '''
             '''"cola", "stsb", "mrpc", "qqp", "qnli", "rte", "wnli", "hans"]'''  )
     return datasets.MetricInfo(
         description=_DESCRIPTION					,						citation=_CITATION					,						inputs_description=_KWARGS_DESCRIPTION					,						features=datasets.Features(
             {
                 '''predictions''': datasets.Value('''int64''' if self.config_name != '''stsb''' else '''float32'''  ),
                 '''references''': datasets.Value('''int64''' if self.config_name != '''stsb''' else '''float32'''  ),
             }  )					,						codebase_urls=[]					,						reference_urls=[]					,						format='''numpy'''					,						)







 def 					UpperCamelCase__(    self					,						__lowerCamelCase					,						__lowerCamelCase  ):



     '''simple docstring'''




     if self.config_name == "cola":
         return {"matthews_correlation": matthews_corrcoef(__lowerCamelCase					,						__lowerCamelCase  )}
     elif self.config_name == "stsb":
         return pearson_and_spearman(__lowerCamelCase					,						__lowerCamelCase  )
     elif self.config_name in ["mrpc", "qqp"]:
         return acc_and_fa(__lowerCamelCase					,						__lowerCamelCase  )
     elif self.config_name in ["sst2", "mnli", "mnli_mismatched", "mnli_matched", "qnli", "rte", "wnli", "hans"]:
         return {"accuracy": simple_accuracy(__lowerCamelCase					,						__lowerCamelCase  )}
     else:
         raise KeyError(
             '''You should supply a configuration name selected in '''
             '''["sst2", "mnli", "mnli_mismatched", "mnli_matched", '''
             '''"cola", "stsb", "mrpc", "qqp", "qnli", "rte", "wnli", "hans"]'''  )











"""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


a_ =			logging.get_logger(__name__)






class 		__snake_case					:





 """simple docstring"""


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



     '''simple docstring'''




     if not conversation_id:
         __A    :						List[Any]						 =     uuid.uuida()
     if past_user_inputs is None:
         __A    :						List[str]						 =     []
     if generated_responses is None:
         __A    :						Tuple						 =     []

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

 def __eq__(    self					,						__lowerCamelCase  ):



     '''simple docstring'''




     if not isinstance(__lowerCamelCase					,						__lowerCamelCase  ):
         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 					UpperCamelCase__(    self					,						__lowerCamelCase					,						__lowerCamelCase = 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    :						str						 =     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    :						Union[str, Any]						 =     text

 def 					UpperCamelCase__(    self  ):



     '''simple docstring'''




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

 def 					UpperCamelCase__(    self					,						__lowerCamelCase  ):



     '''simple docstring'''




     self.generated_responses.append(__lowerCamelCase  )

 def 					UpperCamelCase__(    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    :						Optional[Any]						 =     F"""Conversation id: {self.uuid} \n"""
     for is_user, text in self.iter_texts():
         __A    :						Tuple						 =     '''user''' if is_user else '''bot'''
         output += F"""{name} >> {text} \n"""
     return output






@add_end_docstrings(
    SCREAMING_SNAKE_CASE__							,							R"""
        min_length_for_response (`int`, *optional*, defaults to 32):
            The minimum length (in number of tokens) for a response.
        minimum_tokens (`int`, *optional*, defaults to 10):
            The minimum length of tokens to leave for a response.
    """							,							)
class 		__snake_case					(		SCREAMING_SNAKE_CASE__     ):





 """simple docstring"""


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



     '''simple docstring'''




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

 def 					UpperCamelCase__(    self					,						__lowerCamelCase=None					,						__lowerCamelCase=None					,						__lowerCamelCase=None					,						**__lowerCamelCase  ):



     '''simple docstring'''




     __A    :						str						 =     {}
     __A    :						List[str]						 =     {}
     __A    :						Any						 =     {}

     if min_length_for_response is not None:
         __A    :						int						 =     min_length_for_response
     if minimum_tokens is not None:
         __A    :						Any						 =     minimum_tokens

     if "max_length" in generate_kwargs:
         __A    :						List[Any]						 =     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    :						str						 =     clean_up_tokenization_spaces

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

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



     '''simple docstring'''




     __A    :						Any						 =     super().__call__(__lowerCamelCase					,						num_workers=__lowerCamelCase					,						**__lowerCamelCase  )
     if isinstance(__lowerCamelCase					,						__lowerCamelCase  ) and len(__lowerCamelCase  ) == 1:
         return outputs[0]
     return outputs

 def 					UpperCamelCase__(    self					,						__lowerCamelCase					,						__lowerCamelCase=32  ):



     '''simple docstring'''




     if not isinstance(__lowerCamelCase					,						__lowerCamelCase  ):
         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    :						List[Any]						 =     self.tokenizer._build_conversation_input_ids(__lowerCamelCase  )
     else:
         # If the tokenizer cannot handle conversations, we default to only the old version
         __A    :						int						 =     self._legacy_parse_and_tokenize(__lowerCamelCase  )

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

 def 					UpperCamelCase__(    self					,						__lowerCamelCase					,						__lowerCamelCase=10					,						**__lowerCamelCase  ):



     '''simple docstring'''




     __A    :						Tuple						 =     generate_kwargs.get('''max_length'''					,						self.model.config.max_length  )

     __A    :						str						 =     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    :						str						 =     max_length - minimum_tokens
         __A    :						Any						 =     model_inputs['''input_ids'''][:, -trim:]
         if "attention_mask" in model_inputs:
             __A    :						Union[str, Any]						 =     model_inputs['''attention_mask'''][:, -trim:]
     __A    :						Dict						 =     model_inputs.pop('''conversation'''  )
     __A    :						List[str]						 =     max_length
     __A    :						Dict						 =     self.model.generate(**__lowerCamelCase					,						**__lowerCamelCase  )
     if self.model.config.is_encoder_decoder:
         __A    :						Any						 =     1
     else:
         __A    :						List[Any]						 =     n
     return {"output_ids": output_ids[:, start_position:], "conversation": conversation}

 def 					UpperCamelCase__(    self					,						__lowerCamelCase					,						__lowerCamelCase=True  ):



     '''simple docstring'''




     __A    :						int						 =     model_outputs['''output_ids''']
     __A    :						Optional[int]						 =     self.tokenizer.decode(
         output_ids[0]					,						skip_special_tokens=__lowerCamelCase					,						clean_up_tokenization_spaces=__lowerCamelCase					,						)
     __A    :						Dict						 =     model_outputs['''conversation''']
     conversation.mark_processed()
     conversation.append_response(__lowerCamelCase  )
     return conversation







 def 					UpperCamelCase__(    self					,						__lowerCamelCase  ):



     '''simple docstring'''




     __A    :						Tuple						 =     self.tokenizer.eos_token_id
     __A    :						List[str]						 =     []
     for is_user, text in conversation.iter_texts():
         if eos_token_id is not None:
             input_ids.extend(self.tokenizer.encode(__lowerCamelCase					,						add_special_tokens=__lowerCamelCase  ) + [eos_token_id]  )
         else:
             input_ids.extend(self.tokenizer.encode(__lowerCamelCase					,						add_special_tokens=__lowerCamelCase  )  )

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




