






'''simple docstring'''


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

from ...utils import logging


SCREAMING_SNAKE_CASE_				      =							logging.get_logger(__name__)






def   lowerCAmelCase__			(					SCREAMING_SNAKE_CASE__       ,		SCREAMING_SNAKE_CASE__		):
 __a    :	Dict									=						nn.functional.normalize(SCREAMING_SNAKE_CASE__		)
 __a    :	Optional[Any]									=						nn.functional.normalize(SCREAMING_SNAKE_CASE__		)
 return torch.mm(SCREAMING_SNAKE_CASE__       ,		normalized_text_embeds.t()		)




class 							lowerCAmelCase					(							lowerCamelCase__   ):







   """simple docstring"""







   _A						=   CLIPConfig

   _A						=   ['CLIPEncoderLayer']



   def __init__( self   ,   _A			)    ->   str:
    super().__init__(_A			)

    __a    :	List[Any]									=						CLIPVisionModel(config.vision_config			)
    __a    :	Dict									=						nn.Linear(config.vision_config.hidden_size   ,   config.projection_dim   ,   bias=_A			)

    __a    :	Optional[int]									=						nn.Parameter(torch.ones(17   ,   config.projection_dim			)   ,   requires_grad=_A			)
    __a    :	Any									=						nn.Parameter(torch.ones(3   ,   config.projection_dim			)   ,   requires_grad=_A			)

    __a    :	Optional[int]									=						nn.Parameter(torch.ones(17			)   ,   requires_grad=_A			)
    __a    :	List[Any]									=						nn.Parameter(torch.ones(3			)   ,   requires_grad=_A			)



   @torch.no_grad()
   def    __magic_name__    ( self   ,   _A   ,   _A			)    ->   Optional[Any]:
    __a    :	Optional[Any]									=						self.vision_model(_A			)[1]  # pooled_output
    __a    :	Any									=						self.visual_projection(_A			)

    # we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16
    __a    :	Union[str, Any]									=						cosine_distance(_A   ,   self.special_care_embeds			).cpu().float().numpy()
    __a    :	str									=						cosine_distance(_A   ,   self.concept_embeds			).cpu().float().numpy()

    __a    :	Tuple									=						[]
    __a    :	str									=						image_embeds.shape[0]
    for i in range(_A			):
     __a    :	Union[str, Any]									=						{'special_scores': {}, 'special_care': [], 'concept_scores': {}, 'bad_concepts': []}

     # increase this value to create a stronger `nfsw` filter
     # at the cost of increasing the possibility of filtering benign images
     __a    :	Dict									=						0.0

     for concept_idx in range(len(special_cos_dist[0]			)			):
      __a    :	Tuple									=						special_cos_dist[i][concept_idx]
      __a    :	Any									=						self.special_care_embeds_weights[concept_idx].item()
      __a    :	Optional[Any]									=						round(concept_cos - concept_threshold + adjustment   ,   3			)
      if result_img["special_scores"][concept_idx] > 0:
       result_img["special_care"].append({concept_idx, result_img['special_scores'][concept_idx]}			)
       __a    :	Dict									=						0.01

     for concept_idx in range(len(cos_dist[0]			)			):
      __a    :	List[Any]									=						cos_dist[i][concept_idx]
      __a    :	Optional[int]									=						self.concept_embeds_weights[concept_idx].item()
      __a    :	List[str]									=						round(concept_cos - concept_threshold + adjustment   ,   3			)
      if result_img["concept_scores"][concept_idx] > 0:
       result_img["bad_concepts"].append(_A			)

     result.append(_A			)

    __a    :	Optional[int]									=						[len(res['bad_concepts']			) > 0 for res in result]

    for idx, has_nsfw_concept in enumerate(_A			):
     if has_nsfw_concept:
      if torch.is_tensor(_A			) or torch.is_tensor(images[0]			):
       __a    :	Optional[Any]									=						torch.zeros_like(images[idx]			)  # black image
      else:
       __a    :	Any									=						np.zeros(images[idx].shape			)  # black image

    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.'			)

    return images, has_nsfw_concepts



   @torch.no_grad()
   def    __magic_name__    ( self   ,   _A   ,   _A			)    ->   Dict:
    __a    :	Union[str, Any]									=						self.vision_model(_A			)[1]  # pooled_output
    __a    :	Optional[Any]									=						self.visual_projection(_A			)

    __a    :	Dict									=						cosine_distance(_A   ,   self.special_care_embeds			)
    __a    :	str									=						cosine_distance(_A   ,   self.concept_embeds			)

    # increase this value to create a stronger `nsfw` filter
    # at the cost of increasing the possibility of filtering benign images
    __a    :	List[Any]									=						0.0

    __a    :	int									=						special_cos_dist - self.special_care_embeds_weights + adjustment
    # special_scores = special_scores.round(decimals=3)
    __a    :	List[Any]									=						torch.any(special_scores > 0   ,   dim=1			)
    __a    :	str									=						special_care * 0.01
    __a    :	Dict									=						special_adjustment.unsqueeze(1			).expand(-1   ,   cos_dist.shape[1]			)

    __a    :	Any									=						(cos_dist - self.concept_embeds_weights) + special_adjustment
    # concept_scores = concept_scores.round(decimals=3)
    __a    :	str									=						torch.any(concept_scores > 0   ,   dim=1			)

    __a    :	str									=						0.0  # black image

    return images, has_nsfw_concepts












'''simple docstring'''


import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin


if is_torch_available():
  import torch

if is_vision_available():
  from PIL import Image

  from transformers import TvltImageProcessor






def   lowerCAmelCase__			(					SCREAMING_SNAKE_CASE__       ,		SCREAMING_SNAKE_CASE__=10       ,		SCREAMING_SNAKE_CASE__=10       ,		SCREAMING_SNAKE_CASE__=False       ,		SCREAMING_SNAKE_CASE__=False		):
 __a    :	Dict									=						[]
 for i in range(image_processor_tester.num_frames		):
  video.append(np.random.randint(255       ,		size=(image_processor_tester.num_channels, width, height)       ,		dtype=np.uinta		)		)

 if not numpify and not torchify:
  # PIL expects the channel dimension as last dimension
  __a    :	List[Any]									=						[Image.fromarray(np.moveaxis(SCREAMING_SNAKE_CASE__       ,		0       ,		-1		)		) for frame in video]

 if torchify:
  __a    :	List[Any]									=						[torch.from_numpy(SCREAMING_SNAKE_CASE__		) for frame in video]

 return video






def   lowerCAmelCase__			(					SCREAMING_SNAKE_CASE__       ,		SCREAMING_SNAKE_CASE__=False       ,		SCREAMING_SNAKE_CASE__=False       ,		SCREAMING_SNAKE_CASE__=False		):
 assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

 __a    :	Optional[int]									=						[]
 for i in range(image_processor_tester.batch_size		):
  if equal_resolution:
   __a    :	Dict									=						image_processor_tester.max_resolution
  else:
   __a			,    __a    :	Union[str, Any]									=						np.random.choice(
       np.arange(image_processor_tester.min_resolution       ,		image_processor_tester.max_resolution		)       ,		2		)
   __a    :	Any									=						prepare_video(
       image_processor_tester=SCREAMING_SNAKE_CASE__       ,		width=SCREAMING_SNAKE_CASE__       ,		height=SCREAMING_SNAKE_CASE__       ,		numpify=SCREAMING_SNAKE_CASE__       ,		torchify=SCREAMING_SNAKE_CASE__       ,		)
  video_inputs.append(SCREAMING_SNAKE_CASE__		)

 return video_inputs




class 							lowerCAmelCase					(							unittest.TestCase   ):







   """simple docstring"""







   def __init__( self   ,   _A   ,   _A=7   ,   _A=3   ,   _A=4   ,   _A=18   ,   _A=30   ,   _A=400   ,   _A=True   ,   _A=None   ,   _A=True   ,   _A=[0.5, 0.5, 0.5]   ,   _A=[0.5, 0.5, 0.5]   ,   _A=True   ,   _A=None   ,   )    ->   str:
    __a    :	Any									=						size if size is not None else {'shortest_edge': 18}
    __a    :	str									=						crop_size if crop_size is not None else {'height': 18, 'width': 18}

    __a    :	Any									=						parent
    __a    :	Tuple									=						batch_size
    __a    :	Any									=						num_channels
    __a    :	Union[str, Any]									=						num_frames
    __a    :	Optional[Any]									=						image_size
    __a    :	Any									=						min_resolution
    __a    :	List[str]									=						max_resolution
    __a    :	Any									=						do_resize
    __a    :	Dict									=						size
    __a    :	Optional[int]									=						do_normalize
    __a    :	Union[str, Any]									=						image_mean
    __a    :	List[Any]									=						image_std
    __a    :	Dict									=						do_center_crop
    __a    :	List[Any]									=						crop_size



   def    __magic_name__    ( self			)    ->   Any:
    return {
        "image_mean": self.image_mean,
        "image_std": self.image_std,
        "do_normalize": self.do_normalize,
        "do_resize": self.do_resize,
        "size": self.size,
        "do_center_crop": self.do_center_crop,
        "crop_size": self.crop_size,
    }




@require_torch
@require_vision
class 							lowerCAmelCase					(							lowerCamelCase__							,      unittest.TestCase   ):







   """simple docstring"""







   _A						=   TvltImageProcessor if is_vision_available() else None



   def    __magic_name__    ( self			)    ->   int:
    __a    :	List[Any]									=						TvltImageProcessorTester(self			)



   @property
   def    __magic_name__    ( self			)    ->   Optional[Any]:
    return self.image_processor_tester.prepare_image_processor_dict()



   def    __magic_name__    ( self			)    ->   Optional[int]:
    __a    :	Any									=						self.image_processing_class(**self.image_processor_dict			)
    self.assertTrue(hasattr(_A   ,   'image_mean'			)			)
    self.assertTrue(hasattr(_A   ,   'image_std'			)			)
    self.assertTrue(hasattr(_A   ,   'do_normalize'			)			)
    self.assertTrue(hasattr(_A   ,   'do_resize'			)			)
    self.assertTrue(hasattr(_A   ,   'do_center_crop'			)			)
    self.assertTrue(hasattr(_A   ,   'size'			)			)



   def    __magic_name__    ( self			)    ->   str:
    # Initialize image_processor
    __a    :	str									=						self.image_processing_class(**self.image_processor_dict			)
    # create random PIL videos
    __a    :	Optional[Any]									=						prepare_video_inputs(self.image_processor_tester   ,   equal_resolution=_A			)
    for video in video_inputs:
     self.assertIsInstance(_A   ,   _A			)
     self.assertIsInstance(video[0]   ,   Image.Image			)

    # Test not batched input
    __a    :	Tuple									=						image_processor(video_inputs[0]   ,   return_tensors='pt'			).pixel_values
    self.assertEqual(
        encoded_videos.shape   ,   (
            1,
            self.image_processor_tester.num_frames,
            self.image_processor_tester.num_channels,
            self.image_processor_tester.crop_size['height'],
            self.image_processor_tester.crop_size['width'],
        )   ,   )

    # Test batched
    __a    :	Dict									=						image_processor(_A   ,   return_tensors='pt'			).pixel_values
    self.assertEqual(
        encoded_videos.shape   ,   (
            self.image_processor_tester.batch_size,
            self.image_processor_tester.num_frames,
            self.image_processor_tester.num_channels,
            self.image_processor_tester.crop_size['height'],
            self.image_processor_tester.crop_size['width'],
        )   ,   )



   def    __magic_name__    ( self			)    ->   Any:
    # Initialize image_processor
    __a    :	Union[str, Any]									=						self.image_processing_class(**self.image_processor_dict			)
    # create random numpy tensors
    __a    :	List[str]									=						prepare_video_inputs(self.image_processor_tester   ,   equal_resolution=_A   ,   numpify=_A			)
    for video in video_inputs:
     self.assertIsInstance(_A   ,   _A			)
     self.assertIsInstance(video[0]   ,   np.ndarray			)

    # Test not batched input
    __a    :	List[str]									=						image_processor(video_inputs[0]   ,   return_tensors='pt'			).pixel_values
    self.assertEqual(
        encoded_videos.shape   ,   (
            1,
            self.image_processor_tester.num_frames,
            self.image_processor_tester.num_channels,
            self.image_processor_tester.crop_size['height'],
            self.image_processor_tester.crop_size['width'],
        )   ,   )

    # Test batched
    __a    :	Dict									=						image_processor(_A   ,   return_tensors='pt'			).pixel_values
    self.assertEqual(
        encoded_videos.shape   ,   (
            self.image_processor_tester.batch_size,
            self.image_processor_tester.num_frames,
            self.image_processor_tester.num_channels,
            self.image_processor_tester.crop_size['height'],
            self.image_processor_tester.crop_size['width'],
        )   ,   )



   def    __magic_name__    ( self			)    ->   Union[str, Any]:
    # Initialize image_processor
    __a    :	Optional[Any]									=						self.image_processing_class(**self.image_processor_dict			)
    # create random PyTorch tensors
    __a    :	Optional[Any]									=						prepare_video_inputs(self.image_processor_tester   ,   equal_resolution=_A   ,   torchify=_A			)
    for video in video_inputs:
     self.assertIsInstance(_A   ,   _A			)
     self.assertIsInstance(video[0]   ,   torch.Tensor			)

    # Test not batched input
    __a    :	Dict									=						image_processor(video_inputs[0]   ,   return_tensors='pt'			).pixel_values
    self.assertEqual(
        encoded_videos.shape   ,   (
            1,
            self.image_processor_tester.num_frames,
            self.image_processor_tester.num_channels,
            self.image_processor_tester.crop_size['height'],
            self.image_processor_tester.crop_size['width'],
        )   ,   )

    # Test batched
    __a    :	Tuple									=						image_processor(_A   ,   return_tensors='pt'			).pixel_values
    self.assertEqual(
        encoded_videos.shape   ,   (
            self.image_processor_tester.batch_size,
            self.image_processor_tester.num_frames,
            self.image_processor_tester.num_channels,
            self.image_processor_tester.crop_size['height'],
            self.image_processor_tester.crop_size['width'],
        )   ,   )




