





from typing import Optional, Tuple

import jax
import jax.numpy as jnp
from flax import linen as nn
from flax.core.frozen_dict import FrozenDict
from transformers import CLIPConfig, FlaxPreTrainedModel
from transformers.models.clip.modeling_flax_clip import FlaxCLIPVisionModule

def 							SCREAMING_SNAKE_CASE__       (  snake_case__  :Any			,							snake_case__  :Optional[int]			,							snake_case__  :List[str]=1E-12     )      ->						Union[str, Any]:
  _lowercase							= jnp.divide(emb_a.T			,							jnp.clip(jnp.linalg.norm(snake_case__			,							axis=1     )			,							a_min=snake_case__     )     ).T
  _lowercase							= jnp.divide(emb_a.T			,							jnp.clip(jnp.linalg.norm(snake_case__			,							axis=1     )			,							a_min=snake_case__     )     ).T
  return jnp.matmul(snake_case__			,							norm_emb_a.T     )





class 				A_				(      nn.Module	):







       """simple docstring"""





       SCREAMING_SNAKE_CASE_      : CLIPConfig
       SCREAMING_SNAKE_CASE_      : jnp.dtype      =  jnp.floataa







       def    __UpperCAmelCase (    self	:	List[str]  )						->					str:
         _lowercase							= FlaxCLIPVisionModule(self.config.vision_config  )
         _lowercase							= nn.Dense(self.config.projection_dim						,use_bias=__A						,dtype=self.dtype  )

         _lowercase							= self.param('concept_embeds'						,jax.nn.initializers.ones						,(17, self.config.projection_dim)  )
         _lowercase							= self.param(
             'special_care_embeds'						,jax.nn.initializers.ones						,(3, self.config.projection_dim)  )

         _lowercase							= self.param('concept_embeds_weights'						,jax.nn.initializers.ones						,(17,)  )
         _lowercase							= self.param('special_care_embeds_weights'						,jax.nn.initializers.ones						,(3,)  )







       def __call__(    self	:	int						,__A	:	Optional[Any]  )						->					int:
         _lowercase							= self.vision_model(__A  )[1]
         _lowercase							= self.visual_projection(__A  )

         _lowercase							= jax_cosine_distance(__A						,self.special_care_embeds  )
         _lowercase							= jax_cosine_distance(__A						,self.concept_embeds  )

         # increase this value to create a stronger `nfsw` filter
         # at the cost of increasing the possibility of filtering benign image inputs
         _lowercase							= 0.0

         _lowercase							= special_cos_dist - self.special_care_embeds_weights[None, :] + adjustment
         _lowercase							= jnp.round(__A						,3  )
         _lowercase							= jnp.any(special_scores > 0						,axis=1						,keepdims=__A  )
         # Use a lower threshold if an image has any special care concept
         _lowercase							= is_special_care * 0.01

         _lowercase							= cos_dist - self.concept_embeds_weights[None, :] + special_adjustment
         _lowercase							= jnp.round(__A						,3  )
         _lowercase							= jnp.any(concept_scores > 0						,axis=1  )

         return has_nsfw_concepts





class 				A_				(      UpperCAmelCase	):







       """simple docstring"""





       SCREAMING_SNAKE_CASE_      : Optional[int]      =  CLIPConfig
       SCREAMING_SNAKE_CASE_      : List[str]      =  '''clip_input'''
       SCREAMING_SNAKE_CASE_      : Optional[int]      =  FlaxStableDiffusionSafetyCheckerModule







       def __init__(    self	:	str						,__A	:	CLIPConfig						,__A	:	Optional[Tuple] = None						,__A	:	int = 0						,__A	:	jnp.dtype = jnp.floataa						,__A	:	bool = True						,**__A	:	Optional[Any]						,)						->					str:
         if input_shape is None:
           _lowercase							= (1, 224, 224, 3)
         _lowercase							= self.module_class(config=__A						,dtype=__A						,**__A  )
         super().__init__(__A						,__A						,input_shape=__A						,seed=__A						,dtype=__A						,_do_init=_do_init  )







       def    __UpperCAmelCase (    self	:	Tuple						,__A	:	jax.random.KeyArray						,__A	:	Tuple						,__A	:	FrozenDict = None  )						->					FrozenDict:
         # init input tensor
         _lowercase							= jax.random.normal(__A						,__A  )

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

         _lowercase							= self.module.init(__A						,__A  )['params']

         return random_params







       def __call__(    self	:	int						,__A	:	Union[str, Any]						,__A	:	dict = None						,)						->					Dict:
         _lowercase							= jnp.transpose(__A						,(0, 2, 3, 1)  )

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






import warnings

from ...utils import logging
from .image_processing_donut import DonutImageProcessor


snake_case 				=       logging.get_logger(__name__)





class 				A_				(      UpperCAmelCase	):







       """simple docstring"""





       def __init__(    self	:	Dict						,*__A	:	Optional[int]						,**__A	:	List[Any]  )						->					None:
         warnings.warn(
             'The class DonutFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please'
             ' use DonutImageProcessor instead.'						,__A						,)
         super().__init__(*__A						,**__A  )