



"""simple docstring"""





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

lowercase  							=					logging.get_logger(__name__)




@add_end_docstrings(A    )
class 			lowercase__			(      A    ):
							'''simple docstring'''







							def __init__(  self	,       **snake_case						)			->							List[Any]:
									super().__init__(**snake_case						)

									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	,       snake_case	,       **snake_case						)			->							Tuple:
									return super().__call__(snake_case	,       **snake_case						)






							def        lowerCamelCase_   (  self	,       **snake_case						)			->							str:
									_UpperCAmelCase							 =		{}
									if "candidate_labels" in kwargs:
											_UpperCAmelCase							 =		kwargs['candidate_labels']
									if "hypothesis_template" in kwargs:
											_UpperCAmelCase							 =		kwargs['hypothesis_template']

									return preprocess_params, {}, {}






							def        lowerCamelCase_   (  self	,       snake_case	,       snake_case=None	,       snake_case="This is a photo of {}."						)			->							int:
									_UpperCAmelCase							 =		load_image(snake_case						)
									_UpperCAmelCase							 =		self.image_processor(images=[image]	,       return_tensors=self.framework						)
									_UpperCAmelCase							 =		candidate_labels
									_UpperCAmelCase							 =		[hypothesis_template.format(snake_case						) for x in candidate_labels]
									_UpperCAmelCase							 =		self.tokenizer(snake_case	,       return_tensors=self.framework	,       padding=snake_case						)
									_UpperCAmelCase							 =		[text_inputs]
									return inputs






							def        lowerCamelCase_   (  self	,       snake_case						)			->							Optional[int]:
									_UpperCAmelCase							 =		model_inputs.pop('candidate_labels'						)
									_UpperCAmelCase							 =		model_inputs.pop('text_inputs'						)
									if isinstance(text_inputs[0]	,       snake_case						):
											_UpperCAmelCase							 =		text_inputs[0]
									else:
											# Batching case.
											_UpperCAmelCase							 =		text_inputs[0][0]

									_UpperCAmelCase							 =		self.model(**snake_case	,       **snake_case						)

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






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

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







"""simple docstring"""





from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available


lowercase  							=					{
    '''configuration_mvp''': ['''MVP_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''MvpConfig''', '''MvpOnnxConfig'''],
    '''tokenization_mvp''': ['''MvpTokenizer'''],
}

try:
					if not is_tokenizers_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					lowercase  							=					['''MvpTokenizerFast''']

try:
					if not is_torch_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					lowercase  							=					[
					    '''MVP_PRETRAINED_MODEL_ARCHIVE_LIST''',
					    '''MvpForCausalLM''',
					    '''MvpForConditionalGeneration''',
					    '''MvpForQuestionAnswering''',
					    '''MvpForSequenceClassification''',
					    '''MvpModel''',
					    '''MvpPreTrainedModel''',
					]

if TYPE_CHECKING:
					from .configuration_mvp import MVP_PRETRAINED_CONFIG_ARCHIVE_MAP, MvpConfig, MvpOnnxConfig
					from .tokenization_mvp import MvpTokenizer

					try:
										if not is_tokenizers_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .tokenization_mvp_fast import MvpTokenizerFast

					try:
										if not is_torch_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .modeling_mvp import (
										    MVP_PRETRAINED_MODEL_ARCHIVE_LIST,
										    MvpForCausalLM,
										    MvpForConditionalGeneration,
										    MvpForQuestionAnswering,
										    MvpForSequenceClassification,
										    MvpModel,
										    MvpPreTrainedModel,
										)

else:
					import sys

					lowercase  							=					_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)


