





import json
import pathlib
import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision, slow
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 DetaImageProcessor

class     __SCREAMING_SNAKE_CASE   (       unittest.TestCase    ):






							def __init__(  self					,	__lowerCAmelCase					,	__lowerCAmelCase=7					,	__lowerCAmelCase=3					,	__lowerCAmelCase=30					,	__lowerCAmelCase=400					,	__lowerCAmelCase=True					,	__lowerCAmelCase=None					,	__lowerCAmelCase=True					,	__lowerCAmelCase=[0.5, 0.5, 0.5]					,	__lowerCAmelCase=[0.5, 0.5, 0.5]					,	__lowerCAmelCase=True					,	__lowerCAmelCase=1 / 255					,	__lowerCAmelCase=True					,	):
														# by setting size["longest_edge"] > max_resolution we're effectively not testing this :p
														UpperCamelCase__			       =     size if size is not None else {"""shortest_edge""": 18, """longest_edge""": 1333}
														UpperCamelCase__			       =     parent
														UpperCamelCase__			       =     batch_size
														UpperCamelCase__			       =     num_channels
														UpperCamelCase__			       =     min_resolution
														UpperCamelCase__			       =     max_resolution
														UpperCamelCase__			       =     do_resize
														UpperCamelCase__			       =     size
														UpperCamelCase__			       =     do_normalize
														UpperCamelCase__			       =     image_mean
														UpperCamelCase__			       =     image_std
														UpperCamelCase__			       =     do_rescale
														UpperCamelCase__			       =     rescale_factor
														UpperCamelCase__			       =     do_pad






							def 			_lowerCamelCase						(  self					):
														return {
														    "do_resize": self.do_resize,
														    "size": self.size,
														    "do_normalize": self.do_normalize,
														    "image_mean": self.image_mean,
														    "image_std": self.image_std,
														    "do_rescale": self.do_rescale,
														    "rescale_factor": self.rescale_factor,
														    "do_pad": self.do_pad,
														}







							def 			_lowerCamelCase						(  self					,	__lowerCAmelCase					,	__lowerCAmelCase=False					):
														if not batched:
																					UpperCamelCase__			       =     image_inputs[0]
																					if isinstance(lowercase_					,	Image.Image					):
																												UpperCamelCase__			       =     image.size
																					else:
																												UpperCamelCase__			       =     image.shape[1], image.shape[2]
																					if w < h:
																												UpperCamelCase__			       =     int(self.size["""shortest_edge"""] * h / w					)
																												UpperCamelCase__			       =     self.size["""shortest_edge"""]
																					elif w > h:
																												UpperCamelCase__			       =     self.size["""shortest_edge"""]
																												UpperCamelCase__			       =     int(self.size["""shortest_edge"""] * w / h					)
																					else:
																												UpperCamelCase__			       =     self.size["""shortest_edge"""]
																												UpperCamelCase__			       =     self.size["""shortest_edge"""]

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

														return expected_height, expected_width






@require_torch
@require_vision
class     __SCREAMING_SNAKE_CASE   (       _UpperCAmelCase				,						unittest.TestCase    ):
							snake_case       : Optional[Any]         =    DetaImageProcessor if is_vision_available() else None






							def 			_lowerCamelCase						(  self					):
														UpperCamelCase__			       =     DetaImageProcessingTester(self					)






							@property
							def 			_lowerCamelCase						(  self					):
														return self.image_processor_tester.prepare_image_processor_dict()






							def 			_lowerCamelCase						(  self					):
														UpperCamelCase__			       =     self.image_processing_class(**self.image_processor_dict					)
														self.assertTrue(hasattr(lowercase_					,	"""image_mean"""					)					)
														self.assertTrue(hasattr(lowercase_					,	"""image_std"""					)					)
														self.assertTrue(hasattr(lowercase_					,	"""do_normalize"""					)					)
														self.assertTrue(hasattr(lowercase_					,	"""do_resize"""					)					)
														self.assertTrue(hasattr(lowercase_					,	"""do_rescale"""					)					)
														self.assertTrue(hasattr(lowercase_					,	"""do_pad"""					)					)
														self.assertTrue(hasattr(lowercase_					,	"""size"""					)					)






							def 			_lowerCamelCase						(  self					):
														UpperCamelCase__			       =     self.image_processing_class.from_dict(self.image_processor_dict					)
														self.assertEqual(image_processor.size					,	{"""shortest_edge""": 18, """longest_edge""": 1333}					)
														self.assertEqual(image_processor.do_pad					,	lowercase_					)






							def 			_lowerCamelCase						(  self					):
														pass






							def 			_lowerCamelCase						(  self					):
														# Initialize image_processing
														UpperCamelCase__			       =     self.image_processing_class(**self.image_processor_dict					)
														# create random PIL images
														UpperCamelCase__			       =     prepare_image_inputs(self.image_processor_tester					,	equal_resolution=lowercase_					)
														for image in image_inputs:
																					self.assertIsInstance(lowercase_					,	Image.Image					)

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

														UpperCamelCase__			       =     self.image_processor_tester.get_expected_values(lowercase_					)

														self.assertEqual(
														    encoded_images.shape					,	(1, self.image_processor_tester.num_channels, expected_height, expected_width)					,	)

														# Test batched
														UpperCamelCase__			       =     self.image_processor_tester.get_expected_values(lowercase_					,	batched=lowercase_					)

														UpperCamelCase__			       =     image_processing(lowercase_					,	return_tensors="""pt"""					).pixel_values
														self.assertEqual(
														    encoded_images.shape					,	(
														        self.image_processor_tester.batch_size,
														        self.image_processor_tester.num_channels,
														        expected_height,
														        expected_width,
														    )					,	)






							def 			_lowerCamelCase						(  self					):
														# Initialize image_processing
														UpperCamelCase__			       =     self.image_processing_class(**self.image_processor_dict					)
														# create random numpy tensors
														UpperCamelCase__			       =     prepare_image_inputs(self.image_processor_tester					,	equal_resolution=lowercase_					,	numpify=lowercase_					)
														for image in image_inputs:
																					self.assertIsInstance(lowercase_					,	np.ndarray					)

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

														UpperCamelCase__			       =     self.image_processor_tester.get_expected_values(lowercase_					)

														self.assertEqual(
														    encoded_images.shape					,	(1, self.image_processor_tester.num_channels, expected_height, expected_width)					,	)

														# Test batched
														UpperCamelCase__			       =     image_processing(lowercase_					,	return_tensors="""pt"""					).pixel_values

														UpperCamelCase__			       =     self.image_processor_tester.get_expected_values(lowercase_					,	batched=lowercase_					)

														self.assertEqual(
														    encoded_images.shape					,	(
														        self.image_processor_tester.batch_size,
														        self.image_processor_tester.num_channels,
														        expected_height,
														        expected_width,
														    )					,	)






							def 			_lowerCamelCase						(  self					):
														# Initialize image_processing
														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_					)
														for image in image_inputs:
																					self.assertIsInstance(lowercase_					,	torch.Tensor					)

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

														UpperCamelCase__			       =     self.image_processor_tester.get_expected_values(lowercase_					)

														self.assertEqual(
														    encoded_images.shape					,	(1, self.image_processor_tester.num_channels, expected_height, expected_width)					,	)

														# Test batched
														UpperCamelCase__			       =     image_processing(lowercase_					,	return_tensors="""pt"""					).pixel_values

														UpperCamelCase__			       =     self.image_processor_tester.get_expected_values(lowercase_					,	batched=lowercase_					)

														self.assertEqual(
														    encoded_images.shape					,	(
														        self.image_processor_tester.batch_size,
														        self.image_processor_tester.num_channels,
														        expected_height,
														        expected_width,
														    )					,	)






							@slow
							def 			_lowerCamelCase						(  self					):
														# prepare image and target
														UpperCamelCase__			       =     Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""					)
														with open("""./tests/fixtures/tests_samples/COCO/coco_annotations.txt"""					,	"""r"""					) as f:
																					UpperCamelCase__			       =     json.loads(f.read()					)

														UpperCamelCase__			       =     {"""image_id""": 39769, """annotations""": target}

														# encode them
														UpperCamelCase__			       =     DetaImageProcessor()
														UpperCamelCase__			       =     image_processing(images=lowercase_					,	annotations=lowercase_					,	return_tensors="""pt"""					)

														# verify pixel values
														UpperCamelCase__			       =     torch.Size([1, 3, 800, 1066]					)
														self.assertEqual(encoding["""pixel_values"""].shape					,	lowercase_					)

														UpperCamelCase__			       =     torch.tensor([0.2796, 0.3138, 0.3481]					)
														self.assertTrue(torch.allclose(encoding["""pixel_values"""][0, 0, 0, :3]					,	lowercase_					,	atol=1E-4					)					)

														# verify area
														UpperCamelCase__			       =     torch.tensor([5887.9600, 11250.2061, 489353.8438, 837122.7500, 147967.5156, 165732.3438]					)
														self.assertTrue(torch.allclose(encoding["""labels"""][0]["""area"""]					,	lowercase_					)					)
														# verify boxes
														UpperCamelCase__			       =     torch.Size([6, 4]					)
														self.assertEqual(encoding["""labels"""][0]["""boxes"""].shape					,	lowercase_					)
														UpperCamelCase__			       =     torch.tensor([0.5503, 0.2765, 0.0604, 0.2215]					)
														self.assertTrue(torch.allclose(encoding["""labels"""][0]["""boxes"""][0]					,	lowercase_					,	atol=1E-3					)					)
														# verify image_id
														UpperCamelCase__			       =     torch.tensor([39769]					)
														self.assertTrue(torch.allclose(encoding["""labels"""][0]["""image_id"""]					,	lowercase_					)					)
														# verify is_crowd
														UpperCamelCase__			       =     torch.tensor([0, 0, 0, 0, 0, 0]					)
														self.assertTrue(torch.allclose(encoding["""labels"""][0]["""iscrowd"""]					,	lowercase_					)					)
														# verify class_labels
														UpperCamelCase__			       =     torch.tensor([75, 75, 63, 65, 17, 17]					)
														self.assertTrue(torch.allclose(encoding["""labels"""][0]["""class_labels"""]					,	lowercase_					)					)
														# verify orig_size
														UpperCamelCase__			       =     torch.tensor([480, 640]					)
														self.assertTrue(torch.allclose(encoding["""labels"""][0]["""orig_size"""]					,	lowercase_					)					)
														# verify size
														UpperCamelCase__			       =     torch.tensor([800, 1066]					)
														self.assertTrue(torch.allclose(encoding["""labels"""][0]["""size"""]					,	lowercase_					)					)







							@slow
							def 			_lowerCamelCase						(  self					):
														# prepare image, target and masks_path
														UpperCamelCase__			       =     Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""					)
														with open("""./tests/fixtures/tests_samples/COCO/coco_panoptic_annotations.txt"""					,	"""r"""					) as f:
																					UpperCamelCase__			       =     json.loads(f.read()					)

														UpperCamelCase__			       =     {"""file_name""": """000000039769.png""", """image_id""": 39769, """segments_info""": target}

														UpperCamelCase__			       =     pathlib.Path("""./tests/fixtures/tests_samples/COCO/coco_panoptic"""					)

														# encode them
														UpperCamelCase__			       =     DetaImageProcessor(format="""coco_panoptic"""					)
														UpperCamelCase__			       =     image_processing(images=lowercase_					,	annotations=lowercase_					,	masks_path=lowercase_					,	return_tensors="""pt"""					)

														# verify pixel values
														UpperCamelCase__			       =     torch.Size([1, 3, 800, 1066]					)
														self.assertEqual(encoding["""pixel_values"""].shape					,	lowercase_					)

														UpperCamelCase__			       =     torch.tensor([0.2796, 0.3138, 0.3481]					)
														self.assertTrue(torch.allclose(encoding["""pixel_values"""][0, 0, 0, :3]					,	lowercase_					,	atol=1E-4					)					)

														# verify area
														UpperCamelCase__			       =     torch.tensor([147979.6875, 165527.0469, 484638.5938, 11292.9375, 5879.6562, 7634.1147]					)
														self.assertTrue(torch.allclose(encoding["""labels"""][0]["""area"""]					,	lowercase_					)					)
														# verify boxes
														UpperCamelCase__			       =     torch.Size([6, 4]					)
														self.assertEqual(encoding["""labels"""][0]["""boxes"""].shape					,	lowercase_					)
														UpperCamelCase__			       =     torch.tensor([0.2625, 0.5437, 0.4688, 0.8625]					)
														self.assertTrue(torch.allclose(encoding["""labels"""][0]["""boxes"""][0]					,	lowercase_					,	atol=1E-3					)					)
														# verify image_id
														UpperCamelCase__			       =     torch.tensor([39769]					)
														self.assertTrue(torch.allclose(encoding["""labels"""][0]["""image_id"""]					,	lowercase_					)					)
														# verify is_crowd
														UpperCamelCase__			       =     torch.tensor([0, 0, 0, 0, 0, 0]					)
														self.assertTrue(torch.allclose(encoding["""labels"""][0]["""iscrowd"""]					,	lowercase_					)					)
														# verify class_labels
														UpperCamelCase__			       =     torch.tensor([17, 17, 63, 75, 75, 93]					)
														self.assertTrue(torch.allclose(encoding["""labels"""][0]["""class_labels"""]					,	lowercase_					)					)
														# verify masks
														UpperCamelCase__			       =     822873
														self.assertEqual(encoding["""labels"""][0]["""masks"""].sum().item()					,	lowercase_					)
														# verify orig_size
														UpperCamelCase__			       =     torch.tensor([480, 640]					)
														self.assertTrue(torch.allclose(encoding["""labels"""][0]["""orig_size"""]					,	lowercase_					)					)
														# verify size
														UpperCamelCase__			       =     torch.tensor([800, 1066]					)
														self.assertTrue(torch.allclose(encoding["""labels"""][0]["""size"""]					,	lowercase_					)					)











from datetime import datetime as dt
import os

from github import Github


UpperCamelCase__     =	[
    "good first issue",
    "good second issue",
    "good difficult issue",
    "feature request",
    "new model",
    "wip",
]





def       _UpperCamelCase       ():






							"""simple docstring"""





							UpperCamelCase__			       =     Github(os.environ["""GITHUB_TOKEN"""]    )
							UpperCamelCase__			       =     g.get_repo("""huggingface/transformers"""    )
							UpperCamelCase__			       =     repo.get_issues(state="""open"""    )

							for issue in open_issues:
														UpperCamelCase__			       =     sorted([comment for comment in issue.get_comments()]				,     key=lambda a__    : i.created_at				,     reverse=a__    )
														UpperCamelCase__			       =     comments[0] if len(a__    ) > 0 else None
														if (
														    last_comment is not None
														    and last_comment.user.login == "github-actions[bot]"
														    and (dt.utcnow() - issue.updated_at).days > 7
														    and (dt.utcnow() - issue.created_at).days >= 30
														    and not any(label.name.lower() in LABELS_TO_EXEMPT for label in issue.get_labels()    )
														):
																					# print(f"Would close issue {issue.number} since it has been 7 days of inactivity since bot mention.")
																					issue.edit(state="""closed"""    )
														elif (
														    (dt.utcnow() - issue.updated_at).days > 23
														    and (dt.utcnow() - issue.created_at).days >= 30
														    and not any(label.name.lower() in LABELS_TO_EXEMPT for label in issue.get_labels()    )
														):
																					# print(f"Would add stale comment to {issue.number}")
																					issue.create_comment(
																					    """This issue has been automatically marked as stale because it has not had """
																					    """recent activity. If you think this still needs to be addressed """
																					    """please comment on this thread.\n\nPlease note that issues that do not follow the """
																					    """[contributing guidelines](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md) """
																					    """are likely to be ignored."""    )


if __name__ == "__main__":
					main()




