



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

a_						:     List[str]	     =		logging.get_logger(__name__)





@add_end_docstrings(UpperCAmelCase_)
class    lowerCamelCase__  (						UpperCAmelCase_):
     """simple docstring"""



     def __init__(self				,			*__a				,			**__a    ):






           '''simple docstring'''



           super().__init__(*__a				,			**__a    )
           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 						_a      (self				,			__a=None    ):






           '''simple docstring'''



           lowerCamelCase								=		{}
           if top_k is not None:
                 lowerCamelCase								=		top_k
           return {}, {}, postprocess_params







     def __call__(self				,			__a				,			**__a    ):






           '''simple docstring'''



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







     def 						_a      (self				,			__a    ):






           '''simple docstring'''



           lowerCamelCase								=		load_image(__a    )
           lowerCamelCase								=		self.image_processor(images=__a				,			return_tensors=self.framework    )
           return model_inputs







     def 						_a      (self				,			__a    ):






           '''simple docstring'''



           lowerCamelCase								=		self.model(**__a    )
           return model_outputs







     def 						_a      (self				,			__a				,			__a=5    ):






           '''simple docstring'''



           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			,							lowerCamelCase								=		probs.topk(__a    )
           elif self.framework == "tf":
                 lowerCamelCase								=		stable_softmax(model_outputs.logits				,			axis=-1    )[0]
                 lowerCamelCase								=		tf.math.top_k(__a				,			k=__a    )
                 lowerCamelCase			,							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(__a				,			__a    )]




from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


a_						:     List[str]	     =		{
    'configuration_clipseg': [
        'CLIPSEG_PRETRAINED_CONFIG_ARCHIVE_MAP',
        'CLIPSegConfig',
        'CLIPSegTextConfig',
        'CLIPSegVisionConfig',
    ],
    'processing_clipseg': ['CLIPSegProcessor'],
}

try:
     if not is_torch_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     a_						:     Any	     =		[
         'CLIPSEG_PRETRAINED_MODEL_ARCHIVE_LIST',
         'CLIPSegModel',
         'CLIPSegPreTrainedModel',
         'CLIPSegTextModel',
         'CLIPSegVisionModel',
         'CLIPSegForImageSegmentation',
     ]

if TYPE_CHECKING:
     from .configuration_clipseg import (
         CLIPSEG_PRETRAINED_CONFIG_ARCHIVE_MAP,
         CLIPSegConfig,
         CLIPSegTextConfig,
         CLIPSegVisionConfig,
     )
     from .processing_clipseg import CLIPSegProcessor

     try:
          if not is_torch_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_clipseg import (
              CLIPSEG_PRETRAINED_MODEL_ARCHIVE_LIST,
              CLIPSegForImageSegmentation,
              CLIPSegModel,
              CLIPSegPreTrainedModel,
              CLIPSegTextModel,
              CLIPSegVisionModel,
          )

else:
     import sys

     a_						:     Dict	     =		_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)