


'''simple docstring'''


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

lowerCamelCase__					=   logging.get_logger(__name__)






@add_end_docstrings(UpperCAmelCase__		)
class 		lowerCAmelCase__	(	UpperCAmelCase__		):




			def __init__(						self     :   Optional[Any]		,     **lowerCamelCase__     :   Tuple       )      ->List[str]:



					'''simple docstring'''







					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     :   Any		,     lowerCamelCase__     :   Any		,     **lowerCamelCase__     :   Optional[int]       )      ->Optional[Any]:



					'''simple docstring'''







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




			def 							lowerCAmelCase__			(						self     :   int		,     **lowerCamelCase__     :   Union[str, Any]       )      ->Optional[Any]:



					'''simple docstring'''







					_UpperCAmelCase						:				List[Any]	    =					{}
					if "candidate_labels" in kwargs:
							_UpperCAmelCase						:				Dict	    =					kwargs["""candidate_labels"""]
					if "hypothesis_template" in kwargs:
							_UpperCAmelCase						:				Optional[Any]	    =					kwargs["""hypothesis_template"""]

					return preprocess_params, {}, {}




			def 							lowerCAmelCase__			(						self     :   Optional[Any]		,     lowerCamelCase__     :   Any		,     lowerCamelCase__     :   str=None		,     lowerCamelCase__     :   str="This is a photo of {}."       )      ->Optional[Any]:



					'''simple docstring'''







					_UpperCAmelCase						:				int	    =					load_image(lowerCamelCase__       )
					_UpperCAmelCase						:				List[str]	    =					self.image_processor(images=[image]		,     return_tensors=self.framework       )
					_UpperCAmelCase						:				List[str]	    =					candidate_labels
					_UpperCAmelCase						:				Tuple	    =					[hypothesis_template.format(lowerCamelCase__       ) for x in candidate_labels]
					_UpperCAmelCase						:				Union[str, Any]	    =					self.tokenizer(lowerCamelCase__		,     return_tensors=self.framework		,     padding=lowerCamelCase__       )
					_UpperCAmelCase						:				Union[str, Any]	    =					[text_inputs]
					return inputs




			def 							lowerCAmelCase__			(						self     :   int		,     lowerCamelCase__     :   List[str]       )      ->Optional[int]:



					'''simple docstring'''







					_UpperCAmelCase						:				List[Any]	    =					model_inputs.pop("candidate_labels"       )
					_UpperCAmelCase						:				Optional[Any]	    =					model_inputs.pop("text_inputs"       )
					if isinstance(text_inputs[0]		,     lowerCamelCase__       ):
							_UpperCAmelCase						:				Optional[Any]	    =					text_inputs[0]
					else:
							# Batching case.
							_UpperCAmelCase						:				Any	    =					text_inputs[0][0]

					_UpperCAmelCase						:				Dict	    =					self.model(**lowerCamelCase__		,     **lowerCamelCase__       )

					_UpperCAmelCase						:				List[Any]	    =					{
					    """candidate_labels""": candidate_labels,
					    """logits""": outputs.logits_per_image,
					}
					return model_outputs






			def 							lowerCAmelCase__			(						self     :   Union[str, Any]		,     lowerCamelCase__     :   List[str]       )      ->Union[str, Any]:



					'''simple docstring'''







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

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








'''simple docstring'''




from __future__ import annotations

import math

class 			UpperCamelCase_ :





	def __init__(  self		,		A    )			->			None:
								UpperCAmelCase     :		Optional[int]							     =		size
								# approximate the overall size of segment tree with given value
								UpperCAmelCase     :		Optional[int]							     =		[0 for i in range(0		,		4 * size    )]
								# create array to store lazy update
								UpperCAmelCase     :		Any							     =		[0 for i in range(0		,		4 * size    )]
								UpperCAmelCase     :		Tuple							     =		[0 for i in range(0		,		4 * size    )]  # flag for lazy update





	def  _lowercase(  self		,		A    )			->			int:
								return idx * 2





	def  _lowercase(  self		,		A    )			->			int:
								return idx * 2 + 1





	def  _lowercase(  self		,		A		,		A		,		A		,		A    )			->			None:
								if left_element == right_element:
															UpperCAmelCase     :		str							     =		a[left_element - 1]
								else:
															UpperCAmelCase     :		Tuple							     =		(left_element + right_element) // 2
															self.build(self.left(A    )		,		A		,		A		,		A    )
															self.build(self.right(A    )		,		mid + 1		,		A		,		A    )
															UpperCAmelCase     :		str							     =		max(
															    self.segment_tree[self.left(A    )]		,		self.segment_tree[self.right(A    )]    )





	def  _lowercase(  self		,		A		,		A		,		A		,		A		,		A		,		A    )			->			bool:
								if self.flag[idx] is True:
															UpperCAmelCase     :		Optional[Any]							     =		self.lazy[idx]
															UpperCAmelCase     :		int							     =		False
															if left_element != right_element:
																						UpperCAmelCase     :		List[str]							     =		self.lazy[idx]
																						UpperCAmelCase     :		Optional[Any]							     =		self.lazy[idx]
																						UpperCAmelCase     :		List[str]							     =		True
																						UpperCAmelCase     :		int							     =		True

								if right_element < a or left_element > b:
															return True
								if left_element >= a and right_element <= b:
															UpperCAmelCase     :		Optional[Any]							     =		val
															if left_element != right_element:
																						UpperCAmelCase     :		Tuple							     =		val
																						UpperCAmelCase     :		int							     =		val
																						UpperCAmelCase     :		Any							     =		True
																						UpperCAmelCase     :		str							     =		True
															return True
								UpperCAmelCase     :		str							     =		(left_element + right_element) // 2
								self.update(self.left(A    )		,		A		,		A		,		A		,		A		,		A    )
								self.update(self.right(A    )		,		mid + 1		,		A		,		A		,		A		,		A    )
								UpperCAmelCase     :		List[str]							     =		max(
								    self.segment_tree[self.left(A    )]		,		self.segment_tree[self.right(A    )]    )
								return True





	def  _lowercase(  self		,		A		,		A		,		A		,		A		,		A    )			->			int | float:
								if self.flag[idx] is True:
															UpperCAmelCase     :		Any							     =		self.lazy[idx]
															UpperCAmelCase     :		Any							     =		False
															if left_element != right_element:
																						UpperCAmelCase     :		Optional[Any]							     =		self.lazy[idx]
																						UpperCAmelCase     :		Tuple							     =		self.lazy[idx]
																						UpperCAmelCase     :		List[str]							     =		True
																						UpperCAmelCase     :		Tuple							     =		True
								if right_element < a or left_element > b:
															return -math.inf
								if left_element >= a and right_element <= b:
															return self.segment_tree[idx]
								UpperCAmelCase     :		Dict							     =		(left_element + right_element) // 2
								UpperCAmelCase     :		List[Any]							     =		self.query(self.left(A    )		,		A		,		A		,		A		,		A    )
								UpperCAmelCase     :		str							     =		self.query(self.right(A    )		,		mid + 1		,		A		,		A		,		A    )
								return max(A		,		A    )






	def __str__(  self    )			->			str:
								return str([self.query(1		,		1		,		self.size		,		A		,		A    ) for i in range(1		,		self.size + 1    )]    )


if __name__ == "__main__":
							a   :			Optional[int]					  =    [1, 2, -4, 7, 3, -5, 6, 1_1, -2_0, 9, 1_4, 1_5, 5, 2, -8]
							a   :			Optional[Any]					  =    1_5
							a   :			Union[str, Any]					  =    SegmentTree(size)
							segt.build(1, 1, size, A)
							print(segt.query(1, 1, size, 4, 6))
							print(segt.query(1, 1, size, 7, 1_1))
							print(segt.query(1, 1, size, 7, 1_2))
							segt.update(1, 1, size, 1, 3, 1_1_1)
							print(segt.query(1, 1, size, 1, 1_5))
							segt.update(1, 1, size, 7, 8, 2_3_5)
							print(segt)

