





'''simple docstring'''

import unittest

import numpy as np
import torch

from diffusers import PNDMPipeline, PNDMScheduler, UNetaDModel
from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device


enable_full_determinism()
class 							__lowerCAmelCase	(       unittest.TestCase					):






       """simple docstring"""





       @property
       def snake_case__		(				self  :						Tuple	)       ->	Tuple:

            '''simple docstring'''




            torch.manual_seed(0	)
            _UpperCamelCase							       =			UNetaDModel(
                block_out_channels=(32, 64)			,					layers_per_block=2			,					sample_size=32			,					in_channels=3			,					out_channels=3			,					down_block_types=('''DownBlock2D''', '''AttnDownBlock2D''')			,					up_block_types=('''AttnUpBlock2D''', '''UpBlock2D''')			,					)
            return model







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

            '''simple docstring'''




            _UpperCamelCase							       =			self.dummy_uncond_unet
            _UpperCamelCase							       =			PNDMScheduler()

            _UpperCamelCase							       =			PNDMPipeline(unet=lowerCAmelCase__			,					scheduler=lowerCAmelCase__	)
            pndm.to(lowerCAmelCase__	)
            pndm.set_progress_bar_config(disable=lowerCAmelCase__	)

            _UpperCamelCase							       =			torch.manual_seed(0	)
            _UpperCamelCase							       =			pndm(generator=lowerCAmelCase__			,					num_inference_steps=20			,					output_type='''numpy'''	).images

            _UpperCamelCase							       =			torch.manual_seed(0	)
            _UpperCamelCase							       =			pndm(generator=lowerCAmelCase__			,					num_inference_steps=20			,					output_type='''numpy'''			,					return_dict=lowerCAmelCase__	)[0]

            _UpperCamelCase							       =			image[0, -3:, -3:, -1]
            _UpperCamelCase							       =			image_from_tuple[0, -3:, -3:, -1]

            assert image.shape == (1, 32, 32, 3)
            _UpperCamelCase							       =			np.array([1.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0]	)

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

@slow
@require_torch
class 							__lowerCAmelCase	(       unittest.TestCase					):






       """simple docstring"""





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

            '''simple docstring'''




            _UpperCamelCase							       =			'''google/ddpm-cifar10-32'''

            _UpperCamelCase							       =			UNetaDModel.from_pretrained(lowerCAmelCase__	)
            _UpperCamelCase							       =			PNDMScheduler()

            _UpperCamelCase							       =			PNDMPipeline(unet=lowerCAmelCase__			,					scheduler=lowerCAmelCase__	)
            pndm.to(lowerCAmelCase__	)
            pndm.set_progress_bar_config(disable=lowerCAmelCase__	)
            _UpperCamelCase							       =			torch.manual_seed(0	)
            _UpperCamelCase							       =			pndm(generator=lowerCAmelCase__			,					output_type='''numpy'''	).images

            _UpperCamelCase							       =			image[0, -3:, -3:, -1]

            assert image.shape == (1, 32, 32, 3)
            _UpperCamelCase							       =			np.array([0.1564, 0.14645, 0.1406, 0.14715, 0.12425, 0.14045, 0.13115, 0.12175, 0.125]	)

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










'''simple docstring'''

# Copyright 2023 The HuggingFace Inc. team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import TYPE_CHECKING

from ..models.auto import AutoModelForVisionaSeq
from ..utils import requires_backends
from .base import PipelineTool


if TYPE_CHECKING:
   from PIL import Image
class 							__lowerCAmelCase	(       __magic_name__					):






       """simple docstring"""
       _snake_case      :	str									=       'Salesforce/blip-image-captioning-base'
       _snake_case      :	Union[str, Any]									=       (
           'This is a tool that generates a description of an image. It takes an input named `image` which should be the '
           'image to caption, and returns a text that contains the description in English.'
       )
       _snake_case      :	List[Any]									=       'image_captioner'
       _snake_case      :	Union[str, Any]									=       AutoModelForVisionaSeq

       _snake_case      :	Dict									=       ['image']
       _snake_case      :	Optional[int]									=       ['text']





       def __init__(				self  :						Union[str, Any]			,					*lowerCAmelCase__  :						Optional[Any]			,					**lowerCAmelCase__  :						Optional[int]	)       ->	Tuple:

            '''simple docstring'''




            requires_backends(self			,					['''vision''']	)
            super().__init__(*lowerCAmelCase__			,					**lowerCAmelCase__	)





       def snake_case__		(				self  :						Tuple			,					lowerCAmelCase__  :						"Image"	)       ->	Optional[int]:

            '''simple docstring'''




            return self.pre_processor(images=lowerCAmelCase__			,					return_tensors='''pt'''	)





       def snake_case__		(				self  :						Any			,					lowerCAmelCase__  :						Union[str, Any]	)       ->	Union[str, Any]:

            '''simple docstring'''




            return self.model.generate(**lowerCAmelCase__	)







       def snake_case__		(				self  :						int			,					lowerCAmelCase__  :						List[str]	)       ->	List[str]:

            '''simple docstring'''




            return self.pre_processor.batch_decode(lowerCAmelCase__			,					skip_special_tokens=lowerCAmelCase__	)[0].strip()



