
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 		__magic_name__ (    unittest.TestCase     ):





		def    _A				(				self     :						str					):
							lowerCAmelCase		:       Optional[Any]							=  tempfile.mkdtemp()
							lowerCAmelCase		:       Optional[Any]							=  SamImageProcessor()
							lowerCAmelCase		:       List[Any]							=  SamProcessor(__UpperCamelCase					)
							processor.save_pretrained(self.tmpdirname					)





		def    _A				(				self     :						Dict    ,	**lowerCamelCase__     :						int					):
							return AutoProcessor.from_pretrained(self.tmpdirname    ,	**__UpperCamelCase					).image_processor





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





		def    _A				(				self     :						Optional[Any]					):
							lowerCAmelCase		:       Union[str, Any]							=  [np.random.randint(2_5_5    ,	size=(3, 3_0, 4_0_0)    ,	dtype=np.uinta					)]

							lowerCAmelCase		:       int							=  [Image.fromarray(np.moveaxis(__UpperCamelCase    ,	0    ,	-1					)					) for x in image_inputs]

							return image_inputs





		def    _A				(				self     :						Optional[int]					):
							lowerCAmelCase		:       Dict							=  SamProcessor(image_processor=self.get_image_processor()					)
							processor.save_pretrained(self.tmpdirname					)

							lowerCAmelCase		:       Any							=  self.get_image_processor(do_normalize=__UpperCamelCase    ,	padding_value=1.0					)

							lowerCAmelCase		:       Optional[int]							=  SamProcessor.from_pretrained(self.tmpdirname    ,	do_normalize=__UpperCamelCase    ,	padding_value=1.0					)

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





		def    _A				(				self     :						int					):
							lowerCAmelCase		:       str							=  self.get_image_processor()

							lowerCAmelCase		:       List[str]							=  SamProcessor(image_processor=__UpperCamelCase					)

							lowerCAmelCase		:       Optional[int]							=  self.prepare_image_inputs()

							lowerCAmelCase		:       List[Any]							=  image_processor(__UpperCamelCase    ,	return_tensors='''np'''					)
							lowerCAmelCase		:       Dict							=  processor(images=__UpperCamelCase    ,	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    _A				(				self     :						Union[str, Any]					):
							lowerCAmelCase		:       Any							=  self.get_image_processor()

							lowerCAmelCase		:       Any							=  SamProcessor(image_processor=__UpperCamelCase					)
							lowerCAmelCase		:       List[str]							=  [torch.ones((1, 3, 5, 5)					)]

							lowerCAmelCase		:       int							=  [[1_7_6_4, 2_6_4_6]]

							lowerCAmelCase		:       List[str]							=  [[6_8_3, 1_0_2_4]]
							lowerCAmelCase		:       Dict							=  processor.post_process_masks(__UpperCamelCase    ,	__UpperCamelCase    ,	__UpperCamelCase					)
							self.assertEqual(masks[0].shape    ,	(1, 3, 1_7_6_4, 2_6_4_6)					)

							lowerCAmelCase		:       Tuple							=  processor.post_process_masks(
							    __UpperCamelCase    ,	torch.tensor(__UpperCamelCase					)    ,	torch.tensor(__UpperCamelCase					)					)
							self.assertEqual(masks[0].shape    ,	(1, 3, 1_7_6_4, 2_6_4_6)					)

							# should also work with np
							lowerCAmelCase		:       Optional[Any]							=  [np.ones((1, 3, 5, 5)					)]
							lowerCAmelCase		:       Dict							=  processor.post_process_masks(__UpperCamelCase    ,	np.array(__UpperCamelCase					)    ,	np.array(__UpperCamelCase					)					)

							self.assertEqual(masks[0].shape    ,	(1, 3, 1_7_6_4, 2_6_4_6)					)

							lowerCAmelCase		:       Union[str, Any]							=  [[1, 0], [0, 1]]
							with self.assertRaises(__UpperCamelCase					):
												lowerCAmelCase		:       List[Any]							=  processor.post_process_masks(__UpperCamelCase    ,	np.array(__UpperCamelCase					)    ,	np.array(__UpperCamelCase					)					)



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





		def    _A				(				self     :						Optional[Any]					):
							lowerCAmelCase		:       Optional[int]							=  tempfile.mkdtemp()
							lowerCAmelCase		:       str							=  SamImageProcessor()
							lowerCAmelCase		:       Union[str, Any]							=  SamProcessor(__UpperCamelCase					)
							processor.save_pretrained(self.tmpdirname					)





		def    _A				(				self     :						Optional[int]    ,	**lowerCamelCase__     :						Optional[Any]					):
							return AutoProcessor.from_pretrained(self.tmpdirname    ,	**__UpperCamelCase					).image_processor





		def    _A				(				self     :						Optional[Any]					):
							shutil.rmtree(self.tmpdirname					)





		def    _A				(				self     :						Optional[int]					):
							lowerCAmelCase		:       int							=  [np.random.randint(2_5_5    ,	size=(3, 3_0, 4_0_0)    ,	dtype=np.uinta					)]

							lowerCAmelCase		:       Optional[int]							=  [Image.fromarray(np.moveaxis(__UpperCamelCase    ,	0    ,	-1					)					) for x in image_inputs]

							return image_inputs





		def    _A				(				self     :						List[str]					):
							lowerCAmelCase		:       Tuple							=  SamProcessor(image_processor=self.get_image_processor()					)
							processor.save_pretrained(self.tmpdirname					)

							lowerCAmelCase		:       Any							=  self.get_image_processor(do_normalize=__UpperCamelCase    ,	padding_value=1.0					)

							lowerCAmelCase		:       Dict							=  SamProcessor.from_pretrained(self.tmpdirname    ,	do_normalize=__UpperCamelCase    ,	padding_value=1.0					)

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





		def    _A				(				self     :						Optional[int]					):
							lowerCAmelCase		:       Tuple							=  self.get_image_processor()

							lowerCAmelCase		:       List[Any]							=  SamProcessor(image_processor=__UpperCamelCase					)

							lowerCAmelCase		:       str							=  self.prepare_image_inputs()

							lowerCAmelCase		:       List[str]							=  image_processor(__UpperCamelCase    ,	return_tensors='''np'''					)
							lowerCAmelCase		:       Dict							=  processor(images=__UpperCamelCase    ,	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    _A				(				self     :						Dict					):
							lowerCAmelCase		:       Dict							=  self.get_image_processor()

							lowerCAmelCase		:       List[Any]							=  SamProcessor(image_processor=__UpperCamelCase					)
							lowerCAmelCase		:       Dict							=  [tf.ones((1, 3, 5, 5)					)]

							lowerCAmelCase		:       Union[str, Any]							=  [[1_7_6_4, 2_6_4_6]]

							lowerCAmelCase		:       int							=  [[6_8_3, 1_0_2_4]]
							lowerCAmelCase		:       List[Any]							=  processor.post_process_masks(__UpperCamelCase    ,	__UpperCamelCase    ,	__UpperCamelCase    ,	return_tensors='''tf'''					)
							self.assertEqual(masks[0].shape    ,	(1, 3, 1_7_6_4, 2_6_4_6)					)

							lowerCAmelCase		:       Tuple							=  processor.post_process_masks(
							    __UpperCamelCase    ,	tf.convert_to_tensor(__UpperCamelCase					)    ,	tf.convert_to_tensor(__UpperCamelCase					)    ,	return_tensors='''tf'''    ,	)
							self.assertEqual(masks[0].shape    ,	(1, 3, 1_7_6_4, 2_6_4_6)					)

							# should also work with np
							lowerCAmelCase		:       Any							=  [np.ones((1, 3, 5, 5)					)]
							lowerCAmelCase		:       Any							=  processor.post_process_masks(
							    __UpperCamelCase    ,	np.array(__UpperCamelCase					)    ,	np.array(__UpperCamelCase					)    ,	return_tensors='''tf'''					)

							self.assertEqual(masks[0].shape    ,	(1, 3, 1_7_6_4, 2_6_4_6)					)

							lowerCAmelCase		:       Dict							=  [[1, 0], [0, 1]]
							with self.assertRaises(tf.errors.InvalidArgumentError					):
												lowerCAmelCase		:       Any							=  processor.post_process_masks(
												    __UpperCamelCase    ,	np.array(__UpperCamelCase					)    ,	np.array(__UpperCamelCase					)    ,	return_tensors='''tf'''					)



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





		def    _A				(				self     :						Dict					):
							lowerCAmelCase		:       Any							=  tempfile.mkdtemp()
							lowerCAmelCase		:       Union[str, Any]							=  SamImageProcessor()
							lowerCAmelCase		:       int							=  SamProcessor(__UpperCamelCase					)
							processor.save_pretrained(self.tmpdirname					)





		def    _A				(				self     :						List[Any]    ,	**lowerCamelCase__     :						str					):
							return AutoProcessor.from_pretrained(self.tmpdirname    ,	**__UpperCamelCase					).image_processor





		def    _A				(				self     :						Any					):
							shutil.rmtree(self.tmpdirname					)





		def    _A				(				self     :						Union[str, Any]					):
							lowerCAmelCase		:       Dict							=  [np.random.randint(2_5_5    ,	size=(3, 3_0, 4_0_0)    ,	dtype=np.uinta					)]

							lowerCAmelCase		:       List[str]							=  [Image.fromarray(np.moveaxis(__UpperCamelCase    ,	0    ,	-1					)					) for x in image_inputs]

							return image_inputs





		@is_pt_tf_cross_test
		def    _A				(				self     :						int					):
							lowerCAmelCase		:       List[str]							=  self.get_image_processor()

							lowerCAmelCase		:       Any							=  SamProcessor(image_processor=__UpperCamelCase					)
							lowerCAmelCase		:       str							=  np.random.randint(0    ,	2    ,	size=(1, 3, 5, 5)					).astype(np.floataa					)
							lowerCAmelCase		:       Dict							=  [tf.convert_to_tensor(__UpperCamelCase					)]
							lowerCAmelCase		:       Tuple							=  [torch.tensor(__UpperCamelCase					)]

							lowerCAmelCase		:       Optional[int]							=  [[1_7_6_4, 2_6_4_6]]

							lowerCAmelCase		:       int							=  [[6_8_3, 1_0_2_4]]
							lowerCAmelCase		:       str							=  processor.post_process_masks(
							    __UpperCamelCase    ,	__UpperCamelCase    ,	__UpperCamelCase    ,	return_tensors='''tf'''					)
							lowerCAmelCase		:       List[Any]							=  processor.post_process_masks(
							    __UpperCamelCase    ,	__UpperCamelCase    ,	__UpperCamelCase    ,	return_tensors='''pt'''					)

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





		@is_pt_tf_cross_test
		def    _A				(				self     :						int					):
							lowerCAmelCase		:       int							=  self.get_image_processor()

							lowerCAmelCase		:       str							=  SamProcessor(image_processor=__UpperCamelCase					)

							lowerCAmelCase		:       int							=  self.prepare_image_inputs()

							lowerCAmelCase		:       Optional[Any]							=  image_processor(__UpperCamelCase    ,	return_tensors='''pt'''					)['''pixel_values'''].numpy()
							lowerCAmelCase		:       Dict							=  processor(images=__UpperCamelCase    ,	return_tensors='''pt'''					)['''pixel_values'''].numpy()

							lowerCAmelCase		:       str							=  image_processor(__UpperCamelCase    ,	return_tensors='''tf'''					)['''pixel_values'''].numpy()
							lowerCAmelCase		:       Tuple							=  processor(images=__UpperCamelCase    ,	return_tensors='''tf'''					)['''pixel_values'''].numpy()

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








from __future__ import annotations

import os
from typing import Any

import requests

lowerCamelCase      :       Tuple    =						'''https://api.github.com'''

# https://docs.github.com/en/free-pro-team@latest/rest/reference/users#get-the-authenticated-user
lowerCamelCase      :       int    =						BASE_URL + '''/user'''

# https://github.com/settings/tokens
lowerCamelCase      :       Any    =						os.environ.get('''USER_TOKEN''', '''''')





def    __lowerCAmelCase   (    __snake_case							):
     __lowerCAmelCase             =       {
         "Authorization": F"""token {auth_token}""",
         "Accept": "application/vnd.github.v3+json",
     }
     return requests.get(__snake_case       ,			headers=__snake_case							).json()


if __name__ == "__main__":  # pragma: no cover
 if USER_TOKEN:
  for key, value in fetch_github_info(USER_TOKEN).items():
   print(F'''{key}: {value}''')
 else:
  raise ValueError('''\'USER_TOKEN\' field cannot be empty.''')






