


"""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      		=			logging.get_logger(__name__)
class 					__magic_name__		(			__UpperCAmelCase     ):






       def  __snake_case						(   self  :    List[Any]  ,     snake_case__  :    List[str]				):

              '''simple docstring'''






              if isinstance(snake_case__  ,     snake_case__				):
                     lowercase		:Union[str, Any]       							=				[label.strip() for label in labels.split(''','''				) if label.strip()]
              return labels







       def __call__(   self  :    str  ,     snake_case__  :    int  ,     snake_case__  :    Optional[int]  ,     snake_case__  :    Optional[int]				):

              '''simple docstring'''






              if len(snake_case__				) == 0 or len(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(snake_case__				)				)

              if isinstance(snake_case__  ,     snake_case__				):
                     lowercase		:Union[str, Any]       							=				[sequences]

              lowercase		:Optional[int]       							=				[]
              for sequence in sequences:
                     sequence_pairs.extend([[sequence, hypothesis_template.format(snake_case__				)] for label in labels]				)

              return sequence_pairs, sequences







@add_end_docstrings(__UpperCAmelCase     )
class 					__magic_name__		(			__UpperCAmelCase     ):






       def __init__(   self  :    Optional[Any]  ,     snake_case__  :    Union[str, Any]=ZeroShotClassificationArgumentHandler()  ,     *snake_case__  :    Optional[int]  ,     **snake_case__  :    Tuple				):

              '''simple docstring'''






              lowercase		:List[Any]       							=				args_parser
              super().__init__(*snake_case__  ,     **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  __snake_case						(   self  :    Dict				):

              '''simple docstring'''






              for label, ind in self.model.config.labelaid.items():
                     if label.lower().startswith('''entail'''				):
                            return ind
              return -1






       def  __snake_case						(   self  :    Tuple  ,     snake_case__  :    Optional[int]  ,     snake_case__  :    List[str]=True  ,     snake_case__  :    Optional[Any]=True  ,     snake_case__  :    int=TruncationStrategy.ONLY_FIRST  ,     **snake_case__  :    Tuple				):

              '''simple docstring'''






              lowercase		:Optional[Any]       							=				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`'''				)
                     lowercase		:List[str]       							=				self.tokenizer.eos_token
              try:
                     lowercase		:Optional[Any]       							=				self.tokenizer(
                         snake_case__  ,     add_special_tokens=snake_case__  ,     return_tensors=snake_case__  ,     padding=snake_case__  ,     truncation=snake_case__  ,     )
              except Exception as e:
                     if "too short" in str(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.

                            lowercase		:Optional[Any]       							=				self.tokenizer(
                                snake_case__  ,     add_special_tokens=snake_case__  ,     return_tensors=snake_case__  ,     padding=snake_case__  ,     truncation=TruncationStrategy.DO_NOT_TRUNCATE  ,     )
                     else:
                            raise e

              return inputs






       def  __snake_case						(   self  :    List[Any]  ,     **snake_case__  :    Optional[int]				):

              '''simple docstring'''






              if kwargs.get('''multi_class'''  ,     snake_case__				) is not None:
                     lowercase		:List[Any]       							=				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.'''				)
              lowercase		:int       							=				{}
              if "candidate_labels" in kwargs:
                     lowercase		:Tuple       							=				self._args_parser._parse_labels(kwargs['''candidate_labels''']				)
              if "hypothesis_template" in kwargs:
                     lowercase		:List[Any]       							=				kwargs['''hypothesis_template''']

              lowercase		:Optional[int]       							=				{}
              if "multi_label" in kwargs:
                     lowercase		:List[Any]       							=				kwargs['''multi_label''']
              return preprocess_params, {}, postprocess_params






       def __call__(   self  :    Union[str, Any]  ,     snake_case__  :    Union[str, List[str]]  ,     *snake_case__  :    Optional[int]  ,     **snake_case__  :    int  ,     ):

              '''simple docstring'''






              if len(snake_case__				) == 0:
                     pass
              elif len(snake_case__				) == 1 and "candidate_labels" not in kwargs:
                     lowercase		:Union[str, Any]       							=				args[0]
              else:
                     raise ValueError(f"""Unable to understand extra arguments {args}"""				)

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






       def  __snake_case						(   self  :    int  ,     snake_case__  :    List[Any]  ,     snake_case__  :    List[str]=None  ,     snake_case__  :    Optional[Any]="This example is {}."				):

              '''simple docstring'''






              lowercase							,  lowercase		:int       							=				self._args_parser(snake_case__  ,     snake_case__  ,     snake_case__				)

              for i, (candidate_label, sequence_pair) in enumerate(zip(snake_case__  ,     snake_case__				)				):
                     lowercase		:List[Any]       							=				self._parse_and_tokenize([sequence_pair]				)

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






       def  __snake_case						(   self  :    Optional[int]  ,     snake_case__  :    List[str]				):

              '''simple docstring'''






              lowercase		:List[str]       							=				inputs['''candidate_label''']
              lowercase		:int       							=				inputs['''sequence''']
              lowercase		:List[Any]       							=				{k: inputs[k] for k in self.tokenizer.model_input_names}
              lowercase		:Tuple       							=				self.model(**snake_case__				)

              lowercase		:List[Any]       							=				{
                  '''candidate_label''': candidate_label,
                  '''sequence''': sequence,
                  '''is_last''': inputs['''is_last'''],
                  **outputs,
              }
              return model_outputs







       def  __snake_case						(   self  :    Tuple  ,     snake_case__  :    Union[str, Any]  ,     snake_case__  :    Union[str, Any]=False				):

              '''simple docstring'''






              lowercase		:Union[str, Any]       							=				[outputs['''candidate_label'''] for outputs in model_outputs]
              lowercase		:int       							=				[outputs['''sequence'''] for outputs in model_outputs]
              lowercase		:Any       							=				np.concatenate([output['''logits'''].numpy() for output in model_outputs]				)
              lowercase		:str       							=				logits.shape[0]
              lowercase		:Tuple       							=				len(snake_case__				)
              lowercase		:Union[str, Any]       							=				N // n
              lowercase		:str       							=				logits.reshape((num_sequences, n, -1)				)

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

              lowercase		:Union[str, Any]       							=				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"""



from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_sentencepiece_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


UpperCAmelCase      		=			{
    '''configuration_xlm_roberta''': [
        '''XLM_ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP''',
        '''XLMRobertaConfig''',
        '''XLMRobertaOnnxConfig''',
    ],
}

try:
 if not is_sentencepiece_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 UpperCAmelCase      		=			['''XLMRobertaTokenizer''']

try:
 if not is_tokenizers_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 UpperCAmelCase      		=			['''XLMRobertaTokenizerFast''']

try:
 if not is_torch_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 UpperCAmelCase      		=			[
     '''XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST''',
     '''XLMRobertaForCausalLM''',
     '''XLMRobertaForMaskedLM''',
     '''XLMRobertaForMultipleChoice''',
     '''XLMRobertaForQuestionAnswering''',
     '''XLMRobertaForSequenceClassification''',
     '''XLMRobertaForTokenClassification''',
     '''XLMRobertaModel''',
     '''XLMRobertaPreTrainedModel''',
 ]

try:
 if not is_tf_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 UpperCAmelCase      		=			[
     '''TF_XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST''',
     '''TFXLMRobertaForCausalLM''',
     '''TFXLMRobertaForMaskedLM''',
     '''TFXLMRobertaForMultipleChoice''',
     '''TFXLMRobertaForQuestionAnswering''',
     '''TFXLMRobertaForSequenceClassification''',
     '''TFXLMRobertaForTokenClassification''',
     '''TFXLMRobertaModel''',
     '''TFXLMRobertaPreTrainedModel''',
 ]

try:
 if not is_flax_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 UpperCAmelCase      		=			[
     '''FLAX_XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST''',
     '''FlaxXLMRobertaForMaskedLM''',
     '''FlaxXLMRobertaForCausalLM''',
     '''FlaxXLMRobertaForMultipleChoice''',
     '''FlaxXLMRobertaForQuestionAnswering''',
     '''FlaxXLMRobertaForSequenceClassification''',
     '''FlaxXLMRobertaForTokenClassification''',
     '''FlaxXLMRobertaModel''',
     '''FlaxXLMRobertaPreTrainedModel''',
 ]

if TYPE_CHECKING:
 from .configuration_xlm_roberta import (
     XLM_ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP,
     XLMRobertaConfig,
     XLMRobertaOnnxConfig,
 )

 try:
  if not is_sentencepiece_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .tokenization_xlm_roberta import XLMRobertaTokenizer

 try:
  if not is_tokenizers_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .tokenization_xlm_roberta_fast import XLMRobertaTokenizerFast

 try:
  if not is_torch_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_xlm_roberta import (
      XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST,
      XLMRobertaForCausalLM,
      XLMRobertaForMaskedLM,
      XLMRobertaForMultipleChoice,
      XLMRobertaForQuestionAnswering,
      XLMRobertaForSequenceClassification,
      XLMRobertaForTokenClassification,
      XLMRobertaModel,
      XLMRobertaPreTrainedModel,
  )

 try:
  if not is_tf_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_tf_xlm_roberta import (
      TF_XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST,
      TFXLMRobertaForCausalLM,
      TFXLMRobertaForMaskedLM,
      TFXLMRobertaForMultipleChoice,
      TFXLMRobertaForQuestionAnswering,
      TFXLMRobertaForSequenceClassification,
      TFXLMRobertaForTokenClassification,
      TFXLMRobertaModel,
      TFXLMRobertaPreTrainedModel,
  )

 try:
  if not is_flax_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_flax_xlm_roberta import (
      FLAX_XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST,
      FlaxXLMRobertaForCausalLM,
      FlaxXLMRobertaForMaskedLM,
      FlaxXLMRobertaForMultipleChoice,
      FlaxXLMRobertaForQuestionAnswering,
      FlaxXLMRobertaForSequenceClassification,
      FlaxXLMRobertaForTokenClassification,
      FlaxXLMRobertaModel,
      FlaxXLMRobertaPreTrainedModel,
  )

else:
 import sys

 UpperCAmelCase      		=			_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)

