





'''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-1_2							):

				"""simple docstring"""






				lowercase         = jnp.divide(emb_a.T		,							jnp.clip(jnp.linalg.norm(lowerCAmelCase_		,							axis=1							)		,							a_min=lowerCAmelCase_							)							).T
				lowercase         = 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       UpperCAmelCase (						nn.Module  ):
		UpperCAmelCase					:	CLIPConfig
		UpperCAmelCase					:	jnp.dtype 			=		jnp.floataa




		def 				lowerCamelCase__   (self      :	int				)					-> Any:
						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							,			(1_7, 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							,			(1_7,)				)
						lowercase         = self.param("special_care_embeds_weights"							,			jax.nn.initializers.ones							,			(3,)				)





		def __call__(self      :	Union[str, Any]							,			A__      :	Dict				)					-> Tuple:
						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.0_1

						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       UpperCAmelCase (						_lowercase  ):
		UpperCAmelCase					:	Tuple 			=		CLIPConfig
		UpperCAmelCase					:	List[Any] 			=		'''clip_input'''
		UpperCAmelCase					:	Union[str, Any] 			=		FlaxStableDiffusionSafetyCheckerModule




		def __init__(self      :	Tuple							,			A__      :	CLIPConfig							,			A__      :	Optional[Tuple] = None							,			A__      :	int = 0							,			A__      :	jnp.dtype = jnp.floataa							,			A__      :	bool = True							,			**A__      :	Tuple							,			)					-> Union[str, Any]:
						if input_shape is None:
										lowercase         = (1, 2_2_4, 2_2_4, 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 				lowerCamelCase__   (self      :	Any							,			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__      :	Any							,			A__      :	dict = None							,			)					-> Optional[Any]:
						lowercase         = jnp.transpose(A__							,			(0, 2, 3, 1)				)

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











'''simple docstring'''

import json
import os
from collections import Counter

import torch
import torchvision
import torchvision.transforms as transforms
from PIL import Image
from torch import nn
from torch.utils.data import Dataset


__lowerCamelCase : Any											=			{1: (1, 1), 2: (2, 1), 3: (3, 1), 4: (2, 2), 5: (5, 1), 6: (3, 2), 7: (7, 1), 8: (4, 2), 9: (3, 3)}

class       UpperCAmelCase (						nn.Module  ):




		def __init__(self      :	Tuple							,			A__      :	Any				)					-> str:
						super().__init__()
						lowercase         = torchvision.models.resnetaaa(pretrained=A__				)
						lowercase         = list(model.children()				)[:-2]
						lowercase         = nn.Sequential(*A__				)
						lowercase         = nn.AdaptiveAvgPoolad(POOLING_BREAKDOWN[args.num_image_embeds]				)





		def 				UpperCAmelCase__   (self      :	List[str]							,			A__      :	Optional[Any]				)					-> Optional[int]:
						# Bx3x224x224 -> Bx2048x7x7 -> Bx2048xN -> BxNx2048
						lowercase         = self.pool(self.model(A__				)				)
						lowercase         = torch.flatten(A__							,			start_dim=2				)
						lowercase         = out.transpose(1							,			2				).contiguous()
						return out  # BxNx2048




class       UpperCAmelCase (						_lowercase  ):




		def __init__(self      :	int							,			A__      :	Optional[int]							,			A__      :	Optional[Any]							,			A__      :	List[Any]							,			A__      :	Tuple							,			A__      :	Optional[int]				)					-> Union[str, Any]:
						lowercase         = [json.loads(A__				) for l in open(A__				)]
						lowercase         = os.path.dirname(A__				)
						lowercase         = tokenizer
						lowercase         = labels
						lowercase         = len(A__				)
						lowercase         = max_seq_length

						lowercase         = transforms




		def __len__(self      :	List[str]				)					-> Dict:
						return len(self.data				)




		def __getitem__(self      :	int							,			A__      :	Union[str, Any]				)					-> List[str]:
						lowercase         = torch.LongTensor(self.tokenizer.encode(self.data[index]["text"]							,			add_special_tokens=A__				)				)
						lowercase   ,							lowercase   ,							lowercase         = sentence[0], sentence[1:-1], sentence[-1]
						lowercase         = sentence[: self.max_seq_length]

						lowercase         = torch.zeros(self.n_classes				)
						lowercase         = 1

						lowercase         = Image.open(os.path.join(self.data_dir							,			self.data[index]["img"]				)				).convert("RGB"				)
						lowercase         = self.transforms(A__				)

						return {
						    "image_start_token": start_token,
						    "image_end_token": end_token,
						    "sentence": sentence,
						    "image": image,
						    "label": label,
						}





		def 				UpperCAmelCase__   (self      :	str				)					-> str:
						lowercase         = Counter()
						for row in self.data:
										label_freqs.update(row["label"]				)
						return label_freqs







def 							UpperCAmelCase_    (	lowerCAmelCase_							):

				"""simple docstring"""






				lowercase         = [len(row["sentence"]							) for row in batch]
				lowercase   ,							lowercase         = len(lowerCAmelCase_							), max(lowerCAmelCase_							)

				lowercase         = torch.zeros(lowerCAmelCase_		,							lowerCAmelCase_		,							dtype=torch.long							)
				lowercase         = torch.zeros(lowerCAmelCase_		,							lowerCAmelCase_		,							dtype=torch.long							)

				for i_batch, (input_row, length) in enumerate(zip(lowerCAmelCase_		,							lowerCAmelCase_							)							):
								lowercase         = input_row["sentence"]
								lowercase         = 1

				lowercase         = torch.stack([row["image"] for row in batch]							)
				lowercase         = torch.stack([row["label"] for row in batch]							)
				lowercase         = torch.stack([row["image_start_token"] for row in batch]							)
				lowercase         = torch.stack([row["image_end_token"] for row in batch]							)

				return text_tensor, mask_tensor, img_tensor, img_start_token, img_end_token, tgt_tensor







def 							UpperCAmelCase_    (	):

				"""simple docstring"""






				return [
				    "Crime",
				    "Drama",
				    "Thriller",
				    "Action",
				    "Comedy",
				    "Romance",
				    "Documentary",
				    "Short",
				    "Mystery",
				    "History",
				    "Family",
				    "Adventure",
				    "Fantasy",
				    "Sci-Fi",
				    "Western",
				    "Horror",
				    "Sport",
				    "War",
				    "Music",
				    "Musical",
				    "Animation",
				    "Biography",
				    "Film-Noir",
				]







def 							UpperCAmelCase_    (	):

				"""simple docstring"""






				return transforms.Compose(
				    [
				        transforms.Resize(256							),
				        transforms.CenterCrop(224							),
				        transforms.ToTensor(),
				        transforms.Normalize(
				            mean=[0.46_77_70_44, 0.44_53_14_29, 0.40_66_10_17]		,							std=[0.12_22_19_94, 0.12_14_58_35, 0.14_38_04_69]		,							),
				    ]							)




