




"""simple docstring"""

import json
from typing import Dict, List, Optional, Tuple, Union

from tokenizers import pre_tokenizers, processors

from ...tokenization_utils_base import AddedToken, BatchEncoding, EncodedInput
from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import PaddingStrategy, logging
from .tokenization_led import LEDTokenizer


_lowerCamelCase     :	Tuple         =				logging.get_logger(__name__)


_lowerCamelCase     :	Optional[Any]         =				{'''vocab_file''': '''vocab.json''', '''merges_file''': '''merges.txt''', '''tokenizer_file''': '''tokenizer.json'''}

_lowerCamelCase     :	Union[str, Any]         =				{
    '''vocab_file''': {
        '''allenai/led-base-16384''': '''https://huggingface.co/allenai/led-base-16384/resolve/main/vocab.json''',
    },
    '''merges_file''': {
        '''allenai/led-base-16384''': '''https://huggingface.co/allenai/led-base-16384/resolve/main/merges.txt''',
    },
    '''tokenizer_file''': {
        '''allenai/led-base-16384''': '''https://huggingface.co/allenai/led-base-16384/resolve/main/tokenizer.json''',
    },
}

_lowerCamelCase     :	Tuple         =				{
    '''allenai/led-base-16384''': 16384,
}

class 				lowercase					(			A__):
				__lowerCAmelCase			:			Tuple     				=      VOCAB_FILES_NAMES
				__lowerCAmelCase			:			Optional[Any]     				=      PRETRAINED_VOCAB_FILES_MAP
				__lowerCAmelCase			:			str     				=      PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
				__lowerCAmelCase			:			Optional[int]     				=      LEDTokenizer
				__lowerCAmelCase			:			Optional[int]     				=      ['''input_ids''', '''attention_mask''']







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



					"""simple docstring"""

					super().__init__(
					    _lowerCamelCase					,   _lowerCamelCase					,   tokenizer_file=_lowerCamelCase					,   errors=_lowerCamelCase					,   bos_token=_lowerCamelCase					,   eos_token=_lowerCamelCase					,   sep_token=_lowerCamelCase					,   cls_token=_lowerCamelCase					,   unk_token=_lowerCamelCase					,   pad_token=_lowerCamelCase					,   mask_token=_lowerCamelCase					,   add_prefix_space=_lowerCamelCase					,   trim_offsets=_lowerCamelCase					,   **_lowerCamelCase					,   )

					A_     : List[str]		      =						json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__()				)
					if pre_tok_state.get('''add_prefix_space'''					,   _lowerCamelCase				) != add_prefix_space:
						A_     : List[str]		      =						getattr(_lowerCamelCase					,   pre_tok_state.pop('''type'''				)				)
						A_     : Union[str, Any]		      =						add_prefix_space
						A_     : Dict		      =						pre_tok_class(**_lowerCamelCase				)

					A_     : Union[str, Any]		      =						add_prefix_space

					# the pre_tokenizer is already updated in the GPT2TokenizerFast `__init__`
					A_     : List[Any]		      =						"post_processor"
					A_     : Optional[Any]		      =						getattr(self.backend_tokenizer					,   _lowerCamelCase					,   _lowerCamelCase				)
					if tokenizer_component_instance:
						A_     : List[Any]		      =						json.loads(tokenizer_component_instance.__getstate__()				)

						# The lists 'sep' and 'cls' must be cased in tuples for the object `post_processor_class`
						if "sep" in state:
							A_     : str		      =						tuple(state['''sep''']				)
						if "cls" in state:
							A_     : List[Any]		      =						tuple(state['''cls''']				)

						A_     : List[str]		      =						False

						if state.get('''add_prefix_space'''					,   _lowerCamelCase				) != add_prefix_space:
							A_     : Optional[int]		      =						add_prefix_space
							A_     : List[str]		      =						True

						if state.get('''trim_offsets'''					,   _lowerCamelCase				) != trim_offsets:
							A_     : str		      =						trim_offsets
							A_     : Optional[int]		      =						True

						if changes_to_apply:
							A_     : Any		      =						getattr(_lowerCamelCase					,   state.pop('''type'''				)				)
							A_     : Optional[Any]		      =						component_class(**_lowerCamelCase				)
							setattr(self.backend_tokenizer					,   _lowerCamelCase					,   _lowerCamelCase				)







				@property
				# Copied from transformers.models.bart.tokenization_bart_fast.BartTokenizerFast.mask_token with BART->LED
				def     a_     (							self      :    str				):



					"""simple docstring"""

					if self._mask_token is None:
						if self.verbose:
							logger.error('''Using mask_token, but it is not set yet.'''				)
						return None
					return str(self._mask_token				)







				@mask_token.setter
				def     a_     (							self      :    str					,   _lowerCamelCase      :    Dict				):



					"""simple docstring"""

					A_     : Any		      =						AddedToken(_lowerCamelCase					,   lstrip=_lowerCamelCase					,   rstrip=_lowerCamelCase				) if isinstance(_lowerCamelCase					,   _lowerCamelCase				) else value
					A_     : Tuple		      =						value







				def     a_     (							self      :    Any					,   *_lowerCamelCase      :    Any					,   **_lowerCamelCase      :    Any				):



					"""simple docstring"""

					A_     : Dict		      =						kwargs.get('''is_split_into_words'''					,   _lowerCamelCase				)

					if is_split_into_words and not self.add_prefix_space:
						raise ValueError(
						    F"""You need to instantiate {self.__class__.__name__} with add_prefix_space=True """
						    '''to use it with pretokenized inputs.'''				)

					return super()._batch_encode_plus(*_lowerCamelCase					,   **_lowerCamelCase				)







				def     a_     (							self      :    List[Any]					,   *_lowerCamelCase      :    List[Any]					,   **_lowerCamelCase      :    int				):



					"""simple docstring"""

					A_     : int		      =						kwargs.get('''is_split_into_words'''					,   _lowerCamelCase				)

					if is_split_into_words and not self.add_prefix_space:
						raise ValueError(
						    F"""You need to instantiate {self.__class__.__name__} with add_prefix_space=True """
						    '''to use it with pretokenized inputs.'''				)

					return super()._encode_plus(*_lowerCamelCase					,   **_lowerCamelCase				)







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



					"""simple docstring"""

					A_     : Dict		      =						self._tokenizer.model.save(_lowerCamelCase					,   name=_lowerCamelCase				)
					return tuple(_lowerCamelCase				)







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



					"""simple docstring"""

					A_     : List[Any]		      =						[self.bos_token_id] + token_ids_a + [self.eos_token_id]
					if token_ids_a is None:
						return output

					return output + [self.eos_token_id] + token_ids_a + [self.eos_token_id]







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



					"""simple docstring"""

					A_     : Optional[int]		      =						[self.sep_token_id]
					A_     : Optional[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     a_     (							self      :    Optional[int]					,   _lowerCamelCase      :    Union[Dict[str, EncodedInput], BatchEncoding]					,   _lowerCamelCase      :    Optional[int] = None					,   _lowerCamelCase      :    PaddingStrategy = PaddingStrategy.DO_NOT_PAD					,   _lowerCamelCase      :    Optional[int] = None					,   _lowerCamelCase      :    Optional[bool] = None					,   ):



					"""simple docstring"""

					A_     : str		      =						super()._pad(
					    encoded_inputs=_lowerCamelCase					,   max_length=_lowerCamelCase					,   padding_strategy=_lowerCamelCase					,   pad_to_multiple_of=_lowerCamelCase					,   return_attention_mask=_lowerCamelCase					,   )

					# Load from model defaults
					if return_attention_mask is None:
						A_     : str		      =						"attention_mask" in self.model_input_names

					if return_attention_mask and "global_attention_mask" in encoded_inputs:
						A_     : List[str]		      =						encoded_inputs[self.model_input_names[0]]
						# `global_attention_mask` need to have the same length as other (sequential) inputs.
						A_     : Union[str, Any]		      =						len(encoded_inputs['''global_attention_mask''']				) != len(_lowerCamelCase				)

						if needs_to_be_padded:
							A_     : Union[str, Any]		      =						len(_lowerCamelCase				) - len(encoded_inputs['''global_attention_mask''']				)

							if self.padding_side == "right":
								# Use `-1` since `0` in `global_attention_mask` means `local attention` instead of `not to attend`
								A_     : Tuple		      =						(
								    encoded_inputs["global_attention_mask"] + [-1] * difference
								)
							elif self.padding_side == "left":
								A_     : Optional[Any]		      =						[-1] * difference + encoded_inputs[
								    "global_attention_mask"
								]
							else:
								raise ValueError('''Invalid padding strategy:''' + str(self.padding_side				)				)

					return encoded_inputs













# Copyright 2023 The HuggingFace Inc. team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import torch

from ..models.auto import AutoModelForSequenceClassification, AutoTokenizer
from .base import PipelineTool





class 			__a  (    A__       ):
 _lowerCAmelCase       :       str        = '''facebook/bart-large-mnli'''
 _lowerCAmelCase       :       Tuple        = (
     '''This is a tool that classifies an English text using provided labels. It takes two inputs: `text`, which '''
     '''should be the text to classify, and `labels`, which should be the list of labels to use for classification. '''
     '''It returns the most likely label in the list of provided `labels` for the input text.'''
 )
 _lowerCAmelCase       :       Any        = '''text_classifier'''
 _lowerCAmelCase       :       int        = AutoTokenizer
 _lowerCAmelCase       :       Union[str, Any]        = AutoModelForSequenceClassification

 _lowerCAmelCase       :       Union[str, Any]        = ['''text''', ['''text''']]
 _lowerCAmelCase       :       Dict        = ['''text''']







 def 					__lowercase					(		self					:   int						):


      '''simple docstring'''






      super().setup()
      UpperCamelCase__			:   Dict       				=  self.model.config
      UpperCamelCase__			:   Union[str, Any]       				=  -1
      for idx, label in config.idalabel.items():
           if label.lower().startswith("entail"						):
                UpperCamelCase__			:   List[str]       				=  int(SCREAMING_SNAKE_CASE						)
      if self.entailment_id == -1:
           raise ValueError("Could not determine the entailment ID from the model config, please pass it at init."						)







 def 					__lowercase					(		self					:   Tuple							,   SCREAMING_SNAKE_CASE					:   Tuple							,   SCREAMING_SNAKE_CASE					:   List[Any]						):


      '''simple docstring'''






      UpperCamelCase__			:   Any       				=  labels
      return self.pre_processor(
          [text] * len(SCREAMING_SNAKE_CASE						)							,   [F'This example is {label}' for label in labels]							,   return_tensors="pt"							,   padding="max_length"							,   )







 def 					__lowercase					(		self					:   Optional[Any]							,   SCREAMING_SNAKE_CASE					:   Dict						):


      '''simple docstring'''






      UpperCamelCase__			:   List[Any]       				=  outputs.logits
      UpperCamelCase__			:   Any       				=  torch.argmax(logits[:, 2]						).item()
      return self._labels[label_id]