





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_					:   Tuple 							=						logging.get_logger(__name__)





@add_end_docstrings(__A  )
class      lowercase__    (							__A  ):







  def __init__(	self    ,						**_lowercase						):
       super().__init__(**_lowercase						)

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







  def 	UpperCAmelCase__		(	self    ,						**_lowercase						):
       lowerCAmelCase_					:       Any    =		{}
       if "candidate_labels" in kwargs:
            lowerCAmelCase_					:       Union[str, Any]    =		kwargs["""candidate_labels"""]
       if "hypothesis_template" in kwargs:
            lowerCAmelCase_					:       str    =		kwargs["""hypothesis_template"""]

       return preprocess_params, {}, {}







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







  def 	UpperCAmelCase__		(	self    ,						_lowercase						):
       lowerCAmelCase_					:       Union[str, Any]    =		model_inputs.pop("""candidate_labels"""						)
       lowerCAmelCase_					:       Dict    =		model_inputs.pop("""text_inputs"""						)
       if isinstance(text_inputs[0]    ,						_lowercase						):
            lowerCAmelCase_					:       Dict    =		text_inputs[0]
       else:
            # Batching case.
            lowerCAmelCase_					:       Union[str, Any]    =		text_inputs[0][0]

       lowerCAmelCase_					:       Dict    =		self.model(**_lowercase    ,						**_lowercase						)

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







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

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







from collections.abc import Sequence






def 							_lowerCAmelCase (  _a   :					Sequence[float]					,   _a   :					bool = False			)	->			float:
     if not arr:
          return 0

     lowerCAmelCase_					:       List[str]    =		0 if allow_empty_subarrays else float("""-inf"""			)
     lowerCAmelCase_					:       List[Any]    =		0.0
     for num in arr:
          lowerCAmelCase_					:       int    =		max(0 if allow_empty_subarrays else num					,   curr_sum + num			)
          lowerCAmelCase_					:       List[str]    =		max(_a					,   _a			)

     return max_sum


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

      testmod()

      UpperCAmelCase_					:   int 							=						[-2, 1, -3, 4, -1, 2, 1, -5, 4]
      print(F"""{max_subarray_sum(nums) = }""")
