

"""simple docstring"""




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


__UpperCAmelCase			=					logging.get_logger(__name__)

__UpperCAmelCase			=					{'openai-gpt': 'https://huggingface.co/openai-gpt/resolve/main/config.json'}







class   __lowercase      (     __lowerCamelCase       ):
  snake_case_												=							"""openai-gpt"""
  snake_case_												=							{
      """max_position_embeddings""": """n_positions""",
      """hidden_size""": """n_embd""",
      """num_attention_heads""": """n_head""",
      """num_hidden_layers""": """n_layer""",
  }

  def __init__(	self		:      int       ,A		:      Any=40_478       ,A		:      List[str]=512       ,A		:      Tuple=768       ,A		:      int=12       ,A		:      Union[str, Any]=12       ,A		:      int="gelu"       ,A		:      List[str]=0.1       ,A		:      int=0.1       ,A		:      List[Any]=0.1       ,A		:      Optional[int]=1e-5       ,A		:      Optional[Any]=0.0_2       ,A		:      List[Any]="cls_index"       ,A		:      List[str]=True       ,A		:      Union[str, Any]=None       ,A		:      int=True       ,A		:      int=0.1       ,**A		:      Optional[int]       ,):






   '''simple docstring'''
   UpperCAmelCase__       :	List[str]        =       vocab_size
   UpperCAmelCase__       :	Optional[int]        =       n_positions
   UpperCAmelCase__       :	Dict        =       n_embd
   UpperCAmelCase__       :	List[str]        =       n_layer
   UpperCAmelCase__       :	List[Any]        =       n_head
   UpperCAmelCase__       :	str        =       afn
   UpperCAmelCase__       :	Dict        =       resid_pdrop
   UpperCAmelCase__       :	Union[str, Any]        =       embd_pdrop
   UpperCAmelCase__       :	Optional[Any]        =       attn_pdrop
   UpperCAmelCase__       :	Tuple        =       layer_norm_epsilon
   UpperCAmelCase__       :	Any        =       initializer_range
   UpperCAmelCase__       :	Union[str, Any]        =       summary_type
   UpperCAmelCase__       :	Tuple        =       summary_use_proj
   UpperCAmelCase__       :	List[str]        =       summary_activation
   UpperCAmelCase__       :	Optional[Any]        =       summary_first_dropout
   UpperCAmelCase__       :	str        =       summary_proj_to_labels
   super().__init__(**A     )








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

_UpperCamelCase		     =    logging.get_logger(__name__)






@add_end_docstrings(__magic_name__       )
class 			__a					(						__magic_name__       ):





       """simple docstring"""


       def __init__(   self			,					*snake_case			,					**snake_case   ):





        """simple docstring"""

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






       def 			SCREAMING_SNAKE_CASE_	(   self			,					snake_case=None			,					snake_case=None			,					snake_case=None			,					**snake_case   ):





        """simple docstring"""

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






       def __call__(   self			,					snake_case			,					snake_case = None			,					**snake_case   ):





        """simple docstring"""

        if isinstance(snake_case			,					(Image.Image, str)   ) and isinstance(snake_case			,					snake_case   ):
         lowerCAmelCase__   :				int 			=				{"image": image, "question": question}
        else:
         lowerCAmelCase__   :				Optional[int] 			=				image
        lowerCAmelCase__   :				Optional[Any] 			=				super().__call__(snake_case			,					**snake_case   )
        return results






       def 			SCREAMING_SNAKE_CASE_	(   self			,					snake_case			,					snake_case=False			,					snake_case=False   ):





        """simple docstring"""

        lowerCAmelCase__   :				Union[str, Any] 			=				load_image(inputs["image"]   )
        lowerCAmelCase__   :				str 			=				self.tokenizer(
            inputs["question"]			,					return_tensors=self.framework			,					padding=snake_case			,					truncation=snake_case   )
        lowerCAmelCase__   :				int 			=				self.image_processor(images=snake_case			,					return_tensors=self.framework   )
        model_inputs.update(snake_case   )
        return model_inputs






       def 			SCREAMING_SNAKE_CASE_	(   self			,					snake_case   ):





        """simple docstring"""

        lowerCAmelCase__   :				Optional[int] 			=				self.model(**snake_case   )
        return model_outputs






       def 			SCREAMING_SNAKE_CASE_	(   self			,					snake_case			,					snake_case=5   ):





        """simple docstring"""

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

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

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






