
"""simple docstring"""





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 						SCREAMING_SNAKE_CASE_ (						__a ):




		"""simple docstring"""





		__lowercase							:  int						   =		['''image_processor''', '''tokenizer''']
		__lowercase							:  List[Any]						   =		'''BlipImageProcessor'''
		__lowercase							:  Dict						   =		('''BertTokenizer''', '''BertTokenizerFast''')




		def __init__(     self							,  lowerCAmelCase__							,  lowerCAmelCase__):
				__SCREAMING_SNAKE_CASE            = False
				super().__init__(lowerCAmelCase__							,  lowerCAmelCase__)
				__SCREAMING_SNAKE_CASE            = self.image_processor




		def __call__(     self							,  lowerCAmelCase__ = None							,  lowerCAmelCase__ = None							,  lowerCAmelCase__ = True							,  lowerCAmelCase__ = False							,  lowerCAmelCase__ = None							,  lowerCAmelCase__ = None							,  lowerCAmelCase__ = 0							,  lowerCAmelCase__ = None							,  lowerCAmelCase__ = None							,  lowerCAmelCase__ = False							,  lowerCAmelCase__ = False							,  lowerCAmelCase__ = False							,  lowerCAmelCase__ = False							,  lowerCAmelCase__ = False							,  lowerCAmelCase__ = True							,  lowerCAmelCase__ = None							,  **lowerCAmelCase__							,  ):
				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:
						__SCREAMING_SNAKE_CASE            = self.tokenizer
						__SCREAMING_SNAKE_CASE            = self.tokenizer(
						    text=lowerCAmelCase__							,  add_special_tokens=lowerCAmelCase__							,  padding=lowerCAmelCase__							,  truncation=lowerCAmelCase__							,  max_length=lowerCAmelCase__							,  stride=lowerCAmelCase__							,  pad_to_multiple_of=lowerCAmelCase__							,  return_attention_mask=lowerCAmelCase__							,  return_overflowing_tokens=lowerCAmelCase__							,  return_special_tokens_mask=lowerCAmelCase__							,  return_offsets_mapping=lowerCAmelCase__							,  return_token_type_ids=lowerCAmelCase__							,  return_length=lowerCAmelCase__							,  verbose=lowerCAmelCase__							,  return_tensors=lowerCAmelCase__							,  **lowerCAmelCase__							,  )
						return text_encoding

				# add pixel_values
				__SCREAMING_SNAKE_CASE            = self.image_processor(lowerCAmelCase__							,  return_tensors=lowerCAmelCase__)

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

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

				return encoding_image_processor




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




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




		@property
		def 	snake_case_     (     self):
				__SCREAMING_SNAKE_CASE            = self.tokenizer.model_input_names
				__SCREAMING_SNAKE_CASE            = self.image_processor.model_input_names
				return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names))






"""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.
from typing import TYPE_CHECKING

import torch

from ..models.auto import AutoModelForVisualQuestionAnswering, AutoProcessor
from ..utils import requires_backends
from .base import PipelineTool


if TYPE_CHECKING:
							from PIL import Image
class 						SCREAMING_SNAKE_CASE_ (						__a ):




		"""simple docstring"""





		__lowercase							:  Tuple						   =		'''dandelin/vilt-b32-finetuned-vqa'''
		__lowercase							:  str						   =		(
		    '''This is a tool that answers a question about an image. It takes an input named `image` which should be the '''
		    '''image containing the information, as well as a `question` which should be the question in English. It '''
		    '''returns a text that is the answer to the question.'''
		)
		__lowercase							:  Optional[int]						   =		'''image_qa'''
		__lowercase							:  Union[str, Any]						   =		AutoProcessor
		__lowercase							:  Optional[Any]						   =		AutoModelForVisualQuestionAnswering

		__lowercase							:  List[str]						   =		['''image''', '''text''']
		__lowercase							:  Optional[Any]						   =		['''text''']




		def __init__(     self							,  *lowerCAmelCase__							,  **lowerCAmelCase__):
				requires_backends(self							,  ["""vision"""])
				super().__init__(*lowerCAmelCase__							,  **lowerCAmelCase__)




		def 	snake_case_     (     self							,  lowerCAmelCase__							,  lowerCAmelCase__):
				return self.pre_processor(lowerCAmelCase__							,  lowerCAmelCase__							,  return_tensors="""pt""")




		def 	snake_case_     (     self							,  lowerCAmelCase__):
				with torch.no_grad():
						return self.model(**lowerCAmelCase__).logits




		def 	snake_case_     (     self							,  lowerCAmelCase__):
				__SCREAMING_SNAKE_CASE            = outputs.argmax(-1).item()
				return self.model.config.idalabel[idx]




