




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(_SCREAMING_SNAKE_CASE   )
class  _lowerCamelCase						(			_SCREAMING_SNAKE_CASE   ):

      """simple docstring"""

      def __init__(     self					: Union[str, Any] ,       *snake_case					: Any ,       **snake_case					: List[Any]				):
          super().__init__(*snake_case ,       **snake_case				)
          self.check_model_type(snake_case				)







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







      def __call__(     self					: Tuple ,       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  		=						{'''image''': image, '''question''': question}
          else:
              __UpperCamelCase  		=						image
          __UpperCamelCase  		=						super().__call__(snake_case ,       **snake_case				)
          return results







      def       snake_case	(     self					: Tuple ,       snake_case					: Optional[Any] ,       snake_case					: Union[str, Any]=False ,       snake_case					: List[str]=False				):
          __UpperCamelCase  		=						load_image(inputs['''image''']				)
          __UpperCamelCase  		=						self.tokenizer(
              inputs['''question'''] ,       return_tensors=self.framework ,       padding=snake_case ,       truncation=snake_case				)
          __UpperCamelCase  		=						self.image_processor(images=snake_case ,       return_tensors=self.framework				)
          model_inputs.update(snake_case				)
          return model_inputs







      def       snake_case	(     self					: Union[str, Any] ,       snake_case					: List[str]				):
          __UpperCamelCase  		=						self.model(**snake_case				)
          return model_outputs







      def       snake_case	(     self					: str ,       snake_case					: str ,       snake_case					: Tuple=5				):
          if top_k > self.model.config.num_labels:
              __UpperCamelCase  		=						self.model.config.num_labels

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

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












from abc import ABC, abstractmethod
from argparse import ArgumentParser

class  _lowerCamelCase						(			_SCREAMING_SNAKE_CASE   ):

      """simple docstring"""

      @staticmethod
      @abstractmethod
      def       snake_case	(     snake_case					: ArgumentParser				):
          raise NotImplementedError()







      @abstractmethod
      def       snake_case	(     self					: Optional[Any]				):
          raise NotImplementedError()






