



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

__A   			=logging.get_logger(__name__)

@add_end_docstrings(__UpperCamelCase   )
class 		UpperCAmelCase__			(			__UpperCamelCase   ):




			'''simple docstring'''






			def __init__(   self							: str       ,       *a_							: int       ,       **a_							: Optional[int] ):
					'''simple docstring'''





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




			def  snake_case__     (   self							: List[str]       ,       a_							: List[Any]=None       ,       a_							: Optional[Any]=None       ,       a_							: Any=None ):
					'''simple docstring'''





					__UpperCAmelCase    :     Optional[int]													=	{}
					if frame_sampling_rate is not None:
							__UpperCAmelCase    :     Union[str, Any]													=	frame_sampling_rate
					if num_frames is not None:
							__UpperCAmelCase    :     Dict													=	num_frames

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




			def __call__(   self							: str       ,       a_							: Union[str, List[str]]       ,       **a_							: List[str] ):
					'''simple docstring'''





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




			def  snake_case__     (   self							: Union[str, Any]       ,       a_							: List[str]       ,       a_							: List[Any]=None       ,       a_							: int=1 ):
					'''simple docstring'''





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

					if video.startswith('''http://''' ) or video.startswith('''https://''' ):
							__UpperCAmelCase    :     Union[str, Any]													=	BytesIO(requests.get(a_ ).content )

					__UpperCAmelCase    :     Dict													=	VideoReader(a_ )
					videoreader.seek(0 )

					__UpperCAmelCase    :     int													=	0
					__UpperCAmelCase    :     Optional[int]													=	num_frames * frame_sampling_rate - 1
					__UpperCAmelCase    :     Optional[int]													=	np.linspace(a_       ,       a_       ,       num=a_       ,       dtype=np.intaa )

					__UpperCAmelCase    :     Tuple													=	videoreader.get_batch(a_ ).asnumpy()
					__UpperCAmelCase    :     int													=	list(a_ )

					__UpperCAmelCase    :     Optional[int]													=	self.image_processor(a_       ,       return_tensors=self.framework )
					return model_inputs




			def  snake_case__     (   self							: str       ,       a_							: List[str] ):
					'''simple docstring'''





					__UpperCAmelCase    :     List[str]													=	self.model(**a_ )
					return model_outputs




			def  snake_case__     (   self							: Tuple       ,       a_							: Optional[int]       ,       a_							: Tuple=5 ):
					'''simple docstring'''





					if top_k > self.model.config.num_labels:
							__UpperCAmelCase    :     Union[str, Any]													=	self.model.config.num_labels

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

					__UpperCAmelCase    :     List[str]													=	scores.tolist()
					__UpperCAmelCase    :     Tuple													=	ids.tolist()
					return [{"score": score, "label": self.model.config.idalabel[_id]} for score, _id in zip(a_       ,       a_ )]










def        a						(      _UpperCAmelCase							:					Any						):







		'''simple docstring'''





		__UpperCAmelCase    :     Any													=	0
		__UpperCAmelCase    :     str													=	len(_UpperCAmelCase						)

		for i in range(n - 1						):
				for j in range(i + 1 ,   _UpperCAmelCase						):
						if arr[i] > arr[j]:
								num_inversions += 1

		return num_inversions

def        a						(      _UpperCAmelCase							:					str						):







		'''simple docstring'''





		if len(_UpperCAmelCase						) <= 1:
				return arr, 0
		__UpperCAmelCase    :     Dict													=	len(_UpperCAmelCase						) // 2
		__UpperCAmelCase    :     Union[str, Any]													=	arr[0:mid]
		__UpperCAmelCase    :     Optional[Any]													=	arr[mid:]

		__UpperCAmelCase							,					__UpperCAmelCase    :     Tuple													=	count_inversions_recursive(_UpperCAmelCase						)
		__UpperCAmelCase							,					__UpperCAmelCase    :     Optional[Any]													=	count_inversions_recursive(_UpperCAmelCase						)
		__UpperCAmelCase							,					__UpperCAmelCase    :     int													=	_count_cross_inversions(_UpperCAmelCase ,   _UpperCAmelCase						)

		__UpperCAmelCase    :     List[str]													=	inversion_p + inversions_q + cross_inversions
		return c, num_inversions

def        a						(      _UpperCAmelCase							:					List[str] ,   _UpperCAmelCase							:					Any						):







		'''simple docstring'''





		__UpperCAmelCase    :     List[Any]													=	[]
		__UpperCAmelCase    :     Any													=	0
		while i < len(_UpperCAmelCase						) and j < len(_UpperCAmelCase						):
				if p[i] > q[j]:
						# if P[1] > Q[j], then P[k] > Q[k] for all  i < k <= len(P)
						# These are all inversions. The claim emerges from the
						# property that P is sorted.
						num_inversion += len(_UpperCAmelCase						) - i
						r.append(q[j]						)
						j += 1
				else:
						r.append(p[i]						)
						i += 1

		if i < len(_UpperCAmelCase						):
				r.extend(p[i:]						)
		else:
				r.extend(q[j:]						)

		return r, num_inversion

def        a						(      ):







		'''simple docstring'''





		__UpperCAmelCase    :     Optional[Any]													=	[10, 2, 1, 5, 5, 2, 11]

		# this arr has 8 inversions:
		# (10, 2), (10, 1), (10, 5), (10, 5), (10, 2), (2, 1), (5, 2), (5, 2)

		__UpperCAmelCase    :     List[str]													=	count_inversions_bf(_UpperCAmelCase						)
		__UpperCAmelCase							,					__UpperCAmelCase    :     Tuple													=	count_inversions_recursive(_UpperCAmelCase						)

		assert num_inversions_bf == num_inversions_recursive == 8

		print('''number of inversions = ''' ,   _UpperCAmelCase						)

		# testing an array with zero inversion (a sorted arr_1)

		arr_a.sort()
		__UpperCAmelCase    :     Any													=	count_inversions_bf(_UpperCAmelCase						)
		__UpperCAmelCase							,					__UpperCAmelCase    :     int													=	count_inversions_recursive(_UpperCAmelCase						)

		assert num_inversions_bf == num_inversions_recursive == 0
		print('''number of inversions = ''' ,   _UpperCAmelCase						)

		# an empty list should also have zero inversions
		__UpperCAmelCase    :     Tuple													=	[]
		__UpperCAmelCase    :     Union[str, Any]													=	count_inversions_bf(_UpperCAmelCase						)
		__UpperCAmelCase							,					__UpperCAmelCase    :     int													=	count_inversions_recursive(_UpperCAmelCase						)

		assert num_inversions_bf == num_inversions_recursive == 0
		print('''number of inversions = ''' ,   _UpperCAmelCase						)


if __name__ == "__main__":
				main()





