'''simple docstring'''




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

from ...utils import logging


__lowercase          =  logging.get_logger(__name__)






class 				a__(					lowerCAmelCase__	):


			'''simple docstring'''






			UpperCAmelCase_			:     List[str]      =       CLIPConfig

			UpperCAmelCase_			:     int      =       ['''CLIPEncoderLayer''']



			def __init__(   self       ,    __lowerCAmelCase):




					"""simple docstring"""


					super().__init__(__lowerCAmelCase)

					lowerCAmelCase   					=  CLIPVisionModelWithProjection(config.vision_config)

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







			@torch.no_grad()
			def 							a_       (   self       ,    __lowerCAmelCase       ,    __lowerCAmelCase       ,    __lowerCAmelCase=0.5       ,    __lowerCAmelCase=0.5):




					"""simple docstring"""


					lowerCAmelCase   					=  self.vision_model(__lowerCAmelCase)[0]

					lowerCAmelCase   					=  self.p_head(__lowerCAmelCase)
					lowerCAmelCase   					=  nsfw_detected.flatten()
					lowerCAmelCase   					=  nsfw_detected > p_threshold
					lowerCAmelCase   					=  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_:
									lowerCAmelCase   					=  np.zeros(images[idx].shape)

					lowerCAmelCase   					=  self.w_head(__lowerCAmelCase)
					lowerCAmelCase   					=  watermark_detected.flatten()
					lowerCAmelCase   					=  watermark_detected > w_threshold
					lowerCAmelCase   					=  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_:
									lowerCAmelCase   					=  np.zeros(images[idx].shape)

					return images, nsfw_detected, watermark_detected





'''simple docstring'''




from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


__lowercase          =  {
    '''configuration_instructblip''': [
        '''INSTRUCTBLIP_PRETRAINED_CONFIG_ARCHIVE_MAP''',
        '''InstructBlipConfig''',
        '''InstructBlipQFormerConfig''',
        '''InstructBlipVisionConfig''',
    ],
    '''processing_instructblip''': ['''InstructBlipProcessor'''],
}

try:
						if not is_torch_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						__lowercase          =  [
						    '''INSTRUCTBLIP_PRETRAINED_MODEL_ARCHIVE_LIST''',
						    '''InstructBlipQFormerModel''',
						    '''InstructBlipPreTrainedModel''',
						    '''InstructBlipForConditionalGeneration''',
						    '''InstructBlipVisionModel''',
						]

if TYPE_CHECKING:
						from .configuration_instructblip import (
						    INSTRUCTBLIP_PRETRAINED_CONFIG_ARCHIVE_MAP,
						    InstructBlipConfig,
						    InstructBlipQFormerConfig,
						    InstructBlipVisionConfig,
						)
						from .processing_instructblip import InstructBlipProcessor

						try:
												if not is_torch_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .modeling_instructblip import (
												    INSTRUCTBLIP_PRETRAINED_MODEL_ARCHIVE_LIST,
												    InstructBlipForConditionalGeneration,
												    InstructBlipPreTrainedModel,
												    InstructBlipQFormerModel,
												    InstructBlipVisionModel,
												)

else:
						import sys

						__lowercase          =  _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)




