






from typing import List, Optional, Union

from ...image_utils import ImageInput
from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy
from ...utils import TensorType


class        __UpperCamelCase    (     _lowerCAmelCase   ):
	__snake_case	:List[str]   =       ['image_processor', 'tokenizer']
	__snake_case	:List[Any]   =       'BlipImageProcessor'
	__snake_case	:Tuple   =       'AutoTokenizer'







	def __init__(  self      :							Tuple       ,				_lowerCAmelCase      :							Union[str, Any]       ,				_lowerCAmelCase      :							int			)		-> List[str]:
		"""simple docstring"""







		__lowercase             =  False
		super().__init__(_lowerCAmelCase       ,				_lowerCAmelCase			)
		__lowercase             =  self.image_processor







	def __call__(  self      :							List[str]       ,				_lowerCAmelCase      :							ImageInput = None       ,				_lowerCAmelCase      :							Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None       ,				_lowerCAmelCase      :							bool = True       ,				_lowerCAmelCase      :							Union[bool, str, PaddingStrategy] = False       ,				_lowerCAmelCase      :							Union[bool, str, TruncationStrategy] = None       ,				_lowerCAmelCase      :							Optional[int] = None       ,				_lowerCAmelCase      :							int = 0       ,				_lowerCAmelCase      :							Optional[int] = None       ,				_lowerCAmelCase      :							Optional[bool] = None       ,				_lowerCAmelCase      :							bool = False       ,				_lowerCAmelCase      :							bool = False       ,				_lowerCAmelCase      :							bool = False       ,				_lowerCAmelCase      :							bool = False       ,				_lowerCAmelCase      :							bool = False       ,				_lowerCAmelCase      :							bool = True       ,				_lowerCAmelCase      :							Optional[Union[str, TensorType]] = None       ,				**_lowerCAmelCase      :							Tuple       ,				)		-> BatchEncoding:
		"""simple docstring"""







		if images is None and text is None:
			raise ValueError("""You have to specify either images or text."""			)

		# Get only text
		if images is None:
			__lowercase             =  self.tokenizer
			__lowercase             =  self.tokenizer(
			    text=_lowerCAmelCase       ,				add_special_tokens=_lowerCAmelCase       ,				padding=_lowerCAmelCase       ,				truncation=_lowerCAmelCase       ,				max_length=_lowerCAmelCase       ,				stride=_lowerCAmelCase       ,				pad_to_multiple_of=_lowerCAmelCase       ,				return_attention_mask=_lowerCAmelCase       ,				return_overflowing_tokens=_lowerCAmelCase       ,				return_special_tokens_mask=_lowerCAmelCase       ,				return_offsets_mapping=_lowerCAmelCase       ,				return_token_type_ids=_lowerCAmelCase       ,				return_length=_lowerCAmelCase       ,				verbose=_lowerCAmelCase       ,				return_tensors=_lowerCAmelCase       ,				**_lowerCAmelCase       ,				)
			return text_encoding

		# add pixel_values
		__lowercase             =  self.image_processor(_lowerCAmelCase       ,				return_tensors=_lowerCAmelCase			)

		if text is not None:
			__lowercase             =  self.tokenizer(
			    text=_lowerCAmelCase       ,				add_special_tokens=_lowerCAmelCase       ,				padding=_lowerCAmelCase       ,				truncation=_lowerCAmelCase       ,				max_length=_lowerCAmelCase       ,				stride=_lowerCAmelCase       ,				pad_to_multiple_of=_lowerCAmelCase       ,				return_attention_mask=_lowerCAmelCase       ,				return_overflowing_tokens=_lowerCAmelCase       ,				return_special_tokens_mask=_lowerCAmelCase       ,				return_offsets_mapping=_lowerCAmelCase       ,				return_token_type_ids=_lowerCAmelCase       ,				return_length=_lowerCAmelCase       ,				verbose=_lowerCAmelCase       ,				return_tensors=_lowerCAmelCase       ,				**_lowerCAmelCase       ,				)
		else:
			__lowercase             =  None

		if text_encoding is not None:
			encoding_image_processor.update(_lowerCAmelCase			)

		return encoding_image_processor







	def  _a   (  self      :							Optional[int]       ,				*_lowerCAmelCase      :							Union[str, Any]       ,				**_lowerCAmelCase      :							Any			)		-> str:
		"""simple docstring"""







		return self.tokenizer.batch_decode(*_lowerCAmelCase       ,				**_lowerCAmelCase			)







	def  _a   (  self      :							Union[str, Any]       ,				*_lowerCAmelCase      :							Any       ,				**_lowerCAmelCase      :							Dict			)		-> Optional[Any]:
		"""simple docstring"""







		return self.tokenizer.decode(*_lowerCAmelCase       ,				**_lowerCAmelCase			)







	@property
	# Copied from transformers.models.blip.processing_blip.BlipProcessor.model_input_names
	def  _a   (  self      :							List[str]			)		-> Tuple:
		"""simple docstring"""







		__lowercase             =  self.tokenizer.model_input_names
		__lowercase             =  self.image_processor.model_input_names
		return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names			)			)









import random
import unittest

import torch

from diffusers import IFInpaintingPipeline
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    (     _lowerCAmelCase   ,			_lowerCAmelCase   ,			unittest.TestCase   ):
	__snake_case	:Any   =       IFInpaintingPipeline
	__snake_case	:str   =       TEXT_GUIDED_IMAGE_INPAINTING_PARAMS - {'width', 'height'}
	__snake_case	:Optional[int]   =       TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS
	__snake_case	:str   =       PipelineTesterMixin.required_optional_params - {'latents'}







	def  _a   (  self      :							Union[str, Any]			)		-> Tuple:
		"""simple docstring"""







		return self._get_dummy_components()







	def  _a   (  self      :							Optional[int]       ,				_lowerCAmelCase      :							List[str]       ,				_lowerCAmelCase      :							Dict=0			)		-> Any:
		"""simple docstring"""







		if str(_lowerCAmelCase			).startswith("""mps"""			):
			__lowercase             =  torch.manual_seed(_lowerCAmelCase			)
		else:
			__lowercase             =  torch.Generator(device=_lowerCAmelCase			).manual_seed(_lowerCAmelCase			)

		__lowercase             =  floats_tensor((1, 3, 32, 32)       ,				rng=random.Random(_lowerCAmelCase			)			).to(_lowerCAmelCase			)
		__lowercase             =  floats_tensor((1, 3, 32, 32)       ,				rng=random.Random(_lowerCAmelCase			)			).to(_lowerCAmelCase			)

		__lowercase             =  {
		    """prompt""": """A painting of a squirrel eating a burger""",
		    """image""": 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  _a   (  self      :							Tuple			)		-> Dict:
		"""simple docstring"""







		self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1e-3			)







	def  _a   (  self      :							Union[str, Any]			)		-> Dict:
		"""simple docstring"""







		self._test_save_load_optional_components()







	@unittest.skipIf(torch_device != """cuda"""       ,				reason="""float16 requires CUDA"""			)
	def  _a   (  self      :							List[str]			)		-> Tuple:
		"""simple docstring"""







		super().test_save_load_floataa(expected_max_diff=1e-1			)







	def  _a   (  self      :							Tuple			)		-> Optional[Any]:
		"""simple docstring"""







		self._test_attention_slicing_forward_pass(expected_max_diff=1e-2			)







	def  _a   (  self      :							str			)		-> Optional[int]:
		"""simple docstring"""







		self._test_save_load_local()







	def  _a   (  self      :							List[str]			)		-> List[Any]:
		"""simple docstring"""







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

