




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(__magic_name__					)
class        __lowerCAmelCase					(			__magic_name__					):

			"""simple docstring"""







			def __init__( self     ,		*lowerCamelCase__     ,		**lowerCamelCase__				)       -> List[Any]:



							'''simple docstring'''
							super().__init__(*lowerCamelCase__     ,		**lowerCamelCase__				)
							requires_backends(self     ,		'decord'				)
							self.check_model_type(lowerCamelCase__				)


			def  lowercase_       ( self     ,		lowerCamelCase__=None     ,		lowerCamelCase__=None     ,		lowerCamelCase__=None				)       -> Union[str, Any]:



							'''simple docstring'''
							__lowerCamelCase									=       {}
							if frame_sampling_rate is not None:
											__lowerCamelCase									=       frame_sampling_rate
							if num_frames is not None:
											__lowerCamelCase									=       num_frames

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


			def __call__( self     ,		lowerCamelCase__     ,		**lowerCamelCase__				)       -> Tuple:



							'''simple docstring'''
							return super().__call__(lowerCamelCase__     ,		**lowerCamelCase__				)


			def  lowercase_       ( self     ,		lowerCamelCase__     ,		lowerCamelCase__=None     ,		lowerCamelCase__=1				)       -> List[str]:



							'''simple docstring'''
							if num_frames is None:
											__lowerCamelCase									=       self.model.config.num_frames

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

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

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

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

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


			def  lowercase_       ( self     ,		lowerCamelCase__				)       -> Dict:



							'''simple docstring'''
							__lowerCamelCase									=       self.model(**lowerCamelCase__				)
							return model_outputs






			def  lowercase_       ( self     ,		lowerCamelCase__     ,		lowerCamelCase__=5				)       -> Tuple:



							'''simple docstring'''
							if top_k > self.model.config.num_labels:
											__lowerCamelCase									=       self.model.config.num_labels

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

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








import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_torch


if is_torch_available():
					import torch

					from transformers.activations import gelu_new, gelu_python, get_activation







@require_torch
class        __lowerCAmelCase					(			unittest.TestCase					):

			"""simple docstring"""







			def  lowercase_       ( self				)       -> int:



							'''simple docstring'''
							__lowerCamelCase									=       torch.tensor([-100, -1, -0.1, 0, 0.1, 1.0, 100]				)
							__lowerCamelCase									=       get_activation('gelu'				)
							self.assertTrue(torch.allclose(gelu_python(lowerCamelCase__				)     ,		torch_builtin(lowerCamelCase__				)				)				)
							self.assertFalse(torch.allclose(gelu_python(lowerCamelCase__				)     ,		gelu_new(lowerCamelCase__				)				)				)


			def  lowercase_       ( self				)       -> Optional[int]:



							'''simple docstring'''
							__lowerCamelCase									=       torch.tensor([-100, -1, -0.1, 0, 0.1, 1.0, 100]				)
							__lowerCamelCase									=       get_activation('gelu'				)
							__lowerCamelCase									=       get_activation('gelu_10'				)

							__lowerCamelCase									=       torch_builtin(lowerCamelCase__				)
							__lowerCamelCase									=       geluaa(lowerCamelCase__				)

							__lowerCamelCase									=       torch.where(y_gelu_aa < 10.0     ,		1     ,		0				)

							self.assertTrue(torch.max(lowerCamelCase__				).item() == 10.0				)
							self.assertTrue(torch.allclose(y_gelu * clipped_mask     ,		y_gelu_aa * clipped_mask				)				)


			def  lowercase_       ( self				)       -> Any:



							'''simple docstring'''
							get_activation('gelu'				)
							get_activation('gelu_10'				)
							get_activation('gelu_fast'				)
							get_activation('gelu_new'				)
							get_activation('gelu_python'				)
							get_activation('gelu_pytorch_tanh'				)
							get_activation('linear'				)
							get_activation('mish'				)
							get_activation('quick_gelu'				)
							get_activation('relu'				)
							get_activation('sigmoid'				)
							get_activation('silu'				)
							get_activation('swish'				)
							get_activation('tanh'				)
							with self.assertRaises(lowerCamelCase__				):
											get_activation('bogus'				)
							with self.assertRaises(lowerCamelCase__				):
											get_activation(lowerCamelCase__				)






			def  lowercase_       ( self				)       -> Dict:



							'''simple docstring'''
							__lowerCamelCase									=       get_activation('gelu'				)
							__lowerCamelCase									=       1
							__lowerCamelCase									=       get_activation('gelu'				)
							self.assertEqual(acta.a     ,		1				)
							with self.assertRaises(lowerCamelCase__				):
											__lowerCamelCase									=       acta.a


