

'''simple docstring'''






# 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     lowercase_			(lowerCamelCase__   ):




  """simple docstring"""




  SCREAMING_SNAKE_CASE					:						Tuple           =    'facebook/bart-large-mnli'
  SCREAMING_SNAKE_CASE					:						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.'
  )
  SCREAMING_SNAKE_CASE					:						List[str]           =    'text_classifier'
  SCREAMING_SNAKE_CASE					:						int           =    AutoTokenizer
  SCREAMING_SNAKE_CASE					:						Any           =    AutoModelForSequenceClassification

  SCREAMING_SNAKE_CASE					:						Optional[int]           =    ['text', ['text']]
  SCREAMING_SNAKE_CASE					:						List[Any]           =    ['text']






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






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






  def       SCREAMING_SNAKE_CASE   (   self     :  Optional[Any]      ,lowercase__     :  Any    ):
   __lowercase													=				outputs.logits
   __lowercase													=				torch.argmax(logits[:, 2]    ).item()
   return self._labels[label_id]




'''simple docstring'''






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

import sentencepiece as spm

from ...tokenization_utils import PreTrainedTokenizer
from ...utils import logging


lowerCAmelCase__      		=			logging.get_logger(__name__)

lowerCAmelCase__      		=			{'''vocab_file''': '''sentencepiece.model'''}

lowerCAmelCase__      		=			{
    '''vocab_file''': {
        '''google/rembert''': '''https://huggingface.co/google/rembert/resolve/main/sentencepiece.model''',
    },
}

lowerCAmelCase__      		=			{
    '''google/rembert''': 256,
}






class     lowercase_			(lowerCamelCase__   ):




  """simple docstring"""




  SCREAMING_SNAKE_CASE					:						Tuple           =    VOCAB_FILES_NAMES
  SCREAMING_SNAKE_CASE					:						Optional[int]           =    PRETRAINED_VOCAB_FILES_MAP
  SCREAMING_SNAKE_CASE					:						Union[str, Any]           =    PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES






  def __init__(   self     :  str      ,lowercase__     :  Optional[Any]      ,lowercase__     :  List[str]=False      ,lowercase__     :  Dict=True      ,lowercase__     :  List[str]=True      ,lowercase__     :  Dict="[CLS]"      ,lowercase__     :  Union[str, Any]="[SEP]"      ,lowercase__     :  List[str]="[UNK]"      ,lowercase__     :  int="[SEP]"      ,lowercase__     :  List[str]="[PAD]"      ,lowercase__     :  Optional[int]="[CLS]"      ,lowercase__     :  List[Any]="[MASK]"      ,**lowercase__     :  int      ,):
   super().__init__(
       do_lower_case=lowercase__      ,remove_space=lowercase__      ,keep_accents=lowercase__      ,bos_token=lowercase__      ,eos_token=lowercase__      ,unk_token=lowercase__      ,sep_token=lowercase__      ,pad_token=lowercase__      ,cls_token=lowercase__      ,mask_token=lowercase__      ,**lowercase__      ,)

   __lowercase													=				do_lower_case
   __lowercase													=				remove_space
   __lowercase													=				keep_accents
   __lowercase													=				vocab_file

   __lowercase													=				spm.SentencePieceProcessor()
   self.sp_model.Load(lowercase__    )






  @property
  def       SCREAMING_SNAKE_CASE   (   self     :  str    ):
   return len(self.sp_model    )






  def       SCREAMING_SNAKE_CASE   (   self     :  str    ):
   __lowercase													=				{self.convert_ids_to_tokens(lowercase__    ): i for i in range(self.vocab_size    )}
   vocab.update(self.added_tokens_encoder    )
   return vocab






  def __getstate__(   self     :  List[Any]    ):
   __lowercase													=				self.__dict__.copy()
   __lowercase													=				None
   return state






  def __setstate__(   self     :  str      ,lowercase__     :  Optional[int]    ):
   __lowercase													=				d
   __lowercase													=				spm.SentencePieceProcessor()
   self.sp_model.Load(self.vocab_file    )






  def       SCREAMING_SNAKE_CASE   (   self     :  List[str]      ,lowercase__     :  List[str]      ,lowercase__     :  List[Any]=False    ):
   __lowercase													=				self.sp_model.EncodeAsPieces(lowercase__    )
   return pieces






  def       SCREAMING_SNAKE_CASE   (   self     :  Tuple      ,lowercase__     :  List[Any]    ):
   return self.sp_model.PieceToId(lowercase__    )






  def       SCREAMING_SNAKE_CASE   (   self     :  Tuple      ,lowercase__     :  str    ):
   return self.sp_model.IdToPiece(lowercase__    )






  def       SCREAMING_SNAKE_CASE   (   self     :  Any      ,lowercase__     :  Tuple    ):
   __lowercase													=				self.sp_model.decode_pieces(lowercase__    )
   return out_string






  def       SCREAMING_SNAKE_CASE   (   self     :  Optional[int]      ,lowercase__     :  List[int]      ,lowercase__     :  Optional[List[int]] = None    ):
   __lowercase													=				[self.sep_token_id]
   __lowercase													=				[self.cls_token_id]
   if token_ids_a is None:
    return cls + token_ids_a + sep
   return cls + token_ids_a + sep + token_ids_a + sep






  def       SCREAMING_SNAKE_CASE   (   self     :  List[Any]      ,lowercase__     :  List[int]      ,lowercase__     :  Optional[List[int]] = None      ,lowercase__     :  bool = False    ):

   if already_has_special_tokens:
    if token_ids_a is not None:
     raise ValueError(
         '''You should not supply a second sequence if the provided sequence of '''
         '''ids is already formatted with special tokens for the model.'''    )
    return [1 if x in [self.sep_token_id, self.cls_token_id] else 0 for x in token_ids_a]

   if token_ids_a is not None:
    return [1] + ([0] * len(lowercase__    )) + [1] + ([0] * len(lowercase__    )) + [1]
   return [1] + ([0] * len(lowercase__    )) + [1]






  def       SCREAMING_SNAKE_CASE   (   self     :  Dict      ,lowercase__     :  List[int]      ,lowercase__     :  Optional[List[int]] = None    ):
   __lowercase													=				[self.sep_token_id]
   __lowercase													=				[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    ) * [0] + len(token_ids_a + sep    ) * [1]






  def       SCREAMING_SNAKE_CASE   (   self     :  Optional[int]      ,lowercase__     :  str      ,lowercase__     :  Optional[str] = None    ):
   if not os.path.isdir(lowercase__    ):
    logger.error('''Vocabulary path ({}) should be a directory'''.format(lowercase__    )    )
    return
   __lowercase													=				os.path.join(
       lowercase__      ,(filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file''']    )

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

   return (out_vocab_file,)

