






"""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	:	int				=      logging.get_logger(__name__)




@add_end_docstrings(__magic_name__   )
class 			lowerCAmelCase__			(    __magic_name__   ):
   def __init__(		self      :	int							,			*snake_case__      :	str							,			**snake_case__      :	Optional[int]							):


        '''simple docstring'''
        super().__init__(*snake_case__							,			**snake_case__							)
        self.check_model_type(snake_case__							)
   def       __a							(		self      :	Any							,			snake_case__      :	Any=None							,			snake_case__      :	Optional[Any]=None							,			snake_case__      :	str=None							,			**snake_case__      :	Any							):


        '''simple docstring'''
        UpperCAmelCase__							,				UpperCAmelCase__						:    List[Any]							=							{}, {}
        if padding is not None:
             UpperCAmelCase__						:    Optional[int]							=							padding
        if truncation is not None:
             UpperCAmelCase__						:    int							=							truncation
        if top_k is not None:
             UpperCAmelCase__						:    List[str]							=							top_k
        return preprocess_params, {}, postprocess_params
   def __call__(		self      :	Tuple							,			snake_case__      :	Union["Image.Image", str]							,			snake_case__      :	str = None							,			**snake_case__      :	Any							):


        '''simple docstring'''
        if isinstance(snake_case__							,			(Image.Image, str)							) and isinstance(snake_case__							,			snake_case__							):
             UpperCAmelCase__						:    Tuple							=							{"image": image, "question": question}
        else:
             UpperCAmelCase__						:    Union[str, Any]							=							image
        UpperCAmelCase__						:    int							=							super().__call__(snake_case__							,			**snake_case__							)
        return results
   def       __a							(		self      :	Any							,			snake_case__      :	Tuple							,			snake_case__      :	List[Any]=False							,			snake_case__      :	List[Any]=False							):


        '''simple docstring'''
        UpperCAmelCase__						:    Any							=							load_image(inputs["image"]							)
        UpperCAmelCase__						:    Union[str, Any]							=							self.tokenizer(
            inputs["question"]							,			return_tensors=self.framework							,			padding=snake_case__							,			truncation=snake_case__							)
        UpperCAmelCase__						:    str							=							self.image_processor(images=snake_case__							,			return_tensors=self.framework							)
        model_inputs.update(snake_case__							)
        return model_inputs
   def       __a							(		self      :	Any							,			snake_case__      :	Optional[int]							):


        '''simple docstring'''
        UpperCAmelCase__						:    Optional[Any]							=							self.model(**snake_case__							)
        return model_outputs




   def       __a							(		self      :	str							,			snake_case__      :	List[Any]							,			snake_case__      :	Tuple=5							):


        '''simple docstring'''
        if top_k > self.model.config.num_labels:
             UpperCAmelCase__						:    str							=							self.model.config.num_labels

        if self.framework == "pt":
             UpperCAmelCase__						:    str							=							model_outputs.logits.sigmoid()[0]
             UpperCAmelCase__							,				UpperCAmelCase__						:    Tuple							=							probs.topk(snake_case__							)
        else:
             raise ValueError(f'Unsupported framework: {self.framework}'							)

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










"""simple docstring"""



import torch
import torch.nn as nn
from transformers.modeling_utils import ModuleUtilsMixin
from transformers.models.ta.modeling_ta import TaBlock, TaConfig, TaLayerNorm

from ...configuration_utils import ConfigMixin, register_to_config
from ...models import ModelMixin




class 			lowerCAmelCase__			(    __magic_name__							,						__magic_name__							,						__magic_name__   ):
   @register_to_config
   def __init__(		self      :	List[Any]							,			snake_case__      :	int							,			snake_case__      :	int							,			snake_case__      :	int							,			snake_case__      :	float							,			snake_case__      :	int							,			snake_case__      :	int							,			snake_case__      :	int							,			snake_case__      :	int							,			snake_case__      :	str							,			snake_case__      :	bool = False							,			):


        '''simple docstring'''
        super().__init__()

        UpperCAmelCase__						:    Optional[int]							=							nn.Embedding(snake_case__							,			snake_case__							)

        UpperCAmelCase__						:    List[Any]							=							nn.Embedding(snake_case__							,			snake_case__							)
        UpperCAmelCase__						:    Optional[Any]							=							False

        UpperCAmelCase__						:    Optional[int]							=							nn.Dropout(p=snake_case__							)

        UpperCAmelCase__						:    Optional[int]							=							TaConfig(
            vocab_size=snake_case__							,			d_model=snake_case__							,			num_heads=snake_case__							,			d_kv=snake_case__							,			d_ff=snake_case__							,			dropout_rate=snake_case__							,			feed_forward_proj=snake_case__							,			is_decoder=snake_case__							,			is_encoder_decoder=snake_case__							,			)

        UpperCAmelCase__						:    Tuple							=							nn.ModuleList()
        for lyr_num in range(snake_case__							):
             UpperCAmelCase__						:    Tuple							=							TaBlock(snake_case__							)
             self.encoders.append(snake_case__							)

        UpperCAmelCase__						:    str							=							TaLayerNorm(snake_case__							)
        UpperCAmelCase__						:    Tuple							=							nn.Dropout(p=snake_case__							)




   def       __a							(		self      :	int							,			snake_case__      :	List[str]							,			snake_case__      :	List[Any]							):


        '''simple docstring'''
        UpperCAmelCase__						:    List[Any]							=							self.token_embedder(snake_case__							)

        UpperCAmelCase__						:    Optional[int]							=							encoder_input_tokens.shape[1]
        UpperCAmelCase__						:    List[Any]							=							torch.arange(snake_case__							,			device=encoder_input_tokens.device							)
        x += self.position_encoding(snake_case__							)

        UpperCAmelCase__						:    Union[str, Any]							=							self.dropout_pre(snake_case__							)

        # inverted the attention mask
        UpperCAmelCase__						:    List[Any]							=							encoder_input_tokens.size()
        UpperCAmelCase__						:    Tuple							=							self.get_extended_attention_mask(snake_case__							,			snake_case__							)

        for lyr in self.encoders:
             UpperCAmelCase__						:    Any							=							lyr(snake_case__							,			snake_case__							)[0]
        UpperCAmelCase__						:    Any							=							self.layer_norm(snake_case__							)

        return self.dropout_post(snake_case__							), encoder_inputs_mask


