

import flax.linen as nn
import jax
import jax.numpy as jnp





class  __UpperCAmelCase					(	nn.Module						):
			UpperCamelCase       					=					42
			UpperCamelCase       					=					jnp.floataa



			def 	__magic_name__   (							self							:					Dict						):
					UpperCAmelCase	:							Tuple						=			nn.Conv(
					    self.out_channels,		kernel_size=(3, 3),		strides=(1, 1),		padding=((1, 1), (1, 1)),		dtype=self.dtype,		)





			def __call__(							self							:					Any,		__A							:					Optional[Any]						):
					UpperCAmelCase			,						UpperCAmelCase			,						UpperCAmelCase			,						UpperCAmelCase	:							int						=			hidden_states.shape
					UpperCAmelCase	:							str						=			jax.image.resize(
					    __lowerCAmelCase,		shape=(batch, height * 2, width * 2, channels),		method='''nearest''',		)
					UpperCAmelCase	:							Dict						=			self.conv(__lowerCAmelCase						)
					return hidden_states






class  __UpperCAmelCase					(	nn.Module						):
			UpperCamelCase       					=					42
			UpperCamelCase       					=					jnp.floataa



			def 	__magic_name__   (							self							:					Dict						):
					UpperCAmelCase	:							List[Any]						=			nn.Conv(
					    self.out_channels,		kernel_size=(3, 3),		strides=(2, 2),		padding=((1, 1), (1, 1)),		dtype=self.dtype,		)





			def __call__(							self							:					int,		__A							:					List[Any]						):
					# pad = ((0, 0), (0, 1), (0, 1), (0, 0))  # pad height and width dim
					# hidden_states = jnp.pad(hidden_states, pad_width=pad)
					UpperCAmelCase	:							List[str]						=			self.conv(__lowerCAmelCase						)
					return hidden_states






class  __UpperCAmelCase					(	nn.Module						):
			UpperCamelCase       					=					42
			UpperCamelCase       					=					None
			UpperCamelCase       					=					0.0
			UpperCamelCase       					=					None
			UpperCamelCase       					=					jnp.floataa



			def 	__magic_name__   (							self							:					Union[str, Any]						):
					UpperCAmelCase	:							Union[str, Any]						=			self.in_channels if self.out_channels is None else self.out_channels

					UpperCAmelCase	:							Any						=			nn.GroupNorm(num_groups=3_2,		epsilon=1E-5						)
					UpperCAmelCase	:							Union[str, Any]						=			nn.Conv(
					    __lowerCAmelCase,		kernel_size=(3, 3),		strides=(1, 1),		padding=((1, 1), (1, 1)),		dtype=self.dtype,		)

					UpperCAmelCase	:							Tuple						=			nn.Dense(__lowerCAmelCase,		dtype=self.dtype						)

					UpperCAmelCase	:							List[str]						=			nn.GroupNorm(num_groups=3_2,		epsilon=1E-5						)
					UpperCAmelCase	:							Tuple						=			nn.Dropout(self.dropout_prob						)
					UpperCAmelCase	:							Optional[int]						=			nn.Conv(
					    __lowerCAmelCase,		kernel_size=(3, 3),		strides=(1, 1),		padding=((1, 1), (1, 1)),		dtype=self.dtype,		)

					UpperCAmelCase	:							Optional[Any]						=			self.in_channels != out_channels if self.use_nin_shortcut is None else self.use_nin_shortcut

					UpperCAmelCase	:							List[str]						=			None
					if use_nin_shortcut:
							UpperCAmelCase	:							Optional[int]						=			nn.Conv(
							    __lowerCAmelCase,		kernel_size=(1, 1),		strides=(1, 1),		padding='''VALID''',		dtype=self.dtype,		)





			def __call__(							self							:					Optional[int],		__A							:					Optional[Any],		__A							:					Tuple,		__A							:					Union[str, Any]=True						):
					UpperCAmelCase	:							List[str]						=			hidden_states
					UpperCAmelCase	:							List[Any]						=			self.norma(__lowerCAmelCase						)
					UpperCAmelCase	:							Optional[int]						=			nn.swish(__lowerCAmelCase						)
					UpperCAmelCase	:							str						=			self.conva(__lowerCAmelCase						)

					UpperCAmelCase	:							Optional[int]						=			self.time_emb_proj(nn.swish(__lowerCAmelCase						)						)
					UpperCAmelCase	:							Tuple						=			jnp.expand_dims(jnp.expand_dims(__lowerCAmelCase,		1						),		1						)
					UpperCAmelCase	:							Any						=			hidden_states + temb

					UpperCAmelCase	:							str						=			self.norma(__lowerCAmelCase						)
					UpperCAmelCase	:							Tuple						=			nn.swish(__lowerCAmelCase						)
					UpperCAmelCase	:							Any						=			self.dropout(__lowerCAmelCase,		__lowerCAmelCase						)
					UpperCAmelCase	:							int						=			self.conva(__lowerCAmelCase						)

					if self.conv_shortcut is not None:
							UpperCAmelCase	:							str						=			self.conv_shortcut(__lowerCAmelCase						)

					return hidden_states + residual








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





@add_end_docstrings(lowerCamelCase__						)
class  __UpperCAmelCase					(	lowerCamelCase__						):



   def __init__(							self							:					Optional[Any],		*__A							:					Tuple,		**__A							:					Tuple						):
     super().__init__(*__A,		**__A						)
     self.check_model_type(__A						)



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



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



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



   def 	__magic_name__   (							self							:					Optional[Any],		__A							:					List[Any]						):
     UpperCAmelCase	:							Optional[int]						=			self.model(**__A						)
     return model_outputs





   def 	__magic_name__   (							self							:					Any,		__A							:					List[str],		__A							:					Union[str, Any]=5						):
     if top_k > self.model.config.num_labels:
       UpperCAmelCase	:							Any						=			self.model.config.num_labels

     if self.framework == "pt":
       UpperCAmelCase	:							Any						=			model_outputs.logits.sigmoid()[0]
       UpperCAmelCase			,						UpperCAmelCase	:							Union[str, Any]						=			probs.topk(__A						)
     else:
       raise ValueError(F'''Unsupported framework: {self.framework}'''						)

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





