



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


a     :    Dict      						=					logging.get_logger(__name__)

class     UpperCamelCase_				(							UpperCAmelCase_   ):





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






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

								if isinstance(_lowercase	,	_lowercase					):
															UpperCAmelCase  :	List[Any]							  =	[sequences]

								UpperCAmelCase  :	List[Any]							  =	[]
								for sequence in sequences:
															sequence_pairs.extend([[sequence, hypothesis_template.format(_lowercase					)] for label in labels]					)

								return sequence_pairs, sequences




@add_end_docstrings(UpperCAmelCase_   )
class     UpperCamelCase_				(							UpperCAmelCase_   ):





	def __init__(						self	,	A=ZeroShotClassificationArgumentHandler()	,	*A	,	**A					)    ->    Union[str, Any]:
								UpperCAmelCase  :	Dict							  =	args_parser
								super().__init__(*_lowercase	,	**_lowercase					)
								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					)    ->    Optional[int]:
								for label, ind in self.model.config.labelaid.items():
															if label.lower().startswith("""entail"""					):
																						return ind
								return -1





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

																						UpperCAmelCase  :	Tuple							  =	self.tokenizer(
																						    _lowercase	,	add_special_tokens=_lowercase	,	return_tensors=_lowercase	,	padding=_lowercase	,	truncation=TruncationStrategy.DO_NOT_TRUNCATE	,	)
															else:
																						raise e

								return inputs





	def 				_lowercase(						self	,	**A					)    ->    Dict:
								if kwargs.get("""multi_class"""	,	_lowercase					) is not None:
															UpperCAmelCase  :	List[str]							  =	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."""					)
								UpperCAmelCase  :	int							  =	{}
								if "candidate_labels" in kwargs:
															UpperCAmelCase  :	Optional[int]							  =	self._args_parser._parse_labels(kwargs["""candidate_labels"""]					)
								if "hypothesis_template" in kwargs:
															UpperCAmelCase  :	Dict							  =	kwargs['hypothesis_template']

								UpperCAmelCase  :	Union[str, Any]							  =	{}
								if "multi_label" in kwargs:
															UpperCAmelCase  :	str							  =	kwargs['multi_label']
								return preprocess_params, {}, postprocess_params





	def __call__(						self	,	A	,	*A	,	**A	,	)    ->    List[Any]:
								if len(_lowercase					) == 0:
															pass
								elif len(_lowercase					) == 1 and "candidate_labels" not in kwargs:
															UpperCAmelCase  :	str							  =	args[0]
								else:
															raise ValueError(f'''Unable to understand extra arguments {args}'''					)

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





	def 				_lowercase(						self	,	A	,	A=None	,	A="This example is {}."					)    ->    Dict:
								UpperCAmelCase  :	int							  =	self._args_parser(_lowercase	,	_lowercase	,	_lowercase					)

								for i, (candidate_label, sequence_pair) in enumerate(zip(_lowercase	,	_lowercase					)					):
															UpperCAmelCase  :	Any							  =	self._parse_and_tokenize([sequence_pair]					)

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





	def 				_lowercase(						self	,	A					)    ->    int:
								UpperCAmelCase  :	Optional[Any]							  =	inputs['candidate_label']
								UpperCAmelCase  :	Optional[int]							  =	inputs['sequence']
								UpperCAmelCase  :	Optional[int]							  =	{k: inputs[k] for k in self.tokenizer.model_input_names}
								UpperCAmelCase  :	List[str]							  =	self.model(**_lowercase					)

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






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

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

								UpperCAmelCase  :	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'''




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:
							a     :    int      						=					None

a     :    List[Any]      						=					logging.get_logger(__name__)

a     :    Dict      						=					{"""vocab_file""": """sentencepiece.bpe.model""", """tokenizer_file""": """tokenizer.json"""}

a     :    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"""
        ),
    },
}

a     :    List[Any]      						=					{
    """moussaKam/mbarthez""": 1_0_2_4,
    """moussaKam/barthez""": 1_0_2_4,
    """moussaKam/barthez-orangesum-title""": 1_0_2_4,
}

a     :    int      						=					"""▁"""

class     UpperCamelCase_				(							__magic_name__    ):
	lowercase    =				VOCAB_FILES_NAMES
	lowercase    =				PRETRAINED_VOCAB_FILES_MAP
	lowercase    =				PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
	lowercase    =				['input_ids', 'attention_mask']
	lowercase    =				BarthezTokenizer





	def __init__(						self	,	A=None	,	A=None	,	A="<s>"	,	A="</s>"	,	A="</s>"	,	A="<s>"	,	A="<unk>"	,	A="<pad>"	,	A="<mask>"	,	**A	,	)    ->    List[Any]:
								# Mask token behave like a normal word, i.e. include the space before it
								UpperCAmelCase  :	Tuple							  =	AddedToken(A	,	lstrip=A	,	rstrip=A					) if isinstance(A	,	A					) else mask_token

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

								UpperCAmelCase  :	Union[str, Any]							  =	vocab_file
								UpperCAmelCase  :	int							  =	False if not self.vocab_file else True





	def 				_lowercase(						self	,	A	,	A = None					)    ->    List[int]:

								if token_ids_a is None:
															return [self.cls_token_id] + token_ids_a + [self.sep_token_id]
								UpperCAmelCase  :	Optional[int]							  =	[self.cls_token_id]
								UpperCAmelCase  :	List[str]							  =	[self.sep_token_id]
								return cls + token_ids_a + sep + sep + token_ids_a + sep





	def 				_lowercase(						self	,	A	,	A = None					)    ->    List[int]:
								UpperCAmelCase  :	Optional[int]							  =	[self.sep_token_id]
								UpperCAmelCase  :	List[Any]							  =	[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 				_lowercase(						self	,	A	,	A = None					)    ->    Tuple[str]:
								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(A					):
															logger.error(f'''Vocabulary path ({save_directory}) should be a directory'''					)
															return
								UpperCAmelCase  :	str							  =	os.path.join(
								    A	,	(filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""]					)

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

								return (out_vocab_file,)

