"""simple docstring"""




import importlib
import os
import sys


# This is required to make the module import works (when the python process is running from the root of the repo)
sys.path.append(".")






def      UpperCAmelCase    (		UpperCamelCase__ ):






							"""simple docstring"""





							A__		   =							test_file.split(os.path.sep )
							if components[0:2] != ["tests", "models"]:
														raise ValueError(
														    '`test_file` should start with `tests/models/` (with `/` being the OS specific path separator). Got '
														    F'''{test_file} instead.''' )
							A__		   =							components[-1]
							if not test_fn.endswith('py' ):
														raise ValueError(F'''`test_file` should be a python file. Got {test_fn} instead.''' )
							if not test_fn.startswith('test_modeling_' ):
														raise ValueError(
														    F'''`test_file` should point to a file name of the form `test_modeling_*.py`. Got {test_fn} instead.''' )

							A__		   =							components[:-1] + [test_fn.replace('.py'				,			'' )]
							A__		   =							'.'.join(UpperCamelCase__ )

							return test_module_path






def      UpperCAmelCase    (		UpperCamelCase__ ):






							"""simple docstring"""





							A__		   =							get_module_path(UpperCamelCase__ )
							A__		   =							importlib.import_module(UpperCamelCase__ )

							return test_module






def      UpperCAmelCase    (		UpperCamelCase__ ):






							"""simple docstring"""





							A__		   =							[]
							A__		   =							get_test_module(UpperCamelCase__ )
							for attr in dir(UpperCamelCase__ ):
														if attr.endswith('ModelTester' ):
																					tester_classes.append(getattr(UpperCamelCase__				,			UpperCamelCase__ ) )

    # sort with class names
							return sorted(UpperCamelCase__				,			key=lambda UpperCamelCase__ : x.__name__ )






def      UpperCAmelCase    (		UpperCamelCase__ ):






							"""simple docstring"""





							A__		   =							[]
							A__		   =							get_test_module(UpperCamelCase__ )
							for attr in dir(UpperCamelCase__ ):
														A__		   =							getattr(UpperCamelCase__				,			UpperCamelCase__ )
														# (TF/Flax)ModelTesterMixin is also an attribute in specific model test module. Let's exclude them by checking
														# `all_model_classes` is not empty (which also excludes other special classes).
														A__		   =							getattr(UpperCamelCase__				,			'all_model_classes'				,			[] )
														if len(UpperCamelCase__ ) > 0:
																					test_classes.append(UpperCamelCase__ )

    # sort with class names
							return sorted(UpperCamelCase__				,			key=lambda UpperCamelCase__ : x.__name__ )






def      UpperCAmelCase    (		UpperCamelCase__ ):






							"""simple docstring"""





							A__		   =							get_test_classes(UpperCamelCase__ )
							A__		   =							set()
							for test_class in test_classes:
														model_classes.update(test_class.all_model_classes )

							# sort with class names
							return sorted(UpperCamelCase__				,			key=lambda UpperCamelCase__ : x.__name__ )






def      UpperCAmelCase    (		UpperCamelCase__ ):






							"""simple docstring"""





							A__		   =							test_class()
							if hasattr(UpperCamelCase__				,			'setUp' ):
														test.setUp()

							A__		   =							None
							if hasattr(UpperCamelCase__				,			'model_tester' ):
														# `(TF/Flax)ModelTesterMixin` has this attribute default to `None`. Let's skip this case.
														if test.model_tester is not None:
																					A__		   =							test.model_tester.__class__

							return model_tester






def      UpperCAmelCase    (		UpperCamelCase__				,			UpperCamelCase__ ):






							"""simple docstring"""





							A__		   =							get_test_classes(UpperCamelCase__ )

							A__		   =							[]
							for test_class in test_classes:
														if model_class in test_class.all_model_classes:
																					target_test_classes.append(UpperCamelCase__ )

    # sort with class names
							return sorted(UpperCamelCase__				,			key=lambda UpperCamelCase__ : x.__name__ )






def      UpperCAmelCase    (		UpperCamelCase__				,			UpperCamelCase__ ):






							"""simple docstring"""





							A__		   =							get_test_classes_for_model(UpperCamelCase__				,			UpperCamelCase__ )

							A__		   =							[]
							for test_class in test_classes:
														A__		   =							get_model_tester_from_test_class(UpperCamelCase__ )
														if tester_class is not None:
																					tester_classes.append(UpperCamelCase__ )

    # sort with class names
							return sorted(UpperCamelCase__				,			key=lambda UpperCamelCase__ : x.__name__ )






def      UpperCAmelCase    (		UpperCamelCase__ ):






							"""simple docstring"""





							A__		   =							get_test_classes(UpperCamelCase__ )
							A__		   =							{test_class: get_model_tester_from_test_class(UpperCamelCase__ ) for test_class in test_classes}
							return test_tester_mapping






def      UpperCAmelCase    (		UpperCamelCase__ ):






							"""simple docstring"""





							A__		   =							get_model_classes(UpperCamelCase__ )
							A__		   =							{
							    model_class: get_test_classes_for_model(UpperCamelCase__				,			UpperCamelCase__ ) for model_class in model_classes
							}
							return model_test_mapping






def      UpperCAmelCase    (		UpperCamelCase__ ):






							"""simple docstring"""





							A__		   =							get_model_classes(UpperCamelCase__ )
							A__		   =							{
							    model_class: get_tester_classes_for_model(UpperCamelCase__				,			UpperCamelCase__ ) for model_class in model_classes
							}
							return model_to_tester_mapping






def      UpperCAmelCase    (		UpperCamelCase__ ):






							"""simple docstring"""





							if isinstance(UpperCamelCase__				,			UpperCamelCase__ ):
														return o
							elif isinstance(UpperCamelCase__				,			UpperCamelCase__ ):
														return o.__name__
							elif isinstance(UpperCamelCase__				,			(list, tuple) ):
														return [to_json(UpperCamelCase__ ) for x in o]
							elif isinstance(UpperCamelCase__				,			UpperCamelCase__ ):
														return {to_json(UpperCamelCase__ ): to_json(UpperCamelCase__ ) for k, v in o.items()}
							else:
														return o






"""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							 =     logging.get_logger(__name__)
@add_end_docstrings(__A       )
class 			UpperCamelCase__(	__A       ):





						def __init__(				self						,*__UpperCAmelCase						,**__UpperCAmelCase   )    -> Tuple:
													super().__init__(*__UpperCAmelCase						,**__UpperCAmelCase   )
													requires_backends(self						,'decord'   )
													self.check_model_type(__UpperCAmelCase   )





						def  snake_case__		(				self						,__UpperCAmelCase=None						,__UpperCAmelCase=None						,__UpperCAmelCase=None   )    -> int:
													A__		   =							{}
													if frame_sampling_rate is not None:
																				A__		   =							frame_sampling_rate
													if num_frames is not None:
																				A__		   =							num_frames

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





						def __call__(				self						,__UpperCAmelCase						,**__UpperCAmelCase   )    -> Dict:
													return super().__call__(__UpperCAmelCase						,**__UpperCAmelCase   )





						def  snake_case__		(				self						,__UpperCAmelCase						,__UpperCAmelCase=None						,__UpperCAmelCase=1   )    -> Union[str, Any]:
													if num_frames is None:
																				A__		   =							self.model.config.num_frames

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

													A__		   =							VideoReader(__UpperCAmelCase   )
													videoreader.seek(0   )

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

													A__		   =							videoreader.get_batch(__UpperCAmelCase   ).asnumpy()
													A__		   =							list(__UpperCAmelCase   )

													A__		   =							self.image_processor(__UpperCAmelCase						,return_tensors=self.framework   )
													return model_inputs





						def  snake_case__		(				self						,__UpperCAmelCase   )    -> Dict:
													A__		   =							self.model(**__UpperCAmelCase   )
													return model_outputs





						def  snake_case__		(				self						,__UpperCAmelCase						,__UpperCAmelCase=5   )    -> Union[str, Any]:
													if top_k > self.model.config.num_labels:
																				A__		   =							self.model.config.num_labels

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

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





