




'''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     _a						(			unittest.TestCase				):
       __a       :  int            =      MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING
       def 					A   (			self		:			Optional[Any]     ,       lowercase		:			Dict     ,       lowercase		:			str     ,       lowercase		:			List[str]       ):



           '''simple docstring'''

           UpperCAmelCase     =	hf_hub_download(
               repo_id='''nateraw/video-demo'''     ,       filename='''archery.mp4'''     ,       repo_type='''dataset'''       )
           UpperCAmelCase     =	VideoClassificationPipeline(model=UpperCamelCase_     ,       image_processor=UpperCamelCase_     ,       top_k=2       )
           UpperCAmelCase     =	[
               example_video_filepath,
               "https://huggingface.co/datasets/nateraw/video-demo/resolve/main/archery.mp4",
           ]
           return video_classifier, examples
       def 					A   (			self		:			int     ,       lowercase		:			Optional[int]     ,       lowercase		:			str       ):



           '''simple docstring'''

           for example in examples:
               UpperCAmelCase     =	video_classifier(UpperCamelCase_       )

               self.assertEqual(
                   UpperCamelCase_     ,       [
                       {'''score''': ANY(UpperCamelCase_       ), '''label''': ANY(UpperCamelCase_       )},
                       {'''score''': ANY(UpperCamelCase_       ), '''label''': ANY(UpperCamelCase_       )},
                   ]     ,       )
       @require_torch
       def 					A   (			self		:			str       ):



           '''simple docstring'''

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

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

           UpperCAmelCase     =	video_classifier(
               [
                   video_file_path,
                   video_file_path,
               ]     ,       top_k=2     ,       )
           self.assertEqual(
               nested_simplify(UpperCamelCase_     ,       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		:			Union[str, Any]       ):



           '''simple docstring'''

           pass






'''simple docstring'''







from typing import Union

from ..utils import add_end_docstrings, is_torch_available, is_vision_available, logging
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_vision_available():
   from PIL import Image

   from ..image_utils import load_image

if is_torch_available():
   from ..models.auto.modeling_auto import MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING

A									=logging.get_logger(__name__)


@add_end_docstrings(__a				)
class     _a						(			__a				):
       def __init__(			self		:			Optional[int]     ,       *lowercase		:			Any     ,       **lowercase		:			Optional[int]       ):



           '''simple docstring'''

           super().__init__(*lowercase     ,       **lowercase       )
           self.check_model_type(lowercase       )
       def 					A   (			self		:			List[str]     ,       lowercase		:			str=None     ,       lowercase		:			List[str]=None     ,       lowercase		:			List[str]=None     ,       **lowercase		:			List[str]       ):



           '''simple docstring'''

           UpperCAmelCase ,  UpperCAmelCase     =	{}, {}
           if padding is not None:
               UpperCAmelCase     =	padding
           if truncation is not None:
               UpperCAmelCase     =	truncation
           if top_k is not None:
               UpperCAmelCase     =	top_k
           return preprocess_params, {}, postprocess_params
       def __call__(			self		:			Optional[Any]     ,       lowercase		:			Union["Image.Image", str]     ,       lowercase		:			str = None     ,       **lowercase		:			Optional[int]       ):



           '''simple docstring'''

           if isinstance(lowercase     ,       (Image.Image, str)       ) and isinstance(lowercase     ,       lowercase       ):
               UpperCAmelCase     =	{'''image''': image, '''question''': question}
           else:
               UpperCAmelCase     =	image
           UpperCAmelCase     =	super().__call__(lowercase     ,       **lowercase       )
           return results
       def 					A   (			self		:			List[Any]     ,       lowercase		:			List[str]     ,       lowercase		:			Any=False     ,       lowercase		:			Any=False       ):



           '''simple docstring'''

           UpperCAmelCase     =	load_image(inputs['''image''']       )
           UpperCAmelCase     =	self.tokenizer(
               inputs['''question''']     ,       return_tensors=self.framework     ,       padding=lowercase     ,       truncation=lowercase       )
           UpperCAmelCase     =	self.image_processor(images=lowercase     ,       return_tensors=self.framework       )
           model_inputs.update(lowercase       )
           return model_inputs
       def 					A   (			self		:			Dict     ,       lowercase		:			Optional[Any]       ):



           '''simple docstring'''

           UpperCAmelCase     =	self.model(**lowercase       )
           return model_outputs







       def 					A   (			self		:			Union[str, Any]     ,       lowercase		:			int     ,       lowercase		:			Optional[Any]=5       ):



           '''simple docstring'''

           if top_k > self.model.config.num_labels:
               UpperCAmelCase     =	self.model.config.num_labels

           if self.framework == "pt":
               UpperCAmelCase     =	model_outputs.logits.sigmoid()[0]
               UpperCAmelCase ,  UpperCAmelCase     =	probs.topk(lowercase       )
           else:
               raise ValueError(f"Unsupported framework: {self.framework}"       )

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




