



'''simple docstring'''
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 							lowercase__     (							unittest.TestCase       ):
	lowercase__      =      MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING





	def      UpperCamelCase_  (			self :    str			,lowerCamelCase__ :    List[Any]			,lowerCamelCase__ :    List[Any]			,lowerCamelCase__ :    str			):







							'''simple docstring'''
							_UpperCamelCase :		int			    =       hf_hub_download(
							    repo_id='nateraw/video-demo'			,filename='archery.mp4'			,repo_type='dataset'			)
							_UpperCamelCase :		List[str]			    =       VideoClassificationPipeline(model=lowerCamelCase__			,image_processor=lowerCamelCase__			,top_k=2			)
							_UpperCamelCase :		List[Any]			    =       [
							    example_video_filepath,
							    'https://huggingface.co/datasets/nateraw/video-demo/resolve/main/archery.mp4',
							]
							return video_classifier, examples





	def      UpperCamelCase_  (			self :    Union[str, Any]			,lowerCamelCase__ :    str			,lowerCamelCase__ :    Optional[Any]			):







							'''simple docstring'''
							for example in examples:
													_UpperCamelCase :		List[str]			    =       video_classifier(lowerCamelCase__			)

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





	@require_torch
	def      UpperCamelCase_  (			self :    Any			):







							'''simple docstring'''
							_UpperCamelCase :		Optional[int]			    =       'hf-internal-testing/tiny-random-VideoMAEForVideoClassification'
							_UpperCamelCase :		int			    =       VideoMAEFeatureExtractor(
							    size={'shortest_edge': 10}			,crop_size={'height': 10, 'width': 10}			)
							_UpperCamelCase :		Dict			    =       pipeline(
							    'video-classification'			,model=lowerCamelCase__			,feature_extractor=lowerCamelCase__			,frame_sampling_rate=4			)

							_UpperCamelCase :		List[Any]			    =       hf_hub_download(repo_id='nateraw/video-demo'			,filename='archery.mp4'			,repo_type='dataset'			)
							_UpperCamelCase :		List[str]			    =       video_classifier(lowerCamelCase__			,top_k=2			)
							self.assertEqual(
							    nested_simplify(lowerCamelCase__			,decimals=4			)			,[{'score': 0.5_1_9_9, 'label': 'LABEL_0'}, {'score': 0.4_8_0_1, 'label': 'LABEL_1'}]			,)

							_UpperCamelCase :		Union[str, Any]			    =       video_classifier(
							    [
							        video_file_path,
							        video_file_path,
							    ]			,top_k=2			,)
							self.assertEqual(
							    nested_simplify(lowerCamelCase__			,decimals=4			)			,[
							        [{'score': 0.5_1_9_9, 'label': 'LABEL_0'}, {'score': 0.4_8_0_1, 'label': 'LABEL_1'}],
							        [{'score': 0.5_1_9_9, 'label': 'LABEL_0'}, {'score': 0.4_8_0_1, 'label': 'LABEL_1'}],
							    ]			,)





	@require_tf
	def      UpperCamelCase_  (			self :    Dict			):







							'''simple docstring'''
							pass







'''simple docstring'''
import tempfile
import unittest

from transformers import AutoModelForSeqaSeqLM, AutoTokenizer
from transformers.testing_utils import (
    is_torch_available,
    require_optimum,
    require_torch,
    slow,
)


if is_torch_available():
					import torch






@require_torch
@require_optimum
@slow
class 							lowercase__     (							unittest.TestCase       ):





	def      UpperCamelCase_  (			self :    str			):







							'''simple docstring'''
							_UpperCamelCase :		Any			    =       'hf-internal-testing/tiny-random-t5'
							_UpperCamelCase :		str			    =       AutoTokenizer.from_pretrained(lowerCamelCase__			)
							_UpperCamelCase :		int			    =       AutoModelForSeqaSeqLM.from_pretrained(lowerCamelCase__			)

							_UpperCamelCase :		List[str]			    =       tokenizer('This is me'			,return_tensors='pt'			)

							_UpperCamelCase :		str			    =       model.to_bettertransformer()

							self.assertTrue(any('BetterTransformer' in mod.__class__.__name__ for _, mod in model.named_modules()			)			)

							_UpperCamelCase :		Optional[Any]			    =       model.generate(**lowerCamelCase__			)

							_UpperCamelCase :		Union[str, Any]			    =       model.reverse_bettertransformer()

							self.assertFalse(any('BetterTransformer' in mod.__class__.__name__ for _, mod in model.named_modules()			)			)

							with tempfile.TemporaryDirectory() as tmpdirname:
													model.save_pretrained(lowerCamelCase__			)

													_UpperCamelCase :		List[Any]			    =       AutoModelForSeqaSeqLM.from_pretrained(lowerCamelCase__			)

													self.assertFalse(
													    any('BetterTransformer' in mod.__class__.__name__ for _, mod in model_reloaded.named_modules()			)			)

													_UpperCamelCase :		Optional[Any]			    =       model_reloaded.generate(**lowerCamelCase__			)
													self.assertTrue(torch.allclose(lowerCamelCase__			,lowerCamelCase__			)			)





	def      UpperCamelCase_  (			self :    Any			):







							'''simple docstring'''
							_UpperCamelCase :		List[Any]			    =       'hf-internal-testing/tiny-random-t5'
							_UpperCamelCase :		List[Any]			    =       AutoModelForSeqaSeqLM.from_pretrained(lowerCamelCase__			)

							_UpperCamelCase :		List[str]			    =       model.to_bettertransformer()

							with tempfile.TemporaryDirectory() as tmpdirname:
													with self.assertRaises(lowerCamelCase__			):
																			model.save_pretrained(lowerCamelCase__			)

													_UpperCamelCase :		str			    =       model.reverse_bettertransformer()
													model.save_pretrained(lowerCamelCase__			)


