






import gc
import tempfile
import unittest

import numpy as np
import torch

from diffusers import VersatileDiffusionTextToImagePipeline
from diffusers.utils.testing_utils import nightly, require_torch_gpu, torch_device


lowerCamelCase__							=     False




class 			snake_case__   (							unittest.TestCase):

       '''simple docstring'''
       pass




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

       '''simple docstring'''



       def 					__lowercase						(							self    ) ->      str:






            '''simple docstring'''





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



       def 					__lowercase						(							self    ) ->      Any:






            '''simple docstring'''





            __snake_case			:Union[str, Any]          =						VersatileDiffusionTextToImagePipeline.from_pretrained("""shi-labs/versatile-diffusion"""    )
            # remove text_unet
            pipe.remove_unused_weights()
            pipe.to(SCREAMING_SNAKE_CASE_    )
            pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_    )

            __snake_case			:Any          =						"""A painting of a squirrel eating a burger """
            __snake_case			:Union[str, Any]          =						torch.manual_seed(0    )
            __snake_case			:int          =						pipe(
                prompt=SCREAMING_SNAKE_CASE_			,						generator=SCREAMING_SNAKE_CASE_			,						guidance_scale=7.5			,						num_inference_steps=2			,						output_type="""numpy"""    ).images

            with tempfile.TemporaryDirectory() as tmpdirname:
                 pipe.save_pretrained(SCREAMING_SNAKE_CASE_    )
                 __snake_case			:Optional[Any]          =						VersatileDiffusionTextToImagePipeline.from_pretrained(SCREAMING_SNAKE_CASE_    )
            pipe.to(SCREAMING_SNAKE_CASE_    )
            pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_    )

            __snake_case			:Union[str, Any]          =						generator.manual_seed(0    )
            __snake_case			:Optional[Any]          =						pipe(
                prompt=SCREAMING_SNAKE_CASE_			,						generator=SCREAMING_SNAKE_CASE_			,						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    ) ->      Optional[int]:






            '''simple docstring'''





            __snake_case			:str          =						VersatileDiffusionTextToImagePipeline.from_pretrained(
                """shi-labs/versatile-diffusion"""			,						torch_dtype=torch.floataa    )
            pipe.to(SCREAMING_SNAKE_CASE_    )
            pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_    )

            __snake_case			:int          =						"""A painting of a squirrel eating a burger """
            __snake_case			:Dict          =						torch.manual_seed(0    )
            __snake_case			:Tuple          =						pipe(
                prompt=SCREAMING_SNAKE_CASE_			,						generator=SCREAMING_SNAKE_CASE_			,						guidance_scale=7.5			,						num_inference_steps=50			,						output_type="""numpy"""    ).images

            __snake_case			:str          =						image[0, 2_53:2_56, 2_53:2_56, -1]

            assert image.shape == (1, 5_12, 5_12, 3)
            __snake_case			:Any          =						np.array([0.33_67, 0.31_69, 0.26_56, 0.38_70, 0.47_90, 0.37_96, 0.40_09, 0.48_78, 0.47_78]    )

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










import unittest

from transformers import load_tool

from .test_tools_common import ToolTesterMixin


lowerCamelCase__							=     """
Hugging Face was founded in 2016 by French entrepreneurs Clément Delangue, Julien Chaumond, and Thomas Wolf originally as a company that developed a chatbot app targeted at teenagers.[2] After open-sourcing the model behind the chatbot, the company pivoted to focus on being a platform for machine learning.

In March 2021, Hugging Face raised $40 million in a Series B funding round.[3]

On April 28, 2021, the company launched the BigScience Research Workshop in collaboration with several other research groups to release an open large language model.[4] In 2022, the workshop concluded with the announcement of BLOOM, a multilingual large language model with 176 billion parameters.[5]
"""




class 			snake_case__   (							unittest.TestCase	,    lowercase_):

       '''simple docstring'''



       def 					__lowercase						(							self    ) ->      List[str]:






            '''simple docstring'''





            __snake_case			:int          =						load_tool("""text-question-answering"""    )
            self.tool.setup()
            __snake_case			:Tuple          =						load_tool("""text-question-answering"""			,						remote=a__    )



       def 					__lowercase						(							self    ) ->      Any:






            '''simple docstring'''





            __snake_case			:List[str]          =						self.tool(a__			,						"""What did Hugging Face do in April 2021?"""    )
            self.assertEqual(a__			,						"""launched the BigScience Research Workshop"""    )



       def 					__lowercase						(							self    ) ->      Any:






            '''simple docstring'''





            __snake_case			:List[str]          =						self.remote_tool(a__			,						"""What did Hugging Face do in April 2021?"""    )
            self.assertEqual(a__			,						"""launched the BigScience Research Workshop"""    )



       def 					__lowercase						(							self    ) ->      Dict:






            '''simple docstring'''





            __snake_case			:Optional[int]          =						self.tool(text=a__			,						question="""What did Hugging Face do in April 2021?"""    )
            self.assertEqual(a__			,						"""launched the BigScience Research Workshop"""    )



       def 					__lowercase						(							self    ) ->      Tuple:






            '''simple docstring'''





            __snake_case			:List[str]          =						self.remote_tool(text=a__			,						question="""What did Hugging Face do in April 2021?"""    )
            self.assertEqual(a__			,						"""launched the BigScience Research Workshop"""    )


