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







				def 						UpperCAmelCase_    (       self     )->		List[Any]:







											'''simple docstring'''






											with tempfile.TemporaryDirectory() as tmpdirname:
																		# pipeline has Flax weights
																		UpperCamelCase   			=     FlaxDiffusionPipeline.from_pretrained(
																		    'hf-internal-testing/tiny-stable-diffusion-pipe'						,				safety_checker=A_						,				cache_dir=A_     )

																		UpperCamelCase   			=     [t[-1] for t in os.walk(os.path.join(A_						,				os.listdir(A_     )[0]						,				'snapshots'     )     )]
																		UpperCamelCase   			=     [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 			SCREAMING_SNAKE_CASE__      (     unittest.TestCase):







				def 						UpperCAmelCase_    (       self     )->		int:







											'''simple docstring'''






											UpperCamelCase			,	UpperCamelCase   			=     FlaxStableDiffusionPipeline.from_pretrained(
											    'hf-internal-testing/tiny-stable-diffusion-pipe'						,				safety_checker=A_     )

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

											UpperCamelCase   			=     jax.random.PRNGKey(0     )
											UpperCamelCase   			=     4

											UpperCamelCase   			=     jax.device_count()
											UpperCamelCase   			=     num_samples * [prompt]
											UpperCamelCase   			=     pipeline.prepare_inputs(A_     )

											# shard inputs and rng
											UpperCamelCase   			=     replicate(A_     )
											UpperCamelCase   			=     jax.random.split(A_						,				A_     )
											UpperCamelCase   			=     shard(A_     )

											UpperCamelCase   			=     pipeline(A_						,				A_						,				A_						,				A_						,				jit=A_     ).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.1_514_745     ) < 1e-3
																		assert np.abs(np.abs(A_						,				dtype=np.floataa     ).sum() - 49_947.875     ) < 5e-1

											UpperCamelCase   			=     pipeline.numpy_to_pil(np.asarray(images.reshape((num_samples,) + images.shape[-3:]     )     )     )
											assert len(A_     ) == num_samples







				def 						UpperCAmelCase_    (       self     )->		Optional[Any]:







											'''simple docstring'''






											UpperCamelCase			,	UpperCamelCase   			=     FlaxStableDiffusionPipeline.from_pretrained(
											    'CompVis/stable-diffusion-v1-4'						,				revision='flax'						,				safety_checker=A_     )

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

											UpperCamelCase   			=     jax.random.PRNGKey(0     )
											UpperCamelCase   			=     50

											UpperCamelCase   			=     jax.device_count()
											UpperCamelCase   			=     num_samples * [prompt]
											UpperCamelCase   			=     pipeline.prepare_inputs(A_     )

											# shard inputs and rng
											UpperCamelCase   			=     replicate(A_     )
											UpperCamelCase   			=     jax.random.split(A_						,				A_     )
											UpperCamelCase   			=     shard(A_     )

											UpperCamelCase   			=     pipeline(A_						,				A_						,				A_						,				A_						,				jit=A_     ).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.05_652_401)     ) < 1e-3
																		assert np.abs((np.abs(A_						,				dtype=np.floataa     ).sum() - 2_383_808.2)     ) < 5e-1







				def 						UpperCAmelCase_    (       self     )->		Any:







											'''simple docstring'''






											UpperCamelCase			,	UpperCamelCase   			=     FlaxStableDiffusionPipeline.from_pretrained(
											    'CompVis/stable-diffusion-v1-4'						,				revision='bf16'						,				dtype=jnp.bfloataa						,				safety_checker=A_     )

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

											UpperCamelCase   			=     jax.random.PRNGKey(0     )
											UpperCamelCase   			=     50

											UpperCamelCase   			=     jax.device_count()
											UpperCamelCase   			=     num_samples * [prompt]
											UpperCamelCase   			=     pipeline.prepare_inputs(A_     )

											# shard inputs and rng
											UpperCamelCase   			=     replicate(A_     )
											UpperCamelCase   			=     jax.random.split(A_						,				A_     )
											UpperCamelCase   			=     shard(A_     )

											UpperCamelCase   			=     pipeline(A_						,				A_						,				A_						,				A_						,				jit=A_     ).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.04_003_906)     ) < 1e-3
																		assert np.abs((np.abs(A_						,				dtype=np.floataa     ).sum() - 2_373_516.75)     ) < 5e-1







				def 						UpperCAmelCase_    (       self     )->		Optional[Any]:







											'''simple docstring'''






											UpperCamelCase			,	UpperCamelCase   			=     FlaxStableDiffusionPipeline.from_pretrained(
											    'CompVis/stable-diffusion-v1-4'						,				revision='bf16'						,				dtype=jnp.bfloataa     )

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

											UpperCamelCase   			=     jax.random.PRNGKey(0     )
											UpperCamelCase   			=     50

											UpperCamelCase   			=     jax.device_count()
											UpperCamelCase   			=     num_samples * [prompt]
											UpperCamelCase   			=     pipeline.prepare_inputs(A_     )

											# shard inputs and rng
											UpperCamelCase   			=     replicate(A_     )
											UpperCamelCase   			=     jax.random.split(A_						,				A_     )
											UpperCamelCase   			=     shard(A_     )

											UpperCamelCase   			=     pipeline(A_						,				A_						,				A_						,				A_						,				jit=A_     ).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.04_003_906)     ) < 1e-3
																		assert np.abs((np.abs(A_						,				dtype=np.floataa     ).sum() - 2_373_516.75)     ) < 5e-1







				def 						UpperCAmelCase_    (       self     )->		str:







											'''simple docstring'''






											UpperCamelCase   			=     FlaxDDIMScheduler(
											    beta_start=0.00_085						,				beta_end=0.012						,				beta_schedule='scaled_linear'						,				set_alpha_to_one=A_						,				steps_offset=1						,				)

											UpperCamelCase			,	UpperCamelCase   			=     FlaxStableDiffusionPipeline.from_pretrained(
											    'CompVis/stable-diffusion-v1-4'						,				revision='bf16'						,				dtype=jnp.bfloataa						,				scheduler=A_						,				safety_checker=A_						,				)
											UpperCamelCase   			=     scheduler.create_state()

											UpperCamelCase   			=     scheduler_state

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

											UpperCamelCase   			=     jax.random.PRNGKey(0     )
											UpperCamelCase   			=     50

											UpperCamelCase   			=     jax.device_count()
											UpperCamelCase   			=     num_samples * [prompt]
											UpperCamelCase   			=     pipeline.prepare_inputs(A_     )

											# shard inputs and rng
											UpperCamelCase   			=     replicate(A_     )
											UpperCamelCase   			=     jax.random.split(A_						,				A_     )
											UpperCamelCase   			=     shard(A_     )

											UpperCamelCase   			=     pipeline(A_						,				A_						,				A_						,				A_						,				jit=A_     ).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.045_043_945)     ) < 1e-3
																		assert np.abs((np.abs(A_						,				dtype=np.floataa     ).sum() - 2_347_693.5)     ) < 5e-1







				def 						UpperCAmelCase_    (       self     )->		str:







											'''simple docstring'''






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

											UpperCamelCase   			=     jax.device_count()
											UpperCamelCase   			=     num_samples * [prompt]
											UpperCamelCase   			=     jax.random.split(jax.random.PRNGKey(0     )						,				A_     )

											UpperCamelCase			,	UpperCamelCase   			=     FlaxStableDiffusionPipeline.from_pretrained(
											    'CompVis/stable-diffusion-v1-4'						,				revision='bf16'						,				dtype=jnp.bfloataa						,				safety_checker=A_						,				)

											UpperCamelCase   			=     replicate(A_     )
											UpperCamelCase   			=     pipeline.prepare_inputs(A_     )
											UpperCamelCase   			=     shard(A_     )
											UpperCamelCase   			=     pipeline(A_						,				A_						,				A_						,				jit=A_     ).images
											assert images.shape == (num_samples, 1, 512, 512, 3)
											UpperCamelCase   			=     images[2, 0, 256, 10:17, 1]

											# With memory efficient attention
											UpperCamelCase			,	UpperCamelCase   			=     FlaxStableDiffusionPipeline.from_pretrained(
											    'CompVis/stable-diffusion-v1-4'						,				revision='bf16'						,				dtype=jnp.bfloataa						,				safety_checker=A_						,				use_memory_efficient_attention=A_						,				)

											UpperCamelCase   			=     replicate(A_     )
											UpperCamelCase   			=     pipeline.prepare_inputs(A_     )
											UpperCamelCase   			=     shard(A_     )
											UpperCamelCase   			=     pipeline(A_						,				A_						,				A_						,				jit=A_     ).images
											assert images_eff.shape == (num_samples, 1, 512, 512, 3)
											UpperCamelCase   			=     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






'''simple docstring'''
import sys
import webbrowser

import requests
from bsa import BeautifulSoup
from fake_useragent import UserAgent

if __name__ == "__main__":
						print('Googling.....')
						lowerCAmelCase					:    List[Any]           = 'https://www.google.com/search?q=' + ' '.join(sys.argv[1:])
						lowerCAmelCase					:    List[Any]           = requests.get(url, headers={'UserAgent': UserAgent().random})
						# res.raise_for_status()
						with open('project1a.html', 'wb') as out_file:  # only for knowing the class
												for data in res.iter_content(1_00_00):
																		out_file.write(data)
						lowerCAmelCase					:    Tuple           = BeautifulSoup(res.text, 'html.parser')
						lowerCAmelCase					:    List[Any]           = list(soup.select('.eZt8xd'))[:5]

						print(len(links))
						for link in links:
												if link.text == "Maps":
																		webbrowser.open(link.get('href'))
												else:
																		webbrowser.open(f"""https://google.com{link.get('href')}""")




