



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      __lowerCAmelCase     (		unittest.TestCase      ):
					"""simple docstring"""






					A__				:		Optional[int]			       =    MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING



					def     _a							(     self					:       List[Any]				,					_snake_case					:       List[str]				,					_snake_case					:       Tuple				,					_snake_case					:       Union[str, Any]					):






								"""simple docstring"""


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



					def     _a							(     self					:       List[str]				,					_snake_case					:       Optional[Any]				,					_snake_case					:       Dict					):






								"""simple docstring"""


								for example in examples:
											A__       =    video_classifier(_snake_case					)

											self.assertEqual(
											    _snake_case				,					[
											        {'score': ANY(_snake_case					), 'label': ANY(_snake_case					)},
											        {'score': ANY(_snake_case					), 'label': ANY(_snake_case					)},
											    ]				,					)



					@require_torch
					def     _a							(     self					:       Dict					):






								"""simple docstring"""


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

								A__       =    hf_hub_download(repo_id='nateraw/video-demo'				,					filename='archery.mp4'				,					repo_type='dataset'					)
								A__       =    video_classifier(_snake_case				,					top_k=2					)
								self.assertEqual(
								    nested_simplify(_snake_case				,					decimals=4					)				,					[{'score': 0.5199, 'label': 'LABEL_0'}, {'score': 0.4801, 'label': 'LABEL_1'}]				,					)

								A__       =    video_classifier(
								    [
								        video_file_path,
								        video_file_path,
								    ]				,					top_k=2				,					)
								self.assertEqual(
								    nested_simplify(_snake_case				,					decimals=4					)				,					[
								        [{'score': 0.5199, 'label': 'LABEL_0'}, {'score': 0.4801, 'label': 'LABEL_1'}],
								        [{'score': 0.5199, 'label': 'LABEL_0'}, {'score': 0.4801, 'label': 'LABEL_1'}],
								    ]				,					)



					@require_tf
					def     _a							(     self					:       List[Any]					):






								"""simple docstring"""


								pass









import unittest

from transformers import is_flax_available
from transformers.testing_utils import require_flax, require_sentencepiece, require_tokenizers, require_torch, slow


if is_flax_available():
							import optax
							from flax.training.common_utils import onehot

							from transformers import AutoTokenizer, FlaxMTaForConditionalGeneration
							from transformers.models.ta.modeling_flax_ta import shift_tokens_right


@require_torch
@require_sentencepiece
@require_tokenizers
@require_flax
class      __lowerCAmelCase     (		unittest.TestCase      ):
					"""simple docstring"""






					@slow
					def     _a							(     self					:       int					):






								"""simple docstring"""


								A__       =    FlaxMTaForConditionalGeneration.from_pretrained('google/mt5-small'					)
								A__       =    AutoTokenizer.from_pretrained('google/mt5-small'					)

								A__       =    tokenizer('Hello there'				,					return_tensors='np'					).input_ids
								A__       =    tokenizer('Hi I am'				,					return_tensors='np'					).input_ids

								A__       =    shift_tokens_right(_snake_case				,					model.config.pad_token_id				,					model.config.decoder_start_token_id					)

								A__       =    model(_snake_case				,					decoder_input_ids=_snake_case					).logits
								A__       =    optax.softmax_cross_entropy(_snake_case				,					onehot(_snake_case				,					logits.shape[-1]					)					).mean()

								A__       =    -(labels.shape[-1] * loss.item())

								A__       =    -84.9127
								self.assertTrue(abs(mtf_score - EXPECTED_SCORE					) < 1E-4					)




