


import unittest

from huggingface_hub import hf_hub_download

from transformers import MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING, VideoMAEFeatureExtractor
from transformers.pipelines import VideoClassificationPipeline, pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_decord,
    require_tf,
    require_torch,
    require_torch_or_tf,
    require_vision,
)

from .test_pipelines_common import ANY



@is_pipeline_test
@require_torch_or_tf
@require_vision
@require_decord
class 				A       (			unittest.TestCase				):
					'''simple docstring'''






					A__     =					MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING

					def     lowerCamelCase__	(self       :		List[str] ,   _UpperCAmelCase       :		List[str] ,   _UpperCAmelCase       :		Dict ,   _UpperCAmelCase       :		Tuple						)		->  Tuple:




							"""simple docstring"""


							lowercase__	     =						hf_hub_download(
							    repo_id="""nateraw/video-demo""" ,   filename="""archery.mp4""" ,   repo_type="""dataset"""						)
							lowercase__	     =						VideoClassificationPipeline(model=_UpperCAmelCase ,   image_processor=_UpperCAmelCase ,   top_k=2						)
							lowercase__	     =						[
							    example_video_filepath,
							    """https://huggingface.co/datasets/nateraw/video-demo/resolve/main/archery.mp4""",
							]
							return video_classifier, examples

					def     lowerCamelCase__	(self       :		List[str] ,   _UpperCAmelCase       :		Union[str, Any] ,   _UpperCAmelCase       :		Tuple						)		->  List[Any]:




							"""simple docstring"""


							for example in examples:
									lowercase__	     =						video_classifier(_UpperCAmelCase						)

									self.assertEqual(
									    _UpperCAmelCase ,   [
									        {"""score""": ANY(_UpperCAmelCase						), """label""": ANY(_UpperCAmelCase						)},
									        {"""score""": ANY(_UpperCAmelCase						), """label""": ANY(_UpperCAmelCase						)},
									    ] ,   )

					@require_torch
					def     lowerCamelCase__	(self       :		str						)		->  List[Any]:




							"""simple docstring"""


							lowercase__	     =						"""hf-internal-testing/tiny-random-VideoMAEForVideoClassification"""
							lowercase__	     =						VideoMAEFeatureExtractor(
							    size={"""shortest_edge""": 10} ,   crop_size={"""height""": 10, """width""": 10}						)
							lowercase__	     =						pipeline(
							    """video-classification""" ,   model=_UpperCAmelCase ,   feature_extractor=_UpperCAmelCase ,   frame_sampling_rate=4						)

							lowercase__	     =						hf_hub_download(repo_id="""nateraw/video-demo""" ,   filename="""archery.mp4""" ,   repo_type="""dataset"""						)
							lowercase__	     =						video_classifier(_UpperCAmelCase ,   top_k=2						)
							self.assertEqual(
							    nested_simplify(_UpperCAmelCase ,   decimals=4						) ,   [{"""score""": 0.5_199, """label""": """LABEL_0"""}, {"""score""": 0.4_801, """label""": """LABEL_1"""}] ,   )

							lowercase__	     =						video_classifier(
							    [
							        video_file_path,
							        video_file_path,
							    ] ,   top_k=2 ,   )
							self.assertEqual(
							    nested_simplify(_UpperCAmelCase ,   decimals=4						) ,   [
							        [{"""score""": 0.5_199, """label""": """LABEL_0"""}, {"""score""": 0.4_801, """label""": """LABEL_1"""}],
							        [{"""score""": 0.5_199, """label""": """LABEL_0"""}, {"""score""": 0.4_801, """label""": """LABEL_1"""}],
							    ] ,   )


					@require_tf
					def     lowerCamelCase__	(self       :		Any						)		->  Union[str, Any]:




							"""simple docstring"""


							pass






"""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 		__snake_case		( SCREAMING_SNAKE_CASE__      :		Optional[int]							)    ->  List[str]:





     '''simple docstring'''

     _UpperCAmelCase     :      Any														=       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.'							)
     _UpperCAmelCase     :      int														=       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.'							)

     _UpperCAmelCase     :      Any														=       components[:-1] + [test_fn.replace(".py"		,    ""							)]
     _UpperCAmelCase     :      List[str]														=       ".".join(SCREAMING_SNAKE_CASE__							)

     return test_module_path

def 		__snake_case		( SCREAMING_SNAKE_CASE__      :		List[Any]							)    ->  Dict:





     '''simple docstring'''

     _UpperCAmelCase     :      Optional[int]														=       get_module_path(SCREAMING_SNAKE_CASE__							)
     _UpperCAmelCase     :      List[Any]														=       importlib.import_module(SCREAMING_SNAKE_CASE__							)

     return test_module

def 		__snake_case		( SCREAMING_SNAKE_CASE__      :		Union[str, Any]							)    ->  List[str]:





     '''simple docstring'''

     _UpperCAmelCase     :      Union[str, Any]														=       []
     _UpperCAmelCase     :      str														=       get_test_module(SCREAMING_SNAKE_CASE__							)
     for attr in dir(SCREAMING_SNAKE_CASE__							):
          if attr.endswith("ModelTester"							):
               tester_classes.append(getattr(SCREAMING_SNAKE_CASE__		,    SCREAMING_SNAKE_CASE__							)							)

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

def 		__snake_case		( SCREAMING_SNAKE_CASE__      :		int							)    ->  Optional[int]:





     '''simple docstring'''

     _UpperCAmelCase     :      Union[str, Any]														=       []
     _UpperCAmelCase     :      Tuple														=       get_test_module(SCREAMING_SNAKE_CASE__							)
     for attr in dir(SCREAMING_SNAKE_CASE__							):
          _UpperCAmelCase     :      List[Any]														=       getattr(SCREAMING_SNAKE_CASE__		,    SCREAMING_SNAKE_CASE__							)
          # (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).
          _UpperCAmelCase     :      Optional[Any]														=       getattr(SCREAMING_SNAKE_CASE__		,    "all_model_classes"		,    []							)
          if len(SCREAMING_SNAKE_CASE__							) > 0:
               test_classes.append(SCREAMING_SNAKE_CASE__							)

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

def 		__snake_case		( SCREAMING_SNAKE_CASE__      :		int							)    ->  int:





     '''simple docstring'''

     _UpperCAmelCase     :      Union[str, Any]														=       get_test_classes(SCREAMING_SNAKE_CASE__							)
     _UpperCAmelCase     :      Tuple														=       set()
     for test_class in test_classes:
          model_classes.update(test_class.all_model_classes							)

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

def 		__snake_case		( SCREAMING_SNAKE_CASE__      :		Tuple							)    ->  Any:





     '''simple docstring'''

     _UpperCAmelCase     :      Optional[Any]														=       test_class()
     if hasattr(SCREAMING_SNAKE_CASE__		,    "setUp"							):
          test.setUp()

     _UpperCAmelCase     :      int														=       None
     if hasattr(SCREAMING_SNAKE_CASE__		,    "model_tester"							):
          # `(TF/Flax)ModelTesterMixin` has this attribute default to `None`. Let's skip this case.
          if test.model_tester is not None:
               _UpperCAmelCase     :      List[str]														=       test.model_tester.__class__

     return model_tester

def 		__snake_case		( SCREAMING_SNAKE_CASE__      :		Optional[int]		,    SCREAMING_SNAKE_CASE__      :		int							)    ->  Union[str, Any]:





     '''simple docstring'''

     _UpperCAmelCase     :      str														=       get_test_classes(SCREAMING_SNAKE_CASE__							)

     _UpperCAmelCase     :      Optional[int]														=       []
     for test_class in test_classes:
          if model_class in test_class.all_model_classes:
               target_test_classes.append(SCREAMING_SNAKE_CASE__							)

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

def 		__snake_case		( SCREAMING_SNAKE_CASE__      :		Optional[int]		,    SCREAMING_SNAKE_CASE__      :		List[Any]							)    ->  Optional[Any]:





     '''simple docstring'''

     _UpperCAmelCase     :      str														=       get_test_classes_for_model(SCREAMING_SNAKE_CASE__		,    SCREAMING_SNAKE_CASE__							)

     _UpperCAmelCase     :      int														=       []
     for test_class in test_classes:
          _UpperCAmelCase     :      Optional[Any]														=       get_model_tester_from_test_class(SCREAMING_SNAKE_CASE__							)
          if tester_class is not None:
               tester_classes.append(SCREAMING_SNAKE_CASE__							)

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

def 		__snake_case		( SCREAMING_SNAKE_CASE__      :		Any							)    ->  int:





     '''simple docstring'''

     _UpperCAmelCase     :      Optional[Any]														=       get_test_classes(SCREAMING_SNAKE_CASE__							)
     _UpperCAmelCase     :      List[Any]														=       {test_class: get_model_tester_from_test_class(SCREAMING_SNAKE_CASE__							) for test_class in test_classes}
     return test_tester_mapping

def 		__snake_case		( SCREAMING_SNAKE_CASE__      :		Optional[int]							)    ->  Dict:





     '''simple docstring'''

     _UpperCAmelCase     :      Optional[Any]														=       get_model_classes(SCREAMING_SNAKE_CASE__							)
     _UpperCAmelCase     :      int														=       {
         model_class: get_test_classes_for_model(SCREAMING_SNAKE_CASE__		,    SCREAMING_SNAKE_CASE__							) for model_class in model_classes
     }
     return model_test_mapping

def 		__snake_case		( SCREAMING_SNAKE_CASE__      :		Optional[int]							)    ->  Tuple:





     '''simple docstring'''

     _UpperCAmelCase     :      Tuple														=       get_model_classes(SCREAMING_SNAKE_CASE__							)
     _UpperCAmelCase     :      Union[str, Any]														=       {
         model_class: get_tester_classes_for_model(SCREAMING_SNAKE_CASE__		,    SCREAMING_SNAKE_CASE__							) for model_class in model_classes
     }
     return model_to_tester_mapping

def 		__snake_case		( SCREAMING_SNAKE_CASE__      :		str							)    ->  List[str]:





     '''simple docstring'''

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