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






    '''simple docstring'''






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

            __UpperCamelCase  :List[str]    						=	[t[-1] for t in os.walk(os.path.join(__lowercase				,				os.listdir(__lowercase)[0]				,				'''snapshots'''))]
            __UpperCamelCase  :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 			lowerCamelCase_  (     unittest.TestCase				):






    '''simple docstring'''






    def     UpperCamelCase__     (			self) ->			List[Any]:
        __UpperCamelCase  :Optional[int]    						=	FlaxStableDiffusionPipeline.from_pretrained(
            '''hf-internal-testing/tiny-stable-diffusion-pipe'''				,				safety_checker=__lowercase)

        __UpperCamelCase  :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'''
        )

        __UpperCamelCase  :str    						=	jax.random.PRNGKey(0)
        __UpperCamelCase  :Optional[int]    						=	4

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

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

        __UpperCamelCase  :Dict    						=	pipeline(__lowercase				,				__lowercase				,				__lowercase				,				__lowercase				,				jit=__lowercase).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_51_47_45) < 1E-3
            assert np.abs(np.abs(__lowercase				,				dtype=np.floataa).sum() - 49_947.875) < 5E-1

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







    def     UpperCamelCase__     (			self) ->			str:
        __UpperCamelCase  :List[Any]    						=	FlaxStableDiffusionPipeline.from_pretrained(
            '''CompVis/stable-diffusion-v1-4'''				,				revision='''flax'''				,				safety_checker=__lowercase)

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

        __UpperCamelCase  :str    						=	jax.random.PRNGKey(0)
        __UpperCamelCase  :Any    						=	50

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

        # shard inputs and rng
        __UpperCamelCase  :Any    						=	replicate(__lowercase)
        __UpperCamelCase  :Dict    						=	jax.random.split(__lowercase				,				__lowercase)
        __UpperCamelCase  :str    						=	shard(__lowercase)

        __UpperCamelCase  :str    						=	pipeline(__lowercase				,				__lowercase				,				__lowercase				,				__lowercase				,				jit=__lowercase).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_65_24_01)) < 1E-3
            assert np.abs((np.abs(__lowercase				,				dtype=np.floataa).sum() - 2_383_808.2)) < 5E-1







    def     UpperCamelCase__     (			self) ->			List[Any]:
        __UpperCamelCase  :Union[str, Any]    						=	FlaxStableDiffusionPipeline.from_pretrained(
            '''CompVis/stable-diffusion-v1-4'''				,				revision='''bf16'''				,				dtype=jnp.bfloataa				,				safety_checker=__lowercase)

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

        __UpperCamelCase  :Optional[int]    						=	jax.random.PRNGKey(0)
        __UpperCamelCase  :List[str]    						=	50

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

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

        __UpperCamelCase  :Dict    						=	pipeline(__lowercase				,				__lowercase				,				__lowercase				,				__lowercase				,				jit=__lowercase).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_00_39_06)) < 1E-3
            assert np.abs((np.abs(__lowercase				,				dtype=np.floataa).sum() - 2_373_516.75)) < 5E-1







    def     UpperCamelCase__     (			self) ->			Tuple:
        __UpperCamelCase  :Union[str, Any]    						=	FlaxStableDiffusionPipeline.from_pretrained(
            '''CompVis/stable-diffusion-v1-4'''				,				revision='''bf16'''				,				dtype=jnp.bfloataa)

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

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

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

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

        __UpperCamelCase  :Optional[int]    						=	pipeline(__lowercase				,				__lowercase				,				__lowercase				,				__lowercase				,				jit=__lowercase).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_00_39_06)) < 1E-3
            assert np.abs((np.abs(__lowercase				,				dtype=np.floataa).sum() - 2_373_516.75)) < 5E-1







    def     UpperCamelCase__     (			self) ->			str:
        __UpperCamelCase  :Dict    						=	FlaxDDIMScheduler(
            beta_start=0.0_00_85				,				beta_end=0.0_12				,				beta_schedule='''scaled_linear'''				,				set_alpha_to_one=__lowercase				,				steps_offset=1				,				)

        __UpperCamelCase  :List[str]    						=	FlaxStableDiffusionPipeline.from_pretrained(
            '''CompVis/stable-diffusion-v1-4'''				,				revision='''bf16'''				,				dtype=jnp.bfloataa				,				scheduler=__lowercase				,				safety_checker=__lowercase				,				)
        __UpperCamelCase  :List[Any]    						=	scheduler.create_state()

        __UpperCamelCase  :int    						=	scheduler_state

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

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

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

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

        __UpperCamelCase  :List[Any]    						=	pipeline(__lowercase				,				__lowercase				,				__lowercase				,				__lowercase				,				jit=__lowercase).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_45_04_39_45)) < 1E-3
            assert np.abs((np.abs(__lowercase				,				dtype=np.floataa).sum() - 2_347_693.5)) < 5E-1







    def     UpperCamelCase__     (			self) ->			Optional[int]:
        __UpperCamelCase  :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'''
        )

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

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

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

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

        __UpperCamelCase  :List[str]    						=	replicate(__lowercase)
        __UpperCamelCase  :Union[str, Any]    						=	pipeline.prepare_inputs(__lowercase)
        __UpperCamelCase  :List[Any]    						=	shard(__lowercase)
        __UpperCamelCase  :Union[str, Any]    						=	pipeline(__lowercase				,				__lowercase				,				__lowercase				,				jit=__lowercase).images
        assert images_eff.shape == (num_samples, 1, 512, 512, 3)
        __UpperCamelCase  :Optional[int]    						=	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






import requests
from bsa import BeautifulSoup



def lowerCamelCase						(     SCREAMING_SNAKE_CASE						,    SCREAMING_SNAKE_CASE			):
    '''simple docstring'''







    __UpperCamelCase  :List[Any]    						=	BeautifulSoup(requests.get(SCREAMING_SNAKE_CASE						,    params=SCREAMING_SNAKE_CASE			).content						,    '''html.parser'''			)
    __UpperCamelCase  :int    						=	soup.find('''div'''						,    attrs={'''class''': '''gs_ri'''}			)
    __UpperCamelCase  :Union[str, Any]    						=	div.find('''div'''						,    attrs={'''class''': '''gs_fl'''}			).find_all('''a'''			)
    return anchors[2].get_text()


if __name__ == "__main__":
    __lowercase											=      {
        '''title''': (
            '''Precisely geometry controlled microsupercapacitors for ultrahigh areal '''
            '''capacitance, volumetric capacitance, and energy density'''
        ),
        '''journal''': '''Chem. Mater.''',
        '''volume''': 30,
        '''pages''': '''3979-3990''',
        '''year''': 2018,
        '''hl''': '''en''',
    }
    print(get_citation('''https://scholar.google.com/scholar_lookup''', params=params))





