
"""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 						SCREAMING_SNAKE_CASE_ (						unittest.TestCase ):




		"""simple docstring"""





		__lowercase							:  Union[str, Any]						   =		MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING




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




		def 	snake_case_     (     self							,  lowerCAmelCase__							,  lowerCAmelCase__):
				for example in examples:
						__SCREAMING_SNAKE_CASE            = video_classifier(lowerCAmelCase__)

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




		@require_torch
		def 	snake_case_     (     self):
				__SCREAMING_SNAKE_CASE            = """hf-internal-testing/tiny-random-VideoMAEForVideoClassification"""
				__SCREAMING_SNAKE_CASE            = VideoMAEFeatureExtractor(
				    size={"""shortest_edge""": 1_0}							,  crop_size={"""height""": 1_0, """width""": 1_0})
				__SCREAMING_SNAKE_CASE            = pipeline(
				    """video-classification"""							,  model=lowerCAmelCase__							,  feature_extractor=lowerCAmelCase__							,  frame_sampling_rate=4)

				__SCREAMING_SNAKE_CASE            = hf_hub_download(repo_id="""nateraw/video-demo"""							,  filename="""archery.mp4"""							,  repo_type="""dataset""")
				__SCREAMING_SNAKE_CASE            = video_classifier(lowerCAmelCase__							,  top_k=2)
				self.assertEqual(
				    nested_simplify(lowerCAmelCase__							,  decimals=4)							,  [{"""score""": 0.51_99, """label""": """LABEL_0"""}, {"""score""": 0.48_01, """label""": """LABEL_1"""}]							,  )

				__SCREAMING_SNAKE_CASE            = video_classifier(
				    [
				        video_file_path,
				        video_file_path,
				    ]							,  top_k=2							,  )
				self.assertEqual(
				    nested_simplify(lowerCAmelCase__							,  decimals=4)							,  [
				        [{"""score""": 0.51_99, """label""": """LABEL_0"""}, {"""score""": 0.48_01, """label""": """LABEL_1"""}],
				        [{"""score""": 0.51_99, """label""": """LABEL_0"""}, {"""score""": 0.48_01, """label""": """LABEL_1"""}],
				    ]							,  )




		@require_tf
		def 	snake_case_     (     self):
				pass






"""simple docstring"""





# Copyright 2023 The HuggingFace Inc. team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import TYPE_CHECKING

import torch

from ..models.auto import AutoModelForVisualQuestionAnswering, AutoProcessor
from ..utils import requires_backends
from .base import PipelineTool


if TYPE_CHECKING:
							from PIL import Image
class 						SCREAMING_SNAKE_CASE_ (						__a ):




		"""simple docstring"""





		__lowercase							:  Tuple						   =		'''dandelin/vilt-b32-finetuned-vqa'''
		__lowercase							:  str						   =		(
		    '''This is a tool that answers a question about an image. It takes an input named `image` which should be the '''
		    '''image containing the information, as well as a `question` which should be the question in English. It '''
		    '''returns a text that is the answer to the question.'''
		)
		__lowercase							:  Optional[int]						   =		'''image_qa'''
		__lowercase							:  Union[str, Any]						   =		AutoProcessor
		__lowercase							:  Optional[Any]						   =		AutoModelForVisualQuestionAnswering

		__lowercase							:  List[str]						   =		['''image''', '''text''']
		__lowercase							:  Optional[Any]						   =		['''text''']




		def __init__(     self							,  *lowerCAmelCase__							,  **lowerCAmelCase__):
				requires_backends(self							,  ["""vision"""])
				super().__init__(*lowerCAmelCase__							,  **lowerCAmelCase__)




		def 	snake_case_     (     self							,  lowerCAmelCase__							,  lowerCAmelCase__):
				return self.pre_processor(lowerCAmelCase__							,  lowerCAmelCase__							,  return_tensors="""pt""")




		def 	snake_case_     (     self							,  lowerCAmelCase__):
				with torch.no_grad():
						return self.model(**lowerCAmelCase__).logits




		def 	snake_case_     (     self							,  lowerCAmelCase__):
				__SCREAMING_SNAKE_CASE            = outputs.argmax(-1).item()
				return self.model.config.idalabel[idx]




