

# 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 				SCREAMING_SNAKE_CASE__ (__snake_case					):
    __lowerCamelCase						:	int														=					"""facebook/bart-large-mnli"""
    __lowerCamelCase						:	Dict														=					(
        """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						:	Optional[Any]														=					"""text_classifier"""
    __lowerCamelCase						:	Optional[int]														=					AutoTokenizer
    __lowerCamelCase						:	str														=					AutoModelForSequenceClassification

    __lowerCamelCase						:	Optional[Any]														=					["""text""", ["""text"""]]
    __lowerCamelCase						:	int														=					["""text"""]





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





    def 				snake_case_    (				self						,       a						,       a):
         lowercase__   :       List[Any]				=	labels
         return self.pre_processor(
             [text] * len(a)						,       [f"""This example is {label}""" for label in labels]						,       return_tensors='pt'						,       padding='max_length'						,       )





    def 				snake_case_    (				self						,       a):
         lowercase__   :       List[str]				=	outputs.logits
         lowercase__   :       Optional[int]				=	torch.argmax(logits[:, 2]).item()
         return self._labels[label_id]




import warnings
from typing import List, Optional, Union

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy
from ...utils import TensorType


class 				SCREAMING_SNAKE_CASE__ (__snake_case					):
    __lowerCamelCase						:	Tuple														=					["""image_processor""", """tokenizer"""]
    __lowerCamelCase						:	int														=					"""ViltImageProcessor"""
    __lowerCamelCase						:	Union[str, Any]														=					("""BertTokenizer""", """BertTokenizerFast""")





    def __init__(				self						,       a=None						,       a=None						,       **a):
         lowercase__   :       Any				=	None
         if "feature_extractor" in kwargs:
              warnings.warn(
                  'The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`'
                  ' instead.'						,       a						,       )
              lowercase__   :       Union[str, Any]				=	kwargs.pop('feature_extractor')

         lowercase__   :       List[Any]				=	image_processor if image_processor is not None else feature_extractor
         if image_processor is None:
              raise ValueError('You need to specify an `image_processor`.')
         if tokenizer is None:
              raise ValueError('You need to specify a `tokenizer`.')

         super().__init__(a						,       a)
         lowercase__   :       str				=	self.image_processor





    def __call__(				self						,       a						,       a = None						,       a = True						,       a = False						,       a = None						,       a = None						,       a = 0						,       a = None						,       a = None						,       a = None						,       a = False						,       a = False						,       a = False						,       a = False						,       a = True						,       a = None						,       **a						,       ):
         lowercase__   :       Optional[Any]				=	self.tokenizer(
             text=a						,       add_special_tokens=a						,       padding=a						,       truncation=a						,       max_length=a						,       stride=a						,       pad_to_multiple_of=a						,       return_token_type_ids=a						,       return_attention_mask=a						,       return_overflowing_tokens=a						,       return_special_tokens_mask=a						,       return_offsets_mapping=a						,       return_length=a						,       verbose=a						,       return_tensors=a						,       **a						,       )
         # add pixel_values + pixel_mask
         lowercase__   :       str				=	self.image_processor(a						,       return_tensors=a)
         encoding.update(a)

         return encoding





    def 				snake_case_    (				self						,       *a						,       **a):
         return self.tokenizer.batch_decode(*a						,       **a)





    def 				snake_case_    (				self						,       *a						,       **a):
         return self.tokenizer.decode(*a						,       **a)





    @property
    def 				snake_case_    (				self):
         lowercase__   :       int				=	self.tokenizer.model_input_names
         lowercase__   :       List[str]				=	self.image_processor.model_input_names
         return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names))





    @property
    def 				snake_case_    (				self):
         warnings.warn(
             '`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.'						,       a						,       )
         return self.image_processor_class





    @property
    def 				snake_case_    (				self):
         warnings.warn(
             '`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.'						,       a						,       )
         return self.image_processor

