


'''simple docstring'''

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_tf_available():
	import tensorflow as tf

	from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING
	from ..tf_utils import stable_softmax

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

_UpperCAmelCase :       Optional[Any]										= logging.get_logger(__name__)


@add_end_docstrings(__A					)
class 					a__ (       __A					):







				"""simple docstring"""



				def __init__(self				,	*__lowercase				,	**__lowercase						):
										super().__init__(*SCREAMING_SNAKE_CASE__				,	**SCREAMING_SNAKE_CASE__						)
										requires_backends(self				,	'''vision'''						)
										self.check_model_type(
										    TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING
										    if self.framework == '''tf'''
										    else MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING						)




				def     _snake_case     (self				,	__lowercase=None						):
										__lowerCAmelCase											= {}
										if top_k is not None:
																__lowerCAmelCase											= top_k
										return {}, {}, postprocess_params




				def __call__(self				,	__lowercase				,	**__lowercase						):
										return super().__call__(SCREAMING_SNAKE_CASE__				,	**SCREAMING_SNAKE_CASE__						)




				def     _snake_case     (self				,	__lowercase						):
										__lowerCAmelCase											= load_image(SCREAMING_SNAKE_CASE__						)
										__lowerCAmelCase											= self.image_processor(images=SCREAMING_SNAKE_CASE__				,	return_tensors=self.framework						)
										return model_inputs




				def     _snake_case     (self				,	__lowercase						):
										__lowerCAmelCase											= self.model(**SCREAMING_SNAKE_CASE__						)
										return model_outputs




				def     _snake_case     (self				,	__lowercase				,	__lowercase=5						):






										if top_k > self.model.config.num_labels:
																__lowerCAmelCase											= self.model.config.num_labels

										if self.framework == "pt":
																__lowerCAmelCase											= model_outputs.logits.softmax(-1						)[0]
																__lowerCAmelCase											= probs.topk(SCREAMING_SNAKE_CASE__						)
										elif self.framework == "tf":
																__lowerCAmelCase											= stable_softmax(model_outputs.logits				,	axis=-1						)[0]
																__lowerCAmelCase											= tf.math.top_k(SCREAMING_SNAKE_CASE__				,	k=SCREAMING_SNAKE_CASE__						)
																__lowerCAmelCase											= topk.values.numpy(), topk.indices.numpy()
										else:
																raise ValueError(F"""Unsupported framework: {self.framework}"""						)

										__lowerCAmelCase											= scores.tolist()
										__lowerCAmelCase											= ids.tolist()
										return [{"score": score, "label": self.model.config.idalabel[_id]} for score, _id in zip(SCREAMING_SNAKE_CASE__				,	SCREAMING_SNAKE_CASE__						)]








"""simple docstring"""
from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


lowerCamelCase_								=		{
    "configuration_blip_2": [
        "BLIP_2_PRETRAINED_CONFIG_ARCHIVE_MAP",
        "Blip2Config",
        "Blip2QFormerConfig",
        "Blip2VisionConfig",
    ],
    "processing_blip_2": ["Blip2Processor"],
}

try:
  if not is_torch_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  lowerCamelCase_								=		[
      "BLIP_2_PRETRAINED_MODEL_ARCHIVE_LIST",
      "Blip2Model",
      "Blip2QFormerModel",
      "Blip2PreTrainedModel",
      "Blip2ForConditionalGeneration",
      "Blip2VisionModel",
  ]

if TYPE_CHECKING:
  from .configuration_blip_a import (
      BLIP_2_PRETRAINED_CONFIG_ARCHIVE_MAP,
      BlipaConfig,
      BlipaQFormerConfig,
      BlipaVisionConfig,
  )
  from .processing_blip_a import BlipaProcessor

  try:
    if not is_torch_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_blip_a import (
        BLIP_2_PRETRAINED_MODEL_ARCHIVE_LIST,
        BlipaForConditionalGeneration,
        BlipaModel,
        BlipaPreTrainedModel,
        BlipaQFormerModel,
        BlipaVisionModel,
    )

else:
  import sys

  lowerCamelCase_								=		_LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)