




from collections import UserDict
from typing import List, Union

from ..utils import (
    add_end_docstrings,
    is_tf_available,
    is_torch_available,
    is_vision_available,
    logging,
    requires_backends,
)
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_vision_available():
			from PIL import Image

			from ..image_utils import load_image

if is_torch_available():
			from ..models.auto.modeling_auto import MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING

if is_tf_available():
			from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING
			from ..tf_utils import stable_softmax

UpperCAmelCase__				     = logging.get_logger(__name__)







@add_end_docstrings(__SCREAMING_SNAKE_CASE  )
class a (     __SCREAMING_SNAKE_CASE  ):
						"""simple docstring"""

						def __init__( self :     int				,							**lowerCamelCase__ :     str				)    ->						Optional[int]:
													"""simple docstring"""


													super().__init__(**lowerCamelCase__				)

													requires_backends(self				,							'''vision'''				)
													self.check_model_type(
													    TF_MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING
													    if self.framework == '''tf'''
													    else MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING				)






						def __call__( self :     str				,							lowerCamelCase__ :     Union[str, List[str], "Image", List["Image"]]				,							**lowerCamelCase__ :     Optional[Any]				)    ->						Optional[Any]:
													"""simple docstring"""


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






						def     UpperCAmelCase_      ( self :     Optional[int]				,							**lowerCamelCase__ :     Optional[Any]				)    ->						Optional[Any]:
													"""simple docstring"""


													__lowercase      =    {}
													if "candidate_labels" in kwargs:
																				__lowercase      =    kwargs['''candidate_labels''']
													if "hypothesis_template" in kwargs:
																				__lowercase      =    kwargs['''hypothesis_template''']

													return preprocess_params, {}, {}






						def     UpperCAmelCase_      ( self :     List[Any]				,							lowerCamelCase__ :     List[str]				,							lowerCamelCase__ :     int=None				,							lowerCamelCase__ :     Union[str, Any]="This is a photo of {}."				)    ->						List[str]:
													"""simple docstring"""


													__lowercase      =    load_image(lowerCamelCase__				)
													__lowercase      =    self.image_processor(images=[image]				,							return_tensors=self.framework				)
													__lowercase      =    candidate_labels
													__lowercase      =    [hypothesis_template.format(lowerCamelCase__				) for x in candidate_labels]
													__lowercase      =    self.tokenizer(lowerCamelCase__				,							return_tensors=self.framework				,							padding=lowerCamelCase__				)
													__lowercase      =    [text_inputs]
													return inputs






						def     UpperCAmelCase_      ( self :     Dict				,							lowerCamelCase__ :     Optional[Any]				)    ->						int:
													"""simple docstring"""


													__lowercase      =    model_inputs.pop('''candidate_labels'''				)
													__lowercase      =    model_inputs.pop('''text_inputs'''				)
													if isinstance(text_inputs[0]				,							lowerCamelCase__				):
																				__lowercase      =    text_inputs[0]
													else:
																				# Batching case.
																				__lowercase      =    text_inputs[0][0]

													__lowercase      =    self.model(**lowerCamelCase__				,							**lowerCamelCase__				)

													__lowercase      =    {
													    '''candidate_labels''': candidate_labels,
													    '''logits''': outputs.logits_per_image,
													}
													return model_outputs






						def     UpperCAmelCase_      ( self :     Union[str, Any]				,							lowerCamelCase__ :     List[Any]				)    ->						str:
													"""simple docstring"""


													__lowercase      =    model_outputs.pop('''candidate_labels'''				)
													__lowercase      =    model_outputs['''logits'''][0]
													if self.framework == "pt":
																				__lowercase      =    logits.softmax(dim=-1				).squeeze(-1				)
																				__lowercase      =    probs.tolist()
																				if not isinstance(lowerCamelCase__				,							lowerCamelCase__				):
																											__lowercase      =    [scores]
													elif self.framework == "tf":
																				__lowercase      =    stable_softmax(lowerCamelCase__				,							axis=-1				)
																				__lowercase      =    probs.numpy().tolist()
													else:
																				raise ValueError(f'Unsupported framework: {self.framework}'				)

													__lowercase      =    [
													    {'''score''': score, '''label''': candidate_label}
													    for score, candidate_label in sorted(zip(lowerCamelCase__				,							lowerCamelCase__				)				,							key=lambda lowerCamelCase__				: -x[0]				)
													]
													return result












import argparse


UpperCAmelCase__				     = "docs/source/_static/js/custom.js"







def 		_A(       UpperCamelCase__							:			Optional[int]				)    -> Optional[int]:
							'''simple docstring'''


							with open(UpperCamelCase__				,       encoding='''utf-8'''				,       newline='''\n'''				) as f:
														__lowercase      =    f.readlines()
							__lowercase      =    0

							# First let's put the right version
							while not lines[index].startswith('''const stableVersion ='''				):
														index += 1
							__lowercase      =    F'const stableVersion = "v{version}"\n'

							# Then update the dictionary
							while not lines[index].startswith('''const versionMapping = {'''				):
														index += 1

							# We go until the end
							while not lines[index].startswith('''}'''				):
														index += 1
							# We add the new version at the end
							lines[index - 1] += F'    "v{version}": "v{version}",\n'

							with open(UpperCamelCase__				,       '''w'''				,       encoding='''utf-8'''				,       newline='''\n'''				) as f:
														f.writelines(UpperCamelCase__				)


if __name__ == "__main__":
			UpperCAmelCase__				     = argparse.ArgumentParser()
			parser.add_argument("--version", help="Release version.")
			UpperCAmelCase__				     = parser.parse_args()
			update_custom_js(args.version)






