




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

from ...utils import logging


a__		     =     logging.get_logger(__name__)


class    SCREAMING_SNAKE_CASE_				(       _UpperCamelCase   ):



	"""simple docstring"""


	__magic_name__   :       str      	=		CLIPConfig

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






	def __init__(					self		:						Dict					,     lowerCAmelCase		:						CLIPConfig	)	->       Optional[Any]:







						"""simple docstring"""







						super().__init__(lowerCAmelCase	)

						__UpperCamelCase					:	List[Any]             =      CLIPVisionModelWithProjection(config.vision_config	)

						__UpperCamelCase					:	Any             =      nn.Linear(config.vision_config.projection_dim					,     1	)
						__UpperCamelCase					:	Optional[Any]             =      nn.Linear(config.vision_config.projection_dim					,     1	)






	@torch.no_grad()
	def 					lowerCamelCase__     (					self		:						Tuple					,     lowerCAmelCase		:						Optional[int]					,     lowerCAmelCase		:						Any					,     lowerCAmelCase		:						Tuple=0.5					,     lowerCAmelCase		:						Any=0.5	)	->       int:







						"""simple docstring"""







						__UpperCamelCase					:	Any             =      self.vision_model(lowerCAmelCase	)[0]

						__UpperCamelCase					:	Union[str, Any]             =      self.p_head(lowerCAmelCase	)
						__UpperCamelCase					:	List[Any]             =      nsfw_detected.flatten()
						__UpperCamelCase					:	str             =      nsfw_detected > p_threshold
						__UpperCamelCase					:	int             =      nsfw_detected.tolist()

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

						__UpperCamelCase					:	int             =      self.w_head(lowerCAmelCase	)
						__UpperCamelCase					:	Optional[Any]             =      watermark_detected.flatten()
						__UpperCamelCase					:	Optional[int]             =      watermark_detected > w_threshold
						__UpperCamelCase					:	Union[str, Any]             =      watermark_detected.tolist()

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

						return images, nsfw_detected, watermark_detected












import re
from pathlib import Path
from unittest import TestCase

import pytest


@pytest.mark.integration
class    SCREAMING_SNAKE_CASE_				(       _UpperCamelCase   ):



	"""simple docstring"""


	def 					lowerCamelCase__     (					self		:						Any					,     lowerCAmelCase		:						str	)	->       List[Any]:







						"""simple docstring"""







						with open(lowerCAmelCase					,     encoding="""utf-8"""	) as input_file:
											__UpperCamelCase					:	Union[str, Any]             =      re.compile(R"""(?!.*\b(?:encoding|rb|w|wb|w+|wb+|ab|ab+)\b)(?<=\s)(open)\((.*)\)"""	)
											__UpperCamelCase					:	str             =      input_file.read()
											__UpperCamelCase					:	Optional[int]             =      regexp.search(lowerCAmelCase	)

						return match






	def 					lowerCamelCase__     (					self		:						Optional[Any]					,     lowerCAmelCase		:						str	)	->       Union[str, Any]:







						"""simple docstring"""







						with open(lowerCAmelCase					,     encoding="""utf-8"""	) as input_file:
											__UpperCamelCase					:	Dict             =      re.compile(R"""#[^\r\n]*print\(|\"[^\r\n]*print\(|\"\"\".*?print\(.*?\"\"\"|(print\()"""					,     re.DOTALL	)
											__UpperCamelCase					:	str             =      input_file.read()
											# use `re.finditer` to handle the case where the ignored groups would be matched first by `re.search`
											__UpperCamelCase					:	List[Any]             =      regexp.finditer(lowerCAmelCase	)

						__UpperCamelCase					:	int             =      [match for match in matches if match is not None and match.group(1	) is not None]
						return matches[0] if matches else None






	def 					lowerCamelCase__     (					self		:						List[Any]	)	->       List[str]:







						"""simple docstring"""







						__UpperCamelCase					:	Optional[int]             =      Path("""./datasets"""	)
						__UpperCamelCase					:	Optional[int]             =      list(dataset_paths.absolute().glob("""**/*.py"""	)	)

						for dataset in dataset_files:
											if self._no_encoding_on_file_open(str(lowerCAmelCase	)	):
																raise AssertionError(F'''open(...) must use utf-8 encoding in {dataset}'''	)






	def 					lowerCamelCase__     (					self		:						Optional[Any]	)	->       Any:







						"""simple docstring"""







						__UpperCamelCase					:	Optional[int]             =      Path("""./datasets"""	)
						__UpperCamelCase					:	Optional[int]             =      list(dataset_paths.absolute().glob("""**/*.py"""	)	)

						for dataset in dataset_files:
											if self._no_print_statements(str(lowerCAmelCase	)	):
																raise AssertionError(F'''print statement found in {dataset}. Use datasets.logger/logging instead.'''	)






