





from typing import Optional, Tuple

import flax.linen as nn
import jax
import jax.numpy as jnp
from configuration_hybrid_clip import HybridCLIPConfig
from flax.core.frozen_dict import FrozenDict

from transformers import FLAX_MODEL_MAPPING, FlaxCLIPVisionModel
from transformers.modeling_flax_utils import FlaxPreTrainedModel
from transformers.models.clip.modeling_flax_clip import FlaxCLIPOutput
from transformers.utils import logging


__lowercase				    =      logging.get_logger(__name__)

class 	__snake_case					(      nn.Module ):






    '''simple docstring'''
    _lowerCAmelCase  =						42
    _lowerCAmelCase  =						jnp.floataa





    def 						A		( self    :		Tuple				):

      """simple docstring"""





      __lowerCAmelCase								=					self.config.text_config
      __lowerCAmelCase								=					self.config.vision_config

      __lowerCAmelCase								=					self.config.projection_dim
      __lowerCAmelCase								=					text_config.hidden_size
      __lowerCAmelCase								=					vision_config.hidden_size

      __lowerCAmelCase								=					FLAX_MODEL_MAPPING[self.config.text_config.__class__].module_class
      __lowerCAmelCase								=					FLAX_MODEL_MAPPING.get(self.config.vision_config.__class__      ,    A__				).module_class

      __lowerCAmelCase								=					text_module(A__      ,    dtype=self.dtype				)
      __lowerCAmelCase								=					vision_module(A__      ,    dtype=self.dtype				)

      __lowerCAmelCase								=					nn.Dense(
          self.projection_dim      ,    dtype=self.dtype      ,    kernel_init=jax.nn.initializers.normal(0.02				)      ,    use_bias=A__      ,    )
      __lowerCAmelCase								=					nn.Dense(
          self.projection_dim      ,    dtype=self.dtype      ,    kernel_init=jax.nn.initializers.normal(0.02				)      ,    use_bias=A__      ,    )
      __lowerCAmelCase								=					self.param('''logit_scale'''      ,    jax.nn.initializers.ones      ,    []				)





    def __call__( self    :		Union[str, Any]      ,    snake_case    :		List[Any]=None      ,    snake_case    :		Any=None      ,    snake_case    :		Any=None      ,    snake_case    :		Optional[int]=None      ,    snake_case    :		str=None      ,    snake_case    :		bool = True      ,    snake_case    :		Optional[Any]=None      ,    snake_case    :		Union[str, Any]=None      ,    snake_case    :		Union[str, Any]=None      ,    ):

      """simple docstring"""





      __lowerCAmelCase								=					return_dict if return_dict is not None else self.config.return_dict

      __lowerCAmelCase								=					self.vision_model(
          pixel_values=A__      ,    deterministic=A__      ,    output_attentions=A__      ,    output_hidden_states=A__      ,    return_dict=A__      ,    )

      __lowerCAmelCase								=					self.text_model(
          input_ids=A__      ,    attention_mask=A__      ,    token_type_ids=A__      ,    position_ids=A__      ,    deterministic=A__      ,    output_attentions=A__      ,    output_hidden_states=A__      ,    return_dict=A__      ,    )

      __lowerCAmelCase								=					vision_outputs[1]
      __lowerCAmelCase								=					self.visual_projection(A__				)

      __lowerCAmelCase								=					text_outputs[1]
      __lowerCAmelCase								=					self.text_projection(A__				)

      # normalized features
      __lowerCAmelCase								=					image_embeds / jnp.linalg.norm(A__      ,    axis=-1      ,    keepdims=A__				)
      __lowerCAmelCase								=					text_embeds / jnp.linalg.norm(A__      ,    axis=-1      ,    keepdims=A__				)

      # cosine similarity as logits
      __lowerCAmelCase								=					jnp.exp(self.logit_scale				)
      __lowerCAmelCase								=					jnp.matmul(A__      ,    image_embeds.T				) * logit_scale
      __lowerCAmelCase								=					logits_per_text.T

      if not return_dict:
        return (logits_per_image, logits_per_text, text_embeds, image_embeds, text_outputs, vision_outputs)

      return FlaxCLIPOutput(
          logits_per_image=A__      ,    logits_per_text=A__      ,    text_embeds=A__      ,    image_embeds=A__      ,    text_model_output=A__      ,    vision_model_output=A__      ,    )


class 	__snake_case					(      _lowerCAmelCase ):






    '''simple docstring'''
    _lowerCAmelCase  =						HybridCLIPConfig
    _lowerCAmelCase  =						FlaxHybridCLIPModule





    def __init__( self    :		List[Any]      ,    snake_case    :		HybridCLIPConfig      ,    snake_case    :		Optional[Tuple] = None      ,    snake_case    :		int = 0      ,    snake_case    :		jnp.dtype = jnp.floataa      ,    **snake_case    :		List[Any]      ,    ):

      """simple docstring"""





      if input_shape is None:
        __lowerCAmelCase								=					((1, 1), (1, config.vision_config.image_size, config.vision_config.image_size, 3))

      __lowerCAmelCase								=					self.module_class(config=A__      ,    dtype=A__      ,    **A__				)
      super().__init__(A__      ,    A__      ,    input_shape=A__      ,    seed=A__      ,    dtype=A__				)





    def 						A		( self    :		List[Any]      ,    snake_case    :		jax.random.PRNGKey      ,    snake_case    :		Tuple      ,    snake_case    :		FrozenDict = None				):

      """simple docstring"""





      __lowerCAmelCase								=					jnp.zeros(input_shape[0]      ,    dtype='''i4'''				)
      __lowerCAmelCase								=					jnp.broadcast_to(jnp.arange(jnp.atleast_ad(A__				).shape[-1]				)      ,    input_shape[0]				)
      __lowerCAmelCase								=					jnp.ones_like(A__				)
      __lowerCAmelCase								=					jnp.ones_like(A__				)

      __lowerCAmelCase								=					jax.random.normal(A__      ,    input_shape[1]				)

      __lowerCAmelCase						,    __lowerCAmelCase								=					jax.random.split(A__				)
      __lowerCAmelCase								=					{'''params''': params_rng, '''dropout''': dropout_rng}

      return self.module.init(A__      ,    A__      ,    A__      ,    A__      ,    A__      ,    A__				)["params"]





    def __call__( self    :		Any      ,    snake_case    :		List[Any]      ,    snake_case    :		Any      ,    snake_case    :		Optional[int]=None      ,    snake_case    :		int=None      ,    snake_case    :		Any=None      ,    snake_case    :		dict = None      ,    snake_case    :		jax.random.PRNGKey = None      ,    snake_case    :		bool = False      ,    snake_case    :		Optional[bool] = None      ,    snake_case    :		Optional[bool] = None      ,    snake_case    :		Optional[bool] = None      ,    ):

      """simple docstring"""





      __lowerCAmelCase								=					output_attentions if output_attentions is not None else self.config.output_attentions
      __lowerCAmelCase								=					(
          output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
      )
      __lowerCAmelCase								=					return_dict if return_dict is not None else self.config.return_dict

      if position_ids is None:
        __lowerCAmelCase								=					jnp.broadcast_to(jnp.arange(jnp.atleast_ad(A__				).shape[-1]				)      ,    input_ids.shape				)

      if token_type_ids is None:
        __lowerCAmelCase								=					jnp.zeros_like(A__				)

      if attention_mask is None:
        __lowerCAmelCase								=					jnp.ones_like(A__				)

      # Handle any PRNG if needed
      __lowerCAmelCase								=					{}
      if dropout_rng is not None:
        __lowerCAmelCase								=					dropout_rng

      return self.module.apply(
          {'''params''': params or self.params}      ,    jnp.array(A__      ,    dtype='''i4'''				)      ,    jnp.array(A__      ,    dtype=jnp.floataa				)      ,    jnp.array(A__      ,    dtype='''i4'''				)      ,    jnp.array(A__      ,    dtype='''i4'''				)      ,    jnp.array(A__      ,    dtype='''i4'''				)      ,    not train      ,    A__      ,    A__      ,    A__      ,    rngs=A__      ,    )





    def 						A		( self    :		Any      ,    snake_case    :		Optional[Any]      ,    snake_case    :		List[str]=None      ,    snake_case    :		str=None      ,    snake_case    :		Tuple=None      ,    snake_case    :		dict = None      ,    snake_case    :		jax.random.PRNGKey = None      ,    snake_case    :		str=False      ,    ):

      """simple docstring"""





      if position_ids is None:
        __lowerCAmelCase								=					jnp.broadcast_to(jnp.arange(jnp.atleast_ad(A__				).shape[-1]				)      ,    input_ids.shape				)

      if token_type_ids is None:
        __lowerCAmelCase								=					jnp.zeros_like(A__				)

      if attention_mask is None:
        __lowerCAmelCase								=					jnp.ones_like(A__				)

      # Handle any PRNG if needed
      __lowerCAmelCase								=					{}
      if dropout_rng is not None:
        __lowerCAmelCase								=					dropout_rng

      def _get_features(snake_case    :		Optional[int]      ,    snake_case    :		List[Any]      ,    snake_case    :		Union[str, Any]      ,    snake_case    :		str      ,    snake_case    :		str      ,    snake_case    :		str				):
        __lowerCAmelCase								=					module.text_model(
            input_ids=A__      ,    attention_mask=A__      ,    position_ids=A__      ,    token_type_ids=A__      ,    deterministic=A__      ,    )
        __lowerCAmelCase								=					text_outputs[1]
        __lowerCAmelCase								=					module.text_projection(A__				)
        return text_features

      return self.module.apply(
          {'''params''': params or self.params}      ,    jnp.array(A__      ,    dtype='''i4'''				)      ,    jnp.array(A__      ,    dtype='''i4'''				)      ,    jnp.array(A__      ,    dtype='''i4'''				)      ,    jnp.array(A__      ,    dtype='''i4'''				)      ,    not train      ,    method=_get_features      ,    rngs=A__      ,    )





    def 						A		( self    :		Optional[Any]      ,    snake_case    :		Optional[Any]      ,    snake_case    :		dict = None      ,    snake_case    :		jax.random.PRNGKey = None      ,    snake_case    :		Any=False				):

      """simple docstring"""





      __lowerCAmelCase								=					{}
      if dropout_rng is not None:
        __lowerCAmelCase								=					dropout_rng

      def _get_features(snake_case    :		Any      ,    snake_case    :		List[Any]      ,    snake_case    :		str				):
        __lowerCAmelCase								=					module.vision_model(pixel_values=A__      ,    deterministic=A__				)
        __lowerCAmelCase								=					vision_outputs[1]  # pooled_output
        __lowerCAmelCase								=					module.visual_projection(A__				)
        return image_features

      return self.module.apply(
          {'''params''': params or self.params}      ,    jnp.array(A__      ,    dtype=jnp.floataa				)      ,    not train      ,    method=_get_features      ,    rngs=A__      ,    )





    @classmethod
    def 						A		( cls    :		int      ,    snake_case    :		str = None      ,    snake_case    :		str = None      ,    *snake_case    :		Dict      ,    **snake_case    :		int      ,    ):

      """simple docstring"""





      __lowerCAmelCase								=					{
          argument[len('''text_'''				) :]: value for argument, value in kwargs.items() if argument.startswith('''text_'''				)
      }

      __lowerCAmelCase								=					{
          argument[len('''vision_'''				) :]: value for argument, value in kwargs.items() if argument.startswith('''vision_'''				)
      }

      # remove text, vision kwargs from kwargs
      for key in kwargs_text.keys():
        del kwargs["text_" + key]
      for key in kwargs_vision.keys():
        del kwargs["vision_" + key]

      # Load and initialize the text and vision model
      __lowerCAmelCase								=					kwargs_text.pop('''model'''      ,    A__				)
      if text_model is None:
        assert (
            text_model_name_or_path is not None
        ), "If `model` is not defined as an argument, a `text_model_name_or_path` has to be defined"
        from transformers import FlaxAutoModel

        if "config" not in kwargs_text:
          from transformers import AutoConfig

          __lowerCAmelCase								=					AutoConfig.from_pretrained(A__				)
          __lowerCAmelCase								=					text_config

        __lowerCAmelCase								=					FlaxAutoModel.from_pretrained(A__      ,    *A__      ,    **A__				)

      __lowerCAmelCase								=					kwargs_vision.pop('''model'''      ,    A__				)
      if vision_model is None:
        assert (
            vision_model_name_or_path is not None
        ), "If `model` is not defined as an argument, a `vision_model_name_or_path` has to be defined"
        from transformers import FlaxAutoModel

        if "config" not in kwargs_vision:
          from transformers import AutoConfig

          __lowerCAmelCase								=					AutoConfig.from_pretrained(A__				)
          __lowerCAmelCase								=					vision_config

        __lowerCAmelCase								=					FlaxAutoModel.from_pretrained(A__      ,    *A__      ,    **A__				)

      # instantiate config with corresponding kwargs
      __lowerCAmelCase								=					kwargs.pop('''dtype'''      ,    jnp.floataa				)
      __lowerCAmelCase								=					HybridCLIPConfig.from_text_vision_configs(text_model.config      ,    vision_model.config      ,    **A__				)

      # init model
      __lowerCAmelCase								=					cls(A__      ,    *A__      ,    dtype=A__      ,    **A__				)

      if vision_config.model_type == "clip":
        __lowerCAmelCase								=					vision_model.params['''vision_model''']
        __lowerCAmelCase								=					vision_model.params['''visual_projection''']['''kernel''']
      else:
        __lowerCAmelCase								=					vision_model.params

      __lowerCAmelCase								=					text_model.params

      return model






from string import ascii_uppercase

def       lowerCAmelCase_      (	lowerCamelCase:							str  ,					lowerCamelCase:							str  ,					lowerCamelCase:							bool = False  ,					lowerCamelCase:							str = ascii_uppercase    )       ->      str:


      '''simple docstring'''




      lowerCamelCase__   	=					keyword.upper()
      lowerCamelCase__   	=					plaintext.upper()
      lowerCamelCase__   	=					set(lowerCamelCase    )

      # create a list of unique characters in the keyword - their order matters
      # it determines how we will map plaintext characters to the ciphertext
      lowerCamelCase__   	=					[]
      for char in keyword:
            if char in alphabet_set and char not in unique_chars:
                  unique_chars.append(lowerCamelCase    )
    # the number of those unique characters will determine the number of rows
      lowerCamelCase__   	=					len(lowerCamelCase    )

      # create a shifted version of the alphabet
      lowerCamelCase__   	=					unique_chars + [
          char for char in alphabet if char not in unique_chars
      ]

      # create a modified alphabet by splitting the shifted alphabet into rows
      lowerCamelCase__   	=					[
          shifted_alphabet[k : k + num_unique_chars_in_keyword]
          for k in range(0  ,					26  ,					lowerCamelCase    )
      ]

      # map the alphabet characters to the modified alphabet characters
      # going 'vertically' through the modified alphabet - consider columns first
      lowerCamelCase__   	=					{}
      lowerCamelCase__   	=					0
      for column in range(lowerCamelCase    ):
            for row in modified_alphabet:
                  # if current row (the last one) is too short, break out of loop
                  if len(lowerCamelCase    ) <= column:
                        break

                  # map current letter to letter in modified alphabet
                  lowerCamelCase__   	=					row[column]
                  letter_index += 1

      if verbose:
            print(lowerCamelCase    )
      # create the encrypted text by mapping the plaintext to the modified alphabet
      return "".join(mapping[char] if char in mapping else char for char in plaintext    )


if __name__ == "__main__":
      # example use
      print(mixed_keyword("""college""", """UNIVERSITY"""))