






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 						__UpperCAmelCase			(     a_							,     a_							,     a_=1E-12):
							snake_case_				=							jnp.divide(emb_a.T							,     jnp.clip(jnp.linalg.norm(lowercase__							,     axis=1)							,     a_min=lowercase__)).T
							snake_case_				=							jnp.divide(emb_a.T							,     jnp.clip(jnp.linalg.norm(lowercase__							,     axis=1)							,     a_min=lowercase__)).T
							return jnp.matmul(lowercase__							,     norm_emb_a.T)




class  UpperCamelCase_      (			nn.Module				):



	'''simple docstring'''




	lowerCAmelCase													= 42
	lowerCAmelCase													= jnp.floataa




	def 							_UpperCamelCase			(       self					)       ->    Any:
								snake_case_				=							FlaxCLIPVisionModule(self.config.vision_config					)
								snake_case_				=							nn.Dense(self.config.projection_dim       ,			use_bias=lowerCAmelCase__       ,			dtype=self.dtype					)

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

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






	def __call__(       self       ,			a					)       ->    Any:
								snake_case_				=							self.vision_model(lowerCAmelCase__					)[1]
								snake_case_				=							self.visual_projection(lowerCAmelCase__					)

								snake_case_				=							jax_cosine_distance(lowerCAmelCase__       ,			self.special_care_embeds					)
								snake_case_				=							jax_cosine_distance(lowerCAmelCase__       ,			self.concept_embeds					)

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

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

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

								return has_nsfw_concepts






class  UpperCamelCase_      (			A__				):



	'''simple docstring'''




	lowerCAmelCase													= CLIPConfig
	lowerCAmelCase													= '''clip_input'''
	lowerCAmelCase													= FlaxStableDiffusionSafetyCheckerModule




	def __init__(       self       ,			a       ,			a = None       ,			a = 0       ,			a = jnp.floataa       ,			a = True       ,			**a       ,			)       ->    Optional[Any]:
								if input_shape is None:
															snake_case_				=							(1, 2_24, 2_24, 3)
								snake_case_				=							self.module_class(config=lowerCAmelCase__       ,			dtype=lowerCAmelCase__       ,			**lowerCAmelCase__					)
								super().__init__(lowerCAmelCase__       ,			lowerCAmelCase__       ,			input_shape=lowerCAmelCase__       ,			seed=lowerCAmelCase__       ,			dtype=lowerCAmelCase__       ,			_do_init=_do_init					)




	def 							_UpperCamelCase			(       self       ,			a       ,			a       ,			a = None					)       ->    FrozenDict:
								# init input tensor
								snake_case_				=							jax.random.normal(lowerCAmelCase__       ,			lowerCAmelCase__					)

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

								snake_case_				=							self.module.init(lowerCAmelCase__       ,			lowerCAmelCase__					)['''params''']

								return random_params






	def __call__(       self       ,			a       ,			a = None       ,			)       ->    Tuple:
								snake_case_				=							jnp.transpose(lowerCAmelCase__       ,			(0, 2, 3, 1)					)

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









import unittest

from transformers.testing_utils import require_bsa
from transformers.utils import is_bsa_available

from ...test_feature_extraction_common import FeatureExtractionSavingTestMixin


if is_bsa_available():
						from transformers import MarkupLMFeatureExtractor
class        _lowercase      (			unittest.TestCase       ):






				'''simple docstring'''



				def __init__(       self			:Optional[Any]			,			lowerCAmelCase__			:Optional[Any]	)		->			str:
										__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		parent



				def 		__magic_name__(       self			:List[Any]	)		->			Tuple:
										return {}




def        _UpperCamelCase							(      ):
						__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		'''<HTML>

    <HEAD>
    <TITLE>sample document</TITLE>
    </HEAD>

    <BODY BGCOLOR="FFFFFF">
    <HR>
    <a href="http://google.com">Goog</a>
    <H1>This is one header</H1>
    <H2>This is a another Header</H2>
    <P>Travel from
        <P>
        <B>SFO to JFK</B>
        <BR>
        <B><I>on May 2, 2015 at 2:00 pm. For details go to confirm.com </I></B>
        <HR>
        <div style="color:#0000FF">
            <h3>Traveler <b> name </b> is
            <p> John Doe </p>
        </div>'''

						__SCREAMING_SNAKE_CASE						:     str					=		'''
    <!DOCTYPE html>
    <html>
    <body>

    <h1>My First Heading</h1>
    <p>My first paragraph.</p>

    </body>
    </html>
    '''

						return [html_string_a, html_string_a]
@require_bsa
class        _lowercase      (			A__						,			unittest.TestCase       ):






				'''simple docstring'''



				SCREAMING_SNAKE_CASE__				:			Optional[int]						=		MarkupLMFeatureExtractor if is_bsa_available() else None


				def 		__magic_name__(       self			:int	)		->			Optional[Any]:
										__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		MarkupLMFeatureExtractionTester(self	)


				@property
				def 		__magic_name__(       self			:Any	)		->			Optional[Any]:
										return self.feature_extract_tester.prepare_feat_extract_dict()



				def 		__magic_name__(       self			:Optional[int]	)		->			Any:
										# Initialize feature_extractor
										__SCREAMING_SNAKE_CASE						:     int					=		self.feature_extraction_class()

										# Test not batched input
										__SCREAMING_SNAKE_CASE						:     Tuple					=		get_html_strings()[0]
										__SCREAMING_SNAKE_CASE						:     Dict					=		feature_extractor(lowerCAmelCase__	)

										# fmt: off
										__SCREAMING_SNAKE_CASE						:     str					=		[['''sample document''', '''Goog''', '''This is one header''', '''This is a another Header''', '''Travel from''', '''SFO to JFK''', '''on May 2, 2015 at 2:00 pm. For details go to confirm.com''', '''Traveler''', '''name''', '''is''', '''John Doe''']]
										__SCREAMING_SNAKE_CASE						:     List[str]					=		[['''/html/head/title''', '''/html/body/a''', '''/html/body/h1''', '''/html/body/h2''', '''/html/body/p''', '''/html/body/p/p/b[1]''', '''/html/body/p/p/b[2]/i''', '''/html/body/p/p/div/h3''', '''/html/body/p/p/div/h3/b''', '''/html/body/p/p/div/h3''', '''/html/body/p/p/div/h3/p''']]
										# fmt: on

										self.assertEqual(encoding.nodes			,			lowerCAmelCase__	)
										self.assertEqual(encoding.xpaths			,			lowerCAmelCase__	)

										# Test batched
										__SCREAMING_SNAKE_CASE						:     Tuple					=		get_html_strings()
										__SCREAMING_SNAKE_CASE						:     Dict					=		feature_extractor(lowerCAmelCase__	)

										# fmt: off
										__SCREAMING_SNAKE_CASE						:     int					=		expected_nodes + [['''My First Heading''', '''My first paragraph.''']]
										__SCREAMING_SNAKE_CASE						:     str					=		expected_xpaths + [['''/html/body/h1''', '''/html/body/p''']]

										self.assertEqual(len(encoding.nodes	)			,			2	)
										self.assertEqual(len(encoding.xpaths	)			,			2	)

										self.assertEqual(encoding.nodes			,			lowerCAmelCase__	)
										self.assertEqual(encoding.xpaths			,			lowerCAmelCase__	)
