



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       _SCREAMING_SNAKE_CASE	(UpperCamelCase ):





     def __init__(       self :							Any     ,  *UpperCamelCase :							Dict     ,  **UpperCamelCase :							Union[str, Any]				)->Any:
      super().__init__(*UpperCamelCase     ,  **UpperCamelCase				)
      requires_backends(self     ,  "decord"				)
      self.check_model_type(UpperCamelCase				)





     def 	__snake_case    (       self :							Any     ,  UpperCamelCase :							Dict=None     ,  UpperCamelCase :							Optional[Any]=None     ,  UpperCamelCase :							str=None				)->Optional[Any]:
      __SCREAMING_SNAKE_CASE       :    Optional[Any]       =	{}
      if frame_sampling_rate is not None:
       __SCREAMING_SNAKE_CASE       :    Any       =	frame_sampling_rate
      if num_frames is not None:
       __SCREAMING_SNAKE_CASE       :    Optional[int]       =	num_frames

      __SCREAMING_SNAKE_CASE       :    Dict       =	{}
      if top_k is not None:
       __SCREAMING_SNAKE_CASE       :    Tuple       =	top_k
      return preprocess_params, {}, postprocess_params





     def __call__(       self :							Union[str, Any]     ,  UpperCamelCase :							Union[str, List[str]]     ,  **UpperCamelCase :							Tuple				)->List[Any]:
      return super().__call__(UpperCamelCase     ,  **UpperCamelCase				)





     def 	__snake_case    (       self :							Dict     ,  UpperCamelCase :							int     ,  UpperCamelCase :							List[Any]=None     ,  UpperCamelCase :							Optional[Any]=1				)->Dict:
      if num_frames is None:
       __SCREAMING_SNAKE_CASE       :    Optional[int]       =	self.model.config.num_frames

      if video.startswith("http://"				) or video.startswith("https://"				):
       __SCREAMING_SNAKE_CASE       :    Optional[int]       =	BytesIO(requests.get(UpperCamelCase				).content				)

      __SCREAMING_SNAKE_CASE       :    List[str]       =	VideoReader(UpperCamelCase				)
      videoreader.seek(0				)

      __SCREAMING_SNAKE_CASE       :    Tuple       =	0
      __SCREAMING_SNAKE_CASE       :    List[str]       =	num_frames * frame_sampling_rate - 1
      __SCREAMING_SNAKE_CASE       :    str       =	np.linspace(UpperCamelCase     ,  UpperCamelCase     ,  num=UpperCamelCase     ,  dtype=np.intaa				)

      __SCREAMING_SNAKE_CASE       :    Optional[Any]       =	videoreader.get_batch(UpperCamelCase				).asnumpy()
      __SCREAMING_SNAKE_CASE       :    str       =	list(UpperCamelCase				)

      __SCREAMING_SNAKE_CASE       :    Optional[Any]       =	self.image_processor(UpperCamelCase     ,  return_tensors=self.framework				)
      return model_inputs





     def 	__snake_case    (       self :							List[str]     ,  UpperCamelCase :							Any				)->Union[str, Any]:
      __SCREAMING_SNAKE_CASE       :    List[str]       =	self.model(**UpperCamelCase				)
      return model_outputs






     def 	__snake_case    (       self :							Optional[int]     ,  UpperCamelCase :							List[str]     ,  UpperCamelCase :							Any=5				)->Union[str, Any]:
      if top_k > self.model.config.num_labels:
       __SCREAMING_SNAKE_CASE       :    Tuple       =	self.model.config.num_labels

      if self.framework == "pt":
       __SCREAMING_SNAKE_CASE       :    Optional[int]       =	model_outputs.logits.softmax(-1				)[0]
       __SCREAMING_SNAKE_CASE,		__SCREAMING_SNAKE_CASE       :    Tuple       =	probs.topk(UpperCamelCase				)
      else:
       raise ValueError(F"""Unsupported framework: {self.framework}"""				)

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











from diffusers.utils.testing_utils import require_onnxruntime

@require_onnxruntime
class       _SCREAMING_SNAKE_CASE	:
     pass






