
"""simple docstring"""




import unittest

from transformers import (
    MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING,
    TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING,
    PreTrainedTokenizer,
    is_vision_available,
)
from transformers.pipelines import ImageClassificationPipeline, pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    require_torch,
    require_torch_or_tf,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
    from PIL import Image
else:






    class 						lowercase:



         @staticmethod
         def  lowercase__     (       *__SCREAMING_SNAKE_CASE    ,							**__SCREAMING_SNAKE_CASE      )		->    str:







             """simple docstring"""



             pass






@is_pipeline_test
@require_torch_or_tf
@require_vision
class 						lowercase(unittest.TestCase					):
     __snake_case:  Tuple  				=    MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING
     __snake_case:  Optional[Any]  				=    TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING



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







         """simple docstring"""



         a__    = ImageClassificationPipeline(model=lowerCAmelCase__    ,							image_processor=lowerCAmelCase__    ,							top_k=2      )
         a__    = [
             Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'      ),
             "http://images.cocodataset.org/val2017/000000039769.jpg",
         ]
         return image_classifier, examples



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







         """simple docstring"""



         a__    = image_classifier('./tests/fixtures/tests_samples/COCO/000000039769.png'      )

         self.assertEqual(
             lowerCAmelCase__    ,							[
                 {'score': ANY(lowerCAmelCase__      ), 'label': ANY(lowerCAmelCase__      )},
                 {'score': ANY(lowerCAmelCase__      ), 'label': ANY(lowerCAmelCase__      )},
             ]    ,							)

         import datasets

         a__    = datasets.load_dataset('hf-internal-testing/fixtures_image_utils'    ,							'image'    ,							split='test'      )

         # Accepts URL + PIL.Image + lists
         a__    = image_classifier(
             [
                 Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'      ),
                 'http://images.cocodataset.org/val2017/000000039769.jpg',
                 # RGBA
                 dataset[0]['file'],
                 # LA
                 dataset[1]['file'],
                 # L
                 dataset[2]['file'],
             ]      )
         self.assertEqual(
             lowerCAmelCase__    ,							[
                 [
                     {'score': ANY(lowerCAmelCase__      ), 'label': ANY(lowerCAmelCase__      )},
                     {'score': ANY(lowerCAmelCase__      ), 'label': ANY(lowerCAmelCase__      )},
                 ],
                 [
                     {'score': ANY(lowerCAmelCase__      ), 'label': ANY(lowerCAmelCase__      )},
                     {'score': ANY(lowerCAmelCase__      ), 'label': ANY(lowerCAmelCase__      )},
                 ],
                 [
                     {'score': ANY(lowerCAmelCase__      ), 'label': ANY(lowerCAmelCase__      )},
                     {'score': ANY(lowerCAmelCase__      ), 'label': ANY(lowerCAmelCase__      )},
                 ],
                 [
                     {'score': ANY(lowerCAmelCase__      ), 'label': ANY(lowerCAmelCase__      )},
                     {'score': ANY(lowerCAmelCase__      ), 'label': ANY(lowerCAmelCase__      )},
                 ],
                 [
                     {'score': ANY(lowerCAmelCase__      ), 'label': ANY(lowerCAmelCase__      )},
                     {'score': ANY(lowerCAmelCase__      ), 'label': ANY(lowerCAmelCase__      )},
                 ],
             ]    ,							)



     @require_torch
     def  lowercase__     (       self      )		->    int:







         """simple docstring"""



         a__    = "hf-internal-testing/tiny-random-vit"
         a__    = pipeline('image-classification'    ,							model=lowerCAmelCase__      )

         a__    = image_classifier('http://images.cocodataset.org/val2017/000000039769.jpg'      )
         self.assertEqual(
             nested_simplify(lowerCAmelCase__    ,							decimals=4      )    ,							[{'label': 'LABEL_1', 'score': 0.5_74}, {'label': 'LABEL_0', 'score': 0.4_26}]    ,							)

         a__    = image_classifier(
             [
                 'http://images.cocodataset.org/val2017/000000039769.jpg',
                 'http://images.cocodataset.org/val2017/000000039769.jpg',
             ]    ,							top_k=2    ,							)
         self.assertEqual(
             nested_simplify(lowerCAmelCase__    ,							decimals=4      )    ,							[
                 [{'label': 'LABEL_1', 'score': 0.5_74}, {'label': 'LABEL_0', 'score': 0.4_26}],
                 [{'label': 'LABEL_1', 'score': 0.5_74}, {'label': 'LABEL_0', 'score': 0.4_26}],
             ]    ,							)



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







         """simple docstring"""



         a__    = "hf-internal-testing/tiny-random-vit"
         a__    = pipeline('image-classification'    ,							model=lowerCAmelCase__    ,							framework='tf'      )

         a__    = image_classifier('http://images.cocodataset.org/val2017/000000039769.jpg'      )
         self.assertEqual(
             nested_simplify(lowerCAmelCase__    ,							decimals=4      )    ,							[{'label': 'LABEL_1', 'score': 0.5_74}, {'label': 'LABEL_0', 'score': 0.4_26}]    ,							)

         a__    = image_classifier(
             [
                 'http://images.cocodataset.org/val2017/000000039769.jpg',
                 'http://images.cocodataset.org/val2017/000000039769.jpg',
             ]    ,							top_k=2    ,							)
         self.assertEqual(
             nested_simplify(lowerCAmelCase__    ,							decimals=4      )    ,							[
                 [{'label': 'LABEL_1', 'score': 0.5_74}, {'label': 'LABEL_0', 'score': 0.4_26}],
                 [{'label': 'LABEL_1', 'score': 0.5_74}, {'label': 'LABEL_0', 'score': 0.4_26}],
             ]    ,							)



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







         """simple docstring"""



         a__    = PreTrainedTokenizer()

         # Assert that the pipeline can be initialized with a feature extractor that is not in any mapping
         a__    = pipeline(
             'image-classification'    ,							model='hf-internal-testing/tiny-random-vit'    ,							tokenizer=lowerCAmelCase__      )

         self.assertIs(image_classifier.tokenizer    ,							lowerCAmelCase__      )




     @slow
     @require_torch
     def  lowercase__     (       self      )		->    Optional[Any]:







         """simple docstring"""



         a__    = pipeline('image-classification'    ,							model='deepmind/vision-perceiver-conv'      )
         a__    = image_classifier('http://images.cocodataset.org/val2017/000000039769.jpg'      )
         self.assertEqual(
             nested_simplify(lowerCAmelCase__    ,							decimals=4      )    ,							[
                 {'score': 0.43_85, 'label': 'tabby, tabby cat'},
                 {'score': 0.3_21, 'label': 'tiger cat'},
                 {'score': 0.05_02, 'label': 'Egyptian cat'},
                 {'score': 0.01_37, 'label': 'crib, cot'},
                 {'score': 0.0_07, 'label': 'radiator'},
             ]    ,							)

         a__    = pipeline('image-classification'    ,							model='deepmind/vision-perceiver-fourier'      )
         a__    = image_classifier('http://images.cocodataset.org/val2017/000000039769.jpg'      )
         self.assertEqual(
             nested_simplify(lowerCAmelCase__    ,							decimals=4      )    ,							[
                 {'score': 0.56_58, 'label': 'tabby, tabby cat'},
                 {'score': 0.13_09, 'label': 'tiger cat'},
                 {'score': 0.07_22, 'label': 'Egyptian cat'},
                 {'score': 0.07_07, 'label': 'remote control, remote'},
                 {'score': 0.00_82, 'label': 'computer keyboard, keypad'},
             ]    ,							)

         a__    = pipeline('image-classification'    ,							model='deepmind/vision-perceiver-learned'      )
         a__    = image_classifier('http://images.cocodataset.org/val2017/000000039769.jpg'      )
         self.assertEqual(
             nested_simplify(lowerCAmelCase__    ,							decimals=4      )    ,							[
                 {'score': 0.30_22, 'label': 'tabby, tabby cat'},
                 {'score': 0.23_62, 'label': 'Egyptian cat'},
                 {'score': 0.18_56, 'label': 'tiger cat'},
                 {'score': 0.03_24, 'label': 'remote control, remote'},
                 {'score': 0.00_96, 'label': 'quilt, comforter, comfort, puff'},
             ]    ,							)




"""simple docstring"""




import gc
import tempfile
import unittest

import numpy as np
import torch

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


a		:					str    	=					False







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



     def  lowercase__     (       self      )		->    Any:







         """simple docstring"""



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



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







         """simple docstring"""



         a__    = VersatileDiffusionDualGuidedPipeline.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      )

         a__    = load_image(
             'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg'      )

         a__    = torch.manual_seed(0      )
         a__    = pipe(
             prompt='first prompt'    ,							image=__SCREAMING_SNAKE_CASE    ,							text_to_image_strength=0.75    ,							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      )
             a__    = VersatileDiffusionDualGuidedPipeline.from_pretrained(__SCREAMING_SNAKE_CASE      )

         pipe.to(__SCREAMING_SNAKE_CASE      )
         pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE      )

         a__    = generator.manual_seed(0      )
         a__    = pipe(
             prompt='first prompt'    ,							image=__SCREAMING_SNAKE_CASE    ,							text_to_image_strength=0.75    ,							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      )		->    Union[str, Any]:







         """simple docstring"""



         a__    = VersatileDiffusionDualGuidedPipeline.from_pretrained('shi-labs/versatile-diffusion'      )
         pipe.remove_unused_weights()
         pipe.to(__SCREAMING_SNAKE_CASE      )
         pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE      )

         a__    = 'cyberpunk 2077'
         a__    = load_image(
             'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg'      )
         a__    = torch.manual_seed(0      )
         a__    = pipe(
             prompt=__SCREAMING_SNAKE_CASE    ,							image=__SCREAMING_SNAKE_CASE    ,							text_to_image_strength=0.75    ,							generator=__SCREAMING_SNAKE_CASE    ,							guidance_scale=7.5    ,							num_inference_steps=5_0    ,							output_type='numpy'    ,							).images

         a__    = image[0, 2_5_3:2_5_6, 2_5_3:2_5_6, -1]

         assert image.shape == (1, 5_1_2, 5_1_2, 3)
         a__    = np.array([0.07_87, 0.08_49, 0.08_26, 0.08_12, 0.08_07, 0.07_95, 0.08_18, 0.07_98, 0.07_79]      )

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


