
"""simple docstring"""





import warnings

from transformers import AutoTokenizer
from transformers.utils import is_torch_available
from transformers.utils.generic import ExplicitEnum

from ...processing_utils import ProcessorMixin


if is_torch_available():
							import torch
class 						SCREAMING_SNAKE_CASE_ (						__a ):




		"""simple docstring"""





		__lowercase							:  str						   =		'''char'''
		__lowercase							:  int						   =		'''bpe'''
		__lowercase							:  Tuple						   =		'''wp'''



__magic_name__       =			(DecodeType.CHARACTER, DecodeType.BPE, DecodeType.WORDPIECE)
class 						SCREAMING_SNAKE_CASE_ (						__a ):




		"""simple docstring"""





		__lowercase							:  Tuple						   =		['''image_processor''', '''char_tokenizer''']
		__lowercase							:  Dict						   =		'''ViTImageProcessor'''
		__lowercase							:  Union[str, Any]						   =		'''MgpstrTokenizer'''




		def __init__(     self							,  lowerCAmelCase__=None							,  lowerCAmelCase__=None							,  **lowerCAmelCase__):
				__SCREAMING_SNAKE_CASE            = None
				if "feature_extractor" in kwargs:
						warnings.warn(
						    """The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`"""
						    """ instead."""							,  lowerCAmelCase__							,  )
						__SCREAMING_SNAKE_CASE            = kwargs.pop("""feature_extractor""")

				__SCREAMING_SNAKE_CASE            = 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`.""")

				__SCREAMING_SNAKE_CASE            = tokenizer
				__SCREAMING_SNAKE_CASE            = AutoTokenizer.from_pretrained("""gpt2""")
				__SCREAMING_SNAKE_CASE            = AutoTokenizer.from_pretrained("""bert-base-uncased""")

				super().__init__(lowerCAmelCase__							,  lowerCAmelCase__)




		def __call__(     self							,  lowerCAmelCase__=None							,  lowerCAmelCase__=None							,  lowerCAmelCase__=None							,  **lowerCAmelCase__):
				if images is None and text is None:
						raise ValueError("""You need to specify either an `images` or `text` input to process.""")

				if images is not None:
						__SCREAMING_SNAKE_CASE            = self.image_processor(lowerCAmelCase__							,  return_tensors=lowerCAmelCase__							,  **lowerCAmelCase__)
				if text is not None:
						__SCREAMING_SNAKE_CASE            = self.char_tokenizer(lowerCAmelCase__							,  return_tensors=lowerCAmelCase__							,  **lowerCAmelCase__)

				if text is None:
						return inputs
				elif images is None:
						return encodings
				else:
						__SCREAMING_SNAKE_CASE            = encodings["""input_ids"""]
						return inputs




		def 	snake_case_     (     self							,  lowerCAmelCase__):
				__SCREAMING_SNAKE_CASE       ,__SCREAMING_SNAKE_CASE       ,__SCREAMING_SNAKE_CASE            = sequences
				__SCREAMING_SNAKE_CASE            = char_preds.size(0)

				__SCREAMING_SNAKE_CASE       ,__SCREAMING_SNAKE_CASE            = self._decode_helper(lowerCAmelCase__							,  """char""")
				__SCREAMING_SNAKE_CASE       ,__SCREAMING_SNAKE_CASE            = self._decode_helper(lowerCAmelCase__							,  """bpe""")
				__SCREAMING_SNAKE_CASE       ,__SCREAMING_SNAKE_CASE            = self._decode_helper(lowerCAmelCase__							,  """wp""")

				__SCREAMING_SNAKE_CASE            = []
				__SCREAMING_SNAKE_CASE            = []
				for i in range(lowerCAmelCase__):
						__SCREAMING_SNAKE_CASE            = [char_scores[i], bpe_scores[i], wp_scores[i]]
						__SCREAMING_SNAKE_CASE            = [char_strs[i], bpe_strs[i], wp_strs[i]]
						__SCREAMING_SNAKE_CASE            = scores.index(max(lowerCAmelCase__))
						final_strs.append(strs[max_score_index])
						final_scores.append(scores[max_score_index])

				__SCREAMING_SNAKE_CASE            = {}
				__SCREAMING_SNAKE_CASE            = final_strs
				__SCREAMING_SNAKE_CASE            = final_scores
				__SCREAMING_SNAKE_CASE            = char_strs
				__SCREAMING_SNAKE_CASE            = bpe_strs
				__SCREAMING_SNAKE_CASE            = wp_strs
				return out




		def 	snake_case_     (     self							,  lowerCAmelCase__							,  lowerCAmelCase__):
				if format == DecodeType.CHARACTER:
						__SCREAMING_SNAKE_CASE            = self.char_decode
						__SCREAMING_SNAKE_CASE            = 1
						__SCREAMING_SNAKE_CASE            = """[s]"""
				elif format == DecodeType.BPE:
						__SCREAMING_SNAKE_CASE            = self.bpe_decode
						__SCREAMING_SNAKE_CASE            = 2
						__SCREAMING_SNAKE_CASE            = """#"""
				elif format == DecodeType.WORDPIECE:
						__SCREAMING_SNAKE_CASE            = self.wp_decode
						__SCREAMING_SNAKE_CASE            = 1_0_2
						__SCREAMING_SNAKE_CASE            = """[SEP]"""
				else:
						raise ValueError(f"Format {format} is not supported.")

				__SCREAMING_SNAKE_CASE       ,__SCREAMING_SNAKE_CASE            = [], []
				__SCREAMING_SNAKE_CASE            = pred_logits.size(0)
				__SCREAMING_SNAKE_CASE            = pred_logits.size(1)
				__SCREAMING_SNAKE_CASE       ,__SCREAMING_SNAKE_CASE            = pred_logits.topk(1							,  dim=-1							,  largest=lowerCAmelCase__							,  sorted=lowerCAmelCase__)
				__SCREAMING_SNAKE_CASE            = preds_index.view(-1							,  lowerCAmelCase__)[:, 1:]
				__SCREAMING_SNAKE_CASE            = decoder(lowerCAmelCase__)
				__SCREAMING_SNAKE_CASE       ,__SCREAMING_SNAKE_CASE            = torch.nn.functional.softmax(lowerCAmelCase__							,  dim=2).max(dim=2)
				__SCREAMING_SNAKE_CASE            = preds_max_prob[:, 1:]

				for index in range(lowerCAmelCase__):
						__SCREAMING_SNAKE_CASE            = preds_str[index].find(lowerCAmelCase__)
						__SCREAMING_SNAKE_CASE            = preds_str[index][:pred_eos]
						__SCREAMING_SNAKE_CASE            = preds_index[index].cpu().tolist()
						__SCREAMING_SNAKE_CASE            = pred_index.index(lowerCAmelCase__) if eos_token in pred_index else -1
						__SCREAMING_SNAKE_CASE            = preds_max_prob[index][: pred_eos_index + 1]
						__SCREAMING_SNAKE_CASE            = pred_max_prob.cumprod(dim=0)[-1] if pred_max_prob.nelement() != 0 else 0.0
						dec_strs.append(lowerCAmelCase__)
						conf_scores.append(lowerCAmelCase__)

				return dec_strs, conf_scores




		def 	snake_case_     (     self							,  lowerCAmelCase__):
				__SCREAMING_SNAKE_CASE            = [seq.replace(""" """							,  """""") for seq in self.char_tokenizer.batch_decode(lowerCAmelCase__)]
				return decode_strs




		def 	snake_case_     (     self							,  lowerCAmelCase__):
				return self.bpe_tokenizer.batch_decode(lowerCAmelCase__)




		def 	snake_case_     (     self							,  lowerCAmelCase__):
				__SCREAMING_SNAKE_CASE            = [seq.replace(""" """							,  """""") for seq in self.wp_tokenizer.batch_decode(lowerCAmelCase__)]
				return decode_strs






"""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]




