
import argparse

import torch

from transformers import FunnelBaseModel, FunnelConfig, FunnelModel, load_tf_weights_in_funnel
from transformers.utils import logging


logging.set_verbosity_info()



def  UpperCamelCase					(							lowerCAmelCase__				,    lowerCAmelCase__				,    lowerCAmelCase__				,    lowerCAmelCase__	):
					'''simple docstring'''
					lowercase					=		FunnelConfig.from_json_file(a_	)
					print(f'Building PyTorch model from configuration: {config}'	)
					lowercase					=		FunnelBaseModel(a_	) if base_model else FunnelModel(a_	)

					# Load weights from tf checkpoint
					load_tf_weights_in_funnel(a_				,    a_				,    a_	)

					# Save pytorch-model
					print(f'Save PyTorch model to {pytorch_dump_path}'	)
					torch.save(model.state_dict()				,    a_	)


if __name__ == "__main__":
							lowercase__	:Any										=					argparse.ArgumentParser()
							# Required parameters
							parser.add_argument(
							    "--tf_checkpoint_path", default=None, type=str, required=True, help="Path to the TensorFlow checkpoint path."
							)
							parser.add_argument(
							    "--config_file",
							    default=None,
							    type=str,
							    required=True,
							    help="The config json file corresponding to the pre-trained model. \nThis specifies the model architecture.",
							)
							parser.add_argument(
							    "--pytorch_dump_path", default=None, type=str, required=True, help="Path to the output PyTorch model."
							)
							parser.add_argument(
							    "--base_model", action="store_true", help="Whether you want just the base model (no decoder) or not."
							)
							lowercase__	:Any										=					parser.parse_args()
							convert_tf_checkpoint_to_pytorch(
							    args.tf_checkpoint_path, args.config_file, args.pytorch_dump_path, args.base_model
							)






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




