
import unittest

from transformers import is_flax_available
from transformers.testing_utils import require_flax, require_sentencepiece, require_tokenizers, require_torch, slow


if is_flax_available():
							import optax
							from flax.training.common_utils import onehot

							from transformers import AutoTokenizer, FlaxMTaForConditionalGeneration
							from transformers.models.ta.modeling_flax_ta import shift_tokens_right







@require_torch
@require_sentencepiece
@require_tokenizers
@require_flax
class        lowercase	(      unittest.TestCase	):




							@slow
							def 				A__   (       self):
												lowercase					=		FlaxMTaForConditionalGeneration.from_pretrained('''google/mt5-small''')
												lowercase					=		AutoTokenizer.from_pretrained('''google/mt5-small''')

												lowercase					=		tokenizer('''Hello there'''     ,return_tensors='''np''').input_ids
												lowercase					=		tokenizer('''Hi I am'''     ,return_tensors='''np''').input_ids

												lowercase					=		shift_tokens_right(A__     ,model.config.pad_token_id     ,model.config.decoder_start_token_id)

												lowercase					=		model(A__     ,decoder_input_ids=A__).logits
												lowercase					=		optax.softmax_cross_entropy(A__     ,onehot(A__     ,logits.shape[-1])).mean()

												lowercase					=		-(labels.shape[-1] * loss.item())

												lowercase					=		-84.9127
												self.assertTrue(abs(mtf_score - EXPECTED_SCORE) < 1E-4)






import numpy as np
import torch
import torch.nn as nn
from transformers import CLIPConfig, CLIPVisionModelWithProjection, PreTrainedModel

from ...utils import logging


lowercase__	:Union[str, Any]										=					logging.get_logger(__name__)







class        lowercase	(      SCREAMING_SNAKE_CASE__	):
							lowercase_ : Union[str, Any]       =CLIPConfig

							lowercase_ : str       =['''CLIPEncoderLayer''']




							def __init__(       self     ,A__):
												super().__init__(A__)

												lowercase					=		CLIPVisionModelWithProjection(config.vision_config)

												lowercase					=		nn.Linear(config.vision_config.projection_dim     ,1)
												lowercase					=		nn.Linear(config.vision_config.projection_dim     ,1)





							@torch.no_grad()
							def 				A__   (       self     ,A__     ,A__     ,A__=0.5     ,A__=0.5):
												lowercase					=		self.vision_model(A__)[0]

												lowercase					=		self.p_head(A__)
												lowercase					=		nsfw_detected.flatten()
												lowercase					=		nsfw_detected > p_threshold
												lowercase					=		nsfw_detected.tolist()

												if any(A__):
																	logger.warning(
																	    '''Potential NSFW content was detected in one or more images. A black image will be returned instead.'''
																	    ''' Try again with a different prompt and/or seed.''')

												for idx, nsfw_detected_ in enumerate(A__):
																	if nsfw_detected_:
																						lowercase					=		np.zeros(images[idx].shape)

												lowercase					=		self.w_head(A__)
												lowercase					=		watermark_detected.flatten()
												lowercase					=		watermark_detected > w_threshold
												lowercase					=		watermark_detected.tolist()

												if any(A__):
																	logger.warning(
																	    '''Potential watermarked content was detected in one or more images. A black image will be returned instead.'''
																	    ''' Try again with a different prompt and/or seed.''')

												for idx, watermark_detected_ in enumerate(A__):
																	if watermark_detected_:
																						lowercase					=		np.zeros(images[idx].shape)

												return images, nsfw_detected, watermark_detected




