



from typing import Any, Dict, List, Union

from ..utils import add_end_docstrings, is_torch_available, is_vision_available, logging, requires_backends
from .base import PIPELINE_INIT_ARGS, ChunkPipeline


if is_vision_available():
							from PIL import Image

							from ..image_utils import load_image

if is_torch_available():
							import torch

							from transformers.modeling_outputs import BaseModelOutput

							from ..models.auto.modeling_auto import MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING

__snake_case    :		Any  =logging.get_logger(__name__)



@add_end_docstrings(lowerCamelCase__)
class 	lowerCamelCase__      (							lowerCamelCase__):




		'''simple docstring'''



		def __init__(self		,**__lowerCamelCase			)					->      str:

						"""simple docstring"""






						super().__init__(**__lowerCamelCase			)

						if self.framework == "tf":
										raise ValueError(f"""The {self.__class__} is only available in PyTorch."""			)

						requires_backends(self		,'''vision'''			)
						self.check_model_type(__lowerCamelCase			)



		def __call__(self		,__lowerCamelCase		,__lowerCamelCase = None		,**__lowerCamelCase		,)					->      Tuple:

						"""simple docstring"""






						if "text_queries" in kwargs:
										lowerCAmelCase__					:    int												=					kwargs.pop('''text_queries'''			)

						if isinstance(__lowerCamelCase		,(str, Image.Image)			):
										lowerCAmelCase__					:    Any												=					{'''image''': image, '''candidate_labels''': candidate_labels}
						else:
										lowerCAmelCase__					:    List[Any]												=					image
						lowerCAmelCase__					:    int												=					super().__call__(__lowerCamelCase		,**__lowerCamelCase			)
						return results



		def 		lowerCAmelCase__				(self		,**__lowerCamelCase			)					->      Union[str, Any]:

						"""simple docstring"""






						lowerCAmelCase__					:    List[str]												=					{}
						if "threshold" in kwargs:
										lowerCAmelCase__					:    Optional[int]												=					kwargs['''threshold''']
						if "top_k" in kwargs:
										lowerCAmelCase__					:    List[str]												=					kwargs['''top_k''']
						return {}, {}, postprocess_params



		def 		lowerCAmelCase__				(self		,__lowerCamelCase			)					->      Tuple:

						"""simple docstring"""






						lowerCAmelCase__					:    Dict												=					load_image(inputs['''image''']			)
						lowerCAmelCase__					:    Any												=					inputs['''candidate_labels''']
						if isinstance(__lowerCamelCase		,__lowerCamelCase			):
										lowerCAmelCase__					:    List[str]												=					candidate_labels.split(''','''			)

						lowerCAmelCase__					:    List[str]												=					torch.tensor([[image.height, image.width]]		,dtype=torch.intaa			)
						for i, candidate_label in enumerate(__lowerCamelCase			):
										lowerCAmelCase__					:    List[Any]												=					self.tokenizer(__lowerCamelCase		,return_tensors=self.framework			)
										lowerCAmelCase__					:    Tuple												=					self.image_processor(__lowerCamelCase		,return_tensors=self.framework			)
										yield {
										    "is_last": i == len(__lowerCamelCase			) - 1,
										    "target_size": target_size,
										    "candidate_label": candidate_label,
										    **text_inputs,
										    **image_features,
										}



		def 		lowerCAmelCase__				(self		,__lowerCamelCase			)					->      Tuple:

						"""simple docstring"""






						lowerCAmelCase__					:    Union[str, Any]												=					model_inputs.pop('''target_size'''			)
						lowerCAmelCase__					:    Union[str, Any]												=					model_inputs.pop('''candidate_label'''			)
						lowerCAmelCase__					:    Any												=					model_inputs.pop('''is_last'''			)

						lowerCAmelCase__					:    List[str]												=					self.model(**__lowerCamelCase			)

						lowerCAmelCase__					:    Tuple												=					{'''target_size''': target_size, '''candidate_label''': candidate_label, '''is_last''': is_last, **outputs}
						return model_outputs



		def 		lowerCAmelCase__				(self		,__lowerCamelCase		,__lowerCamelCase=0.1		,__lowerCamelCase=None			)					->      List[Any]:

						"""simple docstring"""






						lowerCAmelCase__					:    Dict												=					[]
						for model_output in model_outputs:
										lowerCAmelCase__					:    Union[str, Any]												=					model_output['''candidate_label''']
										lowerCAmelCase__					:    Optional[int]												=					BaseModelOutput(__lowerCamelCase			)
										lowerCAmelCase__					:    Any												=					self.image_processor.post_process_object_detection(
										    outputs=__lowerCamelCase		,threshold=__lowerCamelCase		,target_sizes=model_output['''target_size''']			)[0]

										for index in outputs["scores"].nonzero():
														lowerCAmelCase__					:    List[str]												=					outputs['''scores'''][index].item()
														lowerCAmelCase__					:    Optional[int]												=					self._get_bounding_box(outputs['''boxes'''][index][0]			)

														lowerCAmelCase__					:    Tuple												=					{'''score''': score, '''label''': label, '''box''': box}
														results.append(__lowerCamelCase			)

						lowerCAmelCase__					:    Dict												=					sorted(__lowerCamelCase		,key=lambda __lowerCamelCase			: x["score"]		,reverse=__lowerCamelCase			)
						if top_k:
										lowerCAmelCase__					:    Tuple												=					results[:top_k]

						return results



		def 		lowerCAmelCase__				(self		,__lowerCamelCase			)					->      Dict[str, int]:

						"""simple docstring"""






						if self.framework != "pt":
										raise ValueError('''The ZeroShotObjectDetectionPipeline is only available in PyTorch.'''			)
						lowerCAmelCase__						,		lowerCAmelCase__						,		lowerCAmelCase__						,		lowerCAmelCase__					:    Dict												=					box.int().tolist()
						lowerCAmelCase__					:    Optional[int]												=					{
						    '''xmin''': xmin,
						    '''ymin''': ymin,
						    '''xmax''': xmax,
						    '''ymax''': ymax,
						}
						return bbox






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



class 	lowerCamelCase__      (							nn.Module):




		'''simple docstring'''



		snake_case_     =42
		snake_case_     =jnp.floataa



		def 		lowerCAmelCase__				(self			)					->      List[str]:

						"""simple docstring"""






						lowerCAmelCase__					:    Dict												=					nn.Conv(
						    self.out_channels		,kernel_size=(3, 3)		,strides=(1, 1)		,padding=((1, 1), (1, 1))		,dtype=self.dtype		,)



		def __call__(self		,__lowerCamelCase			)					->      Union[str, Any]:

						"""simple docstring"""






						lowerCAmelCase__						,		lowerCAmelCase__						,		lowerCAmelCase__						,		lowerCAmelCase__					:    Dict												=					hidden_states.shape
						lowerCAmelCase__					:    Dict												=					jax.image.resize(
						    __lowerCamelCase		,shape=(batch, height * 2, width * 2, channels)		,method='''nearest'''		,)
						lowerCAmelCase__					:    Dict												=					self.conv(__lowerCamelCase			)
						return hidden_states



class 	lowerCamelCase__      (							nn.Module):




		'''simple docstring'''



		snake_case_     =42
		snake_case_     =jnp.floataa



		def 		lowerCAmelCase__				(self			)					->      int:

						"""simple docstring"""






						lowerCAmelCase__					:    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		,__lowerCamelCase			)					->      Any:

						"""simple docstring"""






						lowerCAmelCase__					:    Optional[int]												=					self.conv(__lowerCamelCase			)
						return hidden_states



class 	lowerCamelCase__      (							nn.Module):




		'''simple docstring'''



		snake_case_     =42
		snake_case_     =None
		snake_case_     =0.0
		snake_case_     =None
		snake_case_     =jnp.floataa



		def 		lowerCAmelCase__				(self			)					->      int:

						"""simple docstring"""






						lowerCAmelCase__					:    List[str]												=					self.in_channels if self.out_channels is None else self.out_channels

						lowerCAmelCase__					:    Union[str, Any]												=					nn.GroupNorm(num_groups=32		,epsilon=1e-5			)
						lowerCAmelCase__					:    Union[str, Any]												=					nn.Conv(
						    __lowerCamelCase		,kernel_size=(3, 3)		,strides=(1, 1)		,padding=((1, 1), (1, 1))		,dtype=self.dtype		,)

						lowerCAmelCase__					:    int												=					nn.Dense(__lowerCamelCase		,dtype=self.dtype			)

						lowerCAmelCase__					:    List[Any]												=					nn.GroupNorm(num_groups=32		,epsilon=1e-5			)
						lowerCAmelCase__					:    Union[str, Any]												=					nn.Dropout(self.dropout_prob			)
						lowerCAmelCase__					:    Optional[int]												=					nn.Conv(
						    __lowerCamelCase		,kernel_size=(3, 3)		,strides=(1, 1)		,padding=((1, 1), (1, 1))		,dtype=self.dtype		,)

						lowerCAmelCase__					:    str												=					self.in_channels != out_channels if self.use_nin_shortcut is None else self.use_nin_shortcut

						lowerCAmelCase__					:    Union[str, Any]												=					None
						if use_nin_shortcut:
										lowerCAmelCase__					:    Optional[Any]												=					nn.Conv(
										    __lowerCamelCase		,kernel_size=(1, 1)		,strides=(1, 1)		,padding='''VALID'''		,dtype=self.dtype		,)



		def __call__(self		,__lowerCamelCase		,__lowerCamelCase		,__lowerCamelCase=True			)					->      Union[str, Any]:

						"""simple docstring"""






						lowerCAmelCase__					:    Tuple												=					hidden_states
						lowerCAmelCase__					:    Dict												=					self.norma(__lowerCamelCase			)
						lowerCAmelCase__					:    Union[str, Any]												=					nn.swish(__lowerCamelCase			)
						lowerCAmelCase__					:    List[Any]												=					self.conva(__lowerCamelCase			)

						lowerCAmelCase__					:    List[Any]												=					self.time_emb_proj(nn.swish(__lowerCamelCase			)			)
						lowerCAmelCase__					:    Dict												=					jnp.expand_dims(jnp.expand_dims(__lowerCamelCase		,1			)		,1			)
						lowerCAmelCase__					:    Optional[int]												=					hidden_states + temb

						lowerCAmelCase__					:    Optional[int]												=					self.norma(__lowerCamelCase			)
						lowerCAmelCase__					:    Optional[int]												=					nn.swish(__lowerCamelCase			)
						lowerCAmelCase__					:    int												=					self.dropout(__lowerCamelCase		,__lowerCamelCase			)
						lowerCAmelCase__					:    List[Any]												=					self.conva(__lowerCamelCase			)

						if self.conv_shortcut is not None:
										lowerCAmelCase__					:    Optional[int]												=					self.conv_shortcut(__lowerCamelCase			)

						return hidden_states + residual

