

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					:  int             =      logging.get_logger(__name__)






@add_end_docstrings(A__	)
class 	UpperCAmelCase__		(	A__	):






  """simple docstring"""


  def __init__(							self						:		Tuple     ,       *__lowerCamelCase						:		Dict     ,       **__lowerCamelCase						:		List[str] )		->							List[Any]:
       super().__init__(*__lowerCamelCase     ,       **__lowerCamelCase )
       requires_backends(self     ,       '''decord''' )
       self.check_model_type(__lowerCamelCase )






  def 				lowercase_		(							self						:		Any     ,       __lowerCamelCase						:		List[str]=None     ,       __lowerCamelCase						:		Union[str, Any]=None     ,       __lowerCamelCase						:		int=None )		->							List[Any]:
       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						:		Tuple     ,       __lowerCamelCase						:		Union[str, List[str]]     ,       **__lowerCamelCase						:		int )		->							Tuple:
       return super().__call__(__lowerCamelCase     ,       **__lowerCamelCase )






  def 				lowercase_		(							self						:		Dict     ,       __lowerCamelCase						:		str     ,       __lowerCamelCase						:		Optional[int]=None     ,       __lowerCamelCase						:		Optional[int]=1 )		->							List[str]:
       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(__lowerCamelCase ).content )

       SCREAMING_SNAKE_CASE__   =   VideoReader(__lowerCamelCase )
       videoreader.seek(0 )

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

       SCREAMING_SNAKE_CASE__   =   videoreader.get_batch(__lowerCamelCase ).asnumpy()
       SCREAMING_SNAKE_CASE__   =   list(__lowerCamelCase )

       SCREAMING_SNAKE_CASE__   =   self.image_processor(__lowerCamelCase     ,       return_tensors=self.framework )
       return model_inputs






  def 				lowercase_		(							self						:		str     ,       __lowerCamelCase						:		Dict )		->							Union[str, Any]:
       SCREAMING_SNAKE_CASE__   =   self.model(**__lowerCamelCase )
       return model_outputs







  def 				lowercase_		(							self						:		Union[str, Any]     ,       __lowerCamelCase						:		List[str]     ,       __lowerCamelCase						:		Optional[Any]=5 )		->							Union[str, Any]:
       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(__lowerCamelCase )
       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(__lowerCamelCase     ,       __lowerCamelCase )]





import warnings

from .generation import TFGenerationMixin






class 	UpperCAmelCase__		(	A__	):






  """simple docstring"""


  warnings.warn(
      "Importing `TFGenerationMixin` from `src/transformers/generation_tf_utils.py` is deprecated and will "
      "be removed in Transformers v5. Import as `from transformers import TFGenerationMixin` instead."						,					A__						,					)


