



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(SCREAMING_SNAKE_CASE					)
class     __a			(  SCREAMING_SNAKE_CASE					):



	def __init__(    self				:     Any					,						**snake_case_				:     str)->				Union[str, 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				:     Any					,						snake_case_				:     Union[str, List[str], "Image", List["Image"]]					,						**snake_case_				:     Any)->				Optional[int]:
						return super().__call__(snake_case_					,						**snake_case_)



	def 		UpperCamelCase				(    self				:     List[Any]					,						**snake_case_				:     str)->				Dict:
						__lowerCAmelCase						 ={}
						if "candidate_labels" in kwargs:
											__lowerCAmelCase						 =kwargs["""candidate_labels"""]
						if "hypothesis_template" in kwargs:
											__lowerCAmelCase						 =kwargs["""hypothesis_template"""]

						return preprocess_params, {}, {}



	def 		UpperCamelCase				(    self				:     Dict					,						snake_case_				:     List[Any]					,						snake_case_				:     int=None					,						snake_case_				:     Union[str, Any]="This is a photo of {}.")->				List[str]:
						__lowerCAmelCase						 =load_image(snake_case_)
						__lowerCAmelCase						 =self.image_processor(images=[image]					,						return_tensors=self.framework)
						__lowerCAmelCase						 =candidate_labels
						__lowerCAmelCase						 =[hypothesis_template.format(snake_case_) for x in candidate_labels]
						__lowerCAmelCase						 =self.tokenizer(snake_case_					,						return_tensors=self.framework					,						padding=snake_case_)
						__lowerCAmelCase						 =[text_inputs]
						return inputs



	def 		UpperCamelCase				(    self				:     List[str]					,						snake_case_				:     str)->				Dict:
						__lowerCAmelCase						 =model_inputs.pop("""candidate_labels""")
						__lowerCAmelCase						 =model_inputs.pop("""text_inputs""")
						if isinstance(text_inputs[0]					,						snake_case_):
											__lowerCAmelCase						 =text_inputs[0]
						else:
											# Batching case.
											__lowerCAmelCase						 =text_inputs[0][0]

						__lowerCAmelCase						 =self.model(**snake_case_					,						**snake_case_)

						__lowerCAmelCase						 ={
						    """candidate_labels""": candidate_labels,
						    """logits""": outputs.logits_per_image,
						}
						return model_outputs



	def 		UpperCamelCase				(    self				:     Union[str, Any]					,						snake_case_				:     List[str])->				Optional[Any]:
						__lowerCAmelCase						 =model_outputs.pop("""candidate_labels""")
						__lowerCAmelCase						 =model_outputs["""logits"""][0]
						if self.framework == "pt":
											__lowerCAmelCase						 =logits.softmax(dim=-1).squeeze(-1)
											__lowerCAmelCase						 =probs.tolist()
											if not isinstance(snake_case_					,						snake_case_):
																__lowerCAmelCase						 =[scores]
						elif self.framework == "tf":
											__lowerCAmelCase						 =stable_softmax(snake_case_					,						axis=-1)
											__lowerCAmelCase						 =probs.numpy().tolist()
						else:
											raise ValueError(F"""Unsupported framework: {self.framework}""")

						__lowerCAmelCase						 =[
						    {"""score""": score, """label""": candidate_label}
						    for score, candidate_label in sorted(zip(snake_case_					,						snake_case_)					,						key=lambda snake_case_: -x[0])
						]
						return result









from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_sentencepiece_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


lowercase_													=      {
    '''configuration_albert''': ['''ALBERT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''AlbertConfig''', '''AlbertOnnxConfig'''],
}

try:
				if not is_sentencepiece_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				lowercase_													=      ['''AlbertTokenizer''']

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

try:
				if not is_torch_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				lowercase_													=      [
				    '''ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST''',
				    '''AlbertForMaskedLM''',
				    '''AlbertForMultipleChoice''',
				    '''AlbertForPreTraining''',
				    '''AlbertForQuestionAnswering''',
				    '''AlbertForSequenceClassification''',
				    '''AlbertForTokenClassification''',
				    '''AlbertModel''',
				    '''AlbertPreTrainedModel''',
				    '''load_tf_weights_in_albert''',
				]

try:
				if not is_tf_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				lowercase_													=      [
				    '''TF_ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST''',
				    '''TFAlbertForMaskedLM''',
				    '''TFAlbertForMultipleChoice''',
				    '''TFAlbertForPreTraining''',
				    '''TFAlbertForQuestionAnswering''',
				    '''TFAlbertForSequenceClassification''',
				    '''TFAlbertForTokenClassification''',
				    '''TFAlbertMainLayer''',
				    '''TFAlbertModel''',
				    '''TFAlbertPreTrainedModel''',
				]

try:
				if not is_flax_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				lowercase_													=      [
				    '''FlaxAlbertForMaskedLM''',
				    '''FlaxAlbertForMultipleChoice''',
				    '''FlaxAlbertForPreTraining''',
				    '''FlaxAlbertForQuestionAnswering''',
				    '''FlaxAlbertForSequenceClassification''',
				    '''FlaxAlbertForTokenClassification''',
				    '''FlaxAlbertModel''',
				    '''FlaxAlbertPreTrainedModel''',
				]

if TYPE_CHECKING:
				from .configuration_albert import ALBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, AlbertConfig, AlbertOnnxConfig

				try:
								if not is_sentencepiece_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .tokenization_albert import AlbertTokenizer

				try:
								if not is_tokenizers_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .tokenization_albert_fast import AlbertTokenizerFast

				try:
								if not is_torch_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .modeling_albert import (
								    ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
								    AlbertForMaskedLM,
								    AlbertForMultipleChoice,
								    AlbertForPreTraining,
								    AlbertForQuestionAnswering,
								    AlbertForSequenceClassification,
								    AlbertForTokenClassification,
								    AlbertModel,
								    AlbertPreTrainedModel,
								    load_tf_weights_in_albert,
								)

				try:
								if not is_tf_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .modeling_tf_albert import (
								    TF_ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
								    TFAlbertForMaskedLM,
								    TFAlbertForMultipleChoice,
								    TFAlbertForPreTraining,
								    TFAlbertForQuestionAnswering,
								    TFAlbertForSequenceClassification,
								    TFAlbertForTokenClassification,
								    TFAlbertMainLayer,
								    TFAlbertModel,
								    TFAlbertPreTrainedModel,
								)

				try:
								if not is_flax_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .modeling_flax_albert import (
								    FlaxAlbertForMaskedLM,
								    FlaxAlbertForMultipleChoice,
								    FlaxAlbertForPreTraining,
								    FlaxAlbertForQuestionAnswering,
								    FlaxAlbertForSequenceClassification,
								    FlaxAlbertForTokenClassification,
								    FlaxAlbertModel,
								    FlaxAlbertPreTrainedModel,
								)
else:
				import sys

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




