



import gc
import random
import unittest

import numpy as np
import torch
from transformers import CLIPImageProcessor, CLIPVisionConfig, CLIPVisionModel

from diffusers import HeunDiscreteScheduler, PriorTransformer, ShapEImgaImgPipeline
from diffusers.pipelines.shap_e import ShapERenderer
from diffusers.utils import floats_tensor, load_image, load_numpy, slow
from diffusers.utils.testing_utils import require_torch_gpu, torch_device

from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference

class  __snake_case      (   SCREAMING_SNAKE_CASE			,unittest.TestCase):







							'''simple docstring'''






							UpperCamelCase__				:      Any         =					ShapEImgaImgPipeline
							UpperCamelCase__				:      int         =					["""image"""]
							UpperCamelCase__				:      List[Any]         =					["""image"""]
							UpperCamelCase__				:      Tuple         =					[
							    """num_images_per_prompt""",
							    """num_inference_steps""",
							    """generator""",
							    """latents""",
							    """guidance_scale""",
							    """frame_size""",
							    """output_type""",
							    """return_dict""",
							]
							UpperCamelCase__				:      Any         =					False


							@property
							def _a      (							self		):
											return 32


							@property
							def _a      (							self		):
											return 32


							@property
							def _a      (							self		):
											return self.time_input_dim * 4


							@property
							def _a      (							self		):
											return 8


							@property
							def _a      (							self		):
											torch.manual_seed(0		)
											a__				      =		CLIPVisionConfig(
											    hidden_size=self.text_embedder_hidden_size  ,  image_size=64  ,  projection_dim=self.text_embedder_hidden_size  ,  intermediate_size=37  ,  num_attention_heads=4  ,  num_channels=3  ,  num_hidden_layers=5  ,  patch_size=1  ,  )

											a__				      =		CLIPVisionModel(a_		)
											return model


							@property
							def _a      (							self		):
											a__				      =		CLIPImageProcessor(
											    crop_size=224  ,  do_center_crop=a_  ,  do_normalize=a_  ,  do_resize=a_  ,  image_mean=[0.48_145_466, 0.4_578_275, 0.40_821_073]  ,  image_std=[0.26_862_954, 0.26_130_258, 0.27_577_711]  ,  resample=3  ,  size=224  ,  )

											return image_processor


							@property
							def _a      (							self		):
											torch.manual_seed(0		)

											a__				      =		{
											    """num_attention_heads""": 2,
											    """attention_head_dim""": 16,
											    """embedding_dim""": self.time_input_dim,
											    """num_embeddings""": 32,
											    """embedding_proj_dim""": self.text_embedder_hidden_size,
											    """time_embed_dim""": self.time_embed_dim,
											    """num_layers""": 1,
											    """clip_embed_dim""": self.time_input_dim * 2,
											    """additional_embeddings""": 0,
											    """time_embed_act_fn""": """gelu""",
											    """norm_in_type""": """layer""",
											    """embedding_proj_norm_type""": """layer""",
											    """encoder_hid_proj_type""": None,
											    """added_emb_type""": None,
											}

											a__				      =		PriorTransformer(**a_		)
											return model


							@property
							def _a      (							self		):
											torch.manual_seed(0		)

											a__				      =		{
											    """param_shapes""": (
											        (self.renderer_dim, 93),
											        (self.renderer_dim, 8),
											        (self.renderer_dim, 8),
											        (self.renderer_dim, 8),
											    ),
											    """d_latent""": self.time_input_dim,
											    """d_hidden""": self.renderer_dim,
											    """n_output""": 12,
											    """background""": (
											        0.1,
											        0.1,
											        0.1,
											    ),
											}
											a__				      =		ShapERenderer(**a_		)
											return model


							def _a      (							self		):
											a__				      =		self.dummy_prior
											a__				      =		self.dummy_image_encoder
											a__				      =		self.dummy_image_processor
											a__				      =		self.dummy_renderer

											a__				      =		HeunDiscreteScheduler(
											    beta_schedule="""exp"""  ,  num_train_timesteps=1_024  ,  prediction_type="""sample"""  ,  use_karras_sigmas=a_  ,  clip_sample=a_  ,  clip_sample_range=1.0  ,  )
											a__				      =		{
											    """prior""": prior,
											    """image_encoder""": image_encoder,
											    """image_processor""": image_processor,
											    """renderer""": renderer,
											    """scheduler""": scheduler,
											}

											return components


							def _a      (							self  ,  a_  ,  a_=0		):
											a__				      =		floats_tensor((1, 3, 64, 64)  ,  rng=random.Random(a_		)		).to(a_		)

											if str(a_		).startswith("""mps"""		):
															a__				      =		torch.manual_seed(a_		)
											else:
															a__				      =		torch.Generator(device=a_		).manual_seed(a_		)
											a__				      =		{
											    """image""": input_image,
											    """generator""": generator,
											    """num_inference_steps""": 1,
											    """frame_size""": 32,
											    """output_type""": """np""",
											}
											return inputs


							def _a      (							self		):
											a__				      =		"""cpu"""

											a__				      =		self.get_dummy_components()

											a__				      =		self.pipeline_class(**a_		)
											a__				      =		pipe.to(a_		)

											pipe.set_progress_bar_config(disable=a_		)

											a__				      =		pipe(**self.get_dummy_inputs(a_		)		)
											a__				      =		output.images[0]
											a__				      =		image[0, -3:, -3:, -1]

											assert image.shape == (20, 32, 32, 3)

											a__				      =		np.array(
											    [
											        0.00_039_216,
											        0.00_039_216,
											        0.00_039_216,
											        0.00_039_216,
											        0.00_039_216,
											        0.00_039_216,
											        0.00_039_216,
											        0.00_039_216,
											        0.00_039_216,
											    ]		)

											assert np.abs(image_slice.flatten() - expected_slice		).max() < 1E-2


							def _a      (							self		):
											# NOTE: Larger batch sizes cause this test to timeout, only test on smaller batches
											self._test_inference_batch_consistent(batch_sizes=[1, 2]		)


							def _a      (							self		):
											a__				      =		torch_device == """cpu"""
											a__				      =		True
											self._test_inference_batch_single_identical(
											    batch_size=2  ,  test_max_difference=a_  ,  relax_max_difference=a_  ,  )







							def _a      (							self		):
											a__				      =		self.get_dummy_components()
											a__				      =		self.pipeline_class(**a_		)
											a__				      =		pipe.to(a_		)
											pipe.set_progress_bar_config(disable=a_		)

											a__				      =		1
											a__				      =		2

											a__				      =		self.get_dummy_inputs(a_		)

											for key in inputs.keys():
															if key in self.batch_params:
																			a__				      =		batch_size * [inputs[key]]

											a__				      =		pipe(**a_  ,  num_images_per_prompt=a_		)[0]

											assert images.shape[0] == batch_size * num_images_per_prompt




@slow
@require_torch_gpu
class  __snake_case      (   unittest.TestCase):







							'''simple docstring'''






							def _a      (							self		):
											# clean up the VRAM after each test
											super().tearDown()
											gc.collect()
											torch.cuda.empty_cache()







							def _a      (							self		):
											a__				      =		load_image(
											    """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/shap_e/corgi.png"""		)
											a__				      =		load_numpy(
											    """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"""
											    """/shap_e/test_shap_e_img2img_out.npy"""		)
											a__				      =		ShapEImgaImgPipeline.from_pretrained("""openai/shap-e-img2img"""		)
											a__				      =		pipe.to(a_		)
											pipe.set_progress_bar_config(disable=a_		)

											a__				      =		torch.Generator(device=a_		).manual_seed(0		)

											a__				      =		pipe(
											    a_  ,  generator=a_  ,  guidance_scale=3.0  ,  num_inference_steps=64  ,  frame_size=64  ,  output_type="""np"""  ,  ).images[0]

											assert images.shape == (20, 64, 64, 3)

											assert_mean_pixel_difference(a_  ,  a_		)






import os
from datetime import datetime as dt

from github import Github


UpperCAmelCase							 =					[
    """good first issue""",
    """good second issue""",
    """good difficult issue""",
    """enhancement""",
    """new pipeline/model""",
    """new scheduler""",
    """wip""",
]

def 			A_      (	):

				"""simple docstring"""
				a__				      =		Github(os.environ["""GITHUB_TOKEN"""]		)
				a__				      =		g.get_repo("""huggingface/diffusers"""		)
				a__				      =		repo.get_issues(state="""open"""		)

				for issue in open_issues:
								a__				      =		sorted(issue.get_comments() ,		key=lambda __a		: i.created_at ,		reverse=__a		)
								a__				      =		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()		)
								):
												# Closes the issue after 7 days of inactivity since the Stalebot notification.
												issue.edit(state="""closed"""		)
								elif (
								    "stale" in issue.get_labels()
								    and last_comment is not None
								    and last_comment.user.login != "github-actions[bot]"
								):
												# Opens the issue if someone other than Stalebot commented.
												issue.edit(state="""open"""		)
												issue.remove_from_labels("""stale"""		)
								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()		)
								):
												# Post a Stalebot notification after 23 days of inactivity.
												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/diffusers/blob/main/CONTRIBUTING.md) """
												    """are likely to be ignored."""		)
												issue.add_to_labels("""stale"""		)


if __name__ == "__main__":
	main()

