



"""simple docstring"""







import numpy as np

from transformers import Pipeline

def       _lowerCamelCase				(							UpperCAmelCase_ :       List[Any]     )	-> Union[str, Any]:






	"""simple docstring"""





	A__											=					np.max(UpperCAmelCase_,	axis=-1,	keepdims=UpperCAmelCase_     )
	A__											=					np.exp(outputs - maxes     )
	return shifted_exp / shifted_exp.sum(axis=-1,	keepdims=UpperCAmelCase_     )






class       UpperCamelCase__			(   _lowerCAmelCase      ):

			"""simple docstring"""


			def   snake_case__	(      self						,	**SCREAMING_SNAKE_CASE__		)      ->	str:
				A__											=					{}
				if "second_text" in kwargs:
					A__											=					kwargs["second_text"]
				return preprocess_kwargs, {}, {}


			def   snake_case__	(      self						,	SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__=None		)      ->	Optional[Any]:
				return self.tokenizer(SCREAMING_SNAKE_CASE__						,	text_pair=SCREAMING_SNAKE_CASE__						,	return_tensors=self.framework		)


			def   snake_case__	(      self						,	SCREAMING_SNAKE_CASE__		)      ->	int:
				return self.model(**SCREAMING_SNAKE_CASE__		)







			def   snake_case__	(      self						,	SCREAMING_SNAKE_CASE__		)      ->	Optional[int]:
				A__											=					model_outputs.logits[0].numpy()
				A__											=					softmax(SCREAMING_SNAKE_CASE__		)

				A__											=					np.argmax(SCREAMING_SNAKE_CASE__		)
				A__											=					self.model.config.idalabel[best_class]
				A__											=					probabilities[best_class].item()
				A__											=					logits.tolist()
				return {"label": label, "score": score, "logits": logits}









"""simple docstring"""







import json
import os
from functools import lru_cache
from typing import List, Optional, Tuple

import regex as re

from ...tokenization_utils import AddedToken, PreTrainedTokenizer
from ...utils import logging


UpperCamelCase     =							logging.get_logger(__name__)


UpperCamelCase     =							{"""vocab_file""": """vocab.json""", """merges_file""": """merges.txt"""}

# See all BART models at https://huggingface.co/models?filter=bart
UpperCamelCase     =							{
    """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""",
    },
}

UpperCamelCase     =							{
    """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,
}

@lru_cache()
def       _lowerCamelCase				(							)	-> Tuple:






	"""simple docstring"""





	A__											=					(
	    list(range(ord("!"     ),	ord("~"     ) + 1     )     ) + list(range(ord("¡"     ),	ord("¬"     ) + 1     )     ) + list(range(ord("®"     ),	ord("ÿ"     ) + 1     )     )
	)
	A__											=					bs[:]
	A__											=					0
	for b in range(2**8     ):
		if b not in bs:
			bs.append(UpperCAmelCase_     )
			cs.append(2**8 + n     )
			n += 1
	A__											=					[chr(UpperCAmelCase_     ) for n in cs]
	return dict(zip(UpperCAmelCase_,	UpperCAmelCase_     )     )

def       _lowerCamelCase				(							UpperCAmelCase_ :       str     )	-> List[str]:






	"""simple docstring"""





	A__											=					set()
	A__											=					word[0]
	for char in word[1:]:
		pairs.add((prev_char, char)     )
		A__											=					char
	return pairs






class       UpperCamelCase__			(   _lowerCAmelCase      ):

			"""simple docstring"""


			A__	:					Union[str, Any]							  =     VOCAB_FILES_NAMES
			A__	:					Optional[Any]							  =     PRETRAINED_VOCAB_FILES_MAP
			A__	:					Dict							  =     PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
			A__	:					Optional[int]							  =     ["input_ids", "attention_mask"]


			def __init__(      self						,	SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__						,	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__						,	)      ->	Tuple:
				A__											=					AddedToken(SCREAMING_SNAKE_CASE__						,	lstrip=SCREAMING_SNAKE_CASE__						,	rstrip=SCREAMING_SNAKE_CASE__		) if isinstance(SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__		) else bos_token
				A__											=					AddedToken(SCREAMING_SNAKE_CASE__						,	lstrip=SCREAMING_SNAKE_CASE__						,	rstrip=SCREAMING_SNAKE_CASE__		) if isinstance(SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__		) else eos_token
				A__											=					AddedToken(SCREAMING_SNAKE_CASE__						,	lstrip=SCREAMING_SNAKE_CASE__						,	rstrip=SCREAMING_SNAKE_CASE__		) if isinstance(SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__		) else sep_token
				A__											=					AddedToken(SCREAMING_SNAKE_CASE__						,	lstrip=SCREAMING_SNAKE_CASE__						,	rstrip=SCREAMING_SNAKE_CASE__		) if isinstance(SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__		) else cls_token
				A__											=					AddedToken(SCREAMING_SNAKE_CASE__						,	lstrip=SCREAMING_SNAKE_CASE__						,	rstrip=SCREAMING_SNAKE_CASE__		) if isinstance(SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__		) else unk_token
				A__											=					AddedToken(SCREAMING_SNAKE_CASE__						,	lstrip=SCREAMING_SNAKE_CASE__						,	rstrip=SCREAMING_SNAKE_CASE__		) if isinstance(SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__		) else pad_token

				# Mask token behave like a normal word, i.e. include the space before it
				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__(
				    errors=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__						,	add_prefix_space=SCREAMING_SNAKE_CASE__						,	**SCREAMING_SNAKE_CASE__						,	)

				with open(SCREAMING_SNAKE_CASE__						,	encoding="utf-8"		) as vocab_handle:
					A__											=					json.load(SCREAMING_SNAKE_CASE__		)
				A__											=					{v: k for k, v in self.encoder.items()}
				A__											=					errors  # how to handle errors in decoding
				A__											=					bytes_to_unicode()
				A__											=					{v: k for k, v in self.byte_encoder.items()}
				with open(SCREAMING_SNAKE_CASE__						,	encoding="utf-8"		) as merges_handle:
					A__											=					merges_handle.read().split("\n"		)[1:-1]
				A__											=					[tuple(merge.split()		) for merge in bpe_merges]
				A__											=					dict(zip(SCREAMING_SNAKE_CASE__						,	range(len(SCREAMING_SNAKE_CASE__		)		)		)		)
				A__											=					{}
				A__											=					add_prefix_space

				# Should have added re.IGNORECASE so BPE merges can happen for capitalized versions of contractions
				A__											=					re.compile(R"'s|'t|'re|'ve|'m|'ll|'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+"		)


			@property
			def   snake_case__	(      self		)      ->	List[Any]:
				return len(self.encoder		)


			def   snake_case__	(      self		)      ->	List[Any]:
				return dict(self.encoder						,	**self.added_tokens_encoder		)


			def   snake_case__	(      self						,	SCREAMING_SNAKE_CASE__		)      ->	Dict:
				if token in self.cache:
					return self.cache[token]
				A__											=					tuple(SCREAMING_SNAKE_CASE__		)
				A__											=					get_pairs(SCREAMING_SNAKE_CASE__		)

				if not pairs:
					return token

				while True:
					A__											=					min(SCREAMING_SNAKE_CASE__						,	key=lambda SCREAMING_SNAKE_CASE__		: self.bpe_ranks.get(SCREAMING_SNAKE_CASE__						,	float("inf"		)		)		)
					if bigram not in self.bpe_ranks:
						break
					A__							,							A__											=					bigram
					A__											=					[]
					A__											=					0
					while i < len(SCREAMING_SNAKE_CASE__		):
						try:
							A__											=					word.index(SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__		)
						except ValueError:
							new_word.extend(word[i:]		)
							break
						else:
							new_word.extend(word[i:j]		)
							A__											=					j

						if word[i] == first and i < len(SCREAMING_SNAKE_CASE__		) - 1 and word[i + 1] == second:
							new_word.append(first + second		)
							i += 2
						else:
							new_word.append(word[i]		)
							i += 1
					A__											=					tuple(SCREAMING_SNAKE_CASE__		)
					A__											=					new_word
					if len(SCREAMING_SNAKE_CASE__		) == 1:
						break
					else:
						A__											=					get_pairs(SCREAMING_SNAKE_CASE__		)
				A__											=					" ".join(SCREAMING_SNAKE_CASE__		)
				A__											=					word
				return word


			def   snake_case__	(      self						,	SCREAMING_SNAKE_CASE__		)      ->	List[Any]:
				A__											=					[]
				for token in re.findall(self.pat						,	SCREAMING_SNAKE_CASE__		):
					A__											=					"".join(
					    self.byte_encoder[b] for b in token.encode("utf-8"		)		)  # Maps all our bytes to unicode strings, avoiding control tokens of the BPE (spaces in our case)
					bpe_tokens.extend(bpe_token for bpe_token in self.bpe(SCREAMING_SNAKE_CASE__		).split(" "		)		)
				return bpe_tokens


			def   snake_case__	(      self						,	SCREAMING_SNAKE_CASE__		)      ->	str:
				return self.encoder.get(SCREAMING_SNAKE_CASE__						,	self.encoder.get(self.unk_token		)		)


			def   snake_case__	(      self						,	SCREAMING_SNAKE_CASE__		)      ->	Any:
				return self.decoder.get(SCREAMING_SNAKE_CASE__		)


			def   snake_case__	(      self						,	SCREAMING_SNAKE_CASE__		)      ->	Dict:
				A__											=					"".join(SCREAMING_SNAKE_CASE__		)
				A__											=					bytearray([self.byte_decoder[c] for c in text]		).decode("utf-8"						,	errors=self.errors		)
				return text


			def   snake_case__	(      self						,	SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__ = None		)      ->	Tuple[str]:
				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"]		)
				A__											=					os.path.join(
				    SCREAMING_SNAKE_CASE__						,	(filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["merges_file"]		)

				with open(SCREAMING_SNAKE_CASE__						,	"w"						,	encoding="utf-8"		) as f:
					f.write(json.dumps(self.encoder						,	indent=2						,	sort_keys=SCREAMING_SNAKE_CASE__						,	ensure_ascii=SCREAMING_SNAKE_CASE__		) + "\n"		)

				A__											=					0
				with open(SCREAMING_SNAKE_CASE__						,	"w"						,	encoding="utf-8"		) as writer:
					writer.write("#version: 0.2\n"		)
					for bpe_tokens, token_index in sorted(self.bpe_ranks.items()						,	key=lambda SCREAMING_SNAKE_CASE__		: kv[1]		):
						if index != token_index:
							logger.warning(
							    f"""Saving vocabulary to {merge_file}: BPE merge indices are not consecutive."""
							    " Please check that the tokenizer is not corrupted!"		)
							A__											=					token_index
						writer.write(" ".join(SCREAMING_SNAKE_CASE__		) + "\n"		)
						index += 1

				return vocab_file, merge_file


			def   snake_case__	(      self						,	SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__ = None		)      ->	List[int]:
				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   snake_case__	(      self						,	SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__ = None						,	SCREAMING_SNAKE_CASE__ = False		)      ->	List[int]:
				if already_has_special_tokens:
					return super().get_special_tokens_mask(
					    token_ids_a=SCREAMING_SNAKE_CASE__						,	token_ids_a=SCREAMING_SNAKE_CASE__						,	already_has_special_tokens=SCREAMING_SNAKE_CASE__		)

				if token_ids_a is None:
					return [1] + ([0] * len(SCREAMING_SNAKE_CASE__		)) + [1]
				return [1] + ([0] * len(SCREAMING_SNAKE_CASE__		)) + [1, 1] + ([0] * len(SCREAMING_SNAKE_CASE__		)) + [1]


			def   snake_case__	(      self						,	SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__ = None		)      ->	List[int]:
				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   snake_case__	(      self						,	SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__=False						,	**SCREAMING_SNAKE_CASE__		)      ->	Optional[int]:
				A__											=					kwargs.pop("add_prefix_space"						,	self.add_prefix_space		)
				if (is_split_into_words or add_prefix_space) and (len(SCREAMING_SNAKE_CASE__		) > 0 and not text[0].isspace()):
					A__											=					" " + text
				return (text, kwargs)




