
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_image_inputs


if is_torch_available():
      import torch

if is_vision_available():
      from PIL import Image

      from transformers import ViltImageProcessor

class   __snake_case				(			unittest.TestCase   ):






    '''simple docstring'''





    def __init__(   self						:		Tuple			,						snake_case						:		List[str]			,						snake_case						:		Dict=7			,						snake_case						:		str=3			,						snake_case						:		Union[str, Any]=18			,						snake_case						:		str=30			,						snake_case						:		str=400			,						snake_case						:		Optional[Any]=True			,						snake_case						:		List[str]=None			,						snake_case						:		Tuple=2			,						snake_case						:		Dict=True			,						snake_case						:		Dict=[0.5, 0.5, 0.5]			,						snake_case						:		Tuple=[0.5, 0.5, 0.5]			,						):

      """simple docstring"""





      __lowerCAmelCase			=				size if size is not None else {'''shortest_edge''': 30}
      __lowerCAmelCase			=				parent
      __lowerCAmelCase			=				batch_size
      __lowerCAmelCase			=				num_channels
      __lowerCAmelCase			=				image_size
      __lowerCAmelCase			=				min_resolution
      __lowerCAmelCase			=				max_resolution
      __lowerCAmelCase			=				do_resize
      __lowerCAmelCase			=				size
      __lowerCAmelCase			=				size_divisor
      __lowerCAmelCase			=				do_normalize
      __lowerCAmelCase			=				image_mean
      __lowerCAmelCase			=				image_std





    def 	A		(   self						:		int							):

      """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,
          "size_divisor": self.size_divisor,
      }





    def 	A		(   self						:		Dict			,						snake_case						:		List[Any]			,						snake_case						:		Any=False							):

      """simple docstring"""





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

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

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

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

      return expected_height, expected_width


@require_torch
@require_vision
class   __snake_case				(			_a       ,				unittest.TestCase   ):






    '''simple docstring'''
    _lowerCAmelCase     =	ViltImageProcessor if is_vision_available() else None





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

      """simple docstring"""





      __lowerCAmelCase			=				ViltImageProcessingTester(self							)





    @property
    def 	A		(   self						:		Any							):

      """simple docstring"""





      return self.image_processor_tester.prepare_image_processor_dict()





    def 	A		(   self						:		List[Any]							):

      """simple docstring"""





      __lowerCAmelCase			=				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			,						'''size'''							)							)
      self.assertTrue(hasattr(snake_case			,						'''size_divisor'''							)							)





    def 	A		(   self						:		Optional[Any]							):

      """simple docstring"""





      __lowerCAmelCase			=				self.image_processing_class.from_dict(self.image_processor_dict							)
      self.assertEqual(image_processor.size			,						{'''shortest_edge''': 30}							)

      __lowerCAmelCase			=				self.image_processing_class.from_dict(self.image_processor_dict			,						size=42							)
      self.assertEqual(image_processor.size			,						{'''shortest_edge''': 42}							)





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

      """simple docstring"""





      pass





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

      """simple docstring"""





      __lowerCAmelCase			=				self.image_processing_class(**self.image_processor_dict							)
      # create random PIL images
      __lowerCAmelCase			=				prepare_image_inputs(self.image_processor_tester			,						equal_resolution=snake_case							)
      for image in image_inputs:
        self.assertIsInstance(snake_case			,						Image.Image							)

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

      __lowerCAmelCase	,						__lowerCAmelCase			=				self.image_processor_tester.get_expected_values(snake_case							)
      self.assertEqual(
          encoded_images.shape			,						(1, self.image_processor_tester.num_channels, expected_height, expected_width)			,						)

      # Test batched
      __lowerCAmelCase			=				image_processing(snake_case			,						return_tensors='''pt'''							).pixel_values

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





    def 	A		(   self						:		Tuple							):

      """simple docstring"""





      __lowerCAmelCase			=				self.image_processing_class(**self.image_processor_dict							)
      # create random numpy tensors
      __lowerCAmelCase			=				prepare_image_inputs(self.image_processor_tester			,						equal_resolution=snake_case			,						numpify=snake_case							)
      for image in image_inputs:
        self.assertIsInstance(snake_case			,						np.ndarray							)

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

      __lowerCAmelCase	,						__lowerCAmelCase			=				self.image_processor_tester.get_expected_values(snake_case							)
      self.assertEqual(
          encoded_images.shape			,						(1, self.image_processor_tester.num_channels, expected_height, expected_width)			,						)

      # Test batched
      __lowerCAmelCase			=				image_processing(snake_case			,						return_tensors='''pt'''							).pixel_values

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





    def 	A		(   self						:		List[str]							):

      """simple docstring"""





      __lowerCAmelCase			=				self.image_processing_class(**self.image_processor_dict							)
      # create random PyTorch tensors
      __lowerCAmelCase			=				prepare_image_inputs(self.image_processor_tester			,						equal_resolution=snake_case			,						torchify=snake_case							)
      for image in image_inputs:
        self.assertIsInstance(snake_case			,						torch.Tensor							)

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

      __lowerCAmelCase	,						__lowerCAmelCase			=				self.image_processor_tester.get_expected_values(snake_case							)
      self.assertEqual(
          encoded_images.shape			,						(1, self.image_processor_tester.num_channels, expected_height, expected_width)			,						)

      # Test batched
      __lowerCAmelCase			=				image_processing(snake_case			,						return_tensors='''pt'''							).pixel_values

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







from typing import TYPE_CHECKING

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


__lowercase													=			{
    """configuration_speech_to_text_2""": ["""SPEECH_TO_TEXT_2_PRETRAINED_CONFIG_ARCHIVE_MAP""", """Speech2Text2Config"""],
    """processing_speech_to_text_2""": ["""Speech2Text2Processor"""],
    """tokenization_speech_to_text_2""": ["""Speech2Text2Tokenizer"""],
}


try:
      if not is_torch_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      __lowercase													=			[
          """SPEECH_TO_TEXT_2_PRETRAINED_MODEL_ARCHIVE_LIST""",
          """Speech2Text2ForCausalLM""",
          """Speech2Text2PreTrainedModel""",
      ]


if TYPE_CHECKING:
      from .configuration_speech_to_text_a import SPEECH_TO_TEXT_2_PRETRAINED_CONFIG_ARCHIVE_MAP, SpeechaTextaConfig
      from .processing_speech_to_text_a import SpeechaTextaProcessor
      from .tokenization_speech_to_text_a import SpeechaTextaTokenizer

      try:
            if not is_torch_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_speech_to_text_a import (
                SPEECH_TO_TEXT_2_PRETRAINED_MODEL_ARCHIVE_LIST,
                SpeechaTextaForCausalLM,
                SpeechaTextaPreTrainedModel,
            )

else:
      import sys

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