





import shutil
import tempfile
import unittest

import numpy as np

from transformers.testing_utils import (
    is_pt_tf_cross_test,
    require_tf,
    require_torch,
    require_torchvision,
    require_vision,
)
from transformers.utils import is_tf_available, is_torch_available, is_vision_available


if is_vision_available():
      from PIL import Image

      from transformers import AutoProcessor, SamImageProcessor, SamProcessor

if is_torch_available():
      import torch

if is_tf_available():
      import tensorflow as tf







@require_vision
@require_torchvision
class     __snake_case       (							unittest.TestCase			):





       '''simple docstring'''







       def 							UpperCAmelCase__ (  self :    int		):
         __snake_case:						Union[str, Any]   =						tempfile.mkdtemp()
         __snake_case:						Tuple   =						SamImageProcessor()
         __snake_case:						Optional[int]   =						SamProcessor(A		)
         processor.save_pretrained(self.tmpdirname		)




       def 							UpperCAmelCase__ (  self :    int							,				**A :    Optional[Any]		):
         return AutoProcessor.from_pretrained(self.tmpdirname							,				**A		).image_processor




       def 							UpperCAmelCase__ (  self :    Dict		):
         shutil.rmtree(self.tmpdirname		)




       def 							UpperCAmelCase__ (  self :    List[str]		):
         __snake_case:						Any   =						[np.random.randint(255							,				size=(3, 30, 400)							,				dtype=np.uinta		)]

         __snake_case:						Optional[Any]   =						[Image.fromarray(np.moveaxis(A							,				0							,				-1		)		) for x in image_inputs]

         return image_inputs




       def 							UpperCAmelCase__ (  self :    int		):
         __snake_case:						Dict   =						SamProcessor(image_processor=self.get_image_processor()		)
         processor.save_pretrained(self.tmpdirname		)

         __snake_case:						Dict   =						self.get_image_processor(do_normalize=A							,				padding_value=1.0		)

         __snake_case:						Tuple   =						SamProcessor.from_pretrained(self.tmpdirname							,				do_normalize=A							,				padding_value=1.0		)

         self.assertEqual(processor.image_processor.to_json_string()							,				image_processor_add_kwargs.to_json_string()		)
         self.assertIsInstance(processor.image_processor							,				A		)




       def 							UpperCAmelCase__ (  self :    Dict		):
         __snake_case:						Optional[int]   =						self.get_image_processor()

         __snake_case:						Dict   =						SamProcessor(image_processor=A		)

         __snake_case:						Optional[Any]   =						self.prepare_image_inputs()

         __snake_case:						Union[str, Any]   =						image_processor(A							,				return_tensors="""np"""		)
         __snake_case:						Optional[int]   =						processor(images=A							,				return_tensors="""np"""		)

         input_feat_extract.pop("""original_sizes"""		)  # pop original_sizes as it is popped in the processor
         input_feat_extract.pop("""reshaped_input_sizes"""		)  # pop original_sizes as it is popped in the processor

         for key in input_feat_extract.keys():
           self.assertAlmostEqual(input_feat_extract[key].sum()							,				input_processor[key].sum()							,				delta=1E-2		)






       @require_torch
       def 							UpperCAmelCase__ (  self :    Dict		):
         __snake_case:						str   =						self.get_image_processor()

         __snake_case:						str   =						SamProcessor(image_processor=A		)
         __snake_case:						Dict   =						[torch.ones((1, 3, 5, 5)		)]

         __snake_case:						str   =						[[1_764, 2_646]]

         __snake_case:						Union[str, Any]   =						[[683, 1_024]]
         __snake_case:						Any   =						processor.post_process_masks(A							,				A							,				A		)
         self.assertEqual(masks[0].shape							,				(1, 3, 1_764, 2_646)		)

         __snake_case:						Dict   =						processor.post_process_masks(
             A							,				torch.tensor(A		)							,				torch.tensor(A		)		)
         self.assertEqual(masks[0].shape							,				(1, 3, 1_764, 2_646)		)

         # should also work with np
         __snake_case:						List[str]   =						[np.ones((1, 3, 5, 5)		)]
         __snake_case:						Union[str, Any]   =						processor.post_process_masks(A							,				np.array(A		)							,				np.array(A		)		)

         self.assertEqual(masks[0].shape							,				(1, 3, 1_764, 2_646)		)

         __snake_case:						Union[str, Any]   =						[[1, 0], [0, 1]]
         with self.assertRaises(A		):
           __snake_case:						Optional[Any]   =						processor.post_process_masks(A							,				np.array(A		)							,				np.array(A		)		)







@require_vision
@require_tf
class     __snake_case       (							unittest.TestCase			):





       '''simple docstring'''







       def 							UpperCAmelCase__ (  self :    List[str]		):
         __snake_case:						Optional[int]   =						tempfile.mkdtemp()
         __snake_case:						Tuple   =						SamImageProcessor()
         __snake_case:						Dict   =						SamProcessor(A		)
         processor.save_pretrained(self.tmpdirname		)




       def 							UpperCAmelCase__ (  self :    str							,				**A :    Any		):
         return AutoProcessor.from_pretrained(self.tmpdirname							,				**A		).image_processor




       def 							UpperCAmelCase__ (  self :    List[Any]		):
         shutil.rmtree(self.tmpdirname		)




       def 							UpperCAmelCase__ (  self :    Optional[int]		):
         __snake_case:						int   =						[np.random.randint(255							,				size=(3, 30, 400)							,				dtype=np.uinta		)]

         __snake_case:						Optional[Any]   =						[Image.fromarray(np.moveaxis(A							,				0							,				-1		)		) for x in image_inputs]

         return image_inputs




       def 							UpperCAmelCase__ (  self :    Union[str, Any]		):
         __snake_case:						int   =						SamProcessor(image_processor=self.get_image_processor()		)
         processor.save_pretrained(self.tmpdirname		)

         __snake_case:						Tuple   =						self.get_image_processor(do_normalize=A							,				padding_value=1.0		)

         __snake_case:						str   =						SamProcessor.from_pretrained(self.tmpdirname							,				do_normalize=A							,				padding_value=1.0		)

         self.assertEqual(processor.image_processor.to_json_string()							,				image_processor_add_kwargs.to_json_string()		)
         self.assertIsInstance(processor.image_processor							,				A		)




       def 							UpperCAmelCase__ (  self :    Optional[Any]		):
         __snake_case:						Any   =						self.get_image_processor()

         __snake_case:						Any   =						SamProcessor(image_processor=A		)

         __snake_case:						List[Any]   =						self.prepare_image_inputs()

         __snake_case:						Dict   =						image_processor(A							,				return_tensors="""np"""		)
         __snake_case:						Optional[int]   =						processor(images=A							,				return_tensors="""np"""		)

         input_feat_extract.pop("""original_sizes"""		)  # pop original_sizes as it is popped in the processor
         input_feat_extract.pop("""reshaped_input_sizes"""		)  # pop reshaped_input_sizes as it is popped in the processor

         for key in input_feat_extract.keys():
           self.assertAlmostEqual(input_feat_extract[key].sum()							,				input_processor[key].sum()							,				delta=1E-2		)






       @require_tf
       def 							UpperCAmelCase__ (  self :    Tuple		):
         __snake_case:						Optional[int]   =						self.get_image_processor()

         __snake_case:						List[Any]   =						SamProcessor(image_processor=A		)
         __snake_case:						str   =						[tf.ones((1, 3, 5, 5)		)]

         __snake_case:						Optional[int]   =						[[1_764, 2_646]]

         __snake_case:						List[str]   =						[[683, 1_024]]
         __snake_case:						Dict   =						processor.post_process_masks(A							,				A							,				A							,				return_tensors="""tf"""		)
         self.assertEqual(masks[0].shape							,				(1, 3, 1_764, 2_646)		)

         __snake_case:						Tuple   =						processor.post_process_masks(
             A							,				tf.convert_to_tensor(A		)							,				tf.convert_to_tensor(A		)							,				return_tensors="""tf"""							,				)
         self.assertEqual(masks[0].shape							,				(1, 3, 1_764, 2_646)		)

         # should also work with np
         __snake_case:						str   =						[np.ones((1, 3, 5, 5)		)]
         __snake_case:						Optional[int]   =						processor.post_process_masks(
             A							,				np.array(A		)							,				np.array(A		)							,				return_tensors="""tf"""		)

         self.assertEqual(masks[0].shape							,				(1, 3, 1_764, 2_646)		)

         __snake_case:						Dict   =						[[1, 0], [0, 1]]
         with self.assertRaises(tf.errors.InvalidArgumentError		):
           __snake_case:						Dict   =						processor.post_process_masks(
               A							,				np.array(A		)							,				np.array(A		)							,				return_tensors="""tf"""		)







@require_vision
@require_torchvision
class     __snake_case       (							unittest.TestCase			):





       '''simple docstring'''







       def 							UpperCAmelCase__ (  self :    Optional[int]		):
         __snake_case:						Tuple   =						tempfile.mkdtemp()
         __snake_case:						Union[str, Any]   =						SamImageProcessor()
         __snake_case:						List[Any]   =						SamProcessor(A		)
         processor.save_pretrained(self.tmpdirname		)




       def 							UpperCAmelCase__ (  self :    str							,				**A :    List[Any]		):
         return AutoProcessor.from_pretrained(self.tmpdirname							,				**A		).image_processor




       def 							UpperCAmelCase__ (  self :    Optional[int]		):
         shutil.rmtree(self.tmpdirname		)




       def 							UpperCAmelCase__ (  self :    Tuple		):
         __snake_case:						Tuple   =						[np.random.randint(255							,				size=(3, 30, 400)							,				dtype=np.uinta		)]

         __snake_case:						Any   =						[Image.fromarray(np.moveaxis(A							,				0							,				-1		)		) for x in image_inputs]

         return image_inputs




       @is_pt_tf_cross_test
       def 							UpperCAmelCase__ (  self :    str		):
         __snake_case:						Optional[int]   =						self.get_image_processor()

         __snake_case:						List[Any]   =						SamProcessor(image_processor=A		)
         __snake_case:						int   =						np.random.randint(0							,				2							,				size=(1, 3, 5, 5)		).astype(np.floataa		)
         __snake_case:						Tuple   =						[tf.convert_to_tensor(A		)]
         __snake_case:						List[Any]   =						[torch.tensor(A		)]

         __snake_case:						Optional[Any]   =						[[1_764, 2_646]]

         __snake_case:						int   =						[[683, 1_024]]
         __snake_case:						Dict   =						processor.post_process_masks(
             A							,				A							,				A							,				return_tensors="""tf"""		)
         __snake_case:						List[Any]   =						processor.post_process_masks(
             A							,				A							,				A							,				return_tensors="""pt"""		)

         self.assertTrue(np.all(tf_masks[0].numpy() == pt_masks[0].numpy()		)		)






       @is_pt_tf_cross_test
       def 							UpperCAmelCase__ (  self :    str		):
         __snake_case:						Any   =						self.get_image_processor()

         __snake_case:						Any   =						SamProcessor(image_processor=A		)

         __snake_case:						Any   =						self.prepare_image_inputs()

         __snake_case:						str   =						image_processor(A							,				return_tensors="""pt"""		)["""pixel_values"""].numpy()
         __snake_case:						List[Any]   =						processor(images=A							,				return_tensors="""pt"""		)["""pixel_values"""].numpy()

         __snake_case:						Any   =						image_processor(A							,				return_tensors="""tf"""		)["""pixel_values"""].numpy()
         __snake_case:						Union[str, Any]   =						processor(images=A							,				return_tensors="""tf"""		)["""pixel_values"""].numpy()

         self.assertTrue(np.allclose(A							,				A		)		)
         self.assertTrue(np.allclose(A							,				A		)		)
         self.assertTrue(np.allclose(A							,				A		)		)













import unittest

from transformers import load_tool

from .test_tools_common import ToolTesterMixin







class     __snake_case       (							unittest.TestCase   ,			__lowerCamelCase			):





       '''simple docstring'''







       def 							UpperCAmelCase__ (  self :    Union[str, Any]		):
         __snake_case:						Optional[int]   =						load_tool("""text-classification"""		)
         self.tool.setup()
         __snake_case:						Dict   =						load_tool("""text-classification"""							,				remote=A		)




       def 							UpperCAmelCase__ (  self :    Optional[int]		):
         __snake_case:						List[str]   =						self.tool("""That's quite cool"""							,				["""positive""", """negative"""]		)
         self.assertEqual(A							,				"""positive"""		)




       def 							UpperCAmelCase__ (  self :    Dict		):
         __snake_case:						List[str]   =						self.remote_tool("""That's quite cool"""							,				["""positive""", """negative"""]		)
         self.assertEqual(A							,				"""positive"""		)




       def 							UpperCAmelCase__ (  self :    Optional[Any]		):
         __snake_case:						Optional[Any]   =						self.tool(text="""That's quite cool"""							,				labels=["""positive""", """negative"""]		)
         self.assertEqual(A							,				"""positive"""		)






       def 							UpperCAmelCase__ (  self :    Tuple		):
         __snake_case:						int   =						self.remote_tool(text="""That's quite cool"""							,				labels=["""positive""", """negative"""]		)
         self.assertEqual(A							,				"""positive"""		)






