






import warnings
from typing import List

import numpy as np

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding
from ...utils import is_flax_available, is_tf_available, is_torch_available



class   lowerCamelCase				(_snake_case      ):







		'''simple docstring'''



		_snake_case   :   str							=  ['''image_processor''', '''tokenizer''']
		_snake_case   :   str							=  '''OwlViTImageProcessor'''
		_snake_case   :   int							=  ('''CLIPTokenizer''', '''CLIPTokenizerFast''')



		def __init__(							self    ,		_UpperCamelCase=None    ,		_UpperCamelCase=None    ,		**_UpperCamelCase       )     ->							Tuple:
							UpperCAmelCase_   :				str						=						None
							if "feature_extractor" in kwargs:
												warnings.warn(
												    'The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`'
												    ' instead.'    ,		_UpperCamelCase    ,		)
												UpperCAmelCase_   :				Union[str, Any]						=						kwargs.pop('feature_extractor'       )

							UpperCAmelCase_   :				Optional[int]						=						image_processor if image_processor is not None else feature_extractor
							if image_processor is None:
												raise ValueError('You need to specify an `image_processor`.'       )
							if tokenizer is None:
												raise ValueError('You need to specify a `tokenizer`.'       )

							super().__init__(_UpperCamelCase    ,		_UpperCamelCase       )



		def __call__(							self    ,		_UpperCamelCase=None    ,		_UpperCamelCase=None    ,		_UpperCamelCase=None    ,		_UpperCamelCase="max_length"    ,		_UpperCamelCase="np"    ,		**_UpperCamelCase       )     ->							Optional[Any]:

							if text is None and query_images is None and images is None:
												raise ValueError(
												    'You have to specify at least one text or query image or image. All three cannot be none.'       )

							if text is not None:
												if isinstance(_UpperCamelCase    ,		_UpperCamelCase       ) or (isinstance(_UpperCamelCase    ,		_UpperCamelCase       ) and not isinstance(text[0]    ,		_UpperCamelCase       )):
																	UpperCAmelCase_   :				List[Any]						=						[self.tokenizer(_UpperCamelCase    ,		padding=_UpperCamelCase    ,		return_tensors=_UpperCamelCase    ,		**_UpperCamelCase       )]

												elif isinstance(_UpperCamelCase    ,		_UpperCamelCase       ) and isinstance(text[0]    ,		_UpperCamelCase       ):
																	UpperCAmelCase_   :				List[Any]						=						[]

																	# Maximum number of queries across batch
																	UpperCAmelCase_   :				int						=						max([len(_UpperCamelCase       ) for t in text]       )

																	# Pad all batch samples to max number of text queries
																	for t in text:
																						if len(_UpperCamelCase       ) != max_num_queries:
																											UpperCAmelCase_   :				Union[str, Any]						=						t + [' '] * (max_num_queries - len(_UpperCamelCase       ))

																						UpperCAmelCase_   :				List[str]						=						self.tokenizer(_UpperCamelCase    ,		padding=_UpperCamelCase    ,		return_tensors=_UpperCamelCase    ,		**_UpperCamelCase       )
																						encodings.append(_UpperCamelCase       )
												else:
																	raise TypeError('Input text should be a string, a list of strings or a nested list of strings'       )

												if return_tensors == "np":
																	UpperCAmelCase_   :				Any						=						np.concatenate([encoding['input_ids'] for encoding in encodings]    ,		axis=0       )
																	UpperCAmelCase_   :				Any						=						np.concatenate([encoding['attention_mask'] for encoding in encodings]    ,		axis=0       )

												elif return_tensors == "jax" and is_flax_available():
																	import jax.numpy as jnp

																	UpperCAmelCase_   :				Dict						=						jnp.concatenate([encoding['input_ids'] for encoding in encodings]    ,		axis=0       )
																	UpperCAmelCase_   :				str						=						jnp.concatenate([encoding['attention_mask'] for encoding in encodings]    ,		axis=0       )

												elif return_tensors == "pt" and is_torch_available():
																	import torch

																	UpperCAmelCase_   :				Optional[int]						=						torch.cat([encoding['input_ids'] for encoding in encodings]    ,		dim=0       )
																	UpperCAmelCase_   :				int						=						torch.cat([encoding['attention_mask'] for encoding in encodings]    ,		dim=0       )

												elif return_tensors == "tf" and is_tf_available():
																	import tensorflow as tf

																	UpperCAmelCase_   :				List[str]						=						tf.stack([encoding['input_ids'] for encoding in encodings]    ,		axis=0       )
																	UpperCAmelCase_   :				str						=						tf.stack([encoding['attention_mask'] for encoding in encodings]    ,		axis=0       )

												else:
																	raise ValueError('Target return tensor type could not be returned'       )

												UpperCAmelCase_   :				List[str]						=						BatchEncoding()
												UpperCAmelCase_   :				str						=						input_ids
												UpperCAmelCase_   :				Tuple						=						attention_mask

							if query_images is not None:
												UpperCAmelCase_   :				Any						=						BatchEncoding()
												UpperCAmelCase_   :				Tuple						=						self.image_processor(
												    _UpperCamelCase    ,		return_tensors=_UpperCamelCase    ,		**_UpperCamelCase       ).pixel_values
												UpperCAmelCase_   :				Any						=						query_pixel_values

							if images is not None:
												UpperCAmelCase_   :				Tuple						=						self.image_processor(_UpperCamelCase    ,		return_tensors=_UpperCamelCase    ,		**_UpperCamelCase       )

							if text is not None and images is not None:
												UpperCAmelCase_   :				Tuple						=						image_features.pixel_values
												return encoding
							elif query_images is not None and images is not None:
												UpperCAmelCase_   :				Optional[int]						=						image_features.pixel_values
												return encoding
							elif text is not None or query_images is not None:
												return encoding
							else:
												return BatchEncoding(data=dict(**_UpperCamelCase       )    ,		tensor_type=_UpperCamelCase       )



		def  __UpperCAmelCase      (							self    ,		*_UpperCamelCase    ,		**_UpperCamelCase       )     ->							Optional[int]:
							return self.image_processor.post_process(*_UpperCamelCase    ,		**_UpperCamelCase       )



		def  __UpperCAmelCase      (							self    ,		*_UpperCamelCase    ,		**_UpperCamelCase       )     ->							Union[str, Any]:
							return self.image_processor.post_process_object_detection(*_UpperCamelCase    ,		**_UpperCamelCase       )



		def  __UpperCAmelCase      (							self    ,		*_UpperCamelCase    ,		**_UpperCamelCase       )     ->							Dict:
							return self.image_processor.post_process_image_guided_detection(*_UpperCamelCase    ,		**_UpperCamelCase       )



		def  __UpperCAmelCase      (							self    ,		*_UpperCamelCase    ,		**_UpperCamelCase       )     ->							Any:
							return self.tokenizer.batch_decode(*_UpperCamelCase    ,		**_UpperCamelCase       )



		def  __UpperCAmelCase      (							self    ,		*_UpperCamelCase    ,		**_UpperCamelCase       )     ->							str:
							return self.tokenizer.decode(*_UpperCamelCase    ,		**_UpperCamelCase       )



		@property
		def  __UpperCAmelCase      (							self       )     ->							Dict:
							warnings.warn(
							    '`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.'    ,		_UpperCamelCase    ,		)
							return self.image_processor_class



		@property
		def  __UpperCAmelCase      (							self       )     ->							List[str]:
							warnings.warn(
							    '`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.'    ,		_UpperCamelCase    ,		)
							return self.image_processor














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      ):







		'''simple docstring'''



		_snake_case   :   List[str]							=  MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING



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



		def  __UpperCAmelCase      (							self    ,		_UpperCamelCase    ,		_UpperCamelCase       )     ->							Dict:
							for example in examples:
												UpperCAmelCase_   :				str						=						video_classifier(_UpperCamelCase       )

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



		@require_torch
		def  __UpperCAmelCase      (							self       )     ->							List[Any]:
							UpperCAmelCase_   :				str						=						'hf-internal-testing/tiny-random-VideoMAEForVideoClassification'
							UpperCAmelCase_   :				Optional[Any]						=						VideoMAEFeatureExtractor(
							    size={'shortest_edge': 1_0}    ,		crop_size={'height': 1_0, 'width': 1_0}       )
							UpperCAmelCase_   :				str						=						pipeline(
							    'video-classification'    ,		model=_UpperCamelCase    ,		feature_extractor=_UpperCamelCase    ,		frame_sampling_rate=4       )

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

							UpperCAmelCase_   :				Tuple						=						video_classifier(
							    [
							        video_file_path,
							        video_file_path,
							    ]    ,		top_k=2    ,		)
							self.assertEqual(
							    nested_simplify(_UpperCamelCase    ,		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  __UpperCAmelCase      (							self       )     ->							Dict:
							pass






