





'''simple docstring'''




import unittest
from typing import Dict, List, Optional, Union

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_image_inputs


if is_torch_available():
    import torch

if is_vision_available():
    from PIL import Image

    from transformers import BridgeTowerImageProcessor






class UpperCAmelCase							(		unittest.TestCase							):



    def __init__(  self     ,			__A     ,			__A = True     ,			__A = None     ,			__A = 32     ,			__A = True     ,			__A = 1 / 255     ,			__A = True     ,			__A = True     ,			__A = [0.4_8_1_4_5_4_6_6, 0.4_5_7_8_2_7_5, 0.4_0_8_2_1_0_7_3]     ,			__A = [0.2_6_8_6_2_9_5_4, 0.2_6_1_3_0_2_5_8, 0.2_7_5_7_7_7_1_1]     ,			__A = True     ,			__A=7     ,			__A=30     ,			__A=400     ,			__A=3     ,			):
     __UpperCAmelCase					=   parent
     __UpperCAmelCase					=   do_resize
     __UpperCAmelCase					=   size if size is not None else {'shortest_edge': 288}
     __UpperCAmelCase					=   size_divisor
     __UpperCAmelCase					=   do_rescale
     __UpperCAmelCase					=   rescale_factor
     __UpperCAmelCase					=   do_normalize
     __UpperCAmelCase					=   do_center_crop
     __UpperCAmelCase					=   image_mean
     __UpperCAmelCase					=   image_std
     __UpperCAmelCase					=   do_pad
     __UpperCAmelCase					=   batch_size
     __UpperCAmelCase					=   num_channels
     __UpperCAmelCase					=   min_resolution
     __UpperCAmelCase					=   max_resolution



    def 					__lowerCamelCase						(  self					):
     return {
         "image_mean": self.image_mean,
         "image_std": self.image_std,
         "do_normalize": self.do_normalize,
         "do_resize": self.do_resize,
         "size": self.size,
         "size_divisor": self.size_divisor,
     }




    def 					__lowerCamelCase						(  self     ,			__A     ,			__A=False					):
     if not batched:
      __UpperCAmelCase					=   self.size['shortest_edge']
      __UpperCAmelCase					=   image_inputs[0]
      if isinstance(__A     ,			Image.Image					):
       __UpperCAmelCase					,    __UpperCAmelCase					=   image.size
      else:
       __UpperCAmelCase					,    __UpperCAmelCase					=   image.shape[1], image.shape[2]
      __UpperCAmelCase					=   size / min(__A     ,			__A					)
      if h < w:
       __UpperCAmelCase					,    __UpperCAmelCase					=   size, scale * w
      else:
       __UpperCAmelCase					,    __UpperCAmelCase					=   scale * h, size

      __UpperCAmelCase					=   int((1_333 / 800) * size					)
      if max(__A     ,			__A					) > max_size:
       __UpperCAmelCase					=   max_size / max(__A     ,			__A					)
       __UpperCAmelCase					=   newh * scale
       __UpperCAmelCase					=   neww * scale

      __UpperCAmelCase					,    __UpperCAmelCase					=   int(newh + 0.5					), int(neww + 0.5					)
      __UpperCAmelCase					,    __UpperCAmelCase					=   (
          newh // self.size_divisor * self.size_divisor,
          neww // self.size_divisor * self.size_divisor,
      )

     else:
      __UpperCAmelCase					=   []
      for image in image_inputs:
       __UpperCAmelCase					,    __UpperCAmelCase					=   self.get_expected_values([image]					)
       expected_values.append((expected_height, expected_width)					)
      __UpperCAmelCase					=   max(__A     ,			key=lambda __A					: item[0]					)[0]
      __UpperCAmelCase					=   max(__A     ,			key=lambda __A					: item[1]					)[1]

     return expected_height, expected_width






@require_torch
@require_vision
class UpperCAmelCase							(		UpperCAmelCase_					,			unittest.TestCase							):
    _A :			List[Any]						   =       BridgeTowerImageProcessor if is_vision_available() else None



    def 					__lowerCamelCase						(  self					):
     __UpperCAmelCase					=   BridgeTowerImageProcessingTester(self					)



    @property
    def 					__lowerCamelCase						(  self					):
     return self.image_processor_tester.prepare_image_processor_dict()



    def 					__lowerCamelCase						(  self					):
     __UpperCAmelCase					=   self.image_processing_class(**self.image_processor_dict					)
     self.assertTrue(hasattr(__A     ,			'image_mean'					)					)
     self.assertTrue(hasattr(__A     ,			'image_std'					)					)
     self.assertTrue(hasattr(__A     ,			'do_normalize'					)					)
     self.assertTrue(hasattr(__A     ,			'do_resize'					)					)
     self.assertTrue(hasattr(__A     ,			'size'					)					)
     self.assertTrue(hasattr(__A     ,			'size_divisor'					)					)



    def 					__lowerCamelCase						(  self					):
     pass



    def 					__lowerCamelCase						(  self					):
     # Initialize image processor
     __UpperCAmelCase					=   self.image_processing_class(**self.image_processor_dict					)
     # create random PIL images
     __UpperCAmelCase					=   prepare_image_inputs(self.image_processor_tester     ,			equal_resolution=__A					)
     for image in image_inputs:
      self.assertIsInstance(__A     ,			Image.Image					)

     # Test not batched input
     __UpperCAmelCase					=   image_processing(image_inputs[0]     ,			return_tensors='pt'					).pixel_values

     __UpperCAmelCase					,    __UpperCAmelCase					=   self.image_processor_tester.get_expected_values(__A					)
     self.assertEqual(
         encoded_images.shape     ,			(1, self.image_processor_tester.num_channels, expected_height, expected_width)     ,			)

     # Test batched
     __UpperCAmelCase					=   image_processing(__A     ,			return_tensors='pt'					).pixel_values

     __UpperCAmelCase					,    __UpperCAmelCase					=   self.image_processor_tester.get_expected_values(__A     ,			batched=__A					)
     self.assertEqual(
         encoded_images.shape     ,			(
             self.image_processor_tester.batch_size,
             self.image_processor_tester.num_channels,
             expected_height,
             expected_width,
         )     ,			)



    def 					__lowerCamelCase						(  self					):
     # Initialize image processor
     __UpperCAmelCase					=   self.image_processing_class(**self.image_processor_dict					)
     # create random numpy tensors
     __UpperCAmelCase					=   prepare_image_inputs(self.image_processor_tester     ,			equal_resolution=__A     ,			numpify=__A					)
     for image in image_inputs:
      self.assertIsInstance(__A     ,			np.ndarray					)

     # Test not batched input
     __UpperCAmelCase					=   image_processing(image_inputs[0]     ,			return_tensors='pt'					).pixel_values

     __UpperCAmelCase					,    __UpperCAmelCase					=   self.image_processor_tester.get_expected_values(__A					)
     self.assertEqual(
         encoded_images.shape     ,			(1, self.image_processor_tester.num_channels, expected_height, expected_width)     ,			)

     # Test batched
     __UpperCAmelCase					=   image_processing(__A     ,			return_tensors='pt'					).pixel_values

     __UpperCAmelCase					,    __UpperCAmelCase					=   self.image_processor_tester.get_expected_values(__A     ,			batched=__A					)
     self.assertEqual(
         encoded_images.shape     ,			(
             self.image_processor_tester.batch_size,
             self.image_processor_tester.num_channels,
             expected_height,
             expected_width,
         )     ,			)




    def 					__lowerCamelCase						(  self					):
     # Initialize image processor
     __UpperCAmelCase					=   self.image_processing_class(**self.image_processor_dict					)
     # create random PyTorch tensors
     __UpperCAmelCase					=   prepare_image_inputs(self.image_processor_tester     ,			equal_resolution=__A     ,			torchify=__A					)
     for image in image_inputs:
      self.assertIsInstance(__A     ,			torch.Tensor					)

     # Test not batched input
     __UpperCAmelCase					=   image_processing(image_inputs[0]     ,			return_tensors='pt'					).pixel_values

     __UpperCAmelCase					,    __UpperCAmelCase					=   self.image_processor_tester.get_expected_values(__A					)
     self.assertEqual(
         encoded_images.shape     ,			(1, self.image_processor_tester.num_channels, expected_height, expected_width)     ,			)

     # Test batched
     __UpperCAmelCase					=   image_processing(__A     ,			return_tensors='pt'					).pixel_values

     __UpperCAmelCase					,    __UpperCAmelCase					=   self.image_processor_tester.get_expected_values(__A     ,			batched=__A					)
     self.assertEqual(
         encoded_images.shape     ,			(
             self.image_processor_tester.batch_size,
             self.image_processor_tester.num_channels,
             expected_height,
             expected_width,
         )     ,			)












'''simple docstring'''




from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


_A:					str							=				{
    """configuration_whisper""": ["""WHISPER_PRETRAINED_CONFIG_ARCHIVE_MAP""", """WhisperConfig""", """WhisperOnnxConfig"""],
    """feature_extraction_whisper""": ["""WhisperFeatureExtractor"""],
    """processing_whisper""": ["""WhisperProcessor"""],
    """tokenization_whisper""": ["""WhisperTokenizer"""],
}

try:
    if not is_tokenizers_available():
        raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
    pass
else:
    _A:					Union[str, Any]							=				["""WhisperTokenizerFast"""]

try:
    if not is_torch_available():
        raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
    pass
else:
    _A:					Optional[Any]							=				[
        """WHISPER_PRETRAINED_MODEL_ARCHIVE_LIST""",
        """WhisperForConditionalGeneration""",
        """WhisperModel""",
        """WhisperPreTrainedModel""",
        """WhisperForAudioClassification""",
    ]

try:
    if not is_tf_available():
        raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
    pass
else:
    _A:					Dict							=				[
        """TF_WHISPER_PRETRAINED_MODEL_ARCHIVE_LIST""",
        """TFWhisperForConditionalGeneration""",
        """TFWhisperModel""",
        """TFWhisperPreTrainedModel""",
    ]

try:
    if not is_flax_available():
        raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
    pass
else:
    _A:					Union[str, Any]							=				[
        """FlaxWhisperForConditionalGeneration""",
        """FlaxWhisperModel""",
        """FlaxWhisperPreTrainedModel""",
        """FlaxWhisperForAudioClassification""",
    ]


if TYPE_CHECKING:
    from .configuration_whisper import WHISPER_PRETRAINED_CONFIG_ARCHIVE_MAP, WhisperConfig, WhisperOnnxConfig
    from .feature_extraction_whisper import WhisperFeatureExtractor
    from .processing_whisper import WhisperProcessor
    from .tokenization_whisper import WhisperTokenizer

    try:
        if not is_tokenizers_available():
            raise OptionalDependencyNotAvailable()
    except OptionalDependencyNotAvailable:
        pass
    else:
        from .tokenization_whisper_fast import WhisperTokenizerFast

    try:
        if not is_torch_available():
            raise OptionalDependencyNotAvailable()
    except OptionalDependencyNotAvailable:
        pass
    else:
        from .modeling_whisper import (
            WHISPER_PRETRAINED_MODEL_ARCHIVE_LIST,
            WhisperForAudioClassification,
            WhisperForConditionalGeneration,
            WhisperModel,
            WhisperPreTrainedModel,
        )

    try:
        if not is_tf_available():
            raise OptionalDependencyNotAvailable()
    except OptionalDependencyNotAvailable:
        pass
    else:
        from .modeling_tf_whisper import (
            TF_WHISPER_PRETRAINED_MODEL_ARCHIVE_LIST,
            TFWhisperForConditionalGeneration,
            TFWhisperModel,
            TFWhisperPreTrainedModel,
        )

    try:
        if not is_flax_available():
            raise OptionalDependencyNotAvailable()
    except OptionalDependencyNotAvailable:
        pass
    else:
        from .modeling_flax_whisper import (
            FlaxWhisperForAudioClassification,
            FlaxWhisperForConditionalGeneration,
            FlaxWhisperModel,
            FlaxWhisperPreTrainedModel,
        )

else:
    import sys

    _A:					int							=				_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)





