

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







@add_end_docstrings(__lowercase  )
class 							A__   (	__lowercase  ):





  def __init__(		self				:     str							, *_a				:     Tuple							, **_a				:     Optional[int]			)     ->   Tuple:





       '''simple docstring'''






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





  def 						A  (		self				:     Optional[int]							, _a				:     Optional[int]=None							, _a				:     List[str]=None							, _a				:     Tuple=None			)     ->   Dict:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							   ={}
       if frame_sampling_rate is not None:
            _SCREAMING_SNAKE_CASE							   =frame_sampling_rate
       if num_frames is not None:
            _SCREAMING_SNAKE_CASE							   =num_frames

       _SCREAMING_SNAKE_CASE							   ={}
       if top_k is not None:
            _SCREAMING_SNAKE_CASE							   =top_k
       return preprocess_params, {}, postprocess_params





  def __call__(		self				:     str							, _a				:     Optional[int]							, **_a				:     int			)     ->   Tuple:





       '''simple docstring'''






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





  def 						A  (		self				:     Union[str, Any]							, _a				:     Optional[Any]							, _a				:     Optional[Any]=None							, _a				:     Any=1			)     ->   List[str]:





       '''simple docstring'''






       if num_frames is None:
            _SCREAMING_SNAKE_CASE							   =self.model.config.num_frames

       if video.startswith('http://'			) or video.startswith('https://'			):
            _SCREAMING_SNAKE_CASE							   =BytesIO(requests.get(_a			).content			)

       _SCREAMING_SNAKE_CASE							   =VideoReader(_a			)
       videoreader.seek(0			)

       _SCREAMING_SNAKE_CASE							   =0
       _SCREAMING_SNAKE_CASE							   =num_frames * frame_sampling_rate - 1
       _SCREAMING_SNAKE_CASE							   =np.linspace(_a							, _a							, num=_a							, dtype=np.intaa			)

       _SCREAMING_SNAKE_CASE							   =videoreader.get_batch(_a			).asnumpy()
       _SCREAMING_SNAKE_CASE							   =list(_a			)

       _SCREAMING_SNAKE_CASE							   =self.image_processor(_a							, return_tensors=self.framework			)
       return model_inputs





  def 						A  (		self				:     List[Any]							, _a				:     Optional[Any]			)     ->   Any:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							   =self.model(**_a			)
       return model_outputs





  def 						A  (		self				:     int							, _a				:     Tuple							, _a				:     List[str]=5			)     ->   Any:





       '''simple docstring'''






       if top_k > self.model.config.num_labels:
            _SCREAMING_SNAKE_CASE							   =self.model.config.num_labels

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

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



'''simple docstring'''
from math import factorial


def 	_lowerCAmelCase (						_UpperCamelCase						:					int							,			_UpperCamelCase						:					int							,			_UpperCamelCase						:					float    )				->      float:
     """simple docstring"""




     if successes > trials:
          raise ValueError('successes must be lower or equal to trials'    )
     if trials < 0 or successes < 0:
          raise ValueError('the function is defined for non-negative integers'    )
     if not isinstance(_UpperCamelCase							,			_UpperCamelCase    ) or not isinstance(_UpperCamelCase							,			_UpperCamelCase    ):
          raise ValueError('the function is defined for non-negative integers'    )
     if not 0 < prob < 1:
          raise ValueError('prob has to be in range of 1 - 0'    )
     _SCREAMING_SNAKE_CASE							   =(prob**successes) * ((1 - prob) ** (trials - successes))
     # Calculate the binomial coefficient: n! / k!(n-k)!
     _SCREAMING_SNAKE_CASE							   =float(factorial(_UpperCamelCase    )    )
     coefficient /= factorial(_UpperCamelCase    ) * factorial(trials - successes    )
     return probability * coefficient


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

     testmod()
     print("Probability of 2 successes out of 4 trails")
     print("with probability of 0.75 is:", end=" ")
     print(binomial_distribution(2, 4, 0.7_5))
