
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 		UpperCamelCase__  (	unittest.TestCase   ):
   _SCREAMING_SNAKE_CASE				:							Optional[Any]     	=				MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING
   def 				lowerCAmelCase      (self			: Optional[int] ,    snake_case_			: Tuple ,    snake_case_			: Union[str, Any] ,    snake_case_			: Union[str, Any]       ):
    __a			:  Optional[Any]		    =  hf_hub_download(
        repo_id='''nateraw/video-demo''' ,    filename='''archery.mp4''' ,    repo_type='''dataset'''       )
    __a			:  Union[str, Any]		    =  VideoClassificationPipeline(model=snake_case_ ,    image_processor=snake_case_ ,    top_k=2       )
    __a			:  Dict		    =  [
        example_video_filepath,
        '''https://huggingface.co/datasets/nateraw/video-demo/resolve/main/archery.mp4''',
    ]
    return video_classifier, examples
   def 				lowerCAmelCase      (self			: str ,    snake_case_			: Tuple ,    snake_case_			: Optional[Any]       ):
    for example in examples:
     __a			:  List[Any]		    =  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 				lowerCAmelCase      (self			: Dict       ):
    __a			:  List[Any]		    =  '''hf-internal-testing/tiny-random-VideoMAEForVideoClassification'''
    __a			:  Any		    =  VideoMAEFeatureExtractor(
        size={'''shortest_edge''': 1_0} ,    crop_size={'''height''': 1_0, '''width''': 1_0}       )
    __a			:  Union[str, Any]		    =  pipeline(
        '''video-classification''' ,    model=snake_case_ ,    feature_extractor=snake_case_ ,    frame_sampling_rate=4       )

    __a			:  Optional[int]		    =  hf_hub_download(repo_id='''nateraw/video-demo''' ,    filename='''archery.mp4''' ,    repo_type='''dataset'''       )
    __a			:  List[Any]		    =  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			:  List[Any]		    =  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 				lowerCAmelCase      (self			: Union[str, Any]       ):
    pass


import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device


if is_torch_available():
      from transformers import AutoModelForSeqaSeqLM, AutoTokenizer



@require_torch
@require_sentencepiece
@require_tokenizers
class 		UpperCamelCase__  (	unittest.TestCase   ):
   @slow
   def 				lowerCAmelCase      (self			: Tuple       ):
    __a			:  List[str]		    =  AutoModelForSeqaSeqLM.from_pretrained('''google/mt5-small''' ,    return_dict=snake_case_       ).to(snake_case_       )
    __a			:  List[Any]		    =  AutoTokenizer.from_pretrained('''google/mt5-small'''       )

    __a			:  Optional[int]		    =  tokenizer('''Hello there''' ,    return_tensors='''pt'''       ).input_ids
    __a			:  Dict		    =  tokenizer('''Hi I am''' ,    return_tensors='''pt'''       ).input_ids

    __a			:  Optional[Any]		    =  model(input_ids.to(snake_case_       ) ,    labels=labels.to(snake_case_       )       ).loss
    __a			:  Tuple		    =  -(labels.shape[-1] * loss.item())

    __a			:  Dict		    =  -84.9127
    self.assertTrue(abs(mtf_score - EXPECTED_SCORE       ) < 1E-4       )
