



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(__lowercase				)
class 		UpperCAmelCase						(						__lowercase				):





   def __init__(	self       ,     **_lowerCAmelCase			):
         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       ,     _lowerCAmelCase       ,     **_lowerCAmelCase			):
         return super().__call__(_lowerCAmelCase       ,     **_lowerCAmelCase			)





   def 	__lowerCAmelCase							(	self       ,     **_lowerCAmelCase			):
         _lowerCAmelCase 			=  {}
         if "candidate_labels" in kwargs:
               _lowerCAmelCase 			=  kwargs["candidate_labels"]
         if "hypothesis_template" in kwargs:
               _lowerCAmelCase 			=  kwargs["hypothesis_template"]

         return preprocess_params, {}, {}





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





   def 	__lowerCAmelCase							(	self       ,     _lowerCAmelCase			):
         _lowerCAmelCase 			=  model_inputs.pop('''candidate_labels'''			)
         _lowerCAmelCase 			=  model_inputs.pop('''text_inputs'''			)
         if isinstance(text_inputs[0]       ,     _lowerCAmelCase			):
               _lowerCAmelCase 			=  text_inputs[0]
         else:
               # Batching case.
               _lowerCAmelCase 			=  text_inputs[0][0]

         _lowerCAmelCase 			=  self.model(**_lowerCAmelCase       ,     **_lowerCAmelCase			)

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





   def 	__lowerCAmelCase							(	self       ,     _lowerCAmelCase			):
         _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(_lowerCAmelCase       ,     _lowerCAmelCase			):
                     _lowerCAmelCase 			=  [scores]
         elif self.framework == "tf":
               _lowerCAmelCase 			=  stable_softmax(_lowerCAmelCase       ,     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(_lowerCAmelCase       ,     _lowerCAmelCase			)       ,     key=lambda _lowerCAmelCase			: -x[0]			)
         ]
         return result







from __future__ import annotations
def 							UpperCAmelCase__		(				_SCREAMING_SNAKE_CASE   :	list				)->list:
      if len(_SCREAMING_SNAKE_CASE				) == 0:
            return []
      _lowerCAmelCase			,   _lowerCAmelCase 			=  min(_SCREAMING_SNAKE_CASE				), max(_SCREAMING_SNAKE_CASE				)
      _lowerCAmelCase 			=  int(max_value - min_value				) + 1
      _lowerCAmelCase 			=  [[] for _ in range(_SCREAMING_SNAKE_CASE				)]

      for i in my_list:
            buckets[int(i - min_value				)].append(_SCREAMING_SNAKE_CASE				)

      return [v for bucket in buckets for v in sorted(_SCREAMING_SNAKE_CASE				)]


if __name__ == "__main__":
    from doctest import testmod

    testmod()
    assert bucket_sort([4, 5, 3, 2, 1]) == [1, 2, 3, 4, 5]
    assert bucket_sort([0, 1, -1_0, 1_5, 2, -2]) == [-1_0, -2, 0, 1, 2, 1_5]