





"""simple docstring"""






import os
import unittest

from transformers import LxmertTokenizer, LxmertTokenizerFast
from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES
from transformers.testing_utils import require_tokenizers

from ...test_tokenization_common import TokenizerTesterMixin



@require_tokenizers
class 					__UpperCAmelCase							(						_UpperCamelCase						,  unittest.TestCase  ):
				__lowerCamelCase				:	Optional[Any]				     =				LxmertTokenizer
				__lowerCamelCase				:	Optional[Any]				     =				LxmertTokenizerFast
				__lowerCamelCase				:	str				     =				True
				__lowerCamelCase				:	Any				     =				True
				def       UpperCAmelCase      (		self    :			str    )	->	Union[str, Any]:



							'''simple docstring'''



							super().setUp()

							a__  :	Tuple		 =			[
							    "[UNK]",
							    "[CLS]",
							    "[SEP]",
							    "want",
							    "##want",
							    "##ed",
							    "wa",
							    "un",
							    "runn",
							    "##ing",
							    ",",
							    "low",
							    "lowest",
							]
							a__  :	Dict		 =			os.path.join(self.tmpdirname	, VOCAB_FILES_NAMES["vocab_file"]    )
							with open(self.vocab_file	, "w"	, encoding="utf-8"    ) as vocab_writer:
										vocab_writer.write("".join([x + "\n" for x in vocab_tokens]    )    )
				def       UpperCAmelCase      (		self    :			List[str]	, a_    :			str    )	->	Dict:



							'''simple docstring'''



							a__  :	List[Any]		 =			"UNwant\u00E9d,running"
							a__  :	Union[str, Any]		 =			"unwanted, running"
							return input_text, output_text
				def       UpperCAmelCase      (		self    :			Optional[int]    )	->	Union[str, Any]:



							'''simple docstring'''



							a__  :	str		 =			self.tokenizer_class(self.vocab_file    )

							a__  :	List[Any]		 =			tokenizer.tokenize("UNwant\u00E9d,running"    )
							self.assertListEqual(a_	, ["un", "##want", "##ed", ",", "runn", "##ing"]    )
							self.assertListEqual(tokenizer.convert_tokens_to_ids(a_    )	, [7, 4, 5, 10, 8, 9]    )






				def       UpperCAmelCase      (		self    :			str    )	->	Optional[int]:



							'''simple docstring'''



							if not self.test_rust_tokenizer:
										return

							a__  :	int		 =			self.get_tokenizer()
							a__  :	List[str]		 =			self.get_rust_tokenizer()

							a__  :	Optional[Any]		 =			"I was born in 92000, and this is falsé."

							a__  :	int		 =			tokenizer.tokenize(a_    )
							a__  :	int		 =			rust_tokenizer.tokenize(a_    )
							self.assertListEqual(a_	, a_    )

							a__  :	str		 =			tokenizer.encode(a_	, add_special_tokens=a_    )
							a__  :	List[str]		 =			rust_tokenizer.encode(a_	, add_special_tokens=a_    )
							self.assertListEqual(a_	, a_    )

							a__  :	Tuple		 =			self.get_rust_tokenizer()
							a__  :	Tuple		 =			tokenizer.encode(a_    )
							a__  :	List[Any]		 =			rust_tokenizer.encode(a_    )
							self.assertListEqual(a_	, a_    )


"""simple docstring"""

from typing import Union

from ..utils import add_end_docstrings, is_torch_available, is_vision_available, logging
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_vision_available():
    from PIL import Image

    from ..image_utils import load_image

if is_torch_available():
    from ..models.auto.modeling_auto import MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING

_UpperCamelCase		     =    logging.get_logger(__name__)






@add_end_docstrings(__magic_name__       )
class 			__a					(						__magic_name__       ):





       """simple docstring"""


       def __init__(   self			,					*snake_case			,					**snake_case   ):





        """simple docstring"""

        super().__init__(*snake_case			,					**snake_case   )
        self.check_model_type(snake_case   )






       def 			SCREAMING_SNAKE_CASE_	(   self			,					snake_case=None			,					snake_case=None			,					snake_case=None			,					**snake_case   ):





        """simple docstring"""

        lowerCAmelCase__			,						lowerCAmelCase__   :				List[Any] 			=				{}, {}
        if padding is not None:
         lowerCAmelCase__   :				Dict 			=				padding
        if truncation is not None:
         lowerCAmelCase__   :				Tuple 			=				truncation
        if top_k is not None:
         lowerCAmelCase__   :				str 			=				top_k
        return preprocess_params, {}, postprocess_params






       def __call__(   self			,					snake_case			,					snake_case = None			,					**snake_case   ):





        """simple docstring"""

        if isinstance(snake_case			,					(Image.Image, str)   ) and isinstance(snake_case			,					snake_case   ):
         lowerCAmelCase__   :				int 			=				{"image": image, "question": question}
        else:
         lowerCAmelCase__   :				Optional[int] 			=				image
        lowerCAmelCase__   :				Optional[Any] 			=				super().__call__(snake_case			,					**snake_case   )
        return results






       def 			SCREAMING_SNAKE_CASE_	(   self			,					snake_case			,					snake_case=False			,					snake_case=False   ):





        """simple docstring"""

        lowerCAmelCase__   :				Union[str, Any] 			=				load_image(inputs["image"]   )
        lowerCAmelCase__   :				str 			=				self.tokenizer(
            inputs["question"]			,					return_tensors=self.framework			,					padding=snake_case			,					truncation=snake_case   )
        lowerCAmelCase__   :				int 			=				self.image_processor(images=snake_case			,					return_tensors=self.framework   )
        model_inputs.update(snake_case   )
        return model_inputs






       def 			SCREAMING_SNAKE_CASE_	(   self			,					snake_case   ):





        """simple docstring"""

        lowerCAmelCase__   :				Optional[int] 			=				self.model(**snake_case   )
        return model_outputs






       def 			SCREAMING_SNAKE_CASE_	(   self			,					snake_case			,					snake_case=5   ):





        """simple docstring"""

        if top_k > self.model.config.num_labels:
         lowerCAmelCase__   :				List[Any] 			=				self.model.config.num_labels

        if self.framework == "pt":
         lowerCAmelCase__   :				Optional[int] 			=				model_outputs.logits.sigmoid()[0]
         lowerCAmelCase__			,						lowerCAmelCase__   :				Optional[int] 			=				probs.topk(snake_case   )
        else:
         raise ValueError(F"""Unsupported framework: {self.framework}"""   )

        lowerCAmelCase__   :				int 			=				scores.tolist()
        lowerCAmelCase__   :				str 			=				ids.tolist()
        return [{"score": score, "answer": self.model.config.idalabel[_id]} for score, _id in zip(snake_case			,					snake_case   )]






