


class      UpperCamelCase					:


   '''simple docstring'''







   def __init__(     self		):
        lowercase_       :Dict     =       0
        lowercase_       :Any     =       0
        lowercase_       :List[str]     =       {}
   def 		UpperCamelCase  (     self   ,							UpperCamelCase_		):
        if vertex not in self.adjacency:
             lowercase_       :Any     =       {}
             self.num_vertices += 1
   def 		UpperCamelCase  (     self   ,							UpperCamelCase_   ,							UpperCamelCase_   ,							UpperCamelCase_		):
        self.add_vertex(UpperCamelCase_		)
        self.add_vertex(UpperCamelCase_		)

        if head == tail:
             return

        lowercase_       :str     =       weight
        lowercase_       :Dict     =       weight
   def 		UpperCamelCase  (     self		):
        lowercase_       :Optional[int]     =       self.get_edges()
        for edge in edges:
             lowercase_    ,      lowercase_    ,      lowercase_       :Any     =       edge
             edges.remove((tail, head, weight)		)
        for i in range(len(UpperCamelCase_		)		):
             lowercase_       :str     =       list(edges[i]		)

        edges.sort(key=lambda UpperCamelCase_		: e[2]		)
        for i in range(len(UpperCamelCase_		) - 1		):
             if edges[i][2] >= edges[i + 1][2]:
                  lowercase_       :Any     =       edges[i][2] + 1
        for edge in edges:
             lowercase_    ,      lowercase_    ,      lowercase_       :Optional[int]     =       edge
             lowercase_       :Optional[Any]     =       weight
             lowercase_       :Optional[int]     =       weight
   def __str__(     self		):
        lowercase_       :Tuple     =       ''''''
        for tail in self.adjacency:
             for head in self.adjacency[tail]:
                  lowercase_       :Tuple     =       self.adjacency[head][tail]
                  string += f"{head} -> {tail} == {weight}\n"
        return string.rstrip('''\n'''		)
   def 		UpperCamelCase  (     self		):
        lowercase_       :int     =       []
        for tail in self.adjacency:
             for head in self.adjacency[tail]:
                  output.append((tail, head, self.adjacency[head][tail])		)
        return output
   def 		UpperCamelCase  (     self		):
        return self.adjacency.keys()
   @staticmethod
   def 		UpperCamelCase  (     UpperCamelCase_=None   ,							UpperCamelCase_=None		):
        lowercase_       :Optional[int]     =       Graph()
        if vertices is None:
             lowercase_       :Optional[Any]     =       []
        if edges is None:
             lowercase_       :Optional[Any]     =       []
        for vertex in vertices:
             g.add_vertex(UpperCamelCase_		)
        for edge in edges:
             g.add_edge(*UpperCamelCase_		)
        return g






   class      UpperCamelCase					:


      '''simple docstring'''







      def __init__(     self		):
           lowercase_       :int     =       {}
           lowercase_       :Tuple     =       {}
      def __len__(     self		):
           return len(self.parent		)
      def 		UpperCamelCase  (     self   ,							UpperCamelCase_		):
           if item in self.parent:
                return self.find(UpperCamelCase_		)

           lowercase_       :Union[str, Any]     =       item
           lowercase_       :Optional[Any]     =       0
           return item
      def 		UpperCamelCase  (     self   ,							UpperCamelCase_		):
           if item not in self.parent:
                return self.make_set(UpperCamelCase_		)
           if item != self.parent[item]:
                lowercase_       :List[Any]     =       self.find(self.parent[item]		)
           return self.parent[item]




      def 		UpperCamelCase  (     self   ,							UpperCamelCase_   ,							UpperCamelCase_		):
           lowercase_       :List[str]     =       self.find(UpperCamelCase_		)
           lowercase_       :Union[str, Any]     =       self.find(UpperCamelCase_		)

           if roota == roota:
                return roota

           if self.rank[roota] > self.rank[roota]:
                lowercase_       :Tuple     =       roota
                return roota

           if self.rank[roota] < self.rank[roota]:
                lowercase_       :Any     =       roota
                return roota

           if self.rank[roota] == self.rank[roota]:
                self.rank[roota] += 1
                lowercase_       :int     =       roota
                return roota
           return None
   @staticmethod
   def 		UpperCamelCase  (     UpperCamelCase_		):
        lowercase_       :Dict     =       graph.num_vertices

        lowercase_       :Optional[int]     =       Graph.UnionFind()
        lowercase_       :Dict     =       []
        while num_components > 1:
             lowercase_       :str     =       {}
             for vertex in graph.get_vertices():
                  lowercase_       :int     =       -1

             lowercase_       :str     =       graph.get_edges()
             for edge in edges:
                  lowercase_    ,      lowercase_    ,      lowercase_       :Tuple     =       edge
                  edges.remove((tail, head, weight)		)
             for edge in edges:
                  lowercase_    ,      lowercase_    ,      lowercase_       :Tuple     =       edge
                  lowercase_       :Optional[Any]     =       union_find.find(UpperCamelCase_		)
                  lowercase_       :Tuple     =       union_find.find(UpperCamelCase_		)
                  if seta != seta:
                       if cheap_edge[seta] == -1 or cheap_edge[seta][2] > weight:
                            lowercase_       :Optional[int]     =       [head, tail, weight]

                       if cheap_edge[seta] == -1 or cheap_edge[seta][2] > weight:
                            lowercase_       :Optional[Any]     =       [head, tail, weight]
             for vertex in cheap_edge:
                  if cheap_edge[vertex] != -1:
                       lowercase_    ,      lowercase_    ,      lowercase_       :Optional[int]     =       cheap_edge[vertex]
                       if union_find.find(UpperCamelCase_		) != union_find.find(UpperCamelCase_		):
                            union_find.union(UpperCamelCase_   ,							UpperCamelCase_		)
                            mst_edges.append(cheap_edge[vertex]		)
                            lowercase_       :str     =       num_components - 1
        lowercase_       :List[Any]     =       Graph.build(edges=UpperCamelCase_		)
        return mst





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

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






@add_end_docstrings(lowercase__     )
class      UpperCamelCase					(		lowercase__     ):


   '''simple docstring'''







   def __init__(     self   ,							*UpperCamelCase_   ,							**UpperCamelCase_		):
        super().__init__(*UpperCamelCase_   ,							**UpperCamelCase_		)
        requires_backends(self   ,							'''decord'''		)
        self.check_model_type(UpperCamelCase_		)
   def 		UpperCamelCase  (     self   ,							UpperCamelCase_=None   ,							UpperCamelCase_=None   ,							UpperCamelCase_=None		):
        lowercase_       :int     =       {}
        if frame_sampling_rate is not None:
             lowercase_       :int     =       frame_sampling_rate
        if num_frames is not None:
             lowercase_       :int     =       num_frames

        lowercase_       :str     =       {}
        if top_k is not None:
             lowercase_       :Optional[int]     =       top_k
        return preprocess_params, {}, postprocess_params
   def __call__(     self   ,							UpperCamelCase_   ,							**UpperCamelCase_		):
        return super().__call__(UpperCamelCase_   ,							**UpperCamelCase_		)
   def 		UpperCamelCase  (     self   ,							UpperCamelCase_   ,							UpperCamelCase_=None   ,							UpperCamelCase_=1		):
        if num_frames is None:
             lowercase_       :str     =       self.model.config.num_frames

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

        lowercase_       :Optional[int]     =       VideoReader(UpperCamelCase_		)
        videoreader.seek(0		)

        lowercase_       :Tuple     =       0
        lowercase_       :Optional[Any]     =       num_frames * frame_sampling_rate - 1
        lowercase_       :Any     =       np.linspace(UpperCamelCase_   ,							UpperCamelCase_   ,							num=UpperCamelCase_   ,							dtype=np.intaa		)

        lowercase_       :Dict     =       videoreader.get_batch(UpperCamelCase_		).asnumpy()
        lowercase_       :List[Any]     =       list(UpperCamelCase_		)

        lowercase_       :Any     =       self.image_processor(UpperCamelCase_   ,							return_tensors=self.framework		)
        return model_inputs
   def 		UpperCamelCase  (     self   ,							UpperCamelCase_		):
        lowercase_       :List[str]     =       self.model(**UpperCamelCase_		)
        return model_outputs




   def 		UpperCamelCase  (     self   ,							UpperCamelCase_   ,							UpperCamelCase_=5		):
        if top_k > self.model.config.num_labels:
             lowercase_       :List[str]     =       self.model.config.num_labels

        if self.framework == "pt":
             lowercase_       :Optional[int]     =       model_outputs.logits.softmax(-1		)[0]
             lowercase_    ,      lowercase_       :Dict     =       probs.topk(UpperCamelCase_		)
        else:
             raise ValueError(f"Unsupported framework: {self.framework}"		)

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

