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

from ..utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_torch_available,
)


SCREAMING_SNAKE_CASE    =					{
    """agents""": ["""Agent""", """AzureOpenAiAgent""", """HfAgent""", """LocalAgent""", """OpenAiAgent"""],
    """base""": ["""PipelineTool""", """RemoteTool""", """Tool""", """launch_gradio_demo""", """load_tool"""],
}

try:
      if not is_torch_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      SCREAMING_SNAKE_CASE    =					["""DocumentQuestionAnsweringTool"""]
      SCREAMING_SNAKE_CASE    =					["""ImageCaptioningTool"""]
      SCREAMING_SNAKE_CASE    =					["""ImageQuestionAnsweringTool"""]
      SCREAMING_SNAKE_CASE    =					["""ImageSegmentationTool"""]
      SCREAMING_SNAKE_CASE    =					["""SpeechToTextTool"""]
      SCREAMING_SNAKE_CASE    =					["""TextClassificationTool"""]
      SCREAMING_SNAKE_CASE    =					["""TextQuestionAnsweringTool"""]
      SCREAMING_SNAKE_CASE    =					["""TextSummarizationTool"""]
      SCREAMING_SNAKE_CASE    =					["""TextToSpeechTool"""]
      SCREAMING_SNAKE_CASE    =					["""TranslationTool"""]

if TYPE_CHECKING:
      from .agents import Agent, AzureOpenAiAgent, HfAgent, LocalAgent, OpenAiAgent
      from .base import PipelineTool, RemoteTool, Tool, launch_gradio_demo, load_tool

      try:
            if not is_torch_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .document_question_answering import DocumentQuestionAnsweringTool
            from .image_captioning import ImageCaptioningTool
            from .image_question_answering import ImageQuestionAnsweringTool
            from .image_segmentation import ImageSegmentationTool
            from .speech_to_text import SpeechToTextTool
            from .text_classification import TextClassificationTool
            from .text_question_answering import TextQuestionAnsweringTool
            from .text_summarization import TextSummarizationTool
            from .text_to_speech import TextToSpeechTool
            from .translation import TranslationTool
else:
      import sys

      SCREAMING_SNAKE_CASE    =					_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)





'''simple docstring'''





import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_video_inputs


if is_torch_available():
      import torch

if is_vision_available():
      from PIL import Image

      from transformers import VideoMAEImageProcessor






class        UpperCAmelCase_       (						unittest.TestCase							):

 '''simple docstring'''







 def __init__(				self		:     str ,							snake_case__		:     Optional[int] ,							snake_case__		:     Union[str, Any]=7 ,							snake_case__		:     Any=3 ,							snake_case__		:     Tuple=10 ,							snake_case__		:     Dict=18 ,							snake_case__		:     Dict=30 ,							snake_case__		:     Tuple=4_00 ,							snake_case__		:     str=True ,							snake_case__		:     Optional[Any]=None ,							snake_case__		:     Union[str, Any]=True ,							snake_case__		:     List[Any]=[0.5, 0.5, 0.5] ,							snake_case__		:     Dict=[0.5, 0.5, 0.5] ,							snake_case__		:     Tuple=None ,							):



   '''simple docstring'''




   UpperCAmelCase__			:				List[str]												=       size if size is not None else {"shortest_edge": 18}
   UpperCAmelCase__			:				Optional[int]												=       crop_size if crop_size is not None else {"height": 18, "width": 18}

   UpperCAmelCase__			:				List[Any]												=       parent
   UpperCAmelCase__			:				Optional[Any]												=       batch_size
   UpperCAmelCase__			:				int												=       num_channels
   UpperCAmelCase__			:				str												=       num_frames
   UpperCAmelCase__			:				List[Any]												=       image_size
   UpperCAmelCase__			:				Optional[int]												=       min_resolution
   UpperCAmelCase__			:				int												=       max_resolution
   UpperCAmelCase__			:				Optional[int]												=       do_resize
   UpperCAmelCase__			:				List[str]												=       size
   UpperCAmelCase__			:				Optional[int]												=       do_normalize
   UpperCAmelCase__			:				int												=       image_mean
   UpperCAmelCase__			:				Any												=       image_std
   UpperCAmelCase__			:				Optional[int]												=       crop_size







 def        UpperCamelCase     (				self		:     Union[str, Any]   ):



   '''simple docstring'''




   return {
       "image_mean": self.image_mean,
       "image_std": self.image_std,
       "do_normalize": self.do_normalize,
       "do_resize": self.do_resize,
       "size": self.size,
       "crop_size": self.crop_size,
   }






@require_torch
@require_vision
class        UpperCAmelCase_       (						A				,							unittest.TestCase							):

 '''simple docstring'''
 lowercase_				:						Any				=       VideoMAEImageProcessor if is_vision_available() else None







 def        UpperCamelCase     (				self		:     List[str]   ):



   '''simple docstring'''




   UpperCAmelCase__			:				Optional[int]												=       VideoMAEImageProcessingTester(self   )







 @property
 def        UpperCamelCase     (				self		:     Any   ):



   '''simple docstring'''




   return self.image_processor_tester.prepare_image_processor_dict()







 def        UpperCamelCase     (				self		:     Any   ):



   '''simple docstring'''




   UpperCAmelCase__			:				str												=       self.image_processing_class(**self.image_processor_dict   )
   self.assertTrue(hasattr(snake_case__ ,							"image_mean"   )   )
   self.assertTrue(hasattr(snake_case__ ,							"image_std"   )   )
   self.assertTrue(hasattr(snake_case__ ,							"do_normalize"   )   )
   self.assertTrue(hasattr(snake_case__ ,							"do_resize"   )   )
   self.assertTrue(hasattr(snake_case__ ,							"do_center_crop"   )   )
   self.assertTrue(hasattr(snake_case__ ,							"size"   )   )







 def        UpperCamelCase     (				self		:     Tuple   ):



   '''simple docstring'''




   UpperCAmelCase__			:				int												=       self.image_processing_class.from_dict(self.image_processor_dict   )
   self.assertEqual(image_processor.size ,							{"shortest_edge": 18}   )
   self.assertEqual(image_processor.crop_size ,							{"height": 18, "width": 18}   )

   UpperCAmelCase__			:				Union[str, Any]												=       self.image_processing_class.from_dict(self.image_processor_dict ,							size=42 ,							crop_size=84   )
   self.assertEqual(image_processor.size ,							{"shortest_edge": 42}   )
   self.assertEqual(image_processor.crop_size ,							{"height": 84, "width": 84}   )







 def        UpperCamelCase     (				self		:     Optional[int]   ):



   '''simple docstring'''




   pass







 def        UpperCamelCase     (				self		:     Union[str, Any]   ):



   '''simple docstring'''




   UpperCAmelCase__			:				List[str]												=       self.image_processing_class(**self.image_processor_dict   )
   # create random PIL videos
   UpperCAmelCase__			:				Optional[int]												=       prepare_video_inputs(self.image_processor_tester ,							equal_resolution=snake_case__   )
   for video in video_inputs:
     self.assertIsInstance(snake_case__ ,							snake_case__   )
     self.assertIsInstance(video[0] ,							Image.Image   )

   # Test not batched input
   UpperCAmelCase__			:				Dict												=       image_processing(video_inputs[0] ,							return_tensors="pt"   ).pixel_values
   self.assertEqual(
       encoded_videos.shape ,							(
           1,
           self.image_processor_tester.num_frames,
           self.image_processor_tester.num_channels,
           self.image_processor_tester.crop_size["height"],
           self.image_processor_tester.crop_size["width"],
       ) ,							)

   # Test batched
   UpperCAmelCase__			:				Tuple												=       image_processing(snake_case__ ,							return_tensors="pt"   ).pixel_values
   self.assertEqual(
       encoded_videos.shape ,							(
           self.image_processor_tester.batch_size,
           self.image_processor_tester.num_frames,
           self.image_processor_tester.num_channels,
           self.image_processor_tester.crop_size["height"],
           self.image_processor_tester.crop_size["width"],
       ) ,							)







 def        UpperCamelCase     (				self		:     Union[str, Any]   ):



   '''simple docstring'''




   UpperCAmelCase__			:				Dict												=       self.image_processing_class(**self.image_processor_dict   )
   # create random numpy tensors
   UpperCAmelCase__			:				List[Any]												=       prepare_video_inputs(self.image_processor_tester ,							equal_resolution=snake_case__ ,							numpify=snake_case__   )
   for video in video_inputs:
     self.assertIsInstance(snake_case__ ,							snake_case__   )
     self.assertIsInstance(video[0] ,							np.ndarray   )

   # Test not batched input
   UpperCAmelCase__			:				Dict												=       image_processing(video_inputs[0] ,							return_tensors="pt"   ).pixel_values
   self.assertEqual(
       encoded_videos.shape ,							(
           1,
           self.image_processor_tester.num_frames,
           self.image_processor_tester.num_channels,
           self.image_processor_tester.crop_size["height"],
           self.image_processor_tester.crop_size["width"],
       ) ,							)

   # Test batched
   UpperCAmelCase__			:				Optional[int]												=       image_processing(snake_case__ ,							return_tensors="pt"   ).pixel_values
   self.assertEqual(
       encoded_videos.shape ,							(
           self.image_processor_tester.batch_size,
           self.image_processor_tester.num_frames,
           self.image_processor_tester.num_channels,
           self.image_processor_tester.crop_size["height"],
           self.image_processor_tester.crop_size["width"],
       ) ,							)







 def        UpperCamelCase     (				self		:     Dict   ):



   '''simple docstring'''




   UpperCAmelCase__			:				int												=       self.image_processing_class(**self.image_processor_dict   )
   # create random PyTorch tensors
   UpperCAmelCase__			:				Dict												=       prepare_video_inputs(self.image_processor_tester ,							equal_resolution=snake_case__ ,							torchify=snake_case__   )
   for video in video_inputs:
     self.assertIsInstance(snake_case__ ,							snake_case__   )
     self.assertIsInstance(video[0] ,							torch.Tensor   )

   # Test not batched input
   UpperCAmelCase__			:				int												=       image_processing(video_inputs[0] ,							return_tensors="pt"   ).pixel_values
   self.assertEqual(
       encoded_videos.shape ,							(
           1,
           self.image_processor_tester.num_frames,
           self.image_processor_tester.num_channels,
           self.image_processor_tester.crop_size["height"],
           self.image_processor_tester.crop_size["width"],
       ) ,							)

   # Test batched
   UpperCAmelCase__			:				Optional[Any]												=       image_processing(snake_case__ ,							return_tensors="pt"   ).pixel_values
   self.assertEqual(
       encoded_videos.shape ,							(
           self.image_processor_tester.batch_size,
           self.image_processor_tester.num_frames,
           self.image_processor_tester.num_channels,
           self.image_processor_tester.crop_size["height"],
           self.image_processor_tester.crop_size["width"],
       ) ,							)



