




"""simple docstring"""





import itertools
import os
import random
import tempfile
import unittest

import numpy as np
from datasets import load_dataset

from transformers import is_speech_available
from transformers.testing_utils import check_json_file_has_correct_format, require_torch, require_torchaudio
from transformers.utils.import_utils import is_torch_available

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


if is_speech_available():
					from transformers import WhisperFeatureExtractor

if is_torch_available():
					import torch

A				=							random.Random()

def        _UpperCamelCase     (      UpperCamelCase					,							UpperCamelCase=1.0					,							UpperCamelCase=None					,							UpperCamelCase=None				)      ->      Optional[Any]:


			"""simple docstring"""
			if rng is None:
						__UpperCAmelCase						:			Tuple												=       global_rng

			__UpperCAmelCase						:			str												=       []
			for batch_idx in range(shape[0]				):
						values.append([]				)
						for _ in range(shape[1]				):
									values[-1].append(rng.random() * scale				)

			return values



@require_torch
@require_torchaudio
class   a__					(						unittest.TestCase ):


			def __init__(   self   :     Tuple   ,	UpperCamelCase_   :     Union[str, Any]   ,	UpperCamelCase_   :     Any=7   ,	UpperCamelCase_   :     int=400   ,	UpperCamelCase_   :     List[Any]=2000   ,	UpperCamelCase_   :     List[str]=10   ,	UpperCamelCase_   :     List[Any]=160   ,	UpperCamelCase_   :     str=8   ,	UpperCamelCase_   :     str=0.0   ,	UpperCamelCase_   :     Union[str, Any]=4000   ,	UpperCamelCase_   :     Any=False   ,	UpperCamelCase_   :     List[str]=True   ,	):


						"""simple docstring"""




						__UpperCAmelCase						:			Optional[Any]												=       parent
						__UpperCAmelCase						:			Dict												=       batch_size
						__UpperCAmelCase						:			List[str]												=       min_seq_length
						__UpperCAmelCase						:			Any												=       max_seq_length
						__UpperCAmelCase						:			Dict												=       (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
						__UpperCAmelCase						:			Dict												=       padding_value
						__UpperCAmelCase						:			Any												=       sampling_rate
						__UpperCAmelCase						:			Any												=       return_attention_mask
						__UpperCAmelCase						:			Any												=       do_normalize
						__UpperCAmelCase						:			Optional[Any]												=       feature_size
						__UpperCAmelCase						:			Tuple												=       chunk_length
						__UpperCAmelCase						:			Any												=       hop_length


			def    a_  (   self   :     Tuple):


						"""simple docstring"""




						return {
						    "feature_size": self.feature_size,
						    "hop_length": self.hop_length,
						    "chunk_length": self.chunk_length,
						    "padding_value": self.padding_value,
						    "sampling_rate": self.sampling_rate,
						    "return_attention_mask": self.return_attention_mask,
						    "do_normalize": self.do_normalize,
						}


			def    a_  (   self   :     Optional[int]   ,	UpperCamelCase_   :     Tuple=False   ,	UpperCamelCase_   :     int=False):


						"""simple docstring"""




						def _flatten(UpperCamelCase_   :     Union[str, Any]):
									return list(itertools.chain(*UpperCamelCase_))

						if equal_length:
									__UpperCAmelCase						:			Tuple												=       [floats_list((self.max_seq_length, self.feature_size)) for _ in range(self.batch_size)]
						else:
									# make sure that inputs increase in size
									__UpperCAmelCase						:			int												=       [
									    floats_list((x, self.feature_size))
									    for x in range(self.min_seq_length   ,	self.max_seq_length   ,	self.seq_length_diff)
									]
						if numpify:
									__UpperCAmelCase						:			List[str]												=       [np.asarray(UpperCamelCase_) for x in speech_inputs]
						return speech_inputs






@require_torch
@require_torchaudio
class   a__					(						__magic_name__				,     unittest.TestCase ):
			lowercase_		=						WhisperFeatureExtractor if is_speech_available() else None


			def    a_  (   self   :     Optional[int]):


						"""simple docstring"""




						__UpperCAmelCase						:			Union[str, Any]												=       WhisperFeatureExtractionTester(self)


			def    a_  (   self   :     Dict):


						"""simple docstring"""




						__UpperCAmelCase						:			Optional[int]												=       self.feature_extraction_class(**self.feat_extract_dict)

						with tempfile.TemporaryDirectory() as tmpdirname:
									__UpperCAmelCase						:			str												=       feat_extract_first.save_pretrained(UpperCamelCase_)[0]
									check_json_file_has_correct_format(UpperCamelCase_)
									__UpperCAmelCase						:			Optional[Any]												=       self.feature_extraction_class.from_pretrained(UpperCamelCase_)

						__UpperCAmelCase						:			Tuple												=       feat_extract_first.to_dict()
						__UpperCAmelCase						:			Any												=       feat_extract_second.to_dict()
						__UpperCAmelCase						:			int												=       feat_extract_first.mel_filters
						__UpperCAmelCase						:			List[Any]												=       feat_extract_second.mel_filters
						self.assertTrue(np.allclose(UpperCamelCase_   ,	UpperCamelCase_))
						self.assertEqual(UpperCamelCase_   ,	UpperCamelCase_)


			def    a_  (   self   :     int):


						"""simple docstring"""




						__UpperCAmelCase						:			int												=       self.feature_extraction_class(**self.feat_extract_dict)

						with tempfile.TemporaryDirectory() as tmpdirname:
									__UpperCAmelCase						:			str												=       os.path.join(UpperCamelCase_   ,	"feat_extract.json")
									feat_extract_first.to_json_file(UpperCamelCase_)
									__UpperCAmelCase						:			List[Any]												=       self.feature_extraction_class.from_json_file(UpperCamelCase_)

						__UpperCAmelCase						:			str												=       feat_extract_first.to_dict()
						__UpperCAmelCase						:			List[Any]												=       feat_extract_second.to_dict()
						__UpperCAmelCase						:			Dict												=       feat_extract_first.mel_filters
						__UpperCAmelCase						:			List[Any]												=       feat_extract_second.mel_filters
						self.assertTrue(np.allclose(UpperCamelCase_   ,	UpperCamelCase_))
						self.assertEqual(UpperCamelCase_   ,	UpperCamelCase_)


			def    a_  (   self   :     Tuple):


						"""simple docstring"""




						__UpperCAmelCase						:			List[Any]												=       self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict())
						# create three inputs of length 800, 1000, and 1200
						__UpperCAmelCase						:			str												=       [floats_list((1, x))[0] for x in range(800   ,	1400   ,	200)]
						__UpperCAmelCase						:			int												=       [np.asarray(UpperCamelCase_) for speech_input in speech_inputs]

						# Test feature size
						__UpperCAmelCase						:			Tuple												=       feature_extractor(UpperCamelCase_   ,	padding="max_length"   ,	return_tensors="np").input_features
						self.assertTrue(input_features.ndim == 3)
						self.assertTrue(input_features.shape[-1] == feature_extractor.nb_max_frames)
						self.assertTrue(input_features.shape[-2] == feature_extractor.feature_size)

						# Test not batched input
						__UpperCAmelCase						:			str												=       feature_extractor(speech_inputs[0]   ,	return_tensors="np").input_features
						__UpperCAmelCase						:			Dict												=       feature_extractor(np_speech_inputs[0]   ,	return_tensors="np").input_features
						self.assertTrue(np.allclose(UpperCamelCase_   ,	UpperCamelCase_   ,	atol=1e-3))

						# Test batched
						__UpperCAmelCase						:			List[str]												=       feature_extractor(UpperCamelCase_   ,	return_tensors="np").input_features
						__UpperCAmelCase						:			str												=       feature_extractor(UpperCamelCase_   ,	return_tensors="np").input_features
						for enc_seq_a, enc_seq_a in zip(UpperCamelCase_   ,	UpperCamelCase_):
									self.assertTrue(np.allclose(UpperCamelCase_   ,	UpperCamelCase_   ,	atol=1e-3))

						# Test 2-D numpy arrays are batched.
						__UpperCAmelCase						:			List[Any]												=       [floats_list((1, x))[0] for x in (800, 800, 800)]
						__UpperCAmelCase						:			Optional[int]												=       np.asarray(UpperCamelCase_)
						__UpperCAmelCase						:			List[Any]												=       feature_extractor(UpperCamelCase_   ,	return_tensors="np").input_features
						__UpperCAmelCase						:			Any												=       feature_extractor(UpperCamelCase_   ,	return_tensors="np").input_features
						for enc_seq_a, enc_seq_a in zip(UpperCamelCase_   ,	UpperCamelCase_):
									self.assertTrue(np.allclose(UpperCamelCase_   ,	UpperCamelCase_   ,	atol=1e-3))

						# Test truncation required
						__UpperCAmelCase						:			Tuple												=       [floats_list((1, x))[0] for x in range(200   ,	(feature_extractor.n_samples + 500)   ,	200)]
						__UpperCAmelCase						:			List[Any]												=       [np.asarray(UpperCamelCase_) for speech_input in speech_inputs]

						__UpperCAmelCase						:			Tuple												=       [x[: feature_extractor.n_samples] for x in speech_inputs]
						__UpperCAmelCase						:			int												=       [np.asarray(UpperCamelCase_) for speech_input in speech_inputs_truncated]

						__UpperCAmelCase						:			Optional[int]												=       feature_extractor(UpperCamelCase_   ,	return_tensors="np").input_features
						__UpperCAmelCase						:			Optional[int]												=       feature_extractor(UpperCamelCase_   ,	return_tensors="np").input_features
						for enc_seq_a, enc_seq_a in zip(UpperCamelCase_   ,	UpperCamelCase_):
									self.assertTrue(np.allclose(UpperCamelCase_   ,	UpperCamelCase_   ,	atol=1e-3))


			def    a_  (   self   :     Union[str, Any]):


						"""simple docstring"""




						import torch

						__UpperCAmelCase						:			Tuple												=       self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict())
						__UpperCAmelCase						:			Dict												=       np.random.rand(100   ,	32).astype(np.floataa)
						__UpperCAmelCase						:			Tuple												=       np_speech_inputs.tolist()

						for inputs in [py_speech_inputs, np_speech_inputs]:
									__UpperCAmelCase						:			str												=       feature_extractor.pad([{"input_features": inputs}]   ,	return_tensors="np")
									self.assertTrue(np_processed.input_features.dtype == np.floataa)
									__UpperCAmelCase						:			Optional[Any]												=       feature_extractor.pad([{"input_features": inputs}]   ,	return_tensors="pt")
									self.assertTrue(pt_processed.input_features.dtype == torch.floataa)


			def    a_  (   self   :     Optional[Any]   ,	UpperCamelCase_   :     str):


						"""simple docstring"""




						__UpperCAmelCase						:			Union[str, Any]												=       load_dataset("hf-internal-testing/librispeech_asr_dummy"   ,	"clean"   ,	split="validation")
						# automatic decoding with librispeech
						__UpperCAmelCase						:			List[str]												=       ds.sort("id").select(range(UpperCamelCase_))[:num_samples]["audio"]

						return [x["array"] for x in speech_samples]


			def    a_  (   self   :     Any):


						"""simple docstring"""




						__UpperCAmelCase						:			Any												=       torch.tensor(
						    [
						        0.1193, -0.0946, -0.1098, -0.0196, 0.0225, -0.0690, -0.1736, 0.0951,
						        0.0971, -0.0817, -0.0702, 0.0162, 0.0260, 0.0017, -0.0192, -0.1678,
						        0.0709, -0.1867, -0.0655, -0.0274, -0.0234, -0.1884, -0.0516, -0.0554,
						        -0.0274, -0.1425, -0.1423, 0.0837, 0.0377, -0.0854
						    ])
						# fmt: on

						__UpperCAmelCase						:			Union[str, Any]												=       self._load_datasamples(1)
						__UpperCAmelCase						:			Optional[int]												=       WhisperFeatureExtractor()
						__UpperCAmelCase						:			Dict												=       feature_extractor(UpperCamelCase_   ,	return_tensors="pt").input_features
						self.assertEqual(input_features.shape   ,	(1, 80, 3000))
						self.assertTrue(torch.allclose(input_features[0, 0, :30]   ,	UpperCamelCase_   ,	atol=1e-4))


			def    a_  (   self   :     Optional[int]):


						"""simple docstring"""




						__UpperCAmelCase						:			Dict												=       self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict())
						__UpperCAmelCase						:			List[Any]												=       self._load_datasamples(1)[0]
						__UpperCAmelCase						:			Dict												=       ((audio - audio.min()) / (audio.max() - audio.min())) * 65535  # Rescale to [0, 65535] to show issue
						__UpperCAmelCase						:			Optional[Any]												=       feat_extract.zero_mean_unit_var_norm([audio]   ,	attention_mask=UpperCamelCase_)[0]

						self.assertTrue(np.all(np.mean(UpperCamelCase_) < 1e-3))
						self.assertTrue(np.all(np.abs(np.var(UpperCamelCase_) - 1) < 1e-3))



"""simple docstring"""




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		(							lowerCamelCase__      ,	lowerCamelCase__      ,	lowerCamelCase__=1e-12  ):





		"""simple docstring"""




		lowerCAmelCase__   = jnp.divide(emb_a.T      ,	jnp.clip(jnp.linalg.norm(lowerCamelCase__      ,	axis=1  )      ,	a_min=lowerCamelCase__  )  ).T
		lowerCAmelCase__   = jnp.divide(emb_a.T      ,	jnp.clip(jnp.linalg.norm(lowerCamelCase__      ,	axis=1  )      ,	a_min=lowerCamelCase__  )  ).T
		return jnp.matmul(lowerCamelCase__      ,	norm_emb_a.T  )

class    a_   (  nn.Module      ):
					UpperCamelCase_			:						CLIPConfig
					UpperCamelCase_			:						jnp.dtype      =     jnp.floataa




					def     _SCREAMING_SNAKE_CASE    (						self    :  Tuple  ):
							lowerCAmelCase__   = FlaxCLIPVisionModule(self.config.vision_config  )
							lowerCAmelCase__   = nn.Dense(self.config.projection_dim	,    use_bias=snake_case__	,    dtype=self.dtype  )

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

							lowerCAmelCase__   = self.param("""concept_embeds_weights"""	,    jax.nn.initializers.ones	,    (17,)  )
							lowerCAmelCase__   = self.param("""special_care_embeds_weights"""	,    jax.nn.initializers.ones	,    (3,)  )




					def __call__(						self    :  Optional[int]	,    snake_case__    :  int  ):
							lowerCAmelCase__   = self.vision_model(snake_case__  )[1]
							lowerCAmelCase__   = self.visual_projection(snake_case__  )

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

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

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

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

							return has_nsfw_concepts

class    a_   (  __UpperCamelCase      ):
					UpperCamelCase_			:						Any      =     CLIPConfig
					UpperCamelCase_			:						str      =     "clip_input"
					UpperCamelCase_			:						str      =     FlaxStableDiffusionSafetyCheckerModule




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




					def     _SCREAMING_SNAKE_CASE    (						self    :  Tuple	,    snake_case__    :  jax.random.KeyArray	,    snake_case__    :  Tuple	,    snake_case__    :  FrozenDict = None  ):
							# init input tensor
							lowerCAmelCase__   = jax.random.normal(snake_case__	,    snake_case__  )

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

							lowerCAmelCase__   = self.module.init(snake_case__	,    snake_case__  )["""params"""]

							return random_params




					def __call__(						self    :  Union[str, Any]	,    snake_case__    :  Union[str, Any]	,    snake_case__    :  dict = None	,    ):
							lowerCAmelCase__   = jnp.transpose(snake_case__	,    (0, 2, 3, 1)  )

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




