


import warnings
from typing import Dict

import numpy as np

from ..utils import ExplicitEnum, add_end_docstrings, is_tf_available, is_torch_available
from .base import PIPELINE_INIT_ARGS, GenericTensor, Pipeline


if is_tf_available():
  from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING

if is_torch_available():
  from ..models.auto.modeling_auto import MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING




def 	__lowercase		(  _UpperCamelCase   )			->str:







      """simple docstring"""

      return 1.0 / (1.0 + np.exp(-_outputs   ))




def 	__lowercase		(  _UpperCamelCase   )			->Dict:







      """simple docstring"""

      lowercase	:      Union[str, Any]          =						np.max(_outputs,			axis=-1,			keepdims=snake_case_   )
      lowercase	:      Optional[int]          =						np.exp(_outputs - maxes   )
      return shifted_exp / shifted_exp.sum(axis=-1,			keepdims=snake_case_   )







class  __SCREAMING_SNAKE_CASE      (	UpperCamelCase__      ):
      A :							List[str]										=   """sigmoid"""
      A :							Tuple										=   """softmax"""
      A :							Union[str, Any]										=   """none"""







@add_end_docstrings(
    UpperCamelCase__     ,  r'\n        return_all_scores (`bool`, *optional*, defaults to `False`):\n            Whether to return all prediction scores or just the one of the predicted class.\n        function_to_apply (`str`, *optional*, defaults to `\"default\"`):\n            The function to apply to the model outputs in order to retrieve the scores. Accepts four different values:\n\n            - `\"default\"`: if the model has a single label, will apply the sigmoid function on the output. If the model\n              has several labels, will apply the softmax function on the output.\n            - `\"sigmoid\"`: Applies the sigmoid function on the output.\n            - `\"softmax\"`: Applies the softmax function on the output.\n            - `\"none\"`: Does not apply any function on the output.\n    '     ,  )
class  __SCREAMING_SNAKE_CASE      (	UpperCamelCase__      ):
      A :							Optional[Any]										=   False
      A :							List[str]										=   ClassificationFunction.NONE


      def __init__(							self							,  **SCREAMING_SNAKE_CASE__						):
            super().__init__(**__a						)

            self.check_model_type(
                TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING
                if self.framework == '''tf'''
                else MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING						)


      def 				__lowerCamelCase	(							self							,  SCREAMING_SNAKE_CASE__=None							,  SCREAMING_SNAKE_CASE__=None							,  SCREAMING_SNAKE_CASE__=""							,  **SCREAMING_SNAKE_CASE__						):
            # Using "" as default argument because we're going to use `top_k=None` in user code to declare
            # "No top_k"
            lowercase	:      Union[str, Any]          =						tokenizer_kwargs

            lowercase	:      List[str]          =						{}
            if hasattr(self.model.config							,  '''return_all_scores'''						) and return_all_scores is None:
                  lowercase	:      Any          =						self.model.config.return_all_scores

            if isinstance(__a							,  __a						) or top_k is None:
                  lowercase	:      Union[str, Any]          =						top_k
                  lowercase	:      List[Any]          =						False
            elif return_all_scores is not None:
                  warnings.warn(
                      '''`return_all_scores` is now deprecated,  if want a similar functionality use `top_k=None` instead of'''
                      ''' `return_all_scores=True` or `top_k=1` instead of `return_all_scores=False`.'''							,  __a							,  )
                  if return_all_scores:
                        lowercase	:      Tuple          =						None
                  else:
                        lowercase	:      Optional[Any]          =						1

            if isinstance(__a							,  __a						):
                  lowercase	:      int          =						ClassificationFunction[function_to_apply.upper()]

            if function_to_apply is not None:
                  lowercase	:      List[Any]          =						function_to_apply
            return preprocess_params, {}, postprocess_params


      def __call__(							self							,  *SCREAMING_SNAKE_CASE__							,  **SCREAMING_SNAKE_CASE__						):
            lowercase	:      Optional[Any]          =						super().__call__(*__a							,  **__a						)
            # TODO try and retrieve it in a nicer way from _sanitize_parameters.
            lowercase	:      Optional[int]          =						'''top_k''' not in kwargs
            if isinstance(args[0]							,  __a						) and _legacy:
                  # This pipeline is odd, and return a list when single item is run
                  return [result]
            else:
                  return result


      def 				__lowerCamelCase	(							self							,  SCREAMING_SNAKE_CASE__							,  **SCREAMING_SNAKE_CASE__						):
            lowercase	:      Tuple          =						self.framework
            if isinstance(__a							,  __a						):
                  return self.tokenizer(**__a							,  return_tensors=__a							,  **__a						)
            elif isinstance(__a							,  __a						) and len(__a						) == 1 and isinstance(inputs[0]							,  __a						) and len(inputs[0]						) == 2:
                  # It used to be valid to use a list of list of list for text pairs, keeping this path for BC
                  return self.tokenizer(
                      text=inputs[0][0]							,  text_pair=inputs[0][1]							,  return_tensors=__a							,  **__a						)
            elif isinstance(__a							,  __a						):
                  # This is likely an invalid usage of the pipeline attempting to pass text pairs.
                  raise ValueError(
                      '''The pipeline received invalid inputs, if you are trying to send text pairs, you can try to send a'''
                      ''' dictionary `{\"text\": \"My text\", \"text_pair\": \"My pair\"}` in order to send a text pair.'''						)
            return self.tokenizer(__a							,  return_tensors=__a							,  **__a						)


      def 				__lowerCamelCase	(							self							,  SCREAMING_SNAKE_CASE__						):
            return self.model(**__a						)






      def 				__lowerCamelCase	(							self							,  SCREAMING_SNAKE_CASE__							,  SCREAMING_SNAKE_CASE__=None							,  SCREAMING_SNAKE_CASE__=1							,  SCREAMING_SNAKE_CASE__=True						):
            # `_legacy` is used to determine if we're running the naked pipeline and in backward
            # compatibility mode, or if running the pipeline with `pipeline(..., top_k=1)` we're running
            # the more natural result containing the list.
            # Default value before `set_parameters`
            if function_to_apply is None:
                  if self.model.config.problem_type == "multi_label_classification" or self.model.config.num_labels == 1:
                        lowercase	:      Optional[Any]          =						ClassificationFunction.SIGMOID
                  elif self.model.config.problem_type == "single_label_classification" or self.model.config.num_labels > 1:
                        lowercase	:      Tuple          =						ClassificationFunction.SOFTMAX
                  elif hasattr(self.model.config							,  '''function_to_apply'''						) and function_to_apply is None:
                        lowercase	:      Union[str, Any]          =						self.model.config.function_to_apply
                  else:
                        lowercase	:      List[Any]          =						ClassificationFunction.NONE

            lowercase	:      Dict          =						model_outputs['''logits'''][0]
            lowercase	:      Dict          =						outputs.numpy()

            if function_to_apply == ClassificationFunction.SIGMOID:
                  lowercase	:      str          =						sigmoid(__a						)
            elif function_to_apply == ClassificationFunction.SOFTMAX:
                  lowercase	:      List[str]          =						softmax(__a						)
            elif function_to_apply == ClassificationFunction.NONE:
                  lowercase	:      Optional[Any]          =						outputs
            else:
                  raise ValueError(f"""Unrecognized `function_to_apply` argument: {function_to_apply}"""						)

            if top_k == 1 and _legacy:
                  return {"label": self.model.config.idalabel[scores.argmax().item()], "score": scores.max().item()}

            lowercase	:      Optional[int]          =						[
                {'''label''': self.model.config.idalabel[i], '''score''': score.item()} for i, score in enumerate(__a						)
            ]
            if not _legacy:
                  dict_scores.sort(key=lambda SCREAMING_SNAKE_CASE__						: x["score"]							,  reverse=__a						)
                  if top_k is not None:
                        lowercase	:      int          =						dict_scores[:top_k]
            return dict_scores


'''simple docstring'''





import logging
import os
from dataclasses import dataclass
from typing import List, Optional, Union

import tqdm
from filelock import FileLock

from transformers import (
    BartTokenizer,
    BartTokenizerFast,
    DataProcessor,
    PreTrainedTokenizer,
    RobertaTokenizer,
    RobertaTokenizerFast,
    XLMRobertaTokenizer,
    is_tf_available,
    is_torch_available,
)


SCREAMING_SNAKE_CASE_:     str  =logging.getLogger(__name__)




@dataclass(frozen=UpperCamelCase__	)
class        __A      :
    a__       :						str
    a__       :						str
    a__       :						Optional[str]          =  None
    a__       :						Optional[str]          =  None
    a__       :						Optional[str]          =  None




@dataclass(frozen=UpperCamelCase__	)
class        __A      :
    a__       :						List[int]
    a__       :						Optional[List[int]]          =  None
    a__       :						Optional[List[int]]          =  None
    a__       :						Optional[Union[int, float]]          =  None
    a__       :						Optional[int]          =  None


if is_torch_available():
    import torch
    from torch.utils.data import Dataset




    class        __A      ( UpperCamelCase__	):
        a__       :						List[InputFeatures]



        def __init__(self    :    Any   ,			__a    :    str   ,			__a    :    PreTrainedTokenizer   ,			__a    :    str   ,			__a    :    Optional[int] = None   ,			__a    :    Dict=False   ,			__a    :    bool = False   ,			):
             UpperCAmelCase_           =   hans_processors[task]()

             UpperCAmelCase_           =   os.path.join(
                 __a   ,			"cached_{}_{}_{}_{}".format(
                     "dev" if evaluate else "train"   ,			tokenizer.__class__.__name__   ,			str(__a )   ,			__a   ,			)   ,			)
             UpperCAmelCase_           =   processor.get_labels()
             if tokenizer.__class__ in (
                 RobertaTokenizer,
                 RobertaTokenizerFast,
                 XLMRobertaTokenizer,
                 BartTokenizer,
                 BartTokenizerFast,
             ):
                  # HACK(label indices are swapped in RoBERTa pretrained model)
                  UpperCAmelCase_    , UpperCAmelCase_           =   label_list[2], label_list[1]
             UpperCAmelCase_           =   label_list

             # Make sure only the first process in distributed training processes the dataset,
             # and the others will use the cache.
             UpperCAmelCase_           =   cached_features_file + ".lock"
             with FileLock(__a ):
                  if os.path.exists(__a ) and not overwrite_cache:
                       logger.info(f"""Loading features from cached file {cached_features_file}""" )
                       UpperCAmelCase_           =   torch.load(__a )
                  else:
                       logger.info(f"""Creating features from dataset file at {data_dir}""" )

                       UpperCAmelCase_           =   (
                           processor.get_dev_examples(__a ) if evaluate else processor.get_train_examples(__a )
                       )

                       logger.info("Training examples: %s"   ,			len(__a ) )
                       UpperCAmelCase_           =   hans_convert_examples_to_features(__a   ,			__a   ,			__a   ,			__a )
                       logger.info("Saving features into cached file %s"   ,			__a )
                       torch.save(self.features   ,			__a )



        def __len__(self    :    List[Any] ):
             return len(self.features )



        def __getitem__(self    :    Any   ,			__a    :    Optional[Any] ):
             return self.features[i]






        def 					_lowercase						(self    :    Union[str, Any] ):
             return self.label_list


if is_tf_available():
    import tensorflow as tf




    class        __A      :
        a__       :						List[InputFeatures]



        def __init__(self    :    Union[str, Any]   ,			__a    :    str   ,			__a    :    PreTrainedTokenizer   ,			__a    :    str   ,			__a    :    Optional[int] = 128   ,			__a    :    Any=False   ,			__a    :    bool = False   ,			):
             UpperCAmelCase_           =   hans_processors[task]()
             UpperCAmelCase_           =   processor.get_labels()
             if tokenizer.__class__ in (
                 RobertaTokenizer,
                 RobertaTokenizerFast,
                 XLMRobertaTokenizer,
                 BartTokenizer,
                 BartTokenizerFast,
             ):
                  # HACK(label indices are swapped in RoBERTa pretrained model)
                  UpperCAmelCase_    , UpperCAmelCase_           =   label_list[2], label_list[1]
             UpperCAmelCase_           =   label_list

             UpperCAmelCase_           =   processor.get_dev_examples(__a ) if evaluate else processor.get_train_examples(__a )
             UpperCAmelCase_           =   hans_convert_examples_to_features(__a   ,			__a   ,			__a   ,			__a )

             def gen():
                  for ex_index, ex in tqdm.tqdm(enumerate(self.features )   ,			desc="convert examples to features" ):
                       if ex_index % 10000 == 0:
                            logger.info("Writing example %d of %d" % (ex_index, len(__a )) )

                       yield (
                           {
                               "example_id": 0,
                               "input_ids": ex.input_ids,
                               "attention_mask": ex.attention_mask,
                               "token_type_ids": ex.token_type_ids,
                           },
                           ex.label,
                       )

             UpperCAmelCase_           =   tf.data.Dataset.from_generator(
                 __a   ,			(
                     {
                         "example_id": tf.intaa,
                         "input_ids": tf.intaa,
                         "attention_mask": tf.intaa,
                         "token_type_ids": tf.intaa,
                     },
                     tf.intaa,
                 )   ,			(
                     {
                         "example_id": tf.TensorShape([] ),
                         "input_ids": tf.TensorShape([None, None] ),
                         "attention_mask": tf.TensorShape([None, None] ),
                         "token_type_ids": tf.TensorShape([None, None] ),
                     },
                     tf.TensorShape([] ),
                 )   ,			)



        def 					_lowercase						(self    :    int ):
             return self.dataset



        def __len__(self    :    Any ):
             return len(self.features )



        def __getitem__(self    :    int   ,			__a    :    Union[str, Any] ):
             return self.features[i]






        def 					_lowercase						(self    :    int ):
             return self.label_list




class        __A      ( UpperCamelCase__	):



    def 					_lowercase						(self    :    List[Any]   ,			__a    :    Dict ):
         return self._create_examples(self._read_tsv(os.path.join(__a   ,			"heuristics_train_set.txt" ) )   ,			"train" )



    def 					_lowercase						(self    :    Any   ,			__a    :    List[Any] ):
         return self._create_examples(self._read_tsv(os.path.join(__a   ,			"heuristics_evaluation_set.txt" ) )   ,			"dev" )



    def 					_lowercase						(self    :    Any ):
         return ["contradiction", "entailment", "neutral"]






    def 					_lowercase						(self    :    Union[str, Any]   ,			__a    :    Optional[int]   ,			__a    :    Union[str, Any] ):
         UpperCAmelCase_           =   []
         for i, line in enumerate(__a ):
              if i == 0:
                   continue
              UpperCAmelCase_           =   "%s-%s" % (set_type, line[0])
              UpperCAmelCase_           =   line[5]
              UpperCAmelCase_           =   line[6]
              UpperCAmelCase_           =   line[7][2:] if line[7].startswith("ex" ) else line[7]
              UpperCAmelCase_           =   line[0]
              examples.append(InputExample(guid=__a   ,			text_a=__a   ,			text_b=__a   ,			label=__a   ,			pairID=__a ) )
         return examples





def 	lowerCAmelCase_				(					snake_case_       : List[InputExample]					,    snake_case_       : List[str]					,    snake_case_       : int					,    snake_case_       : PreTrainedTokenizer					,    )    ->      Optional[Any]:


     '''simple docstring'''




     UpperCAmelCase_           =   {label: i for i, label in enumerate(snake_case_			)}

     UpperCAmelCase_           =   []
     for ex_index, example in tqdm.tqdm(enumerate(snake_case_			)					,    desc="convert examples to features"			):
          if ex_index % 1_00_00 == 0:
               logger.info("Writing example %d" % (ex_index)			)

          UpperCAmelCase_           =   tokenizer(
              example.text_a					,    example.text_b					,    add_special_tokens=snake_case_					,    max_length=snake_case_					,    padding="max_length"					,    truncation=snake_case_					,    return_overflowing_tokens=snake_case_					,    )

          UpperCAmelCase_           =   label_map[example.label] if example.label in label_map else 0

          UpperCAmelCase_           =   int(example.pairID			)

          features.append(InputFeatures(**snake_case_					,    label=snake_case_					,    pairID=snake_case_			)			)

     for i, example in enumerate(examples[:5]			):
          logger.info("*** Example ***"			)
          logger.info(f"""guid: {example}"""			)
          logger.info(f"""features: {features[i]}"""			)

     return features


SCREAMING_SNAKE_CASE_:     int  ={
    'hans': 3,
}






SCREAMING_SNAKE_CASE_:     Any  ={
    'hans': HansProcessor,
}




