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

lowerCamelCase_  :	List[Any] 				=					logging.get_logger(__name__)

@add_end_docstrings(_lowerCamelCase					)
class     _UpperCamelCase			(   _lowerCamelCase					):






			'''simple docstring'''

			def __init__(    self	:    Optional[int]  ,		*snake_case_	:    Any  ,		**snake_case_	:    List[Any] ):
							super().__init__(*snake_case_  ,		**snake_case_ )
							self.check_model_type(snake_case_ )

			def  lowerCAmelCase__		(    self	:    Optional[int]  ,		snake_case_	:    Any=None  ,		snake_case_	:    str=None  ,		snake_case_	:    Union[str, Any]=None  ,		**snake_case_	:    Union[str, Any] ):
							UpperCamelCase_,				UpperCamelCase_:							str		=  {}, {}
							if padding is not None:
											UpperCamelCase_:							List[str]		=  padding
							if truncation is not None:
											UpperCamelCase_:							Dict		=  truncation
							if top_k is not None:
											UpperCamelCase_:							str		=  top_k
							return preprocess_params, {}, postprocess_params

			def __call__(    self	:    List[Any]  ,		snake_case_	:    Union["Image.Image", str]  ,		snake_case_	:    str = None  ,		**snake_case_	:    Dict ):


							if isinstance(snake_case_  ,		(Image.Image, str) ) and isinstance(snake_case_  ,		snake_case_ ):
											UpperCamelCase_:							List[Any]		=  {"""image""": image, """question""": question}
							else:
											UpperCamelCase_:							Tuple		=  image
							UpperCamelCase_:							Optional[Any]		=  super().__call__(snake_case_  ,		**snake_case_ )
							return results

			def  lowerCAmelCase__		(    self	:    List[str]  ,		snake_case_	:    Optional[int]  ,		snake_case_	:    Optional[Any]=False  ,		snake_case_	:    Any=False ):
							UpperCamelCase_:							Optional[int]		=  load_image(inputs["""image"""] )
							UpperCamelCase_:							Optional[Any]		=  self.tokenizer(
							    inputs["""question"""]  ,		return_tensors=self.framework  ,		padding=snake_case_  ,		truncation=snake_case_ )
							UpperCamelCase_:							Any		=  self.image_processor(images=snake_case_  ,		return_tensors=self.framework )
							model_inputs.update(snake_case_ )
							return model_inputs

			def  lowerCAmelCase__		(    self	:    Union[str, Any]  ,		snake_case_	:    Tuple ):
							UpperCamelCase_:							Union[str, Any]		=  self.model(**snake_case_ )
							return model_outputs




			def  lowerCAmelCase__		(    self	:    str  ,		snake_case_	:    Optional[Any]  ,		snake_case_	:    Tuple=5 ):


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

							if self.framework == "pt":
											UpperCamelCase_:							Optional[int]		=  model_outputs.logits.sigmoid()[0]
											UpperCamelCase_,				UpperCamelCase_:							List[Any]		=  probs.topk(snake_case_ )
							else:
											raise ValueError(f'''Unsupported framework: {self.framework}''' )

							UpperCamelCase_:							Union[str, Any]		=  scores.tolist()
							UpperCamelCase_:							Any		=  ids.tolist()
							return [{"score": score, "answer": self.model.config.idalabel[_id]} for score, _id in zip(snake_case_  ,		snake_case_ )]








from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding

class     _UpperCamelCase			(   _A					):






			'''simple docstring'''

			__UpperCamelCase :  Union[str, Any]    =	["""image_processor""", """tokenizer"""]
			__UpperCamelCase :  List[Any]    =	"""AutoImageProcessor"""
			__UpperCamelCase :  Tuple    =	"""AutoTokenizer"""

			def __init__(    self	:    Any  ,		snake_case_	:    Optional[Any]  ,		snake_case_	:    Any ):
							super().__init__(snake_case_  ,		snake_case_ )
							UpperCamelCase_:							int		=  self.image_processor

			def __call__(    self	:    str  ,		snake_case_	:    Optional[int]=None  ,		snake_case_	:    int=None  ,		snake_case_	:    Dict=None  ,		**snake_case_	:    Optional[int] ):

							if text is None and images is None:
											raise ValueError("""You have to specify either text or images. Both cannot be none.""" )

							if text is not None:
											UpperCamelCase_:							List[Any]		=  self.tokenizer(snake_case_  ,		return_tensors=snake_case_  ,		**snake_case_ )

							if images is not None:
											UpperCamelCase_:							Any		=  self.image_processor(snake_case_  ,		return_tensors=snake_case_  ,		**snake_case_ )

							if text is not None and images is not None:
											UpperCamelCase_:							Tuple		=  image_features.pixel_values
											return encoding
							elif text is not None:
											return encoding
							else:
											return BatchEncoding(data=dict(**snake_case_ )  ,		tensor_type=snake_case_ )

			def  lowerCAmelCase__		(    self	:    List[str]  ,		*snake_case_	:    int  ,		**snake_case_	:    Optional[Any] ):
							return self.tokenizer.batch_decode(*snake_case_  ,		**snake_case_ )

			def  lowerCAmelCase__		(    self	:    int  ,		*snake_case_	:    Optional[Any]  ,		**snake_case_	:    str ):
							return self.tokenizer.decode(*snake_case_  ,		**snake_case_ )




			@property
			def  lowerCAmelCase__		(    self	:    Union[str, Any] ):
							return ["input_ids", "attention_mask", "pixel_values"]






