'''simple docstring'''




from collections import UserDict
from typing import List, Union

from ..utils import (
    add_end_docstrings,
    is_tf_available,
    is_torch_available,
    is_vision_available,
    logging,
    requires_backends,
)
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_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING

if is_tf_available():
  from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING
  from ..tf_utils import stable_softmax

__UpperCAmelCase												=logging.get_logger(__name__)




@add_end_docstrings(lowerCAmelCase__			)
class    a__				(       lowerCAmelCase__			):






     def __init__(			self		:		List[Any]  ,    **a		:		Any   ):





         """simple docstring"""



         super().__init__(**_SCREAMING_SNAKE_CASE   )

         requires_backends(self  ,    '''vision'''   )
         self.check_model_type(
             TF_MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING
             if self.framework == '''tf'''
             else MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING   )






     def __call__(			self		:		Any  ,    a		:		Optional[int]  ,    **a		:		int   ):





         """simple docstring"""



         return super().__call__(_SCREAMING_SNAKE_CASE  ,    **_SCREAMING_SNAKE_CASE   )






     def        SCREAMING_SNAKE_CASE__       (			self		:		List[str]  ,    **a		:		Union[str, Any]   ):





         """simple docstring"""



         __lowerCamelCase          =  {}
         if "candidate_labels" in kwargs:
             __lowerCamelCase          =  kwargs['''candidate_labels''']
         if "hypothesis_template" in kwargs:
             __lowerCamelCase          =  kwargs['''hypothesis_template''']

         return preprocess_params, {}, {}






     def        SCREAMING_SNAKE_CASE__       (			self		:		str  ,    a		:		Optional[Any]  ,    a		:		int=None  ,    a		:		Dict="This is a photo of {}."   ):





         """simple docstring"""



         __lowerCamelCase          =  load_image(_SCREAMING_SNAKE_CASE   )
         __lowerCamelCase          =  self.image_processor(images=[image]  ,    return_tensors=self.framework   )
         __lowerCamelCase          =  candidate_labels
         __lowerCamelCase          =  [hypothesis_template.format(_SCREAMING_SNAKE_CASE   ) for x in candidate_labels]
         __lowerCamelCase          =  self.tokenizer(_SCREAMING_SNAKE_CASE  ,    return_tensors=self.framework  ,    padding=_SCREAMING_SNAKE_CASE   )
         __lowerCamelCase          =  [text_inputs]
         return inputs






     def        SCREAMING_SNAKE_CASE__       (			self		:		Dict  ,    a		:		Optional[Any]   ):





         """simple docstring"""



         __lowerCamelCase          =  model_inputs.pop('''candidate_labels'''   )
         __lowerCamelCase          =  model_inputs.pop('''text_inputs'''   )
         if isinstance(text_inputs[0]  ,    _SCREAMING_SNAKE_CASE   ):
             __lowerCamelCase          =  text_inputs[0]
         else:
             # Batching case.
             __lowerCamelCase          =  text_inputs[0][0]

         __lowerCamelCase          =  self.model(**_SCREAMING_SNAKE_CASE  ,    **_SCREAMING_SNAKE_CASE   )

         __lowerCamelCase          =  {
             '''candidate_labels''': candidate_labels,
             '''logits''': outputs.logits_per_image,
         }
         return model_outputs






     def        SCREAMING_SNAKE_CASE__       (			self		:		Dict  ,    a		:		Tuple   ):





         """simple docstring"""



         __lowerCamelCase          =  model_outputs.pop('''candidate_labels'''   )
         __lowerCamelCase          =  model_outputs['''logits'''][0]
         if self.framework == "pt":
             __lowerCamelCase          =  logits.softmax(dim=-1   ).squeeze(-1   )
             __lowerCamelCase          =  probs.tolist()
             if not isinstance(_SCREAMING_SNAKE_CASE  ,    _SCREAMING_SNAKE_CASE   ):
                 __lowerCamelCase          =  [scores]
         elif self.framework == "tf":
             __lowerCamelCase          =  stable_softmax(_SCREAMING_SNAKE_CASE  ,    axis=-1   )
             __lowerCamelCase          =  probs.numpy().tolist()
         else:
             raise ValueError(f"""Unsupported framework: {self.framework}"""   )

         __lowerCamelCase          =  [
             {'''score''': score, '''label''': candidate_label}
             for score, candidate_label in sorted(zip(_SCREAMING_SNAKE_CASE  ,    _SCREAMING_SNAKE_CASE   )  ,    key=lambda a   : -x[0]   )
         ]
         return result







'''simple docstring'''




import torch
from torch import nn

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




class    a__				(       UpperCAmelCase__							,       UpperCAmelCase__			):






     @register_to_config
     def __init__(			self		:		Any  ,    *,
         a		:		int = 4  ,    a		:		int = 7_68  ,    a		:		int  ,    a		:		Optional[int]  ,    ):





         """simple docstring"""



         super().__init__()

         __lowerCamelCase          =  nn.Parameter(torch.zeros(a   )   )

         # parameters for additional clip time embeddings
         __lowerCamelCase          =  nn.Linear(a  ,    a   )
         __lowerCamelCase          =  nn.Linear(a  ,    a   )

         # parameters for encoder hidden states
         __lowerCamelCase          =  clip_extra_context_tokens
         __lowerCamelCase          =  nn.Linear(
             a  ,    self.clip_extra_context_tokens * cross_attention_dim   )
         __lowerCamelCase          =  nn.Linear(a  ,    a   )
         __lowerCamelCase          =  nn.LayerNorm(a   )






     def        SCREAMING_SNAKE_CASE__       (			self		:		int  ,    *, a		:		Union[str, Any]  ,    a		:		Any  ,    a		:		str  ,    a		:		Any   ):





         """simple docstring"""



         if do_classifier_free_guidance:
             # Add the classifier free guidance embeddings to the image embeddings
             __lowerCamelCase          =  image_embeddings.shape[0]
             __lowerCamelCase          =  self.learned_classifier_free_guidance_embeddings.unsqueeze(0   )
             __lowerCamelCase          =  classifier_free_guidance_embeddings.expand(
                 a  ,    -1   )
             __lowerCamelCase          =  torch.cat([classifier_free_guidance_embeddings, image_embeddings]  ,    dim=0   )

         # The image embeddings batch size and the text embeddings batch size are equal
         assert image_embeddings.shape[0] == prompt_embeds.shape[0]

         __lowerCamelCase          =  prompt_embeds.shape[0]

         # "Specifically, we modify the architecture described in Nichol et al. (2021) by projecting and
         # adding CLIP embeddings to the existing timestep embedding, ...
         __lowerCamelCase          =  self.embedding_proj(a   )
         __lowerCamelCase          =  self.clip_image_embeddings_project_to_time_embeddings(a   )
         __lowerCamelCase          =  time_projected_image_embeddings + time_projected_prompt_embeds

         # ... and by projecting CLIP embeddings into four
         # extra tokens of context that are concatenated to the sequence of outputs from the GLIDE text encoder"
         __lowerCamelCase          =  self.clip_extra_context_tokens_proj(a   )
         __lowerCamelCase          =  clip_extra_context_tokens.reshape(a  ,    -1  ,    self.clip_extra_context_tokens   )
         __lowerCamelCase          =  clip_extra_context_tokens.permute(0  ,    2  ,    1   )

         __lowerCamelCase          =  self.encoder_hidden_states_proj(a   )
         __lowerCamelCase          =  self.text_encoder_hidden_states_norm(a   )
         __lowerCamelCase          =  torch.cat([clip_extra_context_tokens, text_encoder_hidden_states]  ,    dim=1   )

         return text_encoder_hidden_states, additive_clip_time_embeddings
