



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

 """simple docstring"""



 _lowercase   :    Union[str, Any]      						=					'''facebook/bart-large-mnli'''
 _lowercase   :    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.'''
 )
 _lowercase   :    Optional[int]      						=					'''text_classifier'''
 _lowercase   :    Any      						=					AutoTokenizer
 _lowercase   :    str      						=					AutoModelForSequenceClassification

 _lowercase   :    Tuple      						=					['''text''', ['''text''']]
 _lowercase   :    Any      						=					['''text''']

 def 						_UpperCAmelCase       (	self      ) ->			Union[str, Any]:
  super().setup()
  a__												= self.model.config
  a__												= -1
  for idx, label in config.idalabel.items():
   if label.lower().startswith('''entail'''      ):
    a__												= 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 						_UpperCAmelCase       (	self		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE      ) ->			str:
  a__												= 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 						_UpperCAmelCase       (	self		,				SCREAMING_SNAKE_CASE      ) ->			Tuple:
  a__												= outputs.logits
  a__												= torch.argmax(logits[:, 2]      ).item()
  return self._labels[label_id]










import warnings

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding
class    __UpperCamelCase			(       _lowercase				):

 """simple docstring"""



 _lowercase   :    Optional[Any]      						=					['''image_processor''', '''tokenizer''']
 _lowercase   :    Optional[int]      						=					'''ChineseCLIPImageProcessor'''
 _lowercase   :    List[Any]      						=					('''BertTokenizer''', '''BertTokenizerFast''')

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

  a__												= 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__(SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE      )
  a__												= self.image_processor

 def __call__(	self		,				SCREAMING_SNAKE_CASE=None		,				SCREAMING_SNAKE_CASE=None		,				SCREAMING_SNAKE_CASE=None		,				**SCREAMING_SNAKE_CASE      ) ->			List[str]:

  if text is None and images is None:
   raise ValueError('''You have to specify either text or images. Both cannot be none.'''      )

  if text is not None:
   a__												= self.tokenizer(SCREAMING_SNAKE_CASE		,				return_tensors=SCREAMING_SNAKE_CASE		,				**SCREAMING_SNAKE_CASE      )

  if images is not None:
   a__												= self.image_processor(SCREAMING_SNAKE_CASE		,				return_tensors=SCREAMING_SNAKE_CASE		,				**SCREAMING_SNAKE_CASE      )

  if text is not None and images is not None:
   a__												= image_features.pixel_values
   return encoding
  elif text is not None:
   return encoding
  else:
   return BatchEncoding(data=dict(**SCREAMING_SNAKE_CASE      )		,				tensor_type=SCREAMING_SNAKE_CASE      )

 def 						_UpperCAmelCase       (	self		,				*SCREAMING_SNAKE_CASE		,				**SCREAMING_SNAKE_CASE      ) ->			List[str]:
  return self.tokenizer.batch_decode(*SCREAMING_SNAKE_CASE		,				**SCREAMING_SNAKE_CASE      )

 def 						_UpperCAmelCase       (	self		,				*SCREAMING_SNAKE_CASE		,				**SCREAMING_SNAKE_CASE      ) ->			int:
  return self.tokenizer.decode(*SCREAMING_SNAKE_CASE		,				**SCREAMING_SNAKE_CASE      )

 @property
 def 						_UpperCAmelCase       (	self      ) ->			List[str]:
  a__												= self.tokenizer.model_input_names
  a__												= self.image_processor.model_input_names
  return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names      )      )



 @property
 def 						_UpperCAmelCase       (	self      ) ->			Optional[int]:
  warnings.warn(
      '''`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.'''		,				SCREAMING_SNAKE_CASE		,				)
  return self.image_processor_class





