



import random
import unittest

import torch

from diffusers import IFInpaintingSuperResolutionPipeline
from diffusers.utils import floats_tensor
from diffusers.utils.import_utils import is_xformers_available
from diffusers.utils.testing_utils import skip_mps, torch_device

from ..pipeline_params import (
    TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS,
    TEXT_GUIDED_IMAGE_INPAINTING_PARAMS,
)
from ..test_pipelines_common import PipelineTesterMixin
from . import IFPipelineTesterMixin



@skip_mps
class     _UpperCAmelCase			(       __UpperCAmelCase			,      __UpperCAmelCase			,      unittest.TestCase     ):
     UpperCamelCase__		    =				IFInpaintingSuperResolutionPipeline
     UpperCamelCase__		    =				TEXT_GUIDED_IMAGE_INPAINTING_PARAMS - {"""width""", """height"""}
     UpperCamelCase__		    =				TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS.union({'''original_image'''}     )
     UpperCamelCase__		    =				PipelineTesterMixin.required_optional_params - {"""latents"""}




     def 		snake_case_		(				self):
         return self._get_superresolution_dummy_components()




     def 		snake_case_		(				self ,						a__ ,						a__=0):
         if str(UpperCAmelCase_).startswith('''mps'''):
             A__		 =					torch.manual_seed(UpperCAmelCase_)
         else:
             A__		 =					torch.Generator(device=UpperCAmelCase_).manual_seed(UpperCAmelCase_)

         A__		 =					floats_tensor((1, 3, 1_6, 1_6) ,						rng=random.Random(UpperCAmelCase_)).to(UpperCAmelCase_)
         A__		 =					floats_tensor((1, 3, 3_2, 3_2) ,						rng=random.Random(UpperCAmelCase_)).to(UpperCAmelCase_)
         A__		 =					floats_tensor((1, 3, 3_2, 3_2) ,						rng=random.Random(UpperCAmelCase_)).to(UpperCAmelCase_)

         A__		 =					{
             '''prompt''': '''A painting of a squirrel eating a burger''',
             '''image''': image,
             '''original_image''': original_image,
             '''mask_image''': mask_image,
             '''generator''': generator,
             '''num_inference_steps''': 2,
             '''output_type''': '''numpy''',
         }

         return inputs




     @unittest.skipIf(
         torch_device != '''cuda''' or not is_xformers_available() ,						reason='''XFormers attention is only available with CUDA and `xformers` installed''' ,						)
     def 		snake_case_		(				self):
         self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1e-3)




     def 		snake_case_		(				self):
         self._test_save_load_optional_components()




     @unittest.skipIf(torch_device != '''cuda''' ,						reason='''float16 requires CUDA''')
     def 		snake_case_		(				self):
         # Due to non-determinism in save load of the hf-internal-testing/tiny-random-t5 text encoder
         super().test_save_load_floataa(expected_max_diff=1e-1)




     def 		snake_case_		(				self):
         self._test_attention_slicing_forward_pass(expected_max_diff=1e-2)




     def 		snake_case_		(				self):
         self._test_save_load_local()




     def 		snake_case_		(				self):
         self._test_inference_batch_single_identical(
             expected_max_diff=1e-2 ,						)





# 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     _UpperCAmelCase			(       A__     ):
     UpperCamelCase__		    =				'''Salesforce/blip-image-captioning-base'''
     UpperCamelCase__		    =				(
         '''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.'''
     )
     UpperCamelCase__		    =				'''image_captioner'''
     UpperCamelCase__		    =				AutoModelForVisionaSeq

     UpperCamelCase__		    =				['''image''']
     UpperCamelCase__		    =				['''text''']




     def __init__(				self ,						*a__ ,						**a__):
         requires_backends(self ,						['''vision'''])
         super().__init__(*a__ ,						**a__)




     def 		snake_case_		(				self ,						a__):
         return self.pre_processor(images=a__ ,						return_tensors='''pt''')




     def 		snake_case_		(				self ,						a__):
         return self.model.generate(**a__)




     def 		snake_case_		(				self ,						a__):
         return self.pre_processor.batch_decode(a__ ,						skip_special_tokens=a__)[0].strip()
