

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


if is_torch_available():
  import torch

if is_vision_available():
  from PIL import Image

  from transformers import SwinaSRImageProcessor
  from transformers.image_transforms import get_image_size







class     a__    (				unittest.TestCase    ):







  """simple docstring"""




  def __init__(     self      :	List[Any]			,			UpperCAmelCase__      :	Optional[Any]			,			UpperCAmelCase__      :	List[str]=7			,			UpperCAmelCase__      :	Union[str, Any]=3			,			UpperCAmelCase__      :	int=1_8			,			UpperCAmelCase__      :	str=3_0			,			UpperCAmelCase__      :	Union[str, Any]=4_0_0			,			UpperCAmelCase__      :	List[str]=True			,			UpperCAmelCase__      :	Dict=1 / 2_5_5			,			UpperCAmelCase__      :	Union[str, Any]=True			,			UpperCAmelCase__      :	List[Any]=8			,			)   ->List[str]:
        """simple docstring"""







        SCREAMING_SNAKE_CASE	: Any					       =				parent
        SCREAMING_SNAKE_CASE	: Dict					       =				batch_size
        SCREAMING_SNAKE_CASE	: str					       =				num_channels
        SCREAMING_SNAKE_CASE	: List[str]					       =				image_size
        SCREAMING_SNAKE_CASE	: Optional[Any]					       =				min_resolution
        SCREAMING_SNAKE_CASE	: Dict					       =				max_resolution
        SCREAMING_SNAKE_CASE	: Tuple					       =				do_rescale
        SCREAMING_SNAKE_CASE	: int					       =				rescale_factor
        SCREAMING_SNAKE_CASE	: Union[str, Any]					       =				do_pad
        SCREAMING_SNAKE_CASE	: Optional[Any]					       =				pad_size


  def 	_lowercase (     self      :	Tuple						)   ->Any:
        """simple docstring"""







        return {
            "do_rescale": self.do_rescale,
            "rescale_factor": self.rescale_factor,
            "do_pad": self.do_pad,
            "pad_size": self.pad_size,
        }




  def 	_lowercase (     self      :	str			,			UpperCAmelCase__      :	Optional[int]=False			,			UpperCAmelCase__      :	Tuple=False			,			UpperCAmelCase__      :	List[Any]=False						)   ->int:
        """simple docstring"""







        assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

        if equal_resolution:
              SCREAMING_SNAKE_CASE	: Tuple					       =				[]
              for i in range(self.batch_size						):
                    image_inputs.append(
                        np.random.randint(
                            2_5_5			,			size=(self.num_channels, self.max_resolution, self.max_resolution)			,			dtype=np.uinta						)						)
        else:
              SCREAMING_SNAKE_CASE	: Tuple					       =				[]
              for i in range(self.batch_size						):
                    SCREAMING_SNAKE_CASE	: List[Any]					       =				np.random.choice(np.arange(self.min_resolution			,			self.max_resolution						)			,			2						)
                    image_inputs.append(np.random.randint(2_5_5			,			size=(self.num_channels, width, height)			,			dtype=np.uinta						)						)

        if not numpify and not torchify:
              # PIL expects the channel dimension as last dimension
              SCREAMING_SNAKE_CASE	: Optional[int]					       =				[Image.fromarray(np.moveaxis(UpperCamelCase__			,			0			,			-1						)						) for x in image_inputs]

        if torchify:
              SCREAMING_SNAKE_CASE	: Union[str, Any]					       =				[torch.from_numpy(UpperCamelCase__						) for x in image_inputs]

        return image_inputs







@require_torch
@require_vision
class     a__    (				lowerCAmelCase__		,     unittest.TestCase    ):







  """simple docstring"""




  UpperCAmelCase__ :	Tuple         =SwinaSRImageProcessor if is_vision_available() else None


  def 	_lowercase (     self      :	Dict						)   ->Optional[Any]:
        """simple docstring"""







        SCREAMING_SNAKE_CASE	: Union[str, Any]					       =				SwinaSRImageProcessingTester(self						)


  @property
  def 	_lowercase (     self      :	Tuple						)   ->List[str]:
        """simple docstring"""







        return self.image_processor_tester.prepare_image_processor_dict()


  def 	_lowercase (     self      :	str						)   ->Any:
        """simple docstring"""







        SCREAMING_SNAKE_CASE	: List[str]					       =				self.image_processing_class(**self.image_processor_dict						)
        self.assertTrue(hasattr(UpperCamelCase__			,			"""do_rescale"""						)						)
        self.assertTrue(hasattr(UpperCamelCase__			,			"""rescale_factor"""						)						)
        self.assertTrue(hasattr(UpperCamelCase__			,			"""do_pad"""						)						)
        self.assertTrue(hasattr(UpperCamelCase__			,			"""pad_size"""						)						)


  def 	_lowercase (     self      :	int						)   ->Optional[int]:
        """simple docstring"""







        pass


  def 	_lowercase (     self      :	Union[str, Any]			,			UpperCAmelCase__      :	str						)   ->int:
        """simple docstring"""







        SCREAMING_SNAKE_CASE	: Any					       =				get_image_size(UpperCamelCase__						)
        SCREAMING_SNAKE_CASE	: Optional[int]					       =				self.image_processor_tester.pad_size

        SCREAMING_SNAKE_CASE	: Dict					       =				(old_height // size + 1) * size - old_height
        SCREAMING_SNAKE_CASE	: int					       =				(old_width // size + 1) * size - old_width
        return old_height + pad_height, old_width + pad_width


  def 	_lowercase (     self      :	Optional[Any]						)   ->Any:
        """simple docstring"""







        SCREAMING_SNAKE_CASE	: Tuple					       =				self.image_processing_class(**self.image_processor_dict						)
        # create random PIL images
        SCREAMING_SNAKE_CASE	: str					       =				self.image_processor_tester.prepare_inputs(equal_resolution=UpperCamelCase__						)
        for image in image_inputs:
              self.assertIsInstance(UpperCamelCase__			,			Image.Image						)

        # Test not batched input
        SCREAMING_SNAKE_CASE	: int					       =				image_processor(image_inputs[0]			,			return_tensors="""pt"""						).pixel_values
        SCREAMING_SNAKE_CASE	: List[str]					       =				self.calculate_expected_size(np.array(image_inputs[0]						)						)
        self.assertEqual(
            encoded_images.shape			,			(
                1,
                self.image_processor_tester.num_channels,
                expected_height,
                expected_width,
            )			,			)


  def 	_lowercase (     self      :	int						)   ->Any:
        """simple docstring"""







        SCREAMING_SNAKE_CASE	: List[Any]					       =				self.image_processing_class(**self.image_processor_dict						)
        # create random numpy tensors
        SCREAMING_SNAKE_CASE	: Optional[int]					       =				self.image_processor_tester.prepare_inputs(equal_resolution=UpperCamelCase__			,			numpify=UpperCamelCase__						)
        for image in image_inputs:
              self.assertIsInstance(UpperCamelCase__			,			np.ndarray						)

        # Test not batched input
        SCREAMING_SNAKE_CASE	: str					       =				image_processor(image_inputs[0]			,			return_tensors="""pt"""						).pixel_values
        SCREAMING_SNAKE_CASE	: int					       =				self.calculate_expected_size(image_inputs[0]						)
        self.assertEqual(
            encoded_images.shape			,			(
                1,
                self.image_processor_tester.num_channels,
                expected_height,
                expected_width,
            )			,			)




  def 	_lowercase (     self      :	Tuple						)   ->List[str]:
        """simple docstring"""







        SCREAMING_SNAKE_CASE	: List[str]					       =				self.image_processing_class(**self.image_processor_dict						)
        # create random PyTorch tensors
        SCREAMING_SNAKE_CASE	: str					       =				self.image_processor_tester.prepare_inputs(equal_resolution=UpperCamelCase__			,			torchify=UpperCamelCase__						)
        for image in image_inputs:
              self.assertIsInstance(UpperCamelCase__			,			torch.Tensor						)

        # Test not batched input
        SCREAMING_SNAKE_CASE	: Optional[int]					       =				image_processor(image_inputs[0]			,			return_tensors="""pt"""						).pixel_values
        SCREAMING_SNAKE_CASE	: Union[str, Any]					       =				self.calculate_expected_size(image_inputs[0]						)
        self.assertEqual(
            encoded_images.shape			,			(
                1,
                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,
)


UpperCAmelCase__	:       int			      =      {
    """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:
  UpperCAmelCase__	:       int			      =      [
      """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

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




