





"""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

lowerCAmelCase__     			=			logging.get_logger(__name__)





@add_end_docstrings(__UpperCAmelCase )
class        _lowerCamelCase      (       __UpperCAmelCase ):


						def __init__(self   ,							*__a   ,							**__a		) ->   Union[str, Any]:
								super().__init__(*__a   ,							**__a		)
								self.check_model_type(__a		)


						def     snake_case_   (self   ,							__a=None   ,							__a=None   ,							__a=None   ,							**__a		) ->   int:
								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   ,							__a   ,							__a = None   ,							**__a		) ->   Tuple:

								if isinstance(__a   ,							(Image.Image, str)		) and isinstance(__a   ,							__a		):
										UpperCamelCase   =	{"image": image, "question": question}
								else:
										UpperCamelCase   =	image
								UpperCamelCase   =	super().__call__(__a   ,							**__a		)
								return results


						def     snake_case_   (self   ,							__a   ,							__a=False   ,							__a=False		) ->   Optional[int]:
								UpperCamelCase   =	load_image(inputs["image"]		)
								UpperCamelCase   =	self.tokenizer(
								    inputs["question"]   ,							return_tensors=self.framework   ,							padding=__a   ,							truncation=__a		)
								UpperCamelCase   =	self.image_processor(images=__a   ,							return_tensors=self.framework		)
								model_inputs.update(__a		)
								return model_inputs


						def     snake_case_   (self   ,							__a		) ->   List[str]:
								UpperCamelCase   =	self.model(**__a		)
								return model_outputs




						def     snake_case_   (self   ,							__a   ,							__a=5		) ->   List[Any]:

								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(__a		)
								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(__a   ,							__a		)]







"""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        _lowerCamelCase      (       unittest.TestCase ):
						UpperCAmelCase_ =			MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING


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


						def     snake_case_   (self   ,							__a   ,							__a		) ->   str:
								for example in examples:
										UpperCamelCase   =	video_classifier(__a		)

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


						@require_torch
						def     snake_case_   (self		) ->   int:
								UpperCamelCase   =	"hf-internal-testing/tiny-random-VideoMAEForVideoClassification"
								UpperCamelCase   =	VideoMAEFeatureExtractor(
								    size={"shortest_edge": 10}   ,							crop_size={"height": 10, "width": 10}		)
								UpperCamelCase   =	pipeline(
								    "video-classification"   ,							model=__a   ,							feature_extractor=__a   ,							frame_sampling_rate=4		)

								UpperCamelCase   =	hf_hub_download(repo_id="nateraw/video-demo"   ,							filename="archery.mp4"   ,							repo_type="dataset"		)
								UpperCamelCase   =	video_classifier(__a   ,							top_k=2		)
								self.assertEqual(
								    nested_simplify(__a   ,							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(__a   ,							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     snake_case_   (self		) ->   Optional[Any]:
								pass






