
from __future__ import annotations

from sys import maxsize
from typing import Generic, TypeVar

lowerCamelCase	   =TypeVar("T")


def 							SCREAMING_SNAKE_CASE_      (			UpperCamelCase__		):
      return (position - 1) // 2


def 							SCREAMING_SNAKE_CASE_      (			UpperCamelCase__		):
      return (2 * position) + 1


def 							SCREAMING_SNAKE_CASE_      (			UpperCamelCase__		):
      return (2 * position) + 2




class 				_lowerCamelCase (      Generic[T] ):





     """simple docstring"""







     def __init__(							self  )  ->   None:




           """simple docstring"""







           UpperCamelCase__    :   Tuple					=				[]
           UpperCamelCase__    :   Tuple					=				{}
           UpperCamelCase__    :   Union[str, Any]					=				0






     def __len__(							self  )  ->   int:




           """simple docstring"""







           return self.elements






     def __repr__(							self  )  ->   str:




           """simple docstring"""







           return str(self.heap  )






     def    __SCREAMING_SNAKE_CASE			(							self  )  ->   bool:




           """simple docstring"""







           return self.elements == 0






     def    __SCREAMING_SNAKE_CASE			(							self						,						__SCREAMING_SNAKE_CASE						,						__SCREAMING_SNAKE_CASE  )  ->   None:




           """simple docstring"""







           self.heap.append((elem, weight)  )
           UpperCamelCase__    :   Tuple					=				self.elements
           self.elements += 1
           self._bubble_up(_UpperCAmelCase  )






     def    __SCREAMING_SNAKE_CASE			(							self  )  ->   T:




           """simple docstring"""







           if self.elements > 1:
                 self._swap_nodes(0						,						self.elements - 1  )
           UpperCamelCase__				,UpperCamelCase__    :   Union[str, Any]					=				self.heap.pop()
           del self.position_map[elem]
           self.elements -= 1
           if self.elements > 0:
                 UpperCamelCase__				,UpperCamelCase__    :   Tuple					=				self.heap[0]
                 self._bubble_down(_UpperCAmelCase  )
           return elem






     def    __SCREAMING_SNAKE_CASE			(							self						,						__SCREAMING_SNAKE_CASE						,						__SCREAMING_SNAKE_CASE  )  ->   None:




           """simple docstring"""







           UpperCamelCase__    :   str					=				self.position_map[elem]
           UpperCamelCase__    :   Optional[Any]					=				(elem, weight)
           if position > 0:
                 UpperCamelCase__    :   str					=				get_parent_position(_UpperCAmelCase  )
                 UpperCamelCase__				,UpperCamelCase__    :   str					=				self.heap[parent_position]
                 if parent_weight > weight:
                       self._bubble_up(_UpperCAmelCase  )
                 else:
                       self._bubble_down(_UpperCAmelCase  )
           else:
                 self._bubble_down(_UpperCAmelCase  )






     def    __SCREAMING_SNAKE_CASE			(							self						,						__SCREAMING_SNAKE_CASE  )  ->   None:




           """simple docstring"""







           UpperCamelCase__    :   Optional[Any]					=				self.position_map[elem]
           if curr_pos == 0:
                 return None
           UpperCamelCase__    :   List[str]					=				get_parent_position(_UpperCAmelCase  )
           UpperCamelCase__				,UpperCamelCase__    :   Optional[Any]					=				self.heap[curr_pos]
           UpperCamelCase__				,UpperCamelCase__    :   List[str]					=				self.heap[parent_position]
           if parent_weight > weight:
                 self._swap_nodes(_UpperCAmelCase						,						_UpperCAmelCase  )
                 return self._bubble_up(_UpperCAmelCase  )
           return None






     def    __SCREAMING_SNAKE_CASE			(							self						,						__SCREAMING_SNAKE_CASE  )  ->   None:




           """simple docstring"""







           UpperCamelCase__    :   Optional[Any]					=				self.position_map[elem]
           UpperCamelCase__				,UpperCamelCase__    :   str					=				self.heap[curr_pos]
           UpperCamelCase__    :   str					=				get_child_left_position(_UpperCAmelCase  )
           UpperCamelCase__    :   List[str]					=				get_child_right_position(_UpperCAmelCase  )
           if child_left_position < self.elements and child_right_position < self.elements:
                 UpperCamelCase__				,UpperCamelCase__    :   Dict					=				self.heap[child_left_position]
                 UpperCamelCase__				,UpperCamelCase__    :   List[str]					=				self.heap[child_right_position]
                 if child_right_weight < child_left_weight and child_right_weight < weight:
                       self._swap_nodes(_UpperCAmelCase						,						_UpperCAmelCase  )
                       return self._bubble_down(_UpperCAmelCase  )
           if child_left_position < self.elements:
                 UpperCamelCase__				,UpperCamelCase__    :   Dict					=				self.heap[child_left_position]
                 if child_left_weight < weight:
                       self._swap_nodes(_UpperCAmelCase						,						_UpperCAmelCase  )
                       return self._bubble_down(_UpperCAmelCase  )
           else:
                 return None
           if child_right_position < self.elements:
                 UpperCamelCase__				,UpperCamelCase__    :   int					=				self.heap[child_right_position]
                 if child_right_weight < weight:
                       self._swap_nodes(_UpperCAmelCase						,						_UpperCAmelCase  )
                       return self._bubble_down(_UpperCAmelCase  )
           return None






     def    __SCREAMING_SNAKE_CASE			(							self						,						__SCREAMING_SNAKE_CASE						,						__SCREAMING_SNAKE_CASE  )  ->   None:




           """simple docstring"""







           UpperCamelCase__    :   int					=				self.heap[nodea_pos][0]
           UpperCamelCase__    :   List[str]					=				self.heap[nodea_pos][0]
           UpperCamelCase__				,UpperCamelCase__    :   Union[str, Any]					=				(
               self.heap[nodea_pos],
               self.heap[nodea_pos],
           )
           UpperCamelCase__    :   Any					=				nodea_pos
           UpperCamelCase__    :   Optional[int]					=				nodea_pos




class 				_lowerCamelCase (      Generic[T] ):





     """simple docstring"""







     def __init__(							self  )  ->   None:




           """simple docstring"""







           UpperCamelCase__    :   List[str]					=				{}
           UpperCamelCase__    :   Any					=				0






     def __repr__(							self  )  ->   str:




           """simple docstring"""







           return str(self.connections  )






     def __len__(							self  )  ->   int:




           """simple docstring"""







           return self.nodes






     def    __SCREAMING_SNAKE_CASE			(							self						,						__SCREAMING_SNAKE_CASE  )  ->   None:




           """simple docstring"""







           if node not in self.connections:
                 UpperCamelCase__    :   Any					=				{}
                 self.nodes += 1






     def    __SCREAMING_SNAKE_CASE			(							self						,						__SCREAMING_SNAKE_CASE						,						__SCREAMING_SNAKE_CASE						,						__SCREAMING_SNAKE_CASE  )  ->   None:




           """simple docstring"""







           self.add_node(_UpperCAmelCase  )
           self.add_node(_UpperCAmelCase  )
           UpperCamelCase__    :   Dict					=				weight
           UpperCamelCase__    :   int					=				weight


def 							SCREAMING_SNAKE_CASE_      (			UpperCamelCase__ ,		):
      UpperCamelCase__    :   Any					=				{node: maxsize for node in graph.connections}
      UpperCamelCase__    :   List[str]					=				{node: None for node in graph.connections}

      UpperCamelCase__    :   Any					=				MinPriorityQueue()
      for node, weight in dist.items():
            priority_queue.push(lowerCAmelCase__ ,		lowerCAmelCase__		)

      if priority_queue.is_empty():
            return dist, parent

      # initialization
      UpperCamelCase__    :   List[str]					=				priority_queue.extract_min()
      UpperCamelCase__    :   int					=				0
      for neighbour in graph.connections[node]:
            if dist[neighbour] > dist[node] + graph.connections[node][neighbour]:
                  UpperCamelCase__    :   str					=				dist[node] + graph.connections[node][neighbour]
                  priority_queue.update_key(lowerCAmelCase__ ,		dist[neighbour]		)
                  UpperCamelCase__    :   int					=				node

    # running prim's algorithm
      while not priority_queue.is_empty():
            UpperCamelCase__    :   str					=				priority_queue.extract_min()
            for neighbour in graph.connections[node]:
                  if dist[neighbour] > dist[node] + graph.connections[node][neighbour]:
                        UpperCamelCase__    :   List[Any]					=				dist[node] + graph.connections[node][neighbour]
                        priority_queue.update_key(lowerCAmelCase__ ,		dist[neighbour]		)
                        UpperCamelCase__    :   Union[str, Any]					=				node
      return dist, parent




from io import BytesIO
from typing import List, Union

import requests

from ..utils import add_end_docstrings, is_decord_available, is_torch_available, logging, requires_backends
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_decord_available():
    import numpy as np
    from decord import VideoReader


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

lowerCamelCase      =logging.get_logger(__name__)




@add_end_docstrings(UpperCamelCase_ )
class 				_lowerCamelCase (      UpperCamelCase_ ):





     """simple docstring"""







     def __init__(							self						,						*__SCREAMING_SNAKE_CASE						,						**__SCREAMING_SNAKE_CASE  )  ->   Dict:




           """simple docstring"""







           super().__init__(*__SCREAMING_SNAKE_CASE						,						**__SCREAMING_SNAKE_CASE  )
           requires_backends(self						,						'''decord'''  )
           self.check_model_type(__SCREAMING_SNAKE_CASE  )






     def    __SCREAMING_SNAKE_CASE			(							self						,						__SCREAMING_SNAKE_CASE=None						,						__SCREAMING_SNAKE_CASE=None						,						__SCREAMING_SNAKE_CASE=None  )  ->   Optional[Any]:




           """simple docstring"""







           UpperCamelCase__    :   List[str]					=				{}
           if frame_sampling_rate is not None:
                 UpperCamelCase__    :   Tuple					=				frame_sampling_rate
           if num_frames is not None:
                 UpperCamelCase__    :   str					=				num_frames

           UpperCamelCase__    :   List[str]					=				{}
           if top_k is not None:
                 UpperCamelCase__    :   List[Any]					=				top_k
           return preprocess_params, {}, postprocess_params






     def __call__(							self						,						__SCREAMING_SNAKE_CASE						,						**__SCREAMING_SNAKE_CASE  )  ->   Tuple:




           """simple docstring"""







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






     def    __SCREAMING_SNAKE_CASE			(							self						,						__SCREAMING_SNAKE_CASE						,						__SCREAMING_SNAKE_CASE=None						,						__SCREAMING_SNAKE_CASE=1  )  ->   Optional[Any]:




           """simple docstring"""







           if num_frames is None:
                 UpperCamelCase__    :   Optional[Any]					=				self.model.config.num_frames

           if video.startswith('''http://'''  ) or video.startswith('''https://'''  ):
                 UpperCamelCase__    :   str					=				BytesIO(requests.get(__SCREAMING_SNAKE_CASE  ).content  )

           UpperCamelCase__    :   Tuple					=				VideoReader(__SCREAMING_SNAKE_CASE  )
           videoreader.seek(0  )

           UpperCamelCase__    :   Optional[int]					=				0
           UpperCamelCase__    :   str					=				num_frames * frame_sampling_rate - 1
           UpperCamelCase__    :   List[Any]					=				np.linspace(__SCREAMING_SNAKE_CASE						,						__SCREAMING_SNAKE_CASE						,						num=__SCREAMING_SNAKE_CASE						,						dtype=np.intaa  )

           UpperCamelCase__    :   str					=				videoreader.get_batch(__SCREAMING_SNAKE_CASE  ).asnumpy()
           UpperCamelCase__    :   Tuple					=				list(__SCREAMING_SNAKE_CASE  )

           UpperCamelCase__    :   Dict					=				self.image_processor(__SCREAMING_SNAKE_CASE						,						return_tensors=self.framework  )
           return model_inputs






     def    __SCREAMING_SNAKE_CASE			(							self						,						__SCREAMING_SNAKE_CASE  )  ->   Union[str, Any]:




           """simple docstring"""







           UpperCamelCase__    :   Tuple					=				self.model(**__SCREAMING_SNAKE_CASE  )
           return model_outputs






     def    __SCREAMING_SNAKE_CASE			(							self						,						__SCREAMING_SNAKE_CASE						,						__SCREAMING_SNAKE_CASE=5  )  ->   str:




           """simple docstring"""







           if top_k > self.model.config.num_labels:
                 UpperCamelCase__    :   Dict					=				self.model.config.num_labels

           if self.framework == "pt":
                 UpperCamelCase__    :   Any					=				model_outputs.logits.softmax(-1  )[0]
                 UpperCamelCase__				,UpperCamelCase__    :   List[str]					=				probs.topk(__SCREAMING_SNAKE_CASE  )
           else:
                 raise ValueError(F'''Unsupported framework: {self.framework}'''  )

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


