"""simple docstring"""


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    a__    (   _a	):
   snake_case_							=      ["image_processor", "tokenizer"]
   snake_case_							=      "OwlViTImageProcessor"
   snake_case_							=      ("CLIPTokenizer", "CLIPTokenizerFast")


   def __init__(     self,				_UpperCAmelCase=None,				_UpperCAmelCase=None,				**_UpperCAmelCase  ):
        '''simple docstring'''



        lowercase__				 =							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,				)
             lowercase__				 =							kwargs.pop("feature_extractor"  )

        lowercase__				 =							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  ):
        '''simple docstring'''



        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  )):
                  lowercase__				 =							[self.tokenizer(_UpperCAmelCase,				padding=_UpperCAmelCase,				return_tensors=_UpperCAmelCase,				**_UpperCAmelCase  )]

             elif isinstance(_UpperCAmelCase,				_UpperCAmelCase  ) and isinstance(text[0],				_UpperCAmelCase  ):
                  lowercase__				 =							[]

                  # Maximum number of queries across batch
                  lowercase__				 =							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:
                            lowercase__				 =							t + [" "] * (max_num_queries - len(_UpperCAmelCase  ))

                       lowercase__				 =							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":
                  lowercase__				 =							np.concatenate([encoding["input_ids"] for encoding in encodings],				axis=0  )
                  lowercase__				 =							np.concatenate([encoding["attention_mask"] for encoding in encodings],				axis=0  )

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

                  lowercase__				 =							jnp.concatenate([encoding["input_ids"] for encoding in encodings],				axis=0  )
                  lowercase__				 =							jnp.concatenate([encoding["attention_mask"] for encoding in encodings],				axis=0  )

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

                  lowercase__				 =							torch.cat([encoding["input_ids"] for encoding in encodings],				dim=0  )
                  lowercase__				 =							torch.cat([encoding["attention_mask"] for encoding in encodings],				dim=0  )

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

                  lowercase__				 =							tf.stack([encoding["input_ids"] for encoding in encodings],				axis=0  )
                  lowercase__				 =							tf.stack([encoding["attention_mask"] for encoding in encodings],				axis=0  )

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

             lowercase__				 =							BatchEncoding()
             lowercase__				 =							input_ids
             lowercase__				 =							attention_mask

        if query_images is not None:
             lowercase__				 =							BatchEncoding()
             lowercase__				 =							self.image_processor(
                 _UpperCAmelCase,				return_tensors=_UpperCAmelCase,				**_UpperCAmelCase  ).pixel_values
             lowercase__				 =							query_pixel_values

        if images is not None:
             lowercase__				 =							self.image_processor(_UpperCAmelCase,				return_tensors=_UpperCAmelCase,				**_UpperCAmelCase  )

        if text is not None and images is not None:
             lowercase__				 =							image_features.pixel_values
             return encoding
        elif query_images is not None and images is not None:
             lowercase__				 =							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 	snake_case__	(     self,				*_UpperCAmelCase,				**_UpperCAmelCase  ):
        '''simple docstring'''



        return self.image_processor.post_process(*_UpperCAmelCase,				**_UpperCAmelCase  )


   def 	snake_case__	(     self,				*_UpperCAmelCase,				**_UpperCAmelCase  ):
        '''simple docstring'''



        return self.image_processor.post_process_object_detection(*_UpperCAmelCase,				**_UpperCAmelCase  )


   def 	snake_case__	(     self,				*_UpperCAmelCase,				**_UpperCAmelCase  ):
        '''simple docstring'''



        return self.image_processor.post_process_image_guided_detection(*_UpperCAmelCase,				**_UpperCAmelCase  )


   def 	snake_case__	(     self,				*_UpperCAmelCase,				**_UpperCAmelCase  ):
        '''simple docstring'''



        return self.tokenizer.batch_decode(*_UpperCAmelCase,				**_UpperCAmelCase  )


   def 	snake_case__	(     self,				*_UpperCAmelCase,				**_UpperCAmelCase  ):
        '''simple docstring'''



        return self.tokenizer.decode(*_UpperCAmelCase,				**_UpperCAmelCase  )


   @property
   def 	snake_case__	(     self  ):
        '''simple docstring'''



        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 	snake_case__	(     self  ):
        '''simple docstring'''



        warnings.warn(
            "`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.",				_UpperCAmelCase,				)
        return self.image_processor





"""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	):
   snake_case_							=      MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING


   def 	snake_case__	(     self,				_UpperCAmelCase,				_UpperCAmelCase,				_UpperCAmelCase  ):
        '''simple docstring'''



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


   def 	snake_case__	(     self,				_UpperCAmelCase,				_UpperCAmelCase  ):
        '''simple docstring'''



        for example in examples:
             lowercase__				 =							video_classifier(_UpperCAmelCase  )

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


   @require_torch
   def 	snake_case__	(     self  ):
        '''simple docstring'''



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

        lowercase__				 =							hf_hub_download(repo_id="nateraw/video-demo",				filename="archery.mp4",				repo_type="dataset"  )
        lowercase__				 =							video_classifier(_UpperCAmelCase,				top_k=2  )
        self.assertEqual(
            nested_simplify(_UpperCAmelCase,				decimals=4  ),				[{"score": 0.5_199, "label": "LABEL_0"}, {"score": 0.4_801, "label": "LABEL_1"}],				)

        lowercase__				 =							video_classifier(
            [
                video_file_path,
                video_file_path,
            ],				top_k=2,				)
        self.assertEqual(
            nested_simplify(_UpperCAmelCase,				decimals=4  ),				[
                [{"score": 0.5_199, "label": "LABEL_0"}, {"score": 0.4_801, "label": "LABEL_1"}],
                [{"score": 0.5_199, "label": "LABEL_0"}, {"score": 0.4_801, "label": "LABEL_1"}],
            ],				)


   @require_tf
   def 	snake_case__	(     self  ):
        '''simple docstring'''



        pass




