

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


__lowerCamelCase										=		logging.get_logger(__name__)
class        _lowercase					(     __UpperCAmelCase     ):







     def 	lowerCAmelCase__    (      self ,						UpperCamelCase_       ):
      if isinstance(UpperCamelCase_ ,						UpperCamelCase_       ):
       __magic_name__     =      [label.strip() for label in labels.split(''','''       ) if label.strip()]
      return labels







     def __call__(      self ,						UpperCamelCase_ ,						UpperCamelCase_ ,						UpperCamelCase_       ):
      if len(UpperCamelCase_       ) == 0 or len(UpperCamelCase_       ) == 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(UpperCamelCase_       )       )

      if isinstance(UpperCamelCase_ ,						UpperCamelCase_       ):
       __magic_name__     =      [sequences]

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

      return sequence_pairs, sequences


@add_end_docstrings(__UpperCAmelCase     )
class        _lowercase					(     __UpperCAmelCase     ):







     def __init__(      self ,						UpperCamelCase_=ZeroShotClassificationArgumentHandler() ,						*UpperCamelCase_ ,						**UpperCamelCase_       ):
      __magic_name__     =      args_parser
      super().__init__(*UpperCamelCase_ ,						**UpperCamelCase_       )
      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 	lowerCAmelCase__    (      self       ):
      for label, ind in self.model.config.labelaid.items():
       if label.lower().startswith('''entail'''       ):
        return ind
      return -1







     def 	lowerCAmelCase__    (      self ,						UpperCamelCase_ ,						UpperCamelCase_=True ,						UpperCamelCase_=True ,						UpperCamelCase_=TruncationStrategy.ONLY_FIRST ,						**UpperCamelCase_       ):
      __magic_name__     =      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`'''       )
       __magic_name__     =      self.tokenizer.eos_token
      try:
       __magic_name__     =      self.tokenizer(
           UpperCamelCase_ ,						add_special_tokens=UpperCamelCase_ ,						return_tensors=UpperCamelCase_ ,						padding=UpperCamelCase_ ,						truncation=UpperCamelCase_ ,						)
      except Exception as e:
       if "too short" in str(UpperCamelCase_       ):
        # 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.

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

      return inputs







     def 	lowerCAmelCase__    (      self ,						**UpperCamelCase_       ):
      if kwargs.get('''multi_class''' ,						UpperCamelCase_       ) is not None:
       __magic_name__     =      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.'''       )
      __magic_name__     =      {}
      if "candidate_labels" in kwargs:
       __magic_name__     =      self._args_parser._parse_labels(kwargs['''candidate_labels''']       )
      if "hypothesis_template" in kwargs:
       __magic_name__     =      kwargs['''hypothesis_template''']

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







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

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







     def 	lowerCAmelCase__    (      self ,						UpperCamelCase_ ,						UpperCamelCase_=None ,						UpperCamelCase_="This example is {}."       ):
      __magic_name__   ,						__magic_name__     =      self._args_parser(UpperCamelCase_ ,						UpperCamelCase_ ,						UpperCamelCase_       )

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

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







     def 	lowerCAmelCase__    (      self ,						UpperCamelCase_       ):
      __magic_name__     =      inputs['''candidate_label''']
      __magic_name__     =      inputs['''sequence''']
      __magic_name__     =      {k: inputs[k] for k in self.tokenizer.model_input_names}
      __magic_name__     =      self.model(**UpperCamelCase_       )

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







     def 	lowerCAmelCase__    (      self ,						UpperCamelCase_ ,						UpperCamelCase_=False       ):
      __magic_name__     =      [outputs['''candidate_label'''] for outputs in model_outputs]
      __magic_name__     =      [outputs['''sequence'''] for outputs in model_outputs]
      __magic_name__     =      np.concatenate([output['''logits'''].numpy() for output in model_outputs]       )
      __magic_name__     =      logits.shape[0]
      __magic_name__     =      len(UpperCamelCase_       )
      __magic_name__     =      N // n
      __magic_name__     =      logits.reshape((num_sequences, n, -1)       )

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

      __magic_name__     =      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 pathlib import Path

import fire





def 	lowercase      (   __UpperCamelCase      ,   __UpperCamelCase      ,   __UpperCamelCase						) ->      Optional[int]:
 __magic_name__     =      Path(__UpperCamelCase						)
 __magic_name__     =      Path(__UpperCamelCase						)
 dest_dir.mkdir(exist_ok=__UpperCamelCase						)
 for path in src_dir.iterdir():
  __magic_name__     =      [x.rstrip() for x in list(path.open().readlines()						)][:n]
  __magic_name__     =      dest_dir.joinpath(path.name						)
  print(__UpperCamelCase						)
  dest_path.open('''w'''						).write('''\n'''.join(__UpperCamelCase						)						)


if __name__ == "__main__":
 fire.Fire(minify)

