
from typing import List, Optional, Union

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





class       __A(				UpperCAmelCase       ):
					SCREAMING_SNAKE_CASE			    =    ['''image_processor''', '''tokenizer''']
					SCREAMING_SNAKE_CASE			    =    '''BlipImageProcessor'''
					SCREAMING_SNAKE_CASE			    =    ('''BertTokenizer''', '''BertTokenizerFast''')

					def __init__(	self					:     List[Any]   ,					__UpperCamelCase					:     int   ,					__UpperCamelCase					:     Tuple   ):
												lowerCamelCase_							  =					False
												super().__init__(__UpperCamelCase   ,					__UpperCamelCase   )
												lowerCamelCase_							  =					self.image_processor

					def __call__(	self					:     Tuple   ,					__UpperCamelCase					:     ImageInput = None   ,					__UpperCamelCase					:     Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None   ,					__UpperCamelCase					:     bool = True   ,					__UpperCamelCase					:     Union[bool, str, PaddingStrategy] = False   ,					__UpperCamelCase					:     Union[bool, str, TruncationStrategy] = None   ,					__UpperCamelCase					:     Optional[int] = None   ,					__UpperCamelCase					:     int = 0   ,					__UpperCamelCase					:     Optional[int] = None   ,					__UpperCamelCase					:     Optional[bool] = None   ,					__UpperCamelCase					:     bool = False   ,					__UpperCamelCase					:     bool = False   ,					__UpperCamelCase					:     bool = False   ,					__UpperCamelCase					:     bool = False   ,					__UpperCamelCase					:     bool = False   ,					__UpperCamelCase					:     bool = True   ,					__UpperCamelCase					:     Optional[Union[str, TensorType]] = None   ,					**__UpperCamelCase					:     str   ,					):
												if images is None and text is None:
																			raise ValueError("""You have to specify either images or text."""   )

												# Get only text
												if images is None:
																			lowerCamelCase_							  =					self.tokenizer
																			lowerCamelCase_							  =					self.tokenizer(
																			    text=__UpperCamelCase   ,					add_special_tokens=__UpperCamelCase   ,					padding=__UpperCamelCase   ,					truncation=__UpperCamelCase   ,					max_length=__UpperCamelCase   ,					stride=__UpperCamelCase   ,					pad_to_multiple_of=__UpperCamelCase   ,					return_attention_mask=__UpperCamelCase   ,					return_overflowing_tokens=__UpperCamelCase   ,					return_special_tokens_mask=__UpperCamelCase   ,					return_offsets_mapping=__UpperCamelCase   ,					return_token_type_ids=__UpperCamelCase   ,					return_length=__UpperCamelCase   ,					verbose=__UpperCamelCase   ,					return_tensors=__UpperCamelCase   ,					**__UpperCamelCase   ,					)
																			return text_encoding

												# add pixel_values
												lowerCamelCase_							  =					self.image_processor(__UpperCamelCase   ,					return_tensors=__UpperCamelCase   )

												if text is not None:
																			lowerCamelCase_							  =					self.tokenizer(
																			    text=__UpperCamelCase   ,					add_special_tokens=__UpperCamelCase   ,					padding=__UpperCamelCase   ,					truncation=__UpperCamelCase   ,					max_length=__UpperCamelCase   ,					stride=__UpperCamelCase   ,					pad_to_multiple_of=__UpperCamelCase   ,					return_attention_mask=__UpperCamelCase   ,					return_overflowing_tokens=__UpperCamelCase   ,					return_special_tokens_mask=__UpperCamelCase   ,					return_offsets_mapping=__UpperCamelCase   ,					return_token_type_ids=__UpperCamelCase   ,					return_length=__UpperCamelCase   ,					verbose=__UpperCamelCase   ,					return_tensors=__UpperCamelCase   ,					**__UpperCamelCase   ,					)
												else:
																			lowerCamelCase_							  =					None

												if text_encoding is not None:
																			encoding_image_processor.update(__UpperCamelCase   )

												return encoding_image_processor

					def    lowercase__      (	self					:     int   ,					*__UpperCamelCase					:     List[str]   ,					**__UpperCamelCase					:     List[str]   ):
												return self.tokenizer.batch_decode(*__UpperCamelCase   ,					**__UpperCamelCase   )

					def    lowercase__      (	self					:     Dict   ,					*__UpperCamelCase					:     Optional[Any]   ,					**__UpperCamelCase					:     Dict   ):
												return self.tokenizer.decode(*__UpperCamelCase   ,					**__UpperCamelCase   )



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







# 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(				UpperCAmelCase       ):
					SCREAMING_SNAKE_CASE			    =    '''facebook/bart-large-mnli'''
					SCREAMING_SNAKE_CASE			    =    (
					    '''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			    =    '''text_classifier'''
					SCREAMING_SNAKE_CASE			    =    AutoTokenizer
					SCREAMING_SNAKE_CASE			    =    AutoModelForSequenceClassification

					SCREAMING_SNAKE_CASE			    =    ['''text''', ['''text''']]
					SCREAMING_SNAKE_CASE			    =    ['''text''']

					def    lowercase__      (	self					:     Union[str, Any]   ):
												super().setup()
												lowerCamelCase_							  =					self.model.config
												lowerCamelCase_							  =					-1
												for idx, label in config.idalabel.items():
																			if label.lower().startswith("""entail"""   ):
																										lowerCamelCase_							  =					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    lowercase__      (	self					:     Any   ,					__UpperCamelCase					:     List[Any]   ,					__UpperCamelCase					:     Optional[Any]   ):
												lowerCamelCase_							  =					labels
												return self.pre_processor(
												    [text] * len(__UpperCamelCase   )   ,					[F'''This example is {label}''' for label in labels]   ,					return_tensors="""pt"""   ,					padding="""max_length"""   ,					)



					def    lowercase__      (	self					:     List[Any]   ,					__UpperCamelCase					:     List[str]   ):
												lowerCamelCase_							  =					outputs.logits
												lowerCamelCase_							  =					torch.argmax(logits[:, 2]   ).item()
												return self._labels[label_id]





