




'''simple docstring'''





from typing import List, Union

import numpy as np

from ..tokenization_utils import TruncationStrategy
from ..utils import add_end_docstrings, logging
from .base import PIPELINE_INIT_ARGS, ArgumentHandler, ChunkPipeline


UpperCamelCase_		:     List[str]          =		logging.get_logger(__name__)

class   _a							(		__lowerCAmelCase				):






     def 							_lowercase      (					self   ,_SCREAMING_SNAKE_CASE       )				->     int:
          if isinstance(_SCREAMING_SNAKE_CASE   ,_SCREAMING_SNAKE_CASE       ):
               _snake_case				= [label.strip() for label in labels.split(","       ) if label.strip()]
          return labels






     def __call__(					self   ,_SCREAMING_SNAKE_CASE   ,_SCREAMING_SNAKE_CASE   ,_SCREAMING_SNAKE_CASE       )				->     Optional[Any]:
          if len(_SCREAMING_SNAKE_CASE       ) == 0 or len(_SCREAMING_SNAKE_CASE       ) == 0:
               raise ValueError("You must include at least one label and at least one sequence."       )
          if hypothesis_template.format(labels[0]       ) == hypothesis_template:
               raise ValueError(
                   (
                       "The provided hypothesis_template \"{}\" was not able to be formatted with the target labels. "
                       "Make sure the passed template includes formatting syntax such as {{}} where the label should go."
                   ).format(_SCREAMING_SNAKE_CASE       )       )

          if isinstance(_SCREAMING_SNAKE_CASE   ,_SCREAMING_SNAKE_CASE       ):
               _snake_case				= [sequences]

          _snake_case				= []
          for sequence in sequences:
               sequence_pairs.extend([[sequence, hypothesis_template.format(_SCREAMING_SNAKE_CASE       )] for label in labels]       )

          return sequence_pairs, sequences







@add_end_docstrings(__lowerCAmelCase				)
class   _a							(		__lowerCAmelCase				):






     def __init__(					self   ,_SCREAMING_SNAKE_CASE=ZeroShotClassificationArgumentHandler()   ,*_SCREAMING_SNAKE_CASE   ,**_SCREAMING_SNAKE_CASE       )				->     List[Any]:
          _snake_case				= args_parser
          super().__init__(*_SCREAMING_SNAKE_CASE   ,**_SCREAMING_SNAKE_CASE       )
          if self.entailment_id == -1:
               logger.warning(
                   "Failed to determine 'entailment' label id from the label2id mapping in the model config. Setting to "
                   "-1. Define a descriptive label2id mapping in the model config to ensure correct outputs."       )






     @property
     def 							_lowercase      (					self       )				->     str:
          for label, ind in self.model.config.labelaid.items():
               if label.lower().startswith("entail"       ):
                    return ind
          return -1






     def 							_lowercase      (					self   ,_SCREAMING_SNAKE_CASE   ,_SCREAMING_SNAKE_CASE=True   ,_SCREAMING_SNAKE_CASE=True   ,_SCREAMING_SNAKE_CASE=TruncationStrategy.ONLY_FIRST   ,**_SCREAMING_SNAKE_CASE       )				->     Dict:
          _snake_case				= self.framework
          if self.tokenizer.pad_token is None:
               # Override for tokenizers not supporting padding
               logger.error(
                   "Tokenizer was not supporting padding necessary for zero-shot, attempting to use "
                   " `pad_token=eos_token`"       )
               _snake_case				= self.tokenizer.eos_token
          try:
               _snake_case				= self.tokenizer(
                   _SCREAMING_SNAKE_CASE   ,add_special_tokens=_SCREAMING_SNAKE_CASE   ,return_tensors=_SCREAMING_SNAKE_CASE   ,padding=_SCREAMING_SNAKE_CASE   ,truncation=_SCREAMING_SNAKE_CASE   ,)
          except Exception as e:
               if "too short" in str(_SCREAMING_SNAKE_CASE       ):
                    # tokenizers might yell that we want to truncate
                    # to a value that is not even reached by the input.
                    # In that case we don't want to truncate.
                    # It seems there's not a really better way to catch that
                    # exception.

                    _snake_case				= self.tokenizer(
                        _SCREAMING_SNAKE_CASE   ,add_special_tokens=_SCREAMING_SNAKE_CASE   ,return_tensors=_SCREAMING_SNAKE_CASE   ,padding=_SCREAMING_SNAKE_CASE   ,truncation=TruncationStrategy.DO_NOT_TRUNCATE   ,)
               else:
                    raise e

          return inputs






     def 							_lowercase      (					self   ,**_SCREAMING_SNAKE_CASE       )				->     List[Any]:
          if kwargs.get("multi_class"   ,_SCREAMING_SNAKE_CASE       ) is not None:
               _snake_case				= kwargs["multi_class"]
               logger.warning(
                   "The `multi_class` argument has been deprecated and renamed to `multi_label`. "
                   "`multi_class` will be removed in a future version of Transformers."       )
          _snake_case				= {}
          if "candidate_labels" in kwargs:
               _snake_case				= self._args_parser._parse_labels(kwargs["candidate_labels"]       )
          if "hypothesis_template" in kwargs:
               _snake_case				= kwargs["hypothesis_template"]

          _snake_case				= {}
          if "multi_label" in kwargs:
               _snake_case				= kwargs["multi_label"]
          return preprocess_params, {}, postprocess_params






     def __call__(					self   ,_SCREAMING_SNAKE_CASE   ,*_SCREAMING_SNAKE_CASE   ,**_SCREAMING_SNAKE_CASE   ,)				->     Dict:
          if len(_SCREAMING_SNAKE_CASE       ) == 0:
               pass
          elif len(_SCREAMING_SNAKE_CASE       ) == 1 and "candidate_labels" not in kwargs:
               _snake_case				= args[0]
          else:
               raise ValueError(f"""Unable to understand extra arguments {args}"""       )

          return super().__call__(_SCREAMING_SNAKE_CASE   ,**_SCREAMING_SNAKE_CASE       )






     def 							_lowercase      (					self   ,_SCREAMING_SNAKE_CASE   ,_SCREAMING_SNAKE_CASE=None   ,_SCREAMING_SNAKE_CASE="This example is {}."       )				->     List[str]:
          _snake_case				,   _snake_case				= self._args_parser(_SCREAMING_SNAKE_CASE   ,_SCREAMING_SNAKE_CASE   ,_SCREAMING_SNAKE_CASE       )

          for i, (candidate_label, sequence_pair) in enumerate(zip(_SCREAMING_SNAKE_CASE   ,_SCREAMING_SNAKE_CASE       )       ):
               _snake_case				= self._parse_and_tokenize([sequence_pair]       )

               yield {
                   "candidate_label": candidate_label,
                   "sequence": sequences[0],
                   "is_last": i == len(_SCREAMING_SNAKE_CASE       ) - 1,
                   **model_input,
               }






     def 							_lowercase      (					self   ,_SCREAMING_SNAKE_CASE       )				->     Dict:
          _snake_case				= inputs["candidate_label"]
          _snake_case				= inputs["sequence"]
          _snake_case				= {k: inputs[k] for k in self.tokenizer.model_input_names}
          _snake_case				= self.model(**_SCREAMING_SNAKE_CASE       )

          _snake_case				= {
              "candidate_label": candidate_label,
              "sequence": sequence,
              "is_last": inputs["is_last"],
              **outputs,
          }
          return model_outputs






     def 							_lowercase      (					self   ,_SCREAMING_SNAKE_CASE   ,_SCREAMING_SNAKE_CASE=False       )				->     List[Any]:
          _snake_case				= [outputs["candidate_label"] for outputs in model_outputs]
          _snake_case				= [outputs["sequence"] for outputs in model_outputs]
          _snake_case				= np.concatenate([output["logits"].numpy() for output in model_outputs]       )
          _snake_case				= logits.shape[0]
          _snake_case				= len(_SCREAMING_SNAKE_CASE       )
          _snake_case				= N // n
          _snake_case				= logits.reshape((num_sequences, n, -1)       )

          if multi_label or len(_SCREAMING_SNAKE_CASE       ) == 1:
               # softmax over the entailment vs. contradiction dim for each label independently
               _snake_case				= self.entailment_id
               _snake_case				= -1 if entailment_id == 0 else 0
               _snake_case				= reshaped_outputs[..., [contradiction_id, entailment_id]]
               _snake_case				= np.exp(_SCREAMING_SNAKE_CASE       ) / np.exp(_SCREAMING_SNAKE_CASE       ).sum(-1   ,keepdims=_SCREAMING_SNAKE_CASE       )
               _snake_case				= scores[..., 1]
          else:
               # softmax the "entailment" logits over all candidate labels
               _snake_case				= reshaped_outputs[..., self.entailment_id]
               _snake_case				= np.exp(_SCREAMING_SNAKE_CASE       ) / np.exp(_SCREAMING_SNAKE_CASE       ).sum(-1   ,keepdims=_SCREAMING_SNAKE_CASE       )

          _snake_case				= list(reversed(scores[0].argsort()       )       )
          return {
              "sequence": sequences[0],
              "labels": [candidate_labels[i] for i in top_inds],
              "scores": scores[0, top_inds].tolist(),
          }








'''simple docstring'''





import json
from typing import List, Optional, Tuple

from tokenizers import pre_tokenizers, processors

from ...tokenization_utils_base import AddedToken, BatchEncoding
from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_bart import BartTokenizer


UpperCamelCase_		:     Union[str, Any]          =		logging.get_logger(__name__)


UpperCamelCase_		:     Optional[Any]          =		{'''vocab_file''': '''vocab.json''', '''merges_file''': '''merges.txt''', '''tokenizer_file''': '''tokenizer.json'''}

# See all BART models at https://huggingface.co/models?filter=bart
UpperCamelCase_		:     Dict          =		{
    '''vocab_file''': {
        '''facebook/bart-base''': '''https://huggingface.co/facebook/bart-base/resolve/main/vocab.json''',
        '''facebook/bart-large''': '''https://huggingface.co/facebook/bart-large/resolve/main/vocab.json''',
        '''facebook/bart-large-mnli''': '''https://huggingface.co/facebook/bart-large-mnli/resolve/main/vocab.json''',
        '''facebook/bart-large-cnn''': '''https://huggingface.co/facebook/bart-large-cnn/resolve/main/vocab.json''',
        '''facebook/bart-large-xsum''': '''https://huggingface.co/facebook/bart-large-xsum/resolve/main/vocab.json''',
        '''yjernite/bart_eli5''': '''https://huggingface.co/yjernite/bart_eli5/resolve/main/vocab.json''',
    },
    '''merges_file''': {
        '''facebook/bart-base''': '''https://huggingface.co/facebook/bart-base/resolve/main/merges.txt''',
        '''facebook/bart-large''': '''https://huggingface.co/facebook/bart-large/resolve/main/merges.txt''',
        '''facebook/bart-large-mnli''': '''https://huggingface.co/facebook/bart-large-mnli/resolve/main/merges.txt''',
        '''facebook/bart-large-cnn''': '''https://huggingface.co/facebook/bart-large-cnn/resolve/main/merges.txt''',
        '''facebook/bart-large-xsum''': '''https://huggingface.co/facebook/bart-large-xsum/resolve/main/merges.txt''',
        '''yjernite/bart_eli5''': '''https://huggingface.co/yjernite/bart_eli5/resolve/main/merges.txt''',
    },
    '''tokenizer_file''': {
        '''facebook/bart-base''': '''https://huggingface.co/facebook/bart-base/resolve/main/tokenizer.json''',
        '''facebook/bart-large''': '''https://huggingface.co/facebook/bart-large/resolve/main/tokenizer.json''',
        '''facebook/bart-large-mnli''': '''https://huggingface.co/facebook/bart-large-mnli/resolve/main/tokenizer.json''',
        '''facebook/bart-large-cnn''': '''https://huggingface.co/facebook/bart-large-cnn/resolve/main/tokenizer.json''',
        '''facebook/bart-large-xsum''': '''https://huggingface.co/facebook/bart-large-xsum/resolve/main/tokenizer.json''',
        '''yjernite/bart_eli5''': '''https://huggingface.co/yjernite/bart_eli5/resolve/main/tokenizer.json''',
    },
}

UpperCamelCase_		:     Tuple          =		{
    '''facebook/bart-base''': 1024,
    '''facebook/bart-large''': 1024,
    '''facebook/bart-large-mnli''': 1024,
    '''facebook/bart-large-cnn''': 1024,
    '''facebook/bart-large-xsum''': 1024,
    '''yjernite/bart_eli5''': 1024,
}

class   _a							(		__lowerCAmelCase				):
     SCREAMING_SNAKE_CASE_				:       Union[str, Any]       							=		VOCAB_FILES_NAMES
     SCREAMING_SNAKE_CASE_				:       int       							=		PRETRAINED_VOCAB_FILES_MAP
     SCREAMING_SNAKE_CASE_				:       Optional[Any]       							=		PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
     SCREAMING_SNAKE_CASE_				:       Any       							=		["""input_ids""", """attention_mask"""]
     SCREAMING_SNAKE_CASE_				:       str       							=		BartTokenizer






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

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

          _snake_case				= add_prefix_space

          # the pre_tokenizer is already updated in the GPT2TokenizerFast `__init__`
          _snake_case				= "post_processor"
          _snake_case				= getattr(self.backend_tokenizer   ,_SCREAMING_SNAKE_CASE   ,_SCREAMING_SNAKE_CASE       )
          if tokenizer_component_instance:
               _snake_case				= 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:
                    _snake_case				= tuple(state["sep"]       )
               if "cls" in state:
                    _snake_case				= tuple(state["cls"]       )

               _snake_case				= False

               if state.get("add_prefix_space"   ,_SCREAMING_SNAKE_CASE       ) != add_prefix_space:
                    _snake_case				= add_prefix_space
                    _snake_case				= True

               if state.get("trim_offsets"   ,_SCREAMING_SNAKE_CASE       ) != trim_offsets:
                    _snake_case				= trim_offsets
                    _snake_case				= True

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






     @property
     def 							_lowercase      (					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 							_lowercase      (					self   ,_SCREAMING_SNAKE_CASE       )				->     str:
          _snake_case				= AddedToken(_SCREAMING_SNAKE_CASE   ,lstrip=_SCREAMING_SNAKE_CASE   ,rstrip=_SCREAMING_SNAKE_CASE       ) if isinstance(_SCREAMING_SNAKE_CASE   ,_SCREAMING_SNAKE_CASE       ) else value
          _snake_case				= value






     def 							_lowercase      (					self   ,*_SCREAMING_SNAKE_CASE   ,**_SCREAMING_SNAKE_CASE       )				->     BatchEncoding:
          _snake_case				= kwargs.get("is_split_into_words"   ,_SCREAMING_SNAKE_CASE       )

          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(*_SCREAMING_SNAKE_CASE   ,**_SCREAMING_SNAKE_CASE       )






     def 							_lowercase      (					self   ,*_SCREAMING_SNAKE_CASE   ,**_SCREAMING_SNAKE_CASE       )				->     BatchEncoding:
          _snake_case				= kwargs.get("is_split_into_words"   ,_SCREAMING_SNAKE_CASE       )

          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(*_SCREAMING_SNAKE_CASE   ,**_SCREAMING_SNAKE_CASE       )






     def 							_lowercase      (					self   ,_SCREAMING_SNAKE_CASE   ,_SCREAMING_SNAKE_CASE = None       )				->     Tuple[str]:
          _snake_case				= self._tokenizer.model.save(_SCREAMING_SNAKE_CASE   ,name=_SCREAMING_SNAKE_CASE       )
          return tuple(_SCREAMING_SNAKE_CASE       )






     def 							_lowercase      (					self   ,_SCREAMING_SNAKE_CASE   ,_SCREAMING_SNAKE_CASE=None       )				->     Tuple:
          _snake_case				= [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 							_lowercase      (					self   ,_SCREAMING_SNAKE_CASE   ,_SCREAMING_SNAKE_CASE = None       )				->     List[int]:
          _snake_case				= [self.sep_token_id]
          _snake_case				= [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]


