




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

__A        =    logging.get_logger(__name__)







@add_end_docstrings(__magic_name__ )
class     __lowerCAmelCase (       __magic_name__ ):

			"""simple docstring"""







			def __init__(      self			,						*lowerCamelCase__			,						**lowerCamelCase__							)    ->      Any:



							'''simple docstring'''
							super().__init__(*lowerCamelCase__			,						**lowerCamelCase__							)
							self.check_model_type(lowerCamelCase__							)


			def       lowercase_    (      self			,						lowerCamelCase__=None			,						lowerCamelCase__=None			,						lowerCamelCase__=None			,						**lowerCamelCase__							)    ->      List[str]:



							'''simple docstring'''
							__lowerCamelCase       ,						__lowerCamelCase		       =    {}, {}
							if padding is not None:
											__lowerCamelCase		       =    padding
							if truncation is not None:
											__lowerCamelCase		       =    truncation
							if top_k is not None:
											__lowerCamelCase		       =    top_k
							return preprocess_params, {}, postprocess_params


			def __call__(      self			,						lowerCamelCase__			,						lowerCamelCase__ = None			,						**lowerCamelCase__							)    ->      List[Any]:



							'''simple docstring'''
							if isinstance(lowerCamelCase__			,						(Image.Image, str)							) and isinstance(lowerCamelCase__			,						lowerCamelCase__							):
											__lowerCamelCase		       =    {'image': image, 'question': question}
							else:
											__lowerCamelCase		       =    image
							__lowerCamelCase		       =    super().__call__(lowerCamelCase__			,						**lowerCamelCase__							)
							return results


			def       lowercase_    (      self			,						lowerCamelCase__			,						lowerCamelCase__=False			,						lowerCamelCase__=False							)    ->      Union[str, Any]:



							'''simple docstring'''
							__lowerCamelCase		       =    load_image(inputs['image']							)
							__lowerCamelCase		       =    self.tokenizer(
							    inputs['question']			,						return_tensors=self.framework			,						padding=lowerCamelCase__			,						truncation=lowerCamelCase__							)
							__lowerCamelCase		       =    self.image_processor(images=lowerCamelCase__			,						return_tensors=self.framework							)
							model_inputs.update(lowerCamelCase__							)
							return model_inputs


			def       lowercase_    (      self			,						lowerCamelCase__							)    ->      Union[str, Any]:



							'''simple docstring'''
							__lowerCamelCase		       =    self.model(**lowerCamelCase__							)
							return model_outputs






			def       lowercase_    (      self			,						lowerCamelCase__			,						lowerCamelCase__=5							)    ->      Optional[int]:



							'''simple docstring'''
							if top_k > self.model.config.num_labels:
											__lowerCamelCase		       =    self.model.config.num_labels

							if self.framework == "pt":
											__lowerCamelCase		       =    model_outputs.logits.sigmoid()[0]
											__lowerCamelCase       ,						__lowerCamelCase		       =    probs.topk(lowerCamelCase__							)
							else:
											raise ValueError(f"""Unsupported framework: {self.framework}"""							)

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








def 			lowerCamelCase_		(       UpperCamelCase__				:      int      )     ->       bool:





				"""simple docstring"""


				return number & 1 == 0


if __name__ == "__main__":
					import doctest

					doctest.testmod()


