

'''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      lowercase_      (lowerCamelCase__ ):




  """simple docstring"""




  def __init__( self  :	Tuple   ,*lowercase__  :	Optional[int]   ,**lowercase__  :	List[str]     ):
   super().__init__(*lowercase__   ,**lowercase__     )
   self.check_model_type(lowercase__     )






  def    SCREAMING_SNAKE_CASE ( self  :	str   ,lowercase__  :	List[Any]=None   ,lowercase__  :	Optional[int]=None   ,lowercase__  :	Tuple=None   ,**lowercase__  :	List[str]     ):
   __lowercase    ,      __lowercase				    =					{}, {}
   if padding is not None:
    __lowercase				    =					padding
   if truncation is not None:
    __lowercase				    =					truncation
   if top_k is not None:
    __lowercase				    =					top_k
   return preprocess_params, {}, postprocess_params






  def __call__( self  :	List[Any]   ,lowercase__  :	Union["Image.Image", str]   ,lowercase__  :	str = None   ,**lowercase__  :	Any     ):
   if isinstance(lowercase__   ,(Image.Image, str)     ) and isinstance(lowercase__   ,lowercase__     ):
    __lowercase				    =					{'''image''': image, '''question''': question}
   else:
    __lowercase				    =					image
   __lowercase				    =					super().__call__(lowercase__   ,**lowercase__     )
   return results






  def    SCREAMING_SNAKE_CASE ( self  :	Any   ,lowercase__  :	Optional[int]   ,lowercase__  :	Optional[int]=False   ,lowercase__  :	List[str]=False     ):
   __lowercase				    =					load_image(inputs['''image''']     )
   __lowercase				    =					self.tokenizer(
       inputs['''question''']   ,return_tensors=self.framework   ,padding=lowercase__   ,truncation=lowercase__     )
   __lowercase				    =					self.image_processor(images=lowercase__   ,return_tensors=self.framework     )
   model_inputs.update(lowercase__     )
   return model_inputs






  def    SCREAMING_SNAKE_CASE ( self  :	Union[str, Any]   ,lowercase__  :	List[str]     ):
   __lowercase				    =					self.model(**lowercase__     )
   return model_outputs






  def    SCREAMING_SNAKE_CASE ( self  :	Dict   ,lowercase__  :	str   ,lowercase__  :	Any=5     ):
   if top_k > self.model.config.num_labels:
    __lowercase				    =					self.model.config.num_labels

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

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


"""simple docstring"""


from ...configuration_utils import PretrainedConfig
from ...utils import logging


UpperCAmelCase__			=    logging.get_logger(__name__)


class 							a					(		lowerCAmelCase_ ):
    _snake_case  :	List[Any]  				=				'timm_backbone'


    def __init__(       self						:	List[Any] ,					__lowerCAmelCase						:	str=None ,					__lowerCAmelCase						:	Optional[int]=3 ,					__lowerCAmelCase						:	Any=True ,					__lowerCAmelCase						:	Optional[int]=True ,					__lowerCAmelCase						:	Optional[Any]=None ,					**__lowerCAmelCase						:	Optional[Any] ,					):
      super().__init__(**__lowerCAmelCase       )
      _UpperCAmelCase    			=   backbone
      _UpperCAmelCase    			=   num_channels
      _UpperCAmelCase    			=   features_only
      _UpperCAmelCase    			=   use_pretrained_backbone
      _UpperCAmelCase    			=   True
      _UpperCAmelCase    			=   out_indices if out_indices is not None else (-1,)
