



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


__snake_case		:int   	=  logging.get_logger(__name__)

class 					_A (			__UpperCAmelCase					):



       def 	_lowerCamelCase    (	self							:  str  ,	__SCREAMING_SNAKE_CASE							:  Dict):


           '''simple docstring'''

           if isinstance(__SCREAMING_SNAKE_CASE  ,	__SCREAMING_SNAKE_CASE):
               __a		 =       [label.strip() for label in labels.split(''',''') if label.strip()]
           return labels





       def __call__(	self							:  List[str]  ,	__SCREAMING_SNAKE_CASE							:  Optional[int]  ,	__SCREAMING_SNAKE_CASE							:  str  ,	__SCREAMING_SNAKE_CASE							:  List[Any]):


           '''simple docstring'''

           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):
               __a		 =       [sequences]

           __a		 =       []
           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(__UpperCAmelCase					)
class 					_A (			__UpperCAmelCase					):



       def __init__(	self							:  Any  ,	__SCREAMING_SNAKE_CASE							:  str=ZeroShotClassificationArgumentHandler()  ,	*__SCREAMING_SNAKE_CASE							:  Optional[Any]  ,	**__SCREAMING_SNAKE_CASE							:  List[str]):


           '''simple docstring'''

           __a		 =       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 	_lowerCamelCase    (	self							:  Any):


           '''simple docstring'''

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



       def 	_lowerCamelCase    (	self							:  Optional[Any]  ,	__SCREAMING_SNAKE_CASE							:  List[Any]  ,	__SCREAMING_SNAKE_CASE							:  Tuple=True  ,	__SCREAMING_SNAKE_CASE							:  Optional[Any]=True  ,	__SCREAMING_SNAKE_CASE							:  Optional[Any]=TruncationStrategy.ONLY_FIRST  ,	**__SCREAMING_SNAKE_CASE							:  str):


           '''simple docstring'''

           __a		 =       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`''')
               __a		 =       self.tokenizer.eos_token
           try:
               __a		 =       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.

                   __a		 =       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 	_lowerCamelCase    (	self							:  Optional[int]  ,	**__SCREAMING_SNAKE_CASE							:  Optional[int]):


           '''simple docstring'''

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

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



       def __call__(	self							:  List[str]  ,	__SCREAMING_SNAKE_CASE							:  Union[str, List[str]]  ,	*__SCREAMING_SNAKE_CASE							:  Any  ,	**__SCREAMING_SNAKE_CASE							:  Any  ,	):


           '''simple docstring'''

           if len(__SCREAMING_SNAKE_CASE) == 0:
               pass
           elif len(__SCREAMING_SNAKE_CASE) == 1 and "candidate_labels" not in kwargs:
               __a		 =       args[0]
           else:
               raise ValueError(F'Unable to understand extra arguments {args}')

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



       def 	_lowerCamelCase    (	self							:  Optional[int]  ,	__SCREAMING_SNAKE_CASE							:  int  ,	__SCREAMING_SNAKE_CASE							:  int=None  ,	__SCREAMING_SNAKE_CASE							:  int="This example is {}."):


           '''simple docstring'''

           __a ,     __a		 =       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)):
               __a		 =       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 	_lowerCamelCase    (	self							:  List[str]  ,	__SCREAMING_SNAKE_CASE							:  List[str]):


           '''simple docstring'''

           __a		 =       inputs['''candidate_label''']
           __a		 =       inputs['''sequence''']
           __a		 =       {k: inputs[k] for k in self.tokenizer.model_input_names}
           __a		 =       self.model(**__SCREAMING_SNAKE_CASE)

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





       def 	_lowerCamelCase    (	self							:  Dict  ,	__SCREAMING_SNAKE_CASE							:  List[Any]  ,	__SCREAMING_SNAKE_CASE							:  Optional[int]=False):


           '''simple docstring'''

           __a		 =       [outputs['''candidate_label'''] for outputs in model_outputs]
           __a		 =       [outputs['''sequence'''] for outputs in model_outputs]
           __a		 =       np.concatenate([output['''logits'''].numpy() for output in model_outputs])
           __a		 =       logits.shape[0]
           __a		 =       len(__SCREAMING_SNAKE_CASE)
           __a		 =       N // n
           __a		 =       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
               __a		 =       self.entailment_id
               __a		 =       -1 if entailment_id == 0 else 0
               __a		 =       reshaped_outputs[..., [contradiction_id, entailment_id]]
               __a		 =       np.exp(__SCREAMING_SNAKE_CASE) / np.exp(__SCREAMING_SNAKE_CASE).sum(-1  ,	keepdims=__SCREAMING_SNAKE_CASE)
               __a		 =       scores[..., 1]
           else:
               # softmax the "entailment" logits over all candidate labels
               __a		 =       reshaped_outputs[..., self.entailment_id]
               __a		 =       np.exp(__SCREAMING_SNAKE_CASE) / np.exp(__SCREAMING_SNAKE_CASE).sum(-1  ,	keepdims=__SCREAMING_SNAKE_CASE)

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










import os
from shutil import copyfile
from typing import List, Optional, Tuple

from ...tokenization_utils import AddedToken
from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import is_sentencepiece_available, logging


if is_sentencepiece_available():
 from .tokenization_barthez import BarthezTokenizer
else:
 __snake_case		:List[Any]   	=  None

__snake_case		:Dict   	=  logging.get_logger(__name__)

__snake_case		:Optional[int]   	=  {'''vocab_file''': '''sentencepiece.bpe.model''', '''tokenizer_file''': '''tokenizer.json'''}

__snake_case		:Union[str, Any]   	=  {
    '''vocab_file''': {
        '''moussaKam/mbarthez''': '''https://huggingface.co/moussaKam/mbarthez/resolve/main/sentencepiece.bpe.model''',
        '''moussaKam/barthez''': '''https://huggingface.co/moussaKam/barthez/resolve/main/sentencepiece.bpe.model''',
        '''moussaKam/barthez-orangesum-title''': (
            '''https://huggingface.co/moussaKam/barthez-orangesum-title/resolve/main/sentencepiece.bpe.model'''
        ),
    },
    '''tokenizer_file''': {
        '''moussaKam/mbarthez''': '''https://huggingface.co/moussaKam/mbarthez/resolve/main/tokenizer.json''',
        '''moussaKam/barthez''': '''https://huggingface.co/moussaKam/barthez/resolve/main/tokenizer.json''',
        '''moussaKam/barthez-orangesum-title''': (
            '''https://huggingface.co/moussaKam/barthez-orangesum-title/resolve/main/tokenizer.json'''
        ),
    },
}

__snake_case		:Optional[Any]   	=  {
    '''moussaKam/mbarthez''': 1024,
    '''moussaKam/barthez''': 1024,
    '''moussaKam/barthez-orangesum-title''': 1024,
}

__snake_case		:Optional[int]   	=  '''▁'''

class 					_A (			__UpperCAmelCase					):
       UpperCamelCase__						:					Tuple		      =		VOCAB_FILES_NAMES
       UpperCamelCase__						:					Tuple		      =		PRETRAINED_VOCAB_FILES_MAP
       UpperCamelCase__						:					str		      =		PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
       UpperCamelCase__						:					str		      =		['''input_ids''', '''attention_mask''']
       UpperCamelCase__						:					Dict		      =		BarthezTokenizer



       def __init__(	self							:  Optional[int]  ,	__SCREAMING_SNAKE_CASE							:  Optional[int]=None  ,	__SCREAMING_SNAKE_CASE							:  Union[str, Any]=None  ,	__SCREAMING_SNAKE_CASE							:  Tuple="<s>"  ,	__SCREAMING_SNAKE_CASE							:  List[str]="</s>"  ,	__SCREAMING_SNAKE_CASE							:  Tuple="</s>"  ,	__SCREAMING_SNAKE_CASE							:  int="<s>"  ,	__SCREAMING_SNAKE_CASE							:  Any="<unk>"  ,	__SCREAMING_SNAKE_CASE							:  int="<pad>"  ,	__SCREAMING_SNAKE_CASE							:  Any="<mask>"  ,	**__SCREAMING_SNAKE_CASE							:  Any  ,	):


           '''simple docstring'''

           __a		 =       AddedToken(__SCREAMING_SNAKE_CASE  ,	lstrip=__SCREAMING_SNAKE_CASE  ,	rstrip=__SCREAMING_SNAKE_CASE) if isinstance(__SCREAMING_SNAKE_CASE  ,	__SCREAMING_SNAKE_CASE) else mask_token

           super().__init__(
               __SCREAMING_SNAKE_CASE  ,	tokenizer_file=__SCREAMING_SNAKE_CASE  ,	bos_token=__SCREAMING_SNAKE_CASE  ,	eos_token=__SCREAMING_SNAKE_CASE  ,	unk_token=__SCREAMING_SNAKE_CASE  ,	sep_token=__SCREAMING_SNAKE_CASE  ,	cls_token=__SCREAMING_SNAKE_CASE  ,	pad_token=__SCREAMING_SNAKE_CASE  ,	mask_token=__SCREAMING_SNAKE_CASE  ,	**__SCREAMING_SNAKE_CASE  ,	)

           __a		 =       vocab_file
           __a		 =       False if not self.vocab_file else True



       def 	_lowerCamelCase    (	self							:  int  ,	__SCREAMING_SNAKE_CASE							:  List[int]  ,	__SCREAMING_SNAKE_CASE							:  Optional[List[int]] = None):


           '''simple docstring'''

           if token_ids_a is None:
               return [self.cls_token_id] + token_ids_a + [self.sep_token_id]
           __a		 =       [self.cls_token_id]
           __a		 =       [self.sep_token_id]
           return cls + token_ids_a + sep + sep + token_ids_a + sep



       def 	_lowerCamelCase    (	self							:  Optional[Any]  ,	__SCREAMING_SNAKE_CASE							:  List[int]  ,	__SCREAMING_SNAKE_CASE							:  Optional[List[int]] = None):


           '''simple docstring'''

           __a		 =       [self.sep_token_id]
           __a		 =       [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]





       def 	_lowerCamelCase    (	self							:  List[str]  ,	__SCREAMING_SNAKE_CASE							:  str  ,	__SCREAMING_SNAKE_CASE							:  Optional[str] = None):


           '''simple docstring'''

           if not self.can_save_slow_tokenizer:
               raise ValueError(
                   '''Your fast tokenizer does not have the necessary information to save the vocabulary for a slow '''
                   '''tokenizer.''')

           if not os.path.isdir(__SCREAMING_SNAKE_CASE):
               logger.error(F'Vocabulary path ({save_directory}) should be a directory')
               return
           __a		 =       os.path.join(
               __SCREAMING_SNAKE_CASE  ,	(filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''])

           if os.path.abspath(self.vocab_file) != os.path.abspath(__SCREAMING_SNAKE_CASE):
               copyfile(self.vocab_file  ,	__SCREAMING_SNAKE_CASE)

           return (out_vocab_file,)





