



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

from ...utils import logging


__lowerCamelCase     :							List[str]  							=				logging.get_logger(__name__)





class  __magic_name__   (							A__						):
		lowercase      :       str          =CLIPConfig

		lowercase      :       Union[str, Any]          =['''CLIPEncoderLayer''']






		def __init__(						self      :     Any				,		UpperCamelCase__      :     CLIPConfig      )				->					Dict:
								'''simple docstring'''







								super().__init__(UpperCamelCase__      )

								UpperCAmelCase         =     CLIPVisionModelWithProjection(config.vision_config      )

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







		@torch.no_grad()
		def SCREAMING_SNAKE_CASE_			(						self      :     Any				,		UpperCamelCase__      :     Optional[int]				,		UpperCamelCase__      :     Any				,		UpperCamelCase__      :     Any=0.5				,		UpperCamelCase__      :     Tuple=0.5      )				->					Dict:
								'''simple docstring'''







								UpperCAmelCase         =     self.vision_model(UpperCamelCase__      )[0]

								UpperCAmelCase         =     self.p_head(UpperCamelCase__      )
								UpperCAmelCase         =     nsfw_detected.flatten()
								UpperCAmelCase         =     nsfw_detected > p_threshold
								UpperCAmelCase         =     nsfw_detected.tolist()

								if any(UpperCamelCase__      ):
														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(UpperCamelCase__      ):
														if nsfw_detected_:
																				UpperCAmelCase         =     np.zeros(images[idx].shape      )

								UpperCAmelCase         =     self.w_head(UpperCamelCase__      )
								UpperCAmelCase         =     watermark_detected.flatten()
								UpperCAmelCase         =     watermark_detected > w_threshold
								UpperCAmelCase         =     watermark_detected.tolist()

								if any(UpperCamelCase__      ):
														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(UpperCamelCase__      ):
														if watermark_detected_:
																				UpperCAmelCase         =     np.zeros(images[idx].shape      )

								return images, nsfw_detected, watermark_detected








import os
import unittest

from transformers.models.cpmant.tokenization_cpmant import VOCAB_FILES_NAMES, CpmAntTokenizer
from transformers.testing_utils import require_jieba, tooslow

from ...test_tokenization_common import TokenizerTesterMixin





@require_jieba
class  __magic_name__   (							A__,  unittest.TestCase						):
		lowercase      :       Optional[Any]          =CpmAntTokenizer
		lowercase      :       Dict          =False






		def SCREAMING_SNAKE_CASE_			(						self      :     Optional[Any]      )				->					Dict:
								'''simple docstring'''







								super().setUp()

								UpperCAmelCase         =     [
								    "<d>",
								    "</d>",
								    "<s>",
								    "</s>",
								    "</_>",
								    "<unk>",
								    "<pad>",
								    "</n>",
								    "我",
								    "是",
								    "C",
								    "P",
								    "M",
								    "A",
								    "n",
								    "t",
								]
								UpperCAmelCase         =     os.path.join(self.tmpdirname				,		VOCAB_FILES_NAMES["vocab_file"]      )
								with open(self.vocab_file				,		"w"				,		encoding="utf-8"      ) as vocab_writer:
														vocab_writer.write("".join([x + "\n" for x in vocab_tokens]      )      )







		@tooslow
		def SCREAMING_SNAKE_CASE_			(						self      :     List[str]      )				->					Dict:
								'''simple docstring'''







								UpperCAmelCase         =     CpmAntTokenizer.from_pretrained("openbmb/cpm-ant-10b"      )
								UpperCAmelCase         =     "今天天气真好！"
								UpperCAmelCase         =     ["今天", "天气", "真", "好", "！"]
								UpperCAmelCase         =     tokenizer.tokenize(UpperCamelCase__      )
								self.assertListEqual(UpperCamelCase__				,		UpperCamelCase__      )
								UpperCAmelCase         =     "今天天气真好！"
								UpperCAmelCase         =     [tokenizer.bos_token] + tokens

								UpperCAmelCase         =     [6, 98_02, 1_49_62, 20_82, 8_31, 2_44]
								self.assertListEqual(tokenizer.convert_tokens_to_ids(UpperCamelCase__      )				,		UpperCamelCase__      )

								UpperCAmelCase         =     tokenizer.decode(UpperCamelCase__      )
								self.assertEqual(UpperCamelCase__				,		UpperCamelCase__      )



