




# 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 				UpperCamelCase_    ( UpperCAmelCase__     ):

    '''simple docstring'''






    UpperCAmelCase__							  =      '''facebook/bart-large-mnli'''
    UpperCAmelCase__							  =      (
        '''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.'''
    )
    UpperCAmelCase__							  =      '''text_classifier'''
    UpperCAmelCase__							  =      AutoTokenizer
    UpperCAmelCase__							  =      AutoModelForSequenceClassification

    UpperCAmelCase__							  =      ['''text''', ['''text''']]
    UpperCAmelCase__							  =      ['''text''']


    def        SCREAMING_SNAKE_CASE							(			self      :	Optional[Any])						->List[str]:





           '''simple docstring'''

           super().setup()
           A__       =    self.model.config
           A__       =    -1
           for idx, label in config.idalabel.items():
                  if label.lower().startswith('''entail'''):
                         A__       =    int(UpperCAmelCase__)
           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      :	List[Any]					, UpperCAmelCase__      :	Dict					, UpperCAmelCase__      :	List[str])						->Optional[Any]:





           '''simple docstring'''

           A__       =    labels
           return self.pre_processor(
               [text] * len(UpperCAmelCase__)					, [f"""This example is {label}""" for label in labels]					, return_tensors='''pt'''					, padding='''max_length'''					, )





    def        SCREAMING_SNAKE_CASE							(			self      :	List[str]					, UpperCAmelCase__      :	Tuple)						->Optional[int]:





           '''simple docstring'''

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








import copy

from ...configuration_utils import PretrainedConfig
from ...utils import logging


_lowerCamelCase	:		Any							       =   logging.get_logger(__name__)


class 				UpperCamelCase_    ( UpperCAmelCase__     ):

    '''simple docstring'''






    UpperCAmelCase__							  =      '''encoder-decoder'''
    UpperCAmelCase__							  =      True


    def __init__(			self      :	List[str]					, **UpperCAmelCase__      :	Union[str, Any])						->List[Any]:





           '''simple docstring'''

           super().__init__(**UpperCAmelCase__)
           assert (
               "encoder" in kwargs and "decoder" in kwargs
           ), "Config has to be initialized with encoder and decoder config"
           A__       =    kwargs.pop('''encoder''')
           A__       =    encoder_config.pop('''model_type''')
           A__       =    kwargs.pop('''decoder''')
           A__       =    decoder_config.pop('''model_type''')

           from ..auto.configuration_auto import AutoConfig

           A__       =    AutoConfig.for_model(UpperCAmelCase__					, **UpperCAmelCase__)
           A__       =    AutoConfig.for_model(UpperCAmelCase__					, **UpperCAmelCase__)
           A__       =    True


    @classmethod
    def        SCREAMING_SNAKE_CASE							(			cls      :	Union[str, Any]					, UpperCAmelCase__      :	PretrainedConfig					, UpperCAmelCase__      :	PretrainedConfig					, **UpperCAmelCase__      :	Union[str, Any])						->PretrainedConfig:





           '''simple docstring'''

           logger.info('''Set `config.is_decoder=True` and `config.add_cross_attention=True` for decoder_config''')
           A__       =    True
           A__       =    True

           return cls(encoder=encoder_config.to_dict()					, decoder=decoder_config.to_dict()					, **UpperCAmelCase__)





    def        SCREAMING_SNAKE_CASE							(			self      :	str)						->Optional[Any]:





           '''simple docstring'''

           A__       =    copy.deepcopy(self.__dict__)
           A__       =    self.encoder.to_dict()
           A__       =    self.decoder.to_dict()
           A__       =    self.__class__.model_type
           return output


