



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

snake_case_			=     logging.get_logger(__name__)


@add_end_docstrings(_lowercase	)
class 	a__     (				_lowercase	):


	def __init__(self : str,					*__UpperCAmelCase : Union[str, Any],					**__UpperCAmelCase : Union[str, Any]      )     ->			Dict:

					"""simple docstring"""

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


	def 				lowercase__  (self : Tuple,					__UpperCAmelCase : Optional[int]=None,					__UpperCAmelCase : Tuple=None,					__UpperCAmelCase : List[str]=None,					**__UpperCAmelCase : int      )     ->			int:

					"""simple docstring"""

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


	def __call__(self : Any,					__UpperCAmelCase : Union["Image.Image", str],					__UpperCAmelCase : str = None,					**__UpperCAmelCase : Any      )     ->			Tuple:

					"""simple docstring"""

					if isinstance(__UpperCAmelCase,					(Image.Image, str)      ) and isinstance(__UpperCAmelCase,					__UpperCAmelCase      ):
									SCREAMING_SNAKE_CASE  :						str      					= {'''image''': image, '''question''': question}
					else:
									SCREAMING_SNAKE_CASE  :						str      					= image
					SCREAMING_SNAKE_CASE  :						Optional[int]      					= super().__call__(__UpperCAmelCase,					**__UpperCAmelCase      )
					return results


	def 				lowercase__  (self : Optional[int],					__UpperCAmelCase : Tuple,					__UpperCAmelCase : Optional[Any]=False,					__UpperCAmelCase : Union[str, Any]=False      )     ->			List[Any]:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						List[str]      					= load_image(inputs['''image''']      )
					SCREAMING_SNAKE_CASE  :						Tuple      					= self.tokenizer(
					    inputs['''question'''],					return_tensors=self.framework,					padding=__UpperCAmelCase,					truncation=__UpperCAmelCase      )
					SCREAMING_SNAKE_CASE  :						int      					= self.image_processor(images=__UpperCAmelCase,					return_tensors=self.framework      )
					model_inputs.update(__UpperCAmelCase      )
					return model_inputs


	def 				lowercase__  (self : Optional[Any],					__UpperCAmelCase : Optional[int]      )     ->			Any:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						str      					= self.model(**__UpperCAmelCase      )
					return model_outputs






	def 				lowercase__  (self : Optional[int],					__UpperCAmelCase : Optional[Any],					__UpperCAmelCase : Union[str, Any]=5      )     ->			Dict:

					"""simple docstring"""

					if top_k > self.model.config.num_labels:
									SCREAMING_SNAKE_CASE  :						Dict      					= self.model.config.num_labels

					if self.framework == "pt":
									SCREAMING_SNAKE_CASE  :						Optional[Any]      					= model_outputs.logits.sigmoid()[0]
									SCREAMING_SNAKE_CASE					,					SCREAMING_SNAKE_CASE  :						Union[str, Any]      					= probs.topk(__UpperCAmelCase      )
					else:
									raise ValueError(F'''Unsupported framework: {self.framework}'''      )

					SCREAMING_SNAKE_CASE  :						Dict      					= scores.tolist()
					SCREAMING_SNAKE_CASE  :						Optional[int]      					= ids.tolist()
					return [{"score": score, "answer": self.model.config.idalabel[_id]} for score, _id in zip(__UpperCAmelCase,					__UpperCAmelCase      )]









'''simple docstring'''

from ..utils import DummyObject, requires_backends


class 	a__     (				metaclass=_lowercase	):
	__magic_name__		:    Dict	    =     ["torch", "transformers", "onnx"]


	def __init__(self : List[str],					*__UpperCAmelCase : Dict,					**__UpperCAmelCase : List[Any]      )     ->			Union[str, Any]:

					"""simple docstring"""

					requires_backends(self,					['''torch''', '''transformers''', '''onnx''']      )


	@classmethod
	def 				lowercase__  (cls : Optional[Any],					*__UpperCAmelCase : Tuple,					**__UpperCAmelCase : int      )     ->			int:

					"""simple docstring"""

					requires_backends(cls,					['''torch''', '''transformers''', '''onnx''']      )






	@classmethod
	def 				lowercase__  (cls : Any,					*__UpperCAmelCase : Tuple,					**__UpperCAmelCase : Tuple      )     ->			Tuple:

					"""simple docstring"""

					requires_backends(cls,					['''torch''', '''transformers''', '''onnx''']      )



class 	a__     (				metaclass=_lowercase	):
	__magic_name__		:    Any	    =     ["torch", "transformers", "onnx"]


	def __init__(self : Dict,					*__UpperCAmelCase : Union[str, Any],					**__UpperCAmelCase : Optional[Any]      )     ->			int:

					"""simple docstring"""

					requires_backends(self,					['''torch''', '''transformers''', '''onnx''']      )


	@classmethod
	def 				lowercase__  (cls : Any,					*__UpperCAmelCase : Tuple,					**__UpperCAmelCase : List[str]      )     ->			Optional[int]:

					"""simple docstring"""

					requires_backends(cls,					['''torch''', '''transformers''', '''onnx''']      )






	@classmethod
	def 				lowercase__  (cls : int,					*__UpperCAmelCase : List[Any],					**__UpperCAmelCase : Any      )     ->			Optional[Any]:

					"""simple docstring"""

					requires_backends(cls,					['''torch''', '''transformers''', '''onnx''']      )



class 	a__     (				metaclass=_lowercase	):
	__magic_name__		:    Optional[Any]	    =     ["torch", "transformers", "onnx"]


	def __init__(self : Dict,					*__UpperCAmelCase : Dict,					**__UpperCAmelCase : Any      )     ->			List[str]:

					"""simple docstring"""

					requires_backends(self,					['''torch''', '''transformers''', '''onnx''']      )


	@classmethod
	def 				lowercase__  (cls : Tuple,					*__UpperCAmelCase : Union[str, Any],					**__UpperCAmelCase : int      )     ->			Optional[Any]:

					"""simple docstring"""

					requires_backends(cls,					['''torch''', '''transformers''', '''onnx''']      )






	@classmethod
	def 				lowercase__  (cls : Union[str, Any],					*__UpperCAmelCase : List[str],					**__UpperCAmelCase : str      )     ->			Optional[int]:

					"""simple docstring"""

					requires_backends(cls,					['''torch''', '''transformers''', '''onnx''']      )



class 	a__     (				metaclass=_lowercase	):
	__magic_name__		:    Tuple	    =     ["torch", "transformers", "onnx"]


	def __init__(self : Union[str, Any],					*__UpperCAmelCase : Optional[int],					**__UpperCAmelCase : List[Any]      )     ->			Tuple:

					"""simple docstring"""

					requires_backends(self,					['''torch''', '''transformers''', '''onnx''']      )


	@classmethod
	def 				lowercase__  (cls : str,					*__UpperCAmelCase : Optional[int],					**__UpperCAmelCase : Tuple      )     ->			str:

					"""simple docstring"""

					requires_backends(cls,					['''torch''', '''transformers''', '''onnx''']      )






	@classmethod
	def 				lowercase__  (cls : int,					*__UpperCAmelCase : List[Any],					**__UpperCAmelCase : List[str]      )     ->			Tuple:

					"""simple docstring"""

					requires_backends(cls,					['''torch''', '''transformers''', '''onnx''']      )



class 	a__     (				metaclass=_lowercase	):
	__magic_name__		:    List[Any]	    =     ["torch", "transformers", "onnx"]


	def __init__(self : Dict,					*__UpperCAmelCase : Optional[Any],					**__UpperCAmelCase : int      )     ->			List[str]:

					"""simple docstring"""

					requires_backends(self,					['''torch''', '''transformers''', '''onnx''']      )


	@classmethod
	def 				lowercase__  (cls : List[str],					*__UpperCAmelCase : Union[str, Any],					**__UpperCAmelCase : Dict      )     ->			Union[str, Any]:

					"""simple docstring"""

					requires_backends(cls,					['''torch''', '''transformers''', '''onnx''']      )






	@classmethod
	def 				lowercase__  (cls : Any,					*__UpperCAmelCase : List[str],					**__UpperCAmelCase : List[Any]      )     ->			Dict:

					"""simple docstring"""

					requires_backends(cls,					['''torch''', '''transformers''', '''onnx''']      )



class 	a__     (				metaclass=_lowercase	):
	__magic_name__		:    Dict	    =     ["torch", "transformers", "onnx"]


	def __init__(self : Tuple,					*__UpperCAmelCase : Optional[Any],					**__UpperCAmelCase : List[str]      )     ->			Any:

					"""simple docstring"""

					requires_backends(self,					['''torch''', '''transformers''', '''onnx''']      )


	@classmethod
	def 				lowercase__  (cls : Tuple,					*__UpperCAmelCase : Any,					**__UpperCAmelCase : Union[str, Any]      )     ->			Optional[int]:

					"""simple docstring"""

					requires_backends(cls,					['''torch''', '''transformers''', '''onnx''']      )






	@classmethod
	def 				lowercase__  (cls : Optional[Any],					*__UpperCAmelCase : Union[str, Any],					**__UpperCAmelCase : Optional[Any]      )     ->			int:

					"""simple docstring"""

					requires_backends(cls,					['''torch''', '''transformers''', '''onnx''']      )




