
'''simple docstring'''

import gc
import tempfile
import unittest

import numpy as np
import torch

from diffusers import VersatileDiffusionPipeline
from diffusers.utils.testing_utils import load_image, nightly, require_torch_gpu, torch_device


snake_case_			=     False


class 	a__     (				unittest.TestCase	):
	pass



@nightly
@require_torch_gpu
class 	a__     (				unittest.TestCase	):


	def 				lowercase__  (self : Union[str, Any]      )     ->			Optional[Any]:

					"""simple docstring"""

					super().tearDown()
					gc.collect()
					torch.cuda.empty_cache()


	def 				lowercase__  (self : Union[str, Any]      )     ->			Optional[Any]:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						List[Any]      					= VersatileDiffusionPipeline.from_pretrained('''shi-labs/versatile-diffusion''',					torch_dtype=torch.floataa      )
					pipe.to(UpperCamelCase_      )
					pipe.set_progress_bar_config(disable=UpperCamelCase_      )

					SCREAMING_SNAKE_CASE  :						Optional[int]      					= load_image(
					    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg'''      )

					SCREAMING_SNAKE_CASE  :						List[Any]      					= torch.manual_seed(0      )
					SCREAMING_SNAKE_CASE  :						Dict      					= pipe.dual_guided(
					    prompt='''first prompt''',					image=UpperCamelCase_,					text_to_image_strength=0.75,					generator=UpperCamelCase_,					guidance_scale=7.5,					num_inference_steps=2,					output_type='''numpy''',					).images

					with tempfile.TemporaryDirectory() as tmpdirname:
									pipe.save_pretrained(UpperCamelCase_      )
									SCREAMING_SNAKE_CASE  :						Union[str, Any]      					= VersatileDiffusionPipeline.from_pretrained(UpperCamelCase_,					torch_dtype=torch.floataa      )
					pipe.to(UpperCamelCase_      )
					pipe.set_progress_bar_config(disable=UpperCamelCase_      )

					SCREAMING_SNAKE_CASE  :						List[str]      					= generator.manual_seed(0      )
					SCREAMING_SNAKE_CASE  :						str      					= pipe.dual_guided(
					    prompt='''first prompt''',					image=UpperCamelCase_,					text_to_image_strength=0.75,					generator=UpperCamelCase_,					guidance_scale=7.5,					num_inference_steps=2,					output_type='''numpy''',					).images

					assert np.abs(image - new_image      ).sum() < 1e-5, "Models don't have the same forward pass"






	def 				lowercase__  (self : int      )     ->			int:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						str      					= VersatileDiffusionPipeline.from_pretrained('''shi-labs/versatile-diffusion''',					torch_dtype=torch.floataa      )
					pipe.to(UpperCamelCase_      )
					pipe.set_progress_bar_config(disable=UpperCamelCase_      )

					SCREAMING_SNAKE_CASE  :						int      					= "cyberpunk 2077"
					SCREAMING_SNAKE_CASE  :						Tuple      					= load_image(
					    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg'''      )
					SCREAMING_SNAKE_CASE  :						List[str]      					= torch.manual_seed(0      )
					SCREAMING_SNAKE_CASE  :						Any      					= pipe.dual_guided(
					    prompt=UpperCamelCase_,					image=UpperCamelCase_,					text_to_image_strength=0.75,					generator=UpperCamelCase_,					guidance_scale=7.5,					num_inference_steps=50,					output_type='''numpy''',					).images

					SCREAMING_SNAKE_CASE  :						Union[str, Any]      					= image[0, 253:256, 253:256, -1]

					assert image.shape == (1, 512, 512, 3)
					SCREAMING_SNAKE_CASE  :						List[Any]      					= np.array([0.1448, 0.1619, 0.1741, 0.1086, 0.1147, 0.1128, 0.1199, 0.1165, 0.1001]      )

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

					SCREAMING_SNAKE_CASE  :						List[str]      					= "A painting of a squirrel eating a burger "
					SCREAMING_SNAKE_CASE  :						int      					= torch.manual_seed(0      )
					SCREAMING_SNAKE_CASE  :						Any      					= pipe.text_to_image(
					    prompt=UpperCamelCase_,					generator=UpperCamelCase_,					guidance_scale=7.5,					num_inference_steps=50,					output_type='''numpy'''      ).images

					SCREAMING_SNAKE_CASE  :						List[str]      					= image[0, 253:256, 253:256, -1]

					assert image.shape == (1, 512, 512, 3)
					SCREAMING_SNAKE_CASE  :						Optional[Any]      					= np.array([0.3367, 0.3169, 0.2656, 0.3870, 0.4790, 0.3796, 0.4009, 0.4878, 0.4778]      )

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

					SCREAMING_SNAKE_CASE  :						Optional[int]      					= pipe.image_variation(UpperCamelCase_,					generator=UpperCamelCase_,					output_type='''numpy'''      ).images

					SCREAMING_SNAKE_CASE  :						Union[str, Any]      					= image[0, 253:256, 253:256, -1]

					assert image.shape == (1, 512, 512, 3)
					SCREAMING_SNAKE_CASE  :						Optional[Any]      					= np.array([0.3076, 0.3123, 0.3284, 0.3782, 0.3770, 0.3894, 0.4297, 0.4331, 0.4456]      )

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









'''simple docstring'''

import unittest

from transformers.testing_utils import CaptureStdout
from transformers.tools.python_interpreter import evaluate






def       __lowercase  (_SCREAMING_SNAKE_CASE      :List[Any]       ):
				return x + 2




class 	a__     (				unittest.TestCase	):


	def 				lowercase__  (self : List[str]      )     ->			int:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						Union[str, Any]      					= '''x = 3'''
					SCREAMING_SNAKE_CASE  :						Dict      					= {}
					SCREAMING_SNAKE_CASE  :						Any      					= evaluate(__UpperCAmelCase,					{},					state=__UpperCAmelCase      )
					assert result == 3
					self.assertDictEqual(__UpperCAmelCase,					{'''x''': 3}      )

					SCREAMING_SNAKE_CASE  :						str      					= '''x = y'''
					SCREAMING_SNAKE_CASE  :						int      					= {'''y''': 5}
					SCREAMING_SNAKE_CASE  :						Optional[int]      					= evaluate(__UpperCAmelCase,					{},					state=__UpperCAmelCase      )
					# evaluate returns the value of the last assignment.
					assert result == 5
					self.assertDictEqual(__UpperCAmelCase,					{'''x''': 5, '''y''': 5}      )


	def 				lowercase__  (self : Optional[int]      )     ->			str:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						int      					= '''y = add_two(x)'''
					SCREAMING_SNAKE_CASE  :						Optional[Any]      					= {'''x''': 3}
					SCREAMING_SNAKE_CASE  :						str      					= evaluate(__UpperCAmelCase,					{'''add_two''': add_two},					state=__UpperCAmelCase      )
					assert result == 5
					self.assertDictEqual(__UpperCAmelCase,					{'''x''': 3, '''y''': 5}      )

					# Won't work without the tool
					with CaptureStdout() as out:
									SCREAMING_SNAKE_CASE  :						Tuple      					= evaluate(__UpperCAmelCase,					{},					state=__UpperCAmelCase      )
					assert result is None
					assert "tried to execute add_two" in out.out


	def 				lowercase__  (self : Any      )     ->			Union[str, Any]:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						Any      					= '''x = 3'''
					SCREAMING_SNAKE_CASE  :						Any      					= {}
					SCREAMING_SNAKE_CASE  :						str      					= evaluate(__UpperCAmelCase,					{},					state=__UpperCAmelCase      )
					assert result == 3
					self.assertDictEqual(__UpperCAmelCase,					{'''x''': 3}      )


	def 				lowercase__  (self : Optional[int]      )     ->			Optional[Any]:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						Optional[int]      					= '''test_dict = {\'x\': x, \'y\': add_two(x)}'''
					SCREAMING_SNAKE_CASE  :						Tuple      					= {'''x''': 3}
					SCREAMING_SNAKE_CASE  :						List[Any]      					= evaluate(__UpperCAmelCase,					{'''add_two''': add_two},					state=__UpperCAmelCase      )
					self.assertDictEqual(__UpperCAmelCase,					{'''x''': 3, '''y''': 5}      )
					self.assertDictEqual(__UpperCAmelCase,					{'''x''': 3, '''test_dict''': {'''x''': 3, '''y''': 5}}      )


	def 				lowercase__  (self : Optional[Any]      )     ->			List[str]:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						Union[str, Any]      					= '''x = 3\ny = 5'''
					SCREAMING_SNAKE_CASE  :						Tuple      					= {}
					SCREAMING_SNAKE_CASE  :						Optional[Any]      					= evaluate(__UpperCAmelCase,					{},					state=__UpperCAmelCase      )
					# evaluate returns the value of the last assignment.
					assert result == 5
					self.assertDictEqual(__UpperCAmelCase,					{'''x''': 3, '''y''': 5}      )


	def 				lowercase__  (self : Tuple      )     ->			Union[str, Any]:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						Any      					= '''text = f\'This is x: {x}.\''''
					SCREAMING_SNAKE_CASE  :						List[str]      					= {'''x''': 3}
					SCREAMING_SNAKE_CASE  :						Dict      					= evaluate(__UpperCAmelCase,					{},					state=__UpperCAmelCase      )
					# evaluate returns the value of the last assignment.
					assert result == "This is x: 3."
					self.assertDictEqual(__UpperCAmelCase,					{'''x''': 3, '''text''': '''This is x: 3.'''}      )


	def 				lowercase__  (self : int      )     ->			str:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						Optional[int]      					= '''if x <= 3:\n    y = 2\nelse:\n    y = 5'''
					SCREAMING_SNAKE_CASE  :						int      					= {'''x''': 3}
					SCREAMING_SNAKE_CASE  :						List[str]      					= evaluate(__UpperCAmelCase,					{},					state=__UpperCAmelCase      )
					# evaluate returns the value of the last assignment.
					assert result == 2
					self.assertDictEqual(__UpperCAmelCase,					{'''x''': 3, '''y''': 2}      )

					SCREAMING_SNAKE_CASE  :						Any      					= {'''x''': 8}
					SCREAMING_SNAKE_CASE  :						int      					= evaluate(__UpperCAmelCase,					{},					state=__UpperCAmelCase      )
					# evaluate returns the value of the last assignment.
					assert result == 5
					self.assertDictEqual(__UpperCAmelCase,					{'''x''': 8, '''y''': 5}      )


	def 				lowercase__  (self : Any      )     ->			int:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						Any      					= '''test_list = [x, add_two(x)]'''
					SCREAMING_SNAKE_CASE  :						List[str]      					= {'''x''': 3}
					SCREAMING_SNAKE_CASE  :						Tuple      					= evaluate(__UpperCAmelCase,					{'''add_two''': add_two},					state=__UpperCAmelCase      )
					self.assertListEqual(__UpperCAmelCase,					[3, 5]      )
					self.assertDictEqual(__UpperCAmelCase,					{'''x''': 3, '''test_list''': [3, 5]}      )


	def 				lowercase__  (self : int      )     ->			List[str]:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						Tuple      					= '''y = x'''
					SCREAMING_SNAKE_CASE  :						Tuple      					= {'''x''': 3}
					SCREAMING_SNAKE_CASE  :						str      					= evaluate(__UpperCAmelCase,					{},					state=__UpperCAmelCase      )
					assert result == 3
					self.assertDictEqual(__UpperCAmelCase,					{'''x''': 3, '''y''': 3}      )


	def 				lowercase__  (self : Optional[Any]      )     ->			Union[str, Any]:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						Dict      					= '''test_list = [x, add_two(x)]\ntest_list[1]'''
					SCREAMING_SNAKE_CASE  :						int      					= {'''x''': 3}
					SCREAMING_SNAKE_CASE  :						Optional[int]      					= evaluate(__UpperCAmelCase,					{'''add_two''': add_two},					state=__UpperCAmelCase      )
					assert result == 5
					self.assertDictEqual(__UpperCAmelCase,					{'''x''': 3, '''test_list''': [3, 5]}      )

					SCREAMING_SNAKE_CASE  :						Dict      					= '''test_dict = {\'x\': x, \'y\': add_two(x)}\ntest_dict[\'y\']'''
					SCREAMING_SNAKE_CASE  :						Tuple      					= {'''x''': 3}
					SCREAMING_SNAKE_CASE  :						Optional[Any]      					= evaluate(__UpperCAmelCase,					{'''add_two''': add_two},					state=__UpperCAmelCase      )
					assert result == 5
					self.assertDictEqual(__UpperCAmelCase,					{'''x''': 3, '''test_dict''': {'''x''': 3, '''y''': 5}}      )






	def 				lowercase__  (self : Optional[Any]      )     ->			List[str]:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						Any      					= '''x = 0\nfor i in range(3):\n    x = i'''
					SCREAMING_SNAKE_CASE  :						List[Any]      					= {}
					SCREAMING_SNAKE_CASE  :						List[str]      					= evaluate(__UpperCAmelCase,					{'''range''': range},					state=__UpperCAmelCase      )
					assert result == 2
					self.assertDictEqual(__UpperCAmelCase,					{'''x''': 2, '''i''': 2}      )




