o
    Û·iE  ã                   @   sZ   d dl Zd dlZd dlmZ d dlmZmZmZ ddl	m
Z
 e
 e¡ZG dd„ deƒZdS )é    N)Ú
CLIPConfigÚCLIPVisionModelWithProjectionÚPreTrainedModelé   )Úloggingc                       s>   e Zd ZeZdgZdef‡ fdd„Ze ¡ ddd„ƒZ	‡  Z
S )	ÚIFSafetyCheckerÚCLIPEncoderLayerÚconfigc                    s@   t ƒ  |¡ t|jƒ| _t |jjd¡| _t |jjd¡| _	d S )Né   )
ÚsuperÚ__init__r   Úvision_configÚvision_modelÚnnÚLinearÚprojection_dimÚp_headÚw_head)Úselfr	   ©Ú	__class__© úe/home/ubuntu/vllm_env/lib/python3.10/site-packages/diffusers/pipelines/deepfloyd_if/safety_checker.pyr      s   zIFSafetyChecker.__init__ç      à?c                 C   sÔ   |   |¡d }|  |¡}| ¡ }||k}| ¡ }t|ƒr!t d¡ t|ƒD ]\}}|r5t 	|| j
¡||< q%|  |¡}	|	 ¡ }	|	|k}	|	 ¡ }	t|	ƒrPt d¡ t|	ƒD ]\}}
|
rdt 	|| j
¡||< qT|||	fS )Nr   z‘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.z˜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.)r   r   ÚflattenÚtolistÚanyÚloggerÚwarningÚ	enumerateÚnpÚzerosÚshaper   )r   Ú
clip_inputÚimagesÚp_thresholdÚw_thresholdÚimage_embedsÚnsfw_detectedÚidxÚnsfw_detected_Úwatermark_detectedÚwatermark_detected_r   r   r   Úforward   s4   
ÿ€
ÿ€
zIFSafetyChecker.forward)r   r   )Ú__name__Ú
__module__Ú__qualname__r   Úconfig_classÚ_no_split_modulesr   ÚtorchÚno_gradr-   Ú__classcell__r   r   r   r   r      s    r   )Únumpyr    r3   Útorch.nnr   Útransformersr   r   r   Úutilsr   Ú
get_loggerr.   r   r   r   r   r   r   Ú<module>   s    
