

"""simple docstring"""




from string import ascii_uppercase
def    __SCREAMING_SNAKE_CASE						(						lowercase__			,							lowercase__			,							lowercase__ = False			,							lowercase__ = ascii_uppercase      ):
					"""simple docstring"""
					A			=					keyword.upper()
					A			=					plaintext.upper()
					A			=					set(lowercase__      )

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

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

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

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

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

					if verbose:
										print(lowercase__      )
					# 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'))












"""simple docstring"""


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


lowerCamelCase__      =      logging.get_logger(__name__)



class    __SCREAMING_SNAKE_CASE			(  nn.Module      ):







  '''simple docstring'''


  SCREAMING_SNAKE_CASE__							:HybridCLIPConfig
  SCREAMING_SNAKE_CASE__							:jnp.dtype		   =    jnp.floataa

  def       __SCREAMING_SNAKE_CASE (						self      :				List[str]		)						-> Tuple:
        _UpperCamelCase					:		List[str]  =				self.config.text_config
        _UpperCamelCase					:		Union[str, Any]  =				self.config.vision_config

        _UpperCamelCase					:		Optional[int]  =				self.config.projection_dim
        _UpperCamelCase					:		List[Any]  =				text_config.hidden_size
        _UpperCamelCase					:		Optional[Any]  =				vision_config.hidden_size

        _UpperCamelCase					:		str  =				FLAX_MODEL_MAPPING[self.config.text_config.__class__].module_class
        _UpperCamelCase					:		int  =				FLAX_MODEL_MAPPING.get(self.config.vision_config.__class__    ,      __a		).module_class

        _UpperCamelCase					:		str  =				text_module(__a    ,      dtype=self.dtype		)
        _UpperCamelCase					:		List[Any]  =				vision_module(__a    ,      dtype=self.dtype		)

        _UpperCamelCase					:		List[str]  =				nn.Dense(
            self.projection_dim    ,      dtype=self.dtype    ,      kernel_init=jax.nn.initializers.normal(0.02		)    ,      use_bias=__a    ,      )
        _UpperCamelCase					:		int  =				nn.Dense(
            self.projection_dim    ,      dtype=self.dtype    ,      kernel_init=jax.nn.initializers.normal(0.02		)    ,      use_bias=__a    ,      )
        _UpperCamelCase					:		int  =				self.param("logit_scale"    ,      jax.nn.initializers.ones    ,      []		)


  def __call__(						self      :				Any    ,      __a      :				List[str]=None    ,      __a      :				Optional[int]=None    ,      __a      :				Tuple=None    ,      __a      :				Tuple=None    ,      __a      :				str=None    ,      __a      :				bool = True    ,      __a      :				Dict=None    ,      __a      :				Any=None    ,      __a      :				Any=None    ,      )						-> List[str]:
        _UpperCamelCase					:		List[str]  =				return_dict if return_dict is not None else self.config.return_dict

        _UpperCamelCase					:		Any  =				self.vision_model(
            pixel_values=__a    ,      deterministic=__a    ,      output_attentions=__a    ,      output_hidden_states=__a    ,      return_dict=__a    ,      )

        _UpperCamelCase					:		Any  =				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    ,      )

        _UpperCamelCase					:		str  =				vision_outputs[1]
        _UpperCamelCase					:		Dict  =				self.visual_projection(__a		)

        _UpperCamelCase					:		Dict  =				text_outputs[1]
        _UpperCamelCase					:		Dict  =				self.text_projection(__a		)

        # normalized features
        _UpperCamelCase					:		Optional[int]  =				image_embeds / jnp.linalg.norm(__a    ,      axis=-1    ,      keepdims=__a		)
        _UpperCamelCase					:		Tuple  =				text_embeds / jnp.linalg.norm(__a    ,      axis=-1    ,      keepdims=__a		)

        # cosine similarity as logits
        _UpperCamelCase					:		Dict  =				jnp.exp(self.logit_scale		)
        _UpperCamelCase					:		Union[str, Any]  =				jnp.matmul(__a    ,      image_embeds.T		) * logit_scale
        _UpperCamelCase					:		List[Any]  =				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    __SCREAMING_SNAKE_CASE			(  _UpperCamelCase      ):







  '''simple docstring'''


  SCREAMING_SNAKE_CASE__							:Tuple		   =    HybridCLIPConfig
  SCREAMING_SNAKE_CASE__							:List[Any]		   =    FlaxHybridCLIPModule

  def __init__(						self      :				Tuple    ,      __a      :				HybridCLIPConfig    ,      __a      :				Optional[Tuple] = None    ,      __a      :				int = 0    ,      __a      :				jnp.dtype = jnp.floataa    ,      **__a      :				Tuple    ,      )						-> List[str]:
        if input_shape is None:
              _UpperCamelCase					:		str  =				((1, 1), (1, config.vision_config.image_size, config.vision_config.image_size, 3))

        _UpperCamelCase					:		List[Any]  =				self.module_class(config=__a    ,      dtype=__a    ,      **__a		)
        super().__init__(__a    ,      __a    ,      input_shape=__a    ,      seed=__a    ,      dtype=__a		)

  def       __SCREAMING_SNAKE_CASE (						self      :				Optional[Any]    ,      __a      :				jax.random.PRNGKey    ,      __a      :				Tuple    ,      __a      :				FrozenDict = None		)						-> FrozenDict:
        # init input tensor
        _UpperCamelCase					:		List[str]  =				jnp.zeros(input_shape[0]    ,      dtype="i4"		)
        _UpperCamelCase					:		List[Any]  =				jnp.broadcast_to(jnp.arange(jnp.atleast_ad(__a		).shape[-1]		)    ,      input_shape[0]		)
        _UpperCamelCase					:		Union[str, Any]  =				jnp.ones_like(__a		)
        _UpperCamelCase					:		str  =				jnp.ones_like(__a		)

        _UpperCamelCase					:		Optional[int]  =				jax.random.normal(__a    ,      input_shape[1]		)

        _UpperCamelCase,     _UpperCamelCase					:		Optional[Any]  =				jax.random.split(__a		)
        _UpperCamelCase					:		Tuple  =				{"params": params_rng, "dropout": dropout_rng}

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

  def __call__(						self      :				Dict    ,      __a      :				int    ,      __a      :				Any    ,      __a      :				Union[str, Any]=None    ,      __a      :				Optional[int]=None    ,      __a      :				Union[str, Any]=None    ,      __a      :				dict = None    ,      __a      :				jax.random.PRNGKey = None    ,      __a      :				bool = False    ,      __a      :				Optional[bool] = None    ,      __a      :				Optional[bool] = None    ,      __a      :				Optional[bool] = None    ,      )						-> Optional[Any]:
        _UpperCamelCase					:		List[Any]  =				output_attentions if output_attentions is not None else self.config.output_attentions
        _UpperCamelCase					:		Tuple  =				(
            output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
        )
        _UpperCamelCase					:		List[str]  =				return_dict if return_dict is not None else self.config.return_dict

        if position_ids is None:
              _UpperCamelCase					:		Union[str, Any]  =				jnp.broadcast_to(jnp.arange(jnp.atleast_ad(__a		).shape[-1]		)    ,      input_ids.shape		)

        if token_type_ids is None:
              _UpperCamelCase					:		str  =				jnp.zeros_like(__a		)

        if attention_mask is None:
              _UpperCamelCase					:		Optional[int]  =				jnp.ones_like(__a		)

        # Handle any PRNG if needed
        _UpperCamelCase					:		Optional[int]  =				{}
        if dropout_rng is not None:
              _UpperCamelCase					:		List[Any]  =				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       __SCREAMING_SNAKE_CASE (						self      :				Dict    ,      __a      :				Any    ,      __a      :				List[str]=None    ,      __a      :				Union[str, Any]=None    ,      __a      :				Any=None    ,      __a      :				dict = None    ,      __a      :				jax.random.PRNGKey = None    ,      __a      :				List[str]=False    ,      )						-> str:
        if position_ids is None:
              _UpperCamelCase					:		Union[str, Any]  =				jnp.broadcast_to(jnp.arange(jnp.atleast_ad(__a		).shape[-1]		)    ,      input_ids.shape		)

        if token_type_ids is None:
              _UpperCamelCase					:		Tuple  =				jnp.zeros_like(__a		)

        if attention_mask is None:
              _UpperCamelCase					:		List[str]  =				jnp.ones_like(__a		)

        # Handle any PRNG if needed
        _UpperCamelCase					:		List[str]  =				{}
        if dropout_rng is not None:
              _UpperCamelCase					:		Dict  =				dropout_rng

        def _get_features(__a      :				Optional[int]    ,      __a      :				Optional[Any]    ,      __a      :				List[Any]    ,      __a      :				Optional[int]    ,      __a      :				Union[str, Any]    ,      __a      :				List[Any]		):
              _UpperCamelCase					:		List[Any]  =				module.text_model(
                  input_ids=__a    ,      attention_mask=__a    ,      position_ids=__a    ,      token_type_ids=__a    ,      deterministic=__a    ,      )
              _UpperCamelCase					:		Tuple  =				text_outputs[1]
              _UpperCamelCase					:		Tuple  =				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       __SCREAMING_SNAKE_CASE (						self      :				Optional[int]    ,      __a      :				int    ,      __a      :				dict = None    ,      __a      :				jax.random.PRNGKey = None    ,      __a      :				Optional[Any]=False		)						-> Tuple:
        _UpperCamelCase					:		List[str]  =				{}
        if dropout_rng is not None:
              _UpperCamelCase					:		Tuple  =				dropout_rng

        def _get_features(__a      :				int    ,      __a      :				List[str]    ,      __a      :				Optional[Any]		):
              _UpperCamelCase					:		Optional[int]  =				module.vision_model(pixel_values=__a    ,      deterministic=__a		)
              _UpperCamelCase					:		Optional[int]  =				vision_outputs[1]  # pooled_output
              _UpperCamelCase					:		str  =				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       __SCREAMING_SNAKE_CASE (						cls      :				List[str]    ,      __a      :				str = None    ,      __a      :				str = None    ,      *__a      :				List[Any]    ,      **__a      :				Tuple    ,      )						-> FlaxPreTrainedModel:
        _UpperCamelCase					:		Union[str, Any]  =				{
            argument[len("text_"		) :]: value for argument, value in kwargs.items() if argument.startswith("text_"		)
        }

        _UpperCamelCase					:		Optional[Any]  =				{
            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
        _UpperCamelCase					:		int  =				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

                    _UpperCamelCase					:		Any  =				AutoConfig.from_pretrained(__a		)
                    _UpperCamelCase					:		int  =				text_config

              _UpperCamelCase					:		Optional[Any]  =				FlaxAutoModel.from_pretrained(__a    ,      *__a    ,      **__a		)

        _UpperCamelCase					:		str  =				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

                    _UpperCamelCase					:		Dict  =				AutoConfig.from_pretrained(__a		)
                    _UpperCamelCase					:		str  =				vision_config

              _UpperCamelCase					:		int  =				FlaxAutoModel.from_pretrained(__a    ,      *__a    ,      **__a		)

        # instantiate config with corresponding kwargs
        _UpperCamelCase					:		Optional[int]  =				kwargs.pop("dtype"    ,      jnp.floataa		)
        _UpperCamelCase					:		Optional[int]  =				HybridCLIPConfig.from_text_vision_configs(text_model.config    ,      vision_model.config    ,      **__a		)

        # init model
        _UpperCamelCase					:		List[str]  =				cls(__a    ,      *__a    ,      dtype=__a    ,      **__a		)

        if vision_config.model_type == "clip":
              _UpperCamelCase					:		Tuple  =				vision_model.params["vision_model"]
              _UpperCamelCase					:		Tuple  =				vision_model.params["visual_projection"]["kernel"]
        else:
              _UpperCamelCase					:		Tuple  =				vision_model.params

        _UpperCamelCase					:		str  =				text_model.params

        return model
