


'''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										=     logging.get_logger(__name__)






@add_end_docstrings(__lowerCamelCase					)
class 	lowerCAmelCase_(    __lowerCamelCase					):
  '''simple docstring'''




  def __init__(   self  ,*__UpperCAmelCase  ,**__UpperCAmelCase				)   ->		Optional[Any]:
      super().__init__(*UpperCamelCase_  ,**UpperCamelCase_				)
      self.check_model_type(UpperCamelCase_				)




  def 							UpperCAmelCase_     (   self  ,__UpperCAmelCase=None  ,__UpperCAmelCase=None  ,__UpperCAmelCase=None  ,**__UpperCAmelCase				)   ->		List[Any]:
      lowerCAmelCase__				,       lowerCAmelCase__			:  int											= {}, {}
      if padding is not None:
          lowerCAmelCase__			:  str											= padding
      if truncation is not None:
          lowerCAmelCase__			:  Optional[Any]											= truncation
      if top_k is not None:
          lowerCAmelCase__			:  List[str]											= top_k
      return preprocess_params, {}, postprocess_params




  def __call__(   self  ,__UpperCAmelCase  ,__UpperCAmelCase = None  ,**__UpperCAmelCase				)   ->		Union[str, Any]:
      if isinstance(UpperCamelCase_  ,(Image.Image, str)				) and isinstance(UpperCamelCase_  ,UpperCamelCase_				):
          lowerCAmelCase__			:  str											= {"""image""": image, """question""": question}
      else:
          lowerCAmelCase__			:  str											= image
      lowerCAmelCase__			:  int											= super().__call__(UpperCamelCase_  ,**UpperCamelCase_				)
      return results




  def 							UpperCAmelCase_     (   self  ,__UpperCAmelCase  ,__UpperCAmelCase=False  ,__UpperCAmelCase=False				)   ->		Union[str, Any]:
      lowerCAmelCase__			:  Tuple											= load_image(inputs["""image"""]				)
      lowerCAmelCase__			:  Optional[Any]											= self.tokenizer(
          inputs["""question"""]  ,return_tensors=self.framework  ,padding=UpperCamelCase_  ,truncation=UpperCamelCase_				)
      lowerCAmelCase__			:  Union[str, Any]											= self.image_processor(images=UpperCamelCase_  ,return_tensors=self.framework				)
      model_inputs.update(UpperCamelCase_				)
      return model_inputs




  def 							UpperCAmelCase_     (   self  ,__UpperCAmelCase				)   ->		Union[str, Any]:
      lowerCAmelCase__			:  Dict											= self.model(**UpperCamelCase_				)
      return model_outputs






  def 							UpperCAmelCase_     (   self  ,__UpperCAmelCase  ,__UpperCAmelCase=5				)   ->		str:
      if top_k > self.model.config.num_labels:
          lowerCAmelCase__			:  Dict											= self.model.config.num_labels

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

      lowerCAmelCase__			:  List[Any]											= scores.tolist()
      lowerCAmelCase__			:  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 	lowerCAmelCase_(    metaclass=SCREAMING_SNAKE_CASE_					):
  '''simple docstring'''




  __lowercase							:				int           =				['''sentencepiece''']




  def __init__(   self  ,*__UpperCAmelCase  ,**__UpperCAmelCase				)   ->		Optional[int]:
      requires_backends(self  ,["""sentencepiece"""]				)







class 	lowerCAmelCase_(    metaclass=SCREAMING_SNAKE_CASE_					):
  '''simple docstring'''




  __lowercase							:				Optional[Any]           =				['''sentencepiece''']




  def __init__(   self  ,*__UpperCAmelCase  ,**__UpperCAmelCase				)   ->		str:
      requires_backends(self  ,["""sentencepiece"""]				)







class 	lowerCAmelCase_(    metaclass=SCREAMING_SNAKE_CASE_					):
  '''simple docstring'''




  __lowercase							:				str           =				['''sentencepiece''']




  def __init__(   self  ,*__UpperCAmelCase  ,**__UpperCAmelCase				)   ->		Optional[Any]:
      requires_backends(self  ,["""sentencepiece"""]				)







class 	lowerCAmelCase_(    metaclass=SCREAMING_SNAKE_CASE_					):
  '''simple docstring'''




  __lowercase							:				Union[str, Any]           =				['''sentencepiece''']




  def __init__(   self  ,*__UpperCAmelCase  ,**__UpperCAmelCase				)   ->		List[Any]:
      requires_backends(self  ,["""sentencepiece"""]				)







class 	lowerCAmelCase_(    metaclass=SCREAMING_SNAKE_CASE_					):
  '''simple docstring'''




  __lowercase							:				int           =				['''sentencepiece''']




  def __init__(   self  ,*__UpperCAmelCase  ,**__UpperCAmelCase				)   ->		Tuple:
      requires_backends(self  ,["""sentencepiece"""]				)







class 	lowerCAmelCase_(    metaclass=SCREAMING_SNAKE_CASE_					):
  '''simple docstring'''




  __lowercase							:				Dict           =				['''sentencepiece''']




  def __init__(   self  ,*__UpperCAmelCase  ,**__UpperCAmelCase				)   ->		Dict:
      requires_backends(self  ,["""sentencepiece"""]				)







class 	lowerCAmelCase_(    metaclass=SCREAMING_SNAKE_CASE_					):
  '''simple docstring'''




  __lowercase							:				Optional[Any]           =				['''sentencepiece''']




  def __init__(   self  ,*__UpperCAmelCase  ,**__UpperCAmelCase				)   ->		List[str]:
      requires_backends(self  ,["""sentencepiece"""]				)







class 	lowerCAmelCase_(    metaclass=SCREAMING_SNAKE_CASE_					):
  '''simple docstring'''




  __lowercase							:				Optional[Any]           =				['''sentencepiece''']




  def __init__(   self  ,*__UpperCAmelCase  ,**__UpperCAmelCase				)   ->		List[Any]:
      requires_backends(self  ,["""sentencepiece"""]				)







class 	lowerCAmelCase_(    metaclass=SCREAMING_SNAKE_CASE_					):
  '''simple docstring'''




  __lowercase							:				Dict           =				['''sentencepiece''']




  def __init__(   self  ,*__UpperCAmelCase  ,**__UpperCAmelCase				)   ->		Any:
      requires_backends(self  ,["""sentencepiece"""]				)







class 	lowerCAmelCase_(    metaclass=SCREAMING_SNAKE_CASE_					):
  '''simple docstring'''




  __lowercase							:				Tuple           =				['''sentencepiece''']




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







class 	lowerCAmelCase_(    metaclass=SCREAMING_SNAKE_CASE_					):
  '''simple docstring'''




  __lowercase							:				Optional[int]           =				['''sentencepiece''']




  def __init__(   self  ,*__UpperCAmelCase  ,**__UpperCAmelCase				)   ->		List[str]:
      requires_backends(self  ,["""sentencepiece"""]				)







class 	lowerCAmelCase_(    metaclass=SCREAMING_SNAKE_CASE_					):
  '''simple docstring'''




  __lowercase							:				Optional[Any]           =				['''sentencepiece''']




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







class 	lowerCAmelCase_(    metaclass=SCREAMING_SNAKE_CASE_					):
  '''simple docstring'''




  __lowercase							:				Optional[int]           =				['''sentencepiece''']




  def __init__(   self  ,*__UpperCAmelCase  ,**__UpperCAmelCase				)   ->		List[str]:
      requires_backends(self  ,["""sentencepiece"""]				)







class 	lowerCAmelCase_(    metaclass=SCREAMING_SNAKE_CASE_					):
  '''simple docstring'''




  __lowercase							:				List[str]           =				['''sentencepiece''']




  def __init__(   self  ,*__UpperCAmelCase  ,**__UpperCAmelCase				)   ->		Any:
      requires_backends(self  ,["""sentencepiece"""]				)







class 	lowerCAmelCase_(    metaclass=SCREAMING_SNAKE_CASE_					):
  '''simple docstring'''




  __lowercase							:				Tuple           =				['''sentencepiece''']




  def __init__(   self  ,*__UpperCAmelCase  ,**__UpperCAmelCase				)   ->		List[str]:
      requires_backends(self  ,["""sentencepiece"""]				)







class 	lowerCAmelCase_(    metaclass=SCREAMING_SNAKE_CASE_					):
  '''simple docstring'''




  __lowercase							:				Dict           =				['''sentencepiece''']




  def __init__(   self  ,*__UpperCAmelCase  ,**__UpperCAmelCase				)   ->		List[Any]:
      requires_backends(self  ,["""sentencepiece"""]				)







class 	lowerCAmelCase_(    metaclass=SCREAMING_SNAKE_CASE_					):
  '''simple docstring'''




  __lowercase							:				Union[str, Any]           =				['''sentencepiece''']




  def __init__(   self  ,*__UpperCAmelCase  ,**__UpperCAmelCase				)   ->		List[Any]:
      requires_backends(self  ,["""sentencepiece"""]				)







class 	lowerCAmelCase_(    metaclass=SCREAMING_SNAKE_CASE_					):
  '''simple docstring'''




  __lowercase							:				Tuple           =				['''sentencepiece''']




  def __init__(   self  ,*__UpperCAmelCase  ,**__UpperCAmelCase				)   ->		str:
      requires_backends(self  ,["""sentencepiece"""]				)







class 	lowerCAmelCase_(    metaclass=SCREAMING_SNAKE_CASE_					):
  '''simple docstring'''




  __lowercase							:				List[Any]           =				['''sentencepiece''']




  def __init__(   self  ,*__UpperCAmelCase  ,**__UpperCAmelCase				)   ->		Optional[int]:
      requires_backends(self  ,["""sentencepiece"""]				)







class 	lowerCAmelCase_(    metaclass=SCREAMING_SNAKE_CASE_					):
  '''simple docstring'''




  __lowercase							:				int           =				['''sentencepiece''']




  def __init__(   self  ,*__UpperCAmelCase  ,**__UpperCAmelCase				)   ->		Optional[Any]:
      requires_backends(self  ,["""sentencepiece"""]				)







class 	lowerCAmelCase_(    metaclass=SCREAMING_SNAKE_CASE_					):
  '''simple docstring'''




  __lowercase							:				Tuple           =				['''sentencepiece''']




  def __init__(   self  ,*__UpperCAmelCase  ,**__UpperCAmelCase				)   ->		Dict:
      requires_backends(self  ,["""sentencepiece"""]				)







class 	lowerCAmelCase_(    metaclass=SCREAMING_SNAKE_CASE_					):
  '''simple docstring'''




  __lowercase							:				str           =				['''sentencepiece''']




  def __init__(   self  ,*__UpperCAmelCase  ,**__UpperCAmelCase				)   ->		List[Any]:
      requires_backends(self  ,["""sentencepiece"""]				)







class 	lowerCAmelCase_(    metaclass=SCREAMING_SNAKE_CASE_					):
  '''simple docstring'''




  __lowercase							:				Union[str, Any]           =				['''sentencepiece''']




  def __init__(   self  ,*__UpperCAmelCase  ,**__UpperCAmelCase				)   ->		List[Any]:
      requires_backends(self  ,["""sentencepiece"""]				)







class 	lowerCAmelCase_(    metaclass=SCREAMING_SNAKE_CASE_					):
  '''simple docstring'''




  __lowercase							:				List[str]           =				['''sentencepiece''']




  def __init__(   self  ,*__UpperCAmelCase  ,**__UpperCAmelCase				)   ->		Any:
      requires_backends(self  ,["""sentencepiece"""]				)







class 	lowerCAmelCase_(    metaclass=SCREAMING_SNAKE_CASE_					):
  '''simple docstring'''




  __lowercase							:				Optional[Any]           =				['''sentencepiece''']




  def __init__(   self  ,*__UpperCAmelCase  ,**__UpperCAmelCase				)   ->		Optional[Any]:
      requires_backends(self  ,["""sentencepiece"""]				)







class 	lowerCAmelCase_(    metaclass=SCREAMING_SNAKE_CASE_					):
  '''simple docstring'''




  __lowercase							:				Optional[int]           =				['''sentencepiece''']




  def __init__(   self  ,*__UpperCAmelCase  ,**__UpperCAmelCase				)   ->		str:
      requires_backends(self  ,["""sentencepiece"""]				)







class 	lowerCAmelCase_(    metaclass=SCREAMING_SNAKE_CASE_					):
  '''simple docstring'''




  __lowercase							:				Any           =				['''sentencepiece''']




  def __init__(   self  ,*__UpperCAmelCase  ,**__UpperCAmelCase				)   ->		int:
      requires_backends(self  ,["""sentencepiece"""]				)







class 	lowerCAmelCase_(    metaclass=SCREAMING_SNAKE_CASE_					):
  '''simple docstring'''




  __lowercase							:				List[Any]           =				['''sentencepiece''']




  def __init__(   self  ,*__UpperCAmelCase  ,**__UpperCAmelCase				)   ->		List[str]:
      requires_backends(self  ,["""sentencepiece"""]				)







class 	lowerCAmelCase_(    metaclass=SCREAMING_SNAKE_CASE_					):
  '''simple docstring'''




  __lowercase							:				Tuple           =				['''sentencepiece''']




  def __init__(   self  ,*__UpperCAmelCase  ,**__UpperCAmelCase				)   ->		str:
      requires_backends(self  ,["""sentencepiece"""]				)







class 	lowerCAmelCase_(    metaclass=SCREAMING_SNAKE_CASE_					):
  '''simple docstring'''




  __lowercase							:				Optional[Any]           =				['''sentencepiece''']




  def __init__(   self  ,*__UpperCAmelCase  ,**__UpperCAmelCase				)   ->		Optional[int]:
      requires_backends(self  ,["""sentencepiece"""]				)







class 	lowerCAmelCase_(    metaclass=SCREAMING_SNAKE_CASE_					):
  '''simple docstring'''




  __lowercase							:				List[Any]           =				['''sentencepiece''']




  def __init__(   self  ,*__UpperCAmelCase  ,**__UpperCAmelCase				)   ->		List[Any]:
      requires_backends(self  ,["""sentencepiece"""]				)


