



"""simple docstring"""







import os
import tempfile
import unittest

import numpy as np

from diffusers.utils import is_flax_available
from diffusers.utils.testing_utils import require_flax, slow


if is_flax_available():
							import jax
							import jax.numpy as jnp
							from flax.jax_utils import replicate
							from flax.training.common_utils import shard

							from diffusers import FlaxDDIMScheduler, FlaxDiffusionPipeline, FlaxStableDiffusionPipeline
@require_flax
class 			__A			(      unittest.TestCase       ):







		'''simple docstring'''





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






				"""simple docstring"""



				with tempfile.TemporaryDirectory() as tmpdirname:
						# pipeline has Flax weights
						lowercase__  :       List[Any]												=     FlaxDiffusionPipeline.from_pretrained(
						    '''hf-internal-testing/tiny-stable-diffusion-pipe'''     ,safety_checker=__UpperCamelCase     ,cache_dir=__UpperCamelCase		)

						lowercase__  :       List[Any]												=     [t[-1] for t in os.walk(os.path.join(__UpperCamelCase     ,os.listdir(__UpperCamelCase		)[0]     ,'''snapshots'''		)		)]
						lowercase__  :       Tuple												=     [item for sublist in all_root_files for item in sublist]

						# None of the downloaded files should be a PyTorch file even if we have some here:
						# https://huggingface.co/hf-internal-testing/tiny-stable-diffusion-pipe/blob/main/unet/diffusion_pytorch_model.bin
						assert not any(f.endswith('''.bin'''		) for f in files		)





@slow
@require_flax
class 			__A			(      unittest.TestCase       ):







		'''simple docstring'''





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






				"""simple docstring"""



				lowercase__  :       Tuple												=     FlaxStableDiffusionPipeline.from_pretrained(
				    '''hf-internal-testing/tiny-stable-diffusion-pipe'''     ,safety_checker=__UpperCamelCase		)

				lowercase__  :       str												=     (
				    """A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of"""
				    """ field, close up, split lighting, cinematic"""
				)

				lowercase__  :       str												=     jax.random.PRNGKey(0		)
				lowercase__  :       int												=     4

				lowercase__  :       Union[str, Any]												=     jax.device_count()
				lowercase__  :       int												=     num_samples * [prompt]
				lowercase__  :       Any												=     pipeline.prepare_inputs(__UpperCamelCase		)

				# shard inputs and rng
				lowercase__  :       str												=     replicate(__UpperCamelCase		)
				lowercase__  :       str												=     jax.random.split(__UpperCamelCase     ,__UpperCamelCase		)
				lowercase__  :       List[str]												=     shard(__UpperCamelCase		)

				lowercase__  :       Tuple												=     pipeline(__UpperCamelCase     ,__UpperCamelCase     ,__UpperCamelCase     ,__UpperCamelCase     ,jit=__UpperCamelCase		).images

				assert images.shape == (num_samples, 1, 64, 64, 3)
				if jax.device_count() == 8:
						assert np.abs(np.abs(images[0, 0, :2, :2, -2:]     ,dtype=np.floataa		).sum() - 4.151_4745		) < 1e-3
						assert np.abs(np.abs(__UpperCamelCase     ,dtype=np.floataa		).sum() - 49_947.875		) < 5e-1

				lowercase__  :       Union[str, Any]												=     pipeline.numpy_to_pil(np.asarray(images.reshape((num_samples,) + images.shape[-3:]		)		)		)
				assert len(__UpperCamelCase		) == num_samples







		def    UpperCAmelCase       (      self		:    Tuple		)   ->						int:






				"""simple docstring"""



				lowercase__  :       Optional[Any]												=     FlaxStableDiffusionPipeline.from_pretrained(
				    '''CompVis/stable-diffusion-v1-4'''     ,revision='''flax'''     ,safety_checker=__UpperCamelCase		)

				lowercase__  :       str												=     (
				    """A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of"""
				    """ field, close up, split lighting, cinematic"""
				)

				lowercase__  :       Optional[Any]												=     jax.random.PRNGKey(0		)
				lowercase__  :       List[str]												=     50

				lowercase__  :       Tuple												=     jax.device_count()
				lowercase__  :       Optional[Any]												=     num_samples * [prompt]
				lowercase__  :       Tuple												=     pipeline.prepare_inputs(__UpperCamelCase		)

				# shard inputs and rng
				lowercase__  :       Tuple												=     replicate(__UpperCamelCase		)
				lowercase__  :       Optional[int]												=     jax.random.split(__UpperCamelCase     ,__UpperCamelCase		)
				lowercase__  :       Dict												=     shard(__UpperCamelCase		)

				lowercase__  :       int												=     pipeline(__UpperCamelCase     ,__UpperCamelCase     ,__UpperCamelCase     ,__UpperCamelCase     ,jit=__UpperCamelCase		).images

				assert images.shape == (num_samples, 1, 512, 512, 3)
				if jax.device_count() == 8:
						assert np.abs((np.abs(images[0, 0, :2, :2, -2:]     ,dtype=np.floataa		).sum() - 0.0565_2401)		) < 1e-3
						assert np.abs((np.abs(__UpperCamelCase     ,dtype=np.floataa		).sum() - 2_383_808.2)		) < 5e-1







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






				"""simple docstring"""



				lowercase__  :       str												=     FlaxStableDiffusionPipeline.from_pretrained(
				    '''CompVis/stable-diffusion-v1-4'''     ,revision='''bf16'''     ,dtype=jnp.bfloataa     ,safety_checker=__UpperCamelCase		)

				lowercase__  :       List[str]												=     (
				    """A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of"""
				    """ field, close up, split lighting, cinematic"""
				)

				lowercase__  :       Union[str, Any]												=     jax.random.PRNGKey(0		)
				lowercase__  :       Any												=     50

				lowercase__  :       int												=     jax.device_count()
				lowercase__  :       Dict												=     num_samples * [prompt]
				lowercase__  :       Dict												=     pipeline.prepare_inputs(__UpperCamelCase		)

				# shard inputs and rng
				lowercase__  :       List[str]												=     replicate(__UpperCamelCase		)
				lowercase__  :       Optional[int]												=     jax.random.split(__UpperCamelCase     ,__UpperCamelCase		)
				lowercase__  :       str												=     shard(__UpperCamelCase		)

				lowercase__  :       List[str]												=     pipeline(__UpperCamelCase     ,__UpperCamelCase     ,__UpperCamelCase     ,__UpperCamelCase     ,jit=__UpperCamelCase		).images

				assert images.shape == (num_samples, 1, 512, 512, 3)
				if jax.device_count() == 8:
						assert np.abs((np.abs(images[0, 0, :2, :2, -2:]     ,dtype=np.floataa		).sum() - 0.0400_3906)		) < 1e-3
						assert np.abs((np.abs(__UpperCamelCase     ,dtype=np.floataa		).sum() - 2_373_516.75)		) < 5e-1







		def    UpperCAmelCase       (      self		:    str		)   ->						Tuple:






				"""simple docstring"""



				lowercase__  :       List[str]												=     FlaxStableDiffusionPipeline.from_pretrained(
				    '''CompVis/stable-diffusion-v1-4'''     ,revision='''bf16'''     ,dtype=jnp.bfloataa		)

				lowercase__  :       Optional[Any]												=     (
				    """A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of"""
				    """ field, close up, split lighting, cinematic"""
				)

				lowercase__  :       Union[str, Any]												=     jax.random.PRNGKey(0		)
				lowercase__  :       Optional[Any]												=     50

				lowercase__  :       str												=     jax.device_count()
				lowercase__  :       str												=     num_samples * [prompt]
				lowercase__  :       Dict												=     pipeline.prepare_inputs(__UpperCamelCase		)

				# shard inputs and rng
				lowercase__  :       Optional[Any]												=     replicate(__UpperCamelCase		)
				lowercase__  :       Tuple												=     jax.random.split(__UpperCamelCase     ,__UpperCamelCase		)
				lowercase__  :       Union[str, Any]												=     shard(__UpperCamelCase		)

				lowercase__  :       List[Any]												=     pipeline(__UpperCamelCase     ,__UpperCamelCase     ,__UpperCamelCase     ,__UpperCamelCase     ,jit=__UpperCamelCase		).images

				assert images.shape == (num_samples, 1, 512, 512, 3)
				if jax.device_count() == 8:
						assert np.abs((np.abs(images[0, 0, :2, :2, -2:]     ,dtype=np.floataa		).sum() - 0.0400_3906)		) < 1e-3
						assert np.abs((np.abs(__UpperCamelCase     ,dtype=np.floataa		).sum() - 2_373_516.75)		) < 5e-1







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






				"""simple docstring"""



				lowercase__  :       int												=     FlaxDDIMScheduler(
				    beta_start=0.0_0085     ,beta_end=0.012     ,beta_schedule='''scaled_linear'''     ,set_alpha_to_one=__UpperCamelCase     ,steps_offset=1     ,)

				lowercase__  :       Dict												=     FlaxStableDiffusionPipeline.from_pretrained(
				    '''CompVis/stable-diffusion-v1-4'''     ,revision='''bf16'''     ,dtype=jnp.bfloataa     ,scheduler=__UpperCamelCase     ,safety_checker=__UpperCamelCase     ,)
				lowercase__  :       str												=     scheduler.create_state()

				lowercase__  :       Optional[int]												=     scheduler_state

				lowercase__  :       Union[str, Any]												=     (
				    """A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of"""
				    """ field, close up, split lighting, cinematic"""
				)

				lowercase__  :       Union[str, Any]												=     jax.random.PRNGKey(0		)
				lowercase__  :       Optional[Any]												=     50

				lowercase__  :       Any												=     jax.device_count()
				lowercase__  :       int												=     num_samples * [prompt]
				lowercase__  :       Optional[Any]												=     pipeline.prepare_inputs(__UpperCamelCase		)

				# shard inputs and rng
				lowercase__  :       str												=     replicate(__UpperCamelCase		)
				lowercase__  :       Union[str, Any]												=     jax.random.split(__UpperCamelCase     ,__UpperCamelCase		)
				lowercase__  :       Any												=     shard(__UpperCamelCase		)

				lowercase__  :       int												=     pipeline(__UpperCamelCase     ,__UpperCamelCase     ,__UpperCamelCase     ,__UpperCamelCase     ,jit=__UpperCamelCase		).images

				assert images.shape == (num_samples, 1, 512, 512, 3)
				if jax.device_count() == 8:
						assert np.abs((np.abs(images[0, 0, :2, :2, -2:]     ,dtype=np.floataa		).sum() - 0.0_4504_3945)		) < 1e-3
						assert np.abs((np.abs(__UpperCamelCase     ,dtype=np.floataa		).sum() - 2_347_693.5)		) < 5e-1







		def    UpperCAmelCase       (      self		:    Optional[Any]		)   ->						Tuple:






				"""simple docstring"""



				lowercase__  :       Optional[Any]												=     (
				    """A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of"""
				    """ field, close up, split lighting, cinematic"""
				)

				lowercase__  :       Any												=     jax.device_count()
				lowercase__  :       Optional[Any]												=     num_samples * [prompt]
				lowercase__  :       int												=     jax.random.split(jax.random.PRNGKey(0		)     ,__UpperCamelCase		)

				lowercase__  :       str												=     FlaxStableDiffusionPipeline.from_pretrained(
				    '''CompVis/stable-diffusion-v1-4'''     ,revision='''bf16'''     ,dtype=jnp.bfloataa     ,safety_checker=__UpperCamelCase     ,)

				lowercase__  :       Any												=     replicate(__UpperCamelCase		)
				lowercase__  :       List[Any]												=     pipeline.prepare_inputs(__UpperCamelCase		)
				lowercase__  :       Optional[int]												=     shard(__UpperCamelCase		)
				lowercase__  :       Optional[Any]												=     pipeline(__UpperCamelCase     ,__UpperCamelCase     ,__UpperCamelCase     ,jit=__UpperCamelCase		).images
				assert images.shape == (num_samples, 1, 512, 512, 3)
				lowercase__  :       int												=     images[2, 0, 256, 10:17, 1]

				# With memory efficient attention
				lowercase__  :       Union[str, Any]												=     FlaxStableDiffusionPipeline.from_pretrained(
				    '''CompVis/stable-diffusion-v1-4'''     ,revision='''bf16'''     ,dtype=jnp.bfloataa     ,safety_checker=__UpperCamelCase     ,use_memory_efficient_attention=__UpperCamelCase     ,)

				lowercase__  :       Dict												=     replicate(__UpperCamelCase		)
				lowercase__  :       Any												=     pipeline.prepare_inputs(__UpperCamelCase		)
				lowercase__  :       Union[str, Any]												=     shard(__UpperCamelCase		)
				lowercase__  :       Tuple												=     pipeline(__UpperCamelCase     ,__UpperCamelCase     ,__UpperCamelCase     ,jit=__UpperCamelCase		).images
				assert images_eff.shape == (num_samples, 1, 512, 512, 3)
				lowercase__  :       str												=     images[2, 0, 256, 10:17, 1]

				# I checked the results visually and they are very similar. However, I saw that the max diff is `1` and the `sum`
				# over the 8 images is exactly `256`, which is very suspicious. Testing a random slice for now.
				assert abs(slice_eff - slice		).max() < 1e-2

from __future__ import annotations

import json

import requests
from bsa import BeautifulSoup
from fake_useragent import UserAgent

SCREAMING_SNAKE_CASE		:     Union[str, Any]         =	{"UserAgent": UserAgent().random}






def     lowerCAmelCase_			(   _SCREAMING_SNAKE_CASE     :				Dict			):
						UpperCamelCase_		:							Tuple  =		script.contents[0]
						UpperCamelCase_		:							List[Any]  =		json.loads(data[data.find("""{\"config\""""			) : -1]			)
						return info["entry_data"]["ProfilePage"][0]["graphql"]["user"]

class        UpperCamelCase						:





						def __init__(self			,     __UpperCamelCase						)				->				List[str]:
												UpperCamelCase_		:							Union[str, Any]  =		f'''https://www.instagram.com/{username}/'''
												UpperCamelCase_		:							Optional[int]  =		self.get_json()





						def        A_							(self						)				->				dict:
												UpperCamelCase_		:							Optional[Any]  =		requests.get(self.url			,     headers=__UpperCamelCase						).text
												UpperCamelCase_		:							Any  =		BeautifulSoup(__UpperCamelCase			,     """html.parser"""						).find_all("""script"""						)
												try:
																		return extract_user_profile(scripts[4]						)
												except (json.decoder.JSONDecodeError, KeyError):
																		return extract_user_profile(scripts[3]						)





						def __repr__(self						)				->				str:
												return f'''{self.__class__.__name__}(\'{self.username}\')'''





						def __str__(self						)				->				str:
												return f'''{self.fullname} ({self.username}) is {self.biography}'''





						@property
						def        A_							(self						)				->				str:
												return self.user_data["username"]





						@property
						def        A_							(self						)				->				str:
												return self.user_data["full_name"]





						@property
						def        A_							(self						)				->				str:
												return self.user_data["biography"]





						@property
						def        A_							(self						)				->				str:
												return self.user_data["business_email"]





						@property
						def        A_							(self						)				->				str:
												return self.user_data["external_url"]





						@property
						def        A_							(self						)				->				int:
												return self.user_data["edge_followed_by"]["count"]





						@property
						def        A_							(self						)				->				int:
												return self.user_data["edge_follow"]["count"]





						@property
						def        A_							(self						)				->				int:
												return self.user_data["edge_owner_to_timeline_media"]["count"]





						@property
						def        A_							(self						)				->				str:
												return self.user_data["profile_pic_url_hd"]





						@property
						def        A_							(self						)				->				bool:
												return self.user_data["is_verified"]





						@property
						def        A_							(self						)				->				bool:
												return self.user_data["is_private"]






def     lowerCAmelCase_			(   _SCREAMING_SNAKE_CASE     :				str = "github"			):
						import os

						if os.environ.get("""CI"""			):
												return  # test failing on GitHub Actions
						UpperCamelCase_		:							Any  =		InstagramUser(_SCREAMING_SNAKE_CASE			)
						assert instagram_user.user_data
						assert isinstance(instagram_user.user_data		, _SCREAMING_SNAKE_CASE			)
						assert instagram_user.username == username
						if username != "github":
												return
						assert instagram_user.fullname == "GitHub"
						assert instagram_user.biography == "Built for developers."
						assert instagram_user.number_of_posts > 150
						assert instagram_user.number_of_followers > 12_0000
						assert instagram_user.number_of_followings > 15
						assert instagram_user.email == "support@github.com"
						assert instagram_user.website == "https://github.com/readme"
						assert instagram_user.profile_picture_url.startswith("""https://instagram."""			)
						assert instagram_user.is_verified is True
						assert instagram_user.is_private is False


if __name__ == "__main__":
	import doctest

	doctest.testmod()
	SCREAMING_SNAKE_CASE		:     Dict         =	InstagramUser("github")
	print(instagram_user)
	print(F'''{instagram_user.number_of_posts = }''')
	print(F'''{instagram_user.number_of_followers = }''')
	print(F'''{instagram_user.number_of_followings = }''')
	print(F'''{instagram_user.email = }''')
	print(F'''{instagram_user.website = }''')
	print(F'''{instagram_user.profile_picture_url = }''')
	print(F'''{instagram_user.is_verified = }''')
	print(F'''{instagram_user.is_private = }''')






