



from typing import List, Union

import numpy as np

from ..tokenization_utils import TruncationStrategy
from ..utils import add_end_docstrings, logging
from .base import PIPELINE_INIT_ARGS, ArgumentHandler, ChunkPipeline


_lowercase     							=						logging.get_logger(__name__)
class 				__A					( A_      ):





			def        _snake_case    (self							,    __magic_name__				):
						if isinstance(__magic_name__							,    __magic_name__				):
									lowerCamelCase__ :      Any								=		[label.strip() for label in labels.split(""","""				) if label.strip()]
						return labels





			def __call__(self							,    __magic_name__							,    __magic_name__							,    __magic_name__				):
						if len(__magic_name__				) == 0 or len(__magic_name__				) == 0:
									raise ValueError("""You must include at least one label and at least one sequence."""				)
						if hypothesis_template.format(labels[0]				) == hypothesis_template:
									raise ValueError(
									    (
									        """The provided hypothesis_template \"{}\" was not able to be formatted with the target labels. """
									        """Make sure the passed template includes formatting syntax such as {{}} where the label should go."""
									    ).format(__magic_name__				)				)

						if isinstance(__magic_name__							,    __magic_name__				):
									lowerCamelCase__ :      str								=		[sequences]

						lowerCamelCase__ :      Optional[int]								=		[]
						for sequence in sequences:
									sequence_pairs.extend([[sequence, hypothesis_template.format(__magic_name__				)] for label in labels]				)

						return sequence_pairs, sequences







@add_end_docstrings(A_      )
class 				__A					( A_      ):





			def __init__(self							,    __magic_name__=ZeroShotClassificationArgumentHandler()							,    *__magic_name__							,    **__magic_name__				):
						lowerCamelCase__ :      Union[str, Any]								=		args_parser
						super().__init__(*__magic_name__							,    **__magic_name__				)
						if self.entailment_id == -1:
									logger.warning(
									    """Failed to determine 'entailment' label id from the label2id mapping in the model config. Setting to """
									    """-1. Define a descriptive label2id mapping in the model config to ensure correct outputs."""				)





			@property
			def        _snake_case    (self				):
						for label, ind in self.model.config.labelaid.items():
									if label.lower().startswith("""entail"""				):
												return ind
						return -1





			def        _snake_case    (self							,    __magic_name__							,    __magic_name__=True							,    __magic_name__=True							,    __magic_name__=TruncationStrategy.ONLY_FIRST							,    **__magic_name__				):
						lowerCamelCase__ :      Dict								=		self.framework
						if self.tokenizer.pad_token is None:
									# Override for tokenizers not supporting padding
									logger.error(
									    """Tokenizer was not supporting padding necessary for zero-shot, attempting to use """
									    """ `pad_token=eos_token`"""				)
									lowerCamelCase__ :      str								=		self.tokenizer.eos_token
						try:
									lowerCamelCase__ :      List[Any]								=		self.tokenizer(
									    __magic_name__							,    add_special_tokens=__magic_name__							,    return_tensors=__magic_name__							,    padding=__magic_name__							,    truncation=__magic_name__							,    )
						except Exception as e:
									if "too short" in str(__magic_name__				):
												# tokenizers might yell that we want to truncate
												# to a value that is not even reached by the input.
												# In that case we don't want to truncate.
												# It seems there's not a really better way to catch that
												# exception.

												lowerCamelCase__ :      Dict								=		self.tokenizer(
												    __magic_name__							,    add_special_tokens=__magic_name__							,    return_tensors=__magic_name__							,    padding=__magic_name__							,    truncation=TruncationStrategy.DO_NOT_TRUNCATE							,    )
									else:
												raise e

						return inputs





			def        _snake_case    (self							,    **__magic_name__				):
						if kwargs.get("""multi_class"""							,    __magic_name__				) is not None:
									lowerCamelCase__ :      int								=		kwargs["""multi_class"""]
									logger.warning(
									    """The `multi_class` argument has been deprecated and renamed to `multi_label`. """
									    """`multi_class` will be removed in a future version of Transformers."""				)
						lowerCamelCase__ :      int								=		{}
						if "candidate_labels" in kwargs:
									lowerCamelCase__ :      List[Any]								=		self._args_parser._parse_labels(kwargs["""candidate_labels"""]				)
						if "hypothesis_template" in kwargs:
									lowerCamelCase__ :      Any								=		kwargs["""hypothesis_template"""]

						lowerCamelCase__ :      List[Any]								=		{}
						if "multi_label" in kwargs:
									lowerCamelCase__ :      Any								=		kwargs["""multi_label"""]
						return preprocess_params, {}, postprocess_params





			def __call__(self							,    __magic_name__							,    *__magic_name__							,    **__magic_name__							,    ):
						if len(__magic_name__				) == 0:
									pass
						elif len(__magic_name__				) == 1 and "candidate_labels" not in kwargs:
									lowerCamelCase__ :      Union[str, Any]								=		args[0]
						else:
									raise ValueError(f"Unable to understand extra arguments {args}"				)

						return super().__call__(__magic_name__							,    **__magic_name__				)





			def        _snake_case    (self							,    __magic_name__							,    __magic_name__=None							,    __magic_name__="This example is {}."				):
						lowerCamelCase__							,lowerCamelCase__ :      Optional[Any]								=		self._args_parser(__magic_name__							,    __magic_name__							,    __magic_name__				)

						for i, (candidate_label, sequence_pair) in enumerate(zip(__magic_name__							,    __magic_name__				)				):
									lowerCamelCase__ :      Union[str, Any]								=		self._parse_and_tokenize([sequence_pair]				)

									yield {
									    "candidate_label": candidate_label,
									    "sequence": sequences[0],
									    "is_last": i == len(__magic_name__				) - 1,
									    **model_input,
									}





			def        _snake_case    (self							,    __magic_name__				):
						lowerCamelCase__ :      int								=		inputs["""candidate_label"""]
						lowerCamelCase__ :      int								=		inputs["""sequence"""]
						lowerCamelCase__ :      str								=		{k: inputs[k] for k in self.tokenizer.model_input_names}
						lowerCamelCase__ :      Optional[int]								=		self.model(**__magic_name__				)

						lowerCamelCase__ :      str								=		{
						    """candidate_label""": candidate_label,
						    """sequence""": sequence,
						    """is_last""": inputs["""is_last"""],
						    **outputs,
						}
						return model_outputs





			def        _snake_case    (self							,    __magic_name__							,    __magic_name__=False				):
						lowerCamelCase__ :      Union[str, Any]								=		[outputs["""candidate_label"""] for outputs in model_outputs]
						lowerCamelCase__ :      Optional[int]								=		[outputs["""sequence"""] for outputs in model_outputs]
						lowerCamelCase__ :      Any								=		np.concatenate([output["""logits"""].numpy() for output in model_outputs]				)
						lowerCamelCase__ :      List[Any]								=		logits.shape[0]
						lowerCamelCase__ :      Union[str, Any]								=		len(__magic_name__				)
						lowerCamelCase__ :      int								=		N // n
						lowerCamelCase__ :      int								=		logits.reshape((num_sequences, n, -1)				)

						if multi_label or len(__magic_name__				) == 1:
									# softmax over the entailment vs. contradiction dim for each label independently
									lowerCamelCase__ :      int								=		self.entailment_id
									lowerCamelCase__ :      str								=		-1 if entailment_id == 0 else 0
									lowerCamelCase__ :      int								=		reshaped_outputs[..., [contradiction_id, entailment_id]]
									lowerCamelCase__ :      Optional[int]								=		np.exp(__magic_name__				) / np.exp(__magic_name__				).sum(-1							,    keepdims=__magic_name__				)
									lowerCamelCase__ :      Tuple								=		scores[..., 1]
						else:
									# softmax the "entailment" logits over all candidate labels
									lowerCamelCase__ :      Dict								=		reshaped_outputs[..., self.entailment_id]
									lowerCamelCase__ :      str								=		np.exp(__magic_name__				) / np.exp(__magic_name__				).sum(-1							,    keepdims=__magic_name__				)

						lowerCamelCase__ :      int								=		list(reversed(scores[0].argsort()				)				)
						return {
						    "sequence": sequences[0],
						    "labels": [candidate_labels[i] for i in top_inds],
						    "scores": scores[0, top_inds].tolist(),
						}








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

from ...utils import logging


_lowercase     							=						logging.get_logger(__name__)






def      _A				(UpperCamelCase		:      Dict      ,			UpperCamelCase		:      Optional[int] )  ->Tuple:



			'''simple docstring'''







			lowerCamelCase__ :      List[str]								=		nn.functional.normalize(UpperCamelCase )
			lowerCamelCase__ :      Tuple								=		nn.functional.normalize(UpperCamelCase )
			return torch.mm(UpperCamelCase      ,			normalized_text_embeds.t() )
class 				__A					( A_      ):
			UpperCamelCase					:Optional[Any]     =			CLIPConfig

			UpperCamelCase					:Optional[Any]     =			['''CLIPEncoderLayer''']





			def __init__(self							,    __magic_name__				):
						super().__init__(__magic_name__				)

						lowerCamelCase__ :      Union[str, Any]								=		CLIPVisionModel(config.vision_config				)
						lowerCamelCase__ :      Any								=		nn.Linear(config.vision_config.hidden_size							,    config.projection_dim							,    bias=__magic_name__				)

						lowerCamelCase__ :      Any								=		nn.Parameter(torch.ones(17							,    config.projection_dim				)							,    requires_grad=__magic_name__				)
						lowerCamelCase__ :      Union[str, Any]								=		nn.Parameter(torch.ones(3							,    config.projection_dim				)							,    requires_grad=__magic_name__				)

						lowerCamelCase__ :      Any								=		nn.Parameter(torch.ones(17				)							,    requires_grad=__magic_name__				)
						lowerCamelCase__ :      Optional[Any]								=		nn.Parameter(torch.ones(3				)							,    requires_grad=__magic_name__				)





			@torch.no_grad()
			def        _snake_case    (self							,    __magic_name__							,    __magic_name__				):
						lowerCamelCase__ :      Any								=		self.vision_model(__magic_name__				)[1]  # pooled_output
						lowerCamelCase__ :      Tuple								=		self.visual_projection(__magic_name__				)

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

						lowerCamelCase__ :      int								=		[]
						lowerCamelCase__ :      str								=		image_embeds.shape[0]
						for i in range(__magic_name__				):
									lowerCamelCase__ :      str								=		{"""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
									lowerCamelCase__ :      Optional[Any]								=		0.0

									for concept_idx in range(len(special_cos_dist[0]				)				):
												lowerCamelCase__ :      List[str]								=		special_cos_dist[i][concept_idx]
												lowerCamelCase__ :      Tuple								=		self.special_care_embeds_weights[concept_idx].item()
												lowerCamelCase__ :      str								=		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]}				)
															lowerCamelCase__ :      List[str]								=		0.01

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

									result.append(__magic_name__				)

						lowerCamelCase__ :      Tuple								=		[len(res["""bad_concepts"""]				) > 0 for res in result]

						return images, has_nsfw_concepts





			@torch.no_grad()
			def        _snake_case    (self							,    __magic_name__							,    __magic_name__				):
						lowerCamelCase__ :      Union[str, Any]								=		self.vision_model(__magic_name__				)[1]  # pooled_output
						lowerCamelCase__ :      str								=		self.visual_projection(__magic_name__				)

						lowerCamelCase__ :      Optional[Any]								=		cosine_distance(__magic_name__							,    self.special_care_embeds				)
						lowerCamelCase__ :      Dict								=		cosine_distance(__magic_name__							,    self.concept_embeds				)

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

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

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

						return images, has_nsfw_concepts



