




'''simple docstring'''







import json
import os
import tempfile

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


if is_torch_available():
   import numpy as np
   import torch

if is_vision_available():
   from PIL import Image





def 						snake_case_							(_a     :							Any   ,       _a     :							Union[str, Any]=False   ,       _a     :							Tuple=False   ,       _a     :							Optional[Any]=False					):
    assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

    UpperCAmelCase      	=		[]
    for i in range(image_processor_tester.batch_size					):
        if equal_resolution:
            UpperCAmelCase      	=		UpperCAmelCase      	=		image_processor_tester.max_resolution
        else:
            # To avoid getting image width/height 0
            UpperCAmelCase      	=		image_processor_tester.min_resolution
            if getattr(_a   ,       '''size_divisor'''   ,       _a					):
                # If `size_divisor` is defined, the image needs to have width/size >= `size_divisor`
                UpperCAmelCase      	=		max(image_processor_tester.size_divisor   ,       _a					)
            UpperCAmelCase	,					UpperCAmelCase      	=		np.random.choice(np.arange(_a   ,       image_processor_tester.max_resolution					)   ,       2					)
        image_inputs.append(
            np.random.randint(2_5_5   ,       size=(image_processor_tester.num_channels, width, height)   ,       dtype=np.uinta					)					)

    if not numpify and not torchify:
        # PIL expects the channel dimension as last dimension
        UpperCAmelCase      	=		[Image.fromarray(np.moveaxis(_a   ,       0   ,       -1					)					) for image in image_inputs]

    if torchify:
        UpperCAmelCase      	=		[torch.from_numpy(_a					) for image in image_inputs]

    return image_inputs





def 						snake_case_							(_a     :							Any   ,       _a     :							str=1_0   ,       _a     :							Dict=1_0   ,       _a     :							Optional[Any]=False   ,       _a     :							int=False					):
    UpperCAmelCase      	=		[]
    for i in range(image_processor_tester.num_frames					):
        video.append(np.random.randint(2_5_5   ,       size=(image_processor_tester.num_channels, width, height)   ,       dtype=np.uinta					)					)

    if not numpify and not torchify:
        # PIL expects the channel dimension as last dimension
        UpperCAmelCase      	=		[Image.fromarray(np.moveaxis(_a   ,       0   ,       -1					)					) for frame in video]

    if torchify:
        UpperCAmelCase      	=		[torch.from_numpy(_a					) for frame in video]

    return video





def 						snake_case_							(_a     :							Optional[Any]   ,       _a     :							str=False   ,       _a     :							List[Any]=False   ,       _a     :							List[Any]=False					):
    assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

    UpperCAmelCase      	=		[]
    for i in range(image_processor_tester.batch_size					):
        if equal_resolution:
            UpperCAmelCase      	=		UpperCAmelCase      	=		image_processor_tester.max_resolution
        else:
            UpperCAmelCase	,					UpperCAmelCase      	=		np.random.choice(
                np.arange(image_processor_tester.min_resolution   ,       image_processor_tester.max_resolution					)   ,       2					)
            UpperCAmelCase      	=		prepare_video(
                image_processor_tester=_a   ,       width=_a   ,       height=_a   ,       numpify=_a   ,       torchify=_a   ,       )
        video_inputs.append(_a					)

    return video_inputs







class       _a  :
       __a    :					int           = None
       def    A				(     self						:     Tuple    ):



           '''simple docstring'''

           UpperCAmelCase      	=		self.image_processing_class(**self.image_processor_dict    )
           UpperCAmelCase      	=		json.loads(image_processor.to_json_string()    )
           for key, value in self.image_processor_dict.items():
               self.assertEqual(obj[key]       ,    lowercase    )
       def    A				(     self						:     List[str]    ):



           '''simple docstring'''

           UpperCAmelCase      	=		self.image_processing_class(**self.image_processor_dict    )

           with tempfile.TemporaryDirectory() as tmpdirname:
               UpperCAmelCase      	=		os.path.join(lowercase       ,    '''image_processor.json'''    )
               image_processor_first.to_json_file(lowercase    )
               UpperCAmelCase      	=		self.image_processing_class.from_json_file(lowercase    )

           self.assertEqual(image_processor_second.to_dict()       ,    image_processor_first.to_dict()    )
       def    A				(     self						:     Any    ):



           '''simple docstring'''

           UpperCAmelCase      	=		self.image_processing_class(**self.image_processor_dict    )

           with tempfile.TemporaryDirectory() as tmpdirname:
               UpperCAmelCase      	=		image_processor_first.save_pretrained(lowercase    )[0]
               check_json_file_has_correct_format(lowercase    )
               UpperCAmelCase      	=		self.image_processing_class.from_pretrained(lowercase    )

           self.assertEqual(image_processor_second.to_dict()       ,    image_processor_first.to_dict()    )
       def    A				(     self						:     str    ):



           '''simple docstring'''

           UpperCAmelCase      	=		self.image_processing_class()
           self.assertIsNotNone(lowercase    )







       @require_torch
       @require_vision
       def    A				(     self						:     Any    ):



           '''simple docstring'''

           if self.test_cast_dtype is not None:
               # 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=lowercase       ,    torchify=lowercase    )

               UpperCAmelCase      	=		image_processor(lowercase       ,    return_tensors='''pt'''    )
               # for layoutLM compatiblity
               self.assertEqual(encoding.pixel_values.device       ,    torch.device('''cpu'''    )    )
               self.assertEqual(encoding.pixel_values.dtype       ,    torch.floataa    )

               UpperCAmelCase      	=		image_processor(lowercase       ,    return_tensors='''pt'''    ).to(torch.floataa    )
               self.assertEqual(encoding.pixel_values.device       ,    torch.device('''cpu'''    )    )
               self.assertEqual(encoding.pixel_values.dtype       ,    torch.floataa    )

               UpperCAmelCase      	=		image_processor(lowercase       ,    return_tensors='''pt'''    ).to('''cpu'''       ,    torch.bfloataa    )
               self.assertEqual(encoding.pixel_values.device       ,    torch.device('''cpu'''    )    )
               self.assertEqual(encoding.pixel_values.dtype       ,    torch.bfloataa    )

               with self.assertRaises(lowercase    ):
                   UpperCAmelCase      	=		image_processor(lowercase       ,    return_tensors='''pt'''    ).to(torch.bfloataa       ,    '''cpu'''    )

               # Try with text + image feature
               UpperCAmelCase      	=		image_processor(lowercase       ,    return_tensors='''pt'''    )
               encoding.update({'''input_ids''': torch.LongTensor([[1, 2, 3], [4, 5, 6]]    )}    )
               UpperCAmelCase      	=		encoding.to(torch.floataa    )

               self.assertEqual(encoding.pixel_values.device       ,    torch.device('''cpu'''    )    )
               self.assertEqual(encoding.pixel_values.dtype       ,    torch.floataa    )
               self.assertEqual(encoding.input_ids.dtype       ,    torch.long    )










'''simple docstring'''







import unittest

from transformers import is_torch_available, load_tool

from .test_tools_common import ToolTesterMixin


if is_torch_available():
   import torch


class       _a  (     unittest.TestCase							,							__a			):
       def    A				(     self						:     str    ):



           '''simple docstring'''

           UpperCAmelCase      	=		load_tool('''speech-to-text'''    )
           self.tool.setup()
       def    A				(     self						:     Dict    ):



           '''simple docstring'''

           UpperCAmelCase      	=		self.tool(torch.ones(3_000    )    )
           self.assertEqual(lowercase       ,    ''' you'''    )







       def    A				(     self						:     Dict    ):



           '''simple docstring'''

           UpperCAmelCase      	=		self.tool(audio=torch.ones(3_000    )    )
           self.assertEqual(lowercase       ,    ''' you'''    )




