





import importlib
import inspect
import json
import os
from collections import OrderedDict

# Build the list of all feature extractors
from ...configuration_utils import PretrainedConfig
from ...dynamic_module_utils import get_class_from_dynamic_module, resolve_trust_remote_code
from ...feature_extraction_utils import FeatureExtractionMixin
from ...image_processing_utils import ImageProcessingMixin
from ...tokenization_utils import TOKENIZER_CONFIG_FILE
from ...utils import FEATURE_EXTRACTOR_NAME, get_file_from_repo, logging
from .auto_factory import _LazyAutoMapping
from .configuration_auto import (
    CONFIG_MAPPING_NAMES,
    AutoConfig,
    model_type_to_module_name,
    replace_list_option_in_docstrings,
)
from .feature_extraction_auto import AutoFeatureExtractor
from .image_processing_auto import AutoImageProcessor
from .tokenization_auto import AutoTokenizer


__lowerCAmelCase			:int	 =     logging.get_logger(__name__)

__lowerCAmelCase			:Tuple	 =     OrderedDict(
    [
        ('align', 'AlignProcessor'),
        ('altclip', 'AltCLIPProcessor'),
        ('bark', 'BarkProcessor'),
        ('blip', 'BlipProcessor'),
        ('blip-2', 'Blip2Processor'),
        ('bridgetower', 'BridgeTowerProcessor'),
        ('chinese_clip', 'ChineseCLIPProcessor'),
        ('clap', 'ClapProcessor'),
        ('clip', 'CLIPProcessor'),
        ('clipseg', 'CLIPSegProcessor'),
        ('flava', 'FlavaProcessor'),
        ('git', 'GitProcessor'),
        ('groupvit', 'CLIPProcessor'),
        ('hubert', 'Wav2Vec2Processor'),
        ('instructblip', 'InstructBlipProcessor'),
        ('layoutlmv2', 'LayoutLMv2Processor'),
        ('layoutlmv3', 'LayoutLMv3Processor'),
        ('markuplm', 'MarkupLMProcessor'),
        ('mctct', 'MCTCTProcessor'),
        ('mgp-str', 'MgpstrProcessor'),
        ('oneformer', 'OneFormerProcessor'),
        ('owlvit', 'OwlViTProcessor'),
        ('pix2struct', 'Pix2StructProcessor'),
        ('sam', 'SamProcessor'),
        ('sew', 'Wav2Vec2Processor'),
        ('sew-d', 'Wav2Vec2Processor'),
        ('speech_to_text', 'Speech2TextProcessor'),
        ('speech_to_text_2', 'Speech2Text2Processor'),
        ('speecht5', 'SpeechT5Processor'),
        ('trocr', 'TrOCRProcessor'),
        ('tvlt', 'TvltProcessor'),
        ('unispeech', 'Wav2Vec2Processor'),
        ('unispeech-sat', 'Wav2Vec2Processor'),
        ('vilt', 'ViltProcessor'),
        ('vision-text-dual-encoder', 'VisionTextDualEncoderProcessor'),
        ('wav2vec2', 'Wav2Vec2Processor'),
        ('wav2vec2-conformer', 'Wav2Vec2Processor'),
        ('wavlm', 'Wav2Vec2Processor'),
        ('whisper', 'WhisperProcessor'),
        ('xclip', 'XCLIPProcessor'),
    ]
)

__lowerCAmelCase			:int	 =     _LazyAutoMapping(CONFIG_MAPPING_NAMES, PROCESSOR_MAPPING_NAMES)







def  A					(				UpperCAmelCase					):
					for module_name, processors in PROCESSOR_MAPPING_NAMES.items():
										if class_name in processors:
															_snake_case							:       Any												= model_type_to_module_name(UpperCAmelCase					)

															_snake_case							:       Dict												= importlib.import_module(F""".{module_name}"""	,	"transformers.models"					)
															try:
																				return getattr(UpperCAmelCase	,	UpperCAmelCase					)
															except AttributeError:
																				continue

					for processor in PROCESSOR_MAPPING._extra_content.values():
										if getattr(UpperCAmelCase	,	"__name__"	,	UpperCAmelCase					) == class_name:
															return processor

    # We did not fine the class, but maybe it's because a dep is missing. In that case, the class will be in the main
    # init and we return the proper dummy to get an appropriate error message.
					_snake_case							:       Optional[int]												= importlib.import_module("transformers"					)
					if hasattr(UpperCAmelCase	,	UpperCAmelCase					):
										return getattr(UpperCAmelCase	,	UpperCAmelCase					)

					return None






class 							_a:







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


							'''simple docstring'''

							raise EnvironmentError(
							    "AutoProcessor is designed to be instantiated "
							    "using the `AutoProcessor.from_pretrained(pretrained_model_name_or_path)` method."					)







		@classmethod
		@replace_list_option_in_docstrings(__snake_case					)
		def 							lowercase							(     cls			,    __snake_case			,    **__snake_case					)   ->   int:


							'''simple docstring'''

							_snake_case							:       List[Any]												= kwargs.pop("config"			,    __snake_case					)
							_snake_case							:       List[str]												= kwargs.pop("trust_remote_code"			,    __snake_case					)
							_snake_case							:       str												= True

							_snake_case							:       int												= None
							_snake_case							:       List[Any]												= None

							# First, let's see if we have a preprocessor config.
							# Filter the kwargs for `get_file_from_repo`.
							_snake_case							:       Any												= {
							    key: kwargs[key] for key in inspect.signature(__snake_case					).parameters.keys() if key in kwargs
							}
							# Let's start by checking whether the processor class is saved in an image processor
							_snake_case							:       Tuple												= get_file_from_repo(
							    __snake_case			,    __snake_case			,    **__snake_case					)
							if preprocessor_config_file is not None:
												_snake_case							,			_snake_case							:       str												= ImageProcessingMixin.get_image_processor_dict(__snake_case			,    **__snake_case					)
												_snake_case							:       Optional[Any]												= config_dict.get("processor_class"			,    __snake_case					)
												if "AutoProcessor" in config_dict.get("auto_map"			,    {}					):
																	_snake_case							:       int												= config_dict["auto_map"]["AutoProcessor"]

        # If not found, let's check whether the processor class is saved in a feature extractor config
							if preprocessor_config_file is not None and processor_class is None:
												_snake_case							,			_snake_case							:       Dict												= FeatureExtractionMixin.get_feature_extractor_dict(__snake_case			,    **__snake_case					)
												_snake_case							:       str												= config_dict.get("processor_class"			,    __snake_case					)
												if "AutoProcessor" in config_dict.get("auto_map"			,    {}					):
																	_snake_case							:       str												= config_dict["auto_map"]["AutoProcessor"]

							if processor_class is None:
												# Next, let's check whether the processor class is saved in a tokenizer
												_snake_case							:       Dict												= get_file_from_repo(
												    __snake_case			,    __snake_case			,    **__snake_case					)
												if tokenizer_config_file is not None:
																	with open(__snake_case			,    encoding="utf-8"					) as reader:
																						_snake_case							:       Dict												= json.load(__snake_case					)

																	_snake_case							:       List[str]												= config_dict.get("processor_class"			,    __snake_case					)
																	if "AutoProcessor" in config_dict.get("auto_map"			,    {}					):
																						_snake_case							:       List[str]												= config_dict["auto_map"]["AutoProcessor"]

							if processor_class is None:
												# Otherwise, load config, if it can be loaded.
												if not isinstance(__snake_case			,    __snake_case					):
																	_snake_case							:       Any												= AutoConfig.from_pretrained(
																	    __snake_case			,    trust_remote_code=__snake_case			,    **__snake_case					)

												# And check if the config contains the processor class.
												_snake_case							:       Optional[Any]												= getattr(__snake_case			,    "processor_class"			,    __snake_case					)
												if hasattr(__snake_case			,    "auto_map"					) and "AutoProcessor" in config.auto_map:
																	_snake_case							:       Any												= config.auto_map["AutoProcessor"]

							if processor_class is not None:
												_snake_case							:       str												= processor_class_from_name(__snake_case					)

							_snake_case							:       Tuple												= processor_auto_map is not None
							_snake_case							:       Optional[Any]												= processor_class is not None or type(__snake_case					) in PROCESSOR_MAPPING
							_snake_case							:       Any												= resolve_trust_remote_code(
							    __snake_case			,    __snake_case			,    __snake_case			,    __snake_case					)

							if has_remote_code and trust_remote_code:
												_snake_case							:       Union[str, Any]												= get_class_from_dynamic_module(
												    __snake_case			,    __snake_case			,    **__snake_case					)
												_snake_case							:       Optional[Any]												= kwargs.pop("code_revision"			,    __snake_case					)
												if os.path.isdir(__snake_case					):
																	processor_class.register_for_auto_class()
												return processor_class.from_pretrained(
												    __snake_case			,    trust_remote_code=__snake_case			,    **__snake_case					)
							elif processor_class is not None:
												return processor_class.from_pretrained(
												    __snake_case			,    trust_remote_code=__snake_case			,    **__snake_case					)
							# Last try: we use the PROCESSOR_MAPPING.
							elif type(__snake_case					) in PROCESSOR_MAPPING:
												return PROCESSOR_MAPPING[type(__snake_case					)].from_pretrained(__snake_case			,    **__snake_case					)

							# At this stage, there doesn't seem to be a `Processor` class available for this model, so let's try a
							# tokenizer.
							try:
												return AutoTokenizer.from_pretrained(
												    __snake_case			,    trust_remote_code=__snake_case			,    **__snake_case					)
							except Exception:
												try:
																	return AutoImageProcessor.from_pretrained(
																	    __snake_case			,    trust_remote_code=__snake_case			,    **__snake_case					)
												except Exception:
																	pass

												try:
																	return AutoFeatureExtractor.from_pretrained(
																	    __snake_case			,    trust_remote_code=__snake_case			,    **__snake_case					)
												except Exception:
																	pass

							raise ValueError(
							    f"""Unrecognized processing class in {pretrained_model_name_or_path}. Can't instantiate a processor, a """
							    "tokenizer, an image processor or a feature extractor for this model. Make sure the repository contains"
							    "the files of at least one of those processing classes."					)







		@staticmethod
		def 							lowercase							(     __snake_case			,    __snake_case					)   ->   str:


							'''simple docstring'''

							PROCESSOR_MAPPING.register(__snake_case			,    __snake_case					)






import gc
import random
import unittest

import numpy as np
import torch
from PIL import Image
from transformers import CLIPImageProcessor, CLIPVisionConfig

from diffusers import AutoencoderKL, PaintByExamplePipeline, PNDMScheduler, UNetaDConditionModel
from diffusers.pipelines.paint_by_example import PaintByExampleImageEncoder
from diffusers.utils import floats_tensor, load_image, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..pipeline_params import IMAGE_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, IMAGE_GUIDED_IMAGE_INPAINTING_PARAMS
from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()






class 							_a(     __A						, unittest.TestCase     ):
		lowerCamelCase__  :Optional[int] 					=		PaintByExamplePipeline
		lowerCamelCase__  :Union[str, Any] 					=		IMAGE_GUIDED_IMAGE_INPAINTING_PARAMS
		lowerCamelCase__  :Optional[Any] 					=		IMAGE_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS
		lowerCamelCase__  :int 					=		frozenset([]     )  # TO_DO: update the image_prams once refactored VaeImageProcessor.preprocess







		def 							lowercase							(     self					)   ->   Dict:


							'''simple docstring'''

							torch.manual_seed(0					)
							_snake_case							:       Union[str, Any]												= UNetaDConditionModel(
							    block_out_channels=(3_2, 6_4)			,    layers_per_block=2			,    sample_size=3_2			,    in_channels=9			,    out_channels=4			,    down_block_types=("DownBlock2D", "CrossAttnDownBlock2D")			,    up_block_types=("CrossAttnUpBlock2D", "UpBlock2D")			,    cross_attention_dim=3_2			,    )
							_snake_case							:       int												= PNDMScheduler(skip_prk_steps=__snake_case					)
							torch.manual_seed(0					)
							_snake_case							:       Any												= AutoencoderKL(
							    block_out_channels=[3_2, 6_4]			,    in_channels=3			,    out_channels=3			,    down_block_types=["DownEncoderBlock2D", "DownEncoderBlock2D"]			,    up_block_types=["UpDecoderBlock2D", "UpDecoderBlock2D"]			,    latent_channels=4			,    )
							torch.manual_seed(0					)
							_snake_case							:       Dict												= CLIPVisionConfig(
							    hidden_size=3_2			,    projection_dim=3_2			,    intermediate_size=3_7			,    layer_norm_eps=1E-05			,    num_attention_heads=4			,    num_hidden_layers=5			,    image_size=3_2			,    patch_size=4			,    )
							_snake_case							:       Any												= PaintByExampleImageEncoder(__snake_case			,    proj_size=3_2					)
							_snake_case							:       Dict												= CLIPImageProcessor(crop_size=3_2			,    size=3_2					)

							_snake_case							:       Any												= {
							    "unet": unet,
							    "scheduler": scheduler,
							    "vae": vae,
							    "image_encoder": image_encoder,
							    "safety_checker": None,
							    "feature_extractor": feature_extractor,
							}
							return components







		def 							lowercase							(     self			,    __snake_case					)   ->   Tuple:


							'''simple docstring'''

							_snake_case							:       List[str]												= np.array(image.convert("RGB"					)					)
							_snake_case							:       str												= image[None].transpose(0			,    3			,    1			,    2					)
							_snake_case							:       Dict												= torch.from_numpy(__snake_case					).to(dtype=torch.floataa					) / 1_27.5 - 1.0
							return image







		def 							lowercase							(     self			,    __snake_case="cpu"			,    __snake_case=0					)   ->   Any:


							'''simple docstring'''

							_snake_case							:       Tuple												= floats_tensor((1, 3, 3_2, 3_2)			,    rng=random.Random(__snake_case					)					).to(__snake_case					)
							_snake_case							:       Union[str, Any]												= image.cpu().permute(0			,    2			,    3			,    1					)[0]
							_snake_case							:       List[Any]												= Image.fromarray(np.uinta(__snake_case					)					).convert("RGB"					).resize((6_4, 6_4)					)
							_snake_case							:       List[Any]												= Image.fromarray(np.uinta(image + 4					)					).convert("RGB"					).resize((6_4, 6_4)					)
							_snake_case							:       Any												= Image.fromarray(np.uinta(__snake_case					)					).convert("RGB"					).resize((3_2, 3_2)					)

							if str(__snake_case					).startswith("mps"					):
												_snake_case							:       Union[str, Any]												= torch.manual_seed(__snake_case					)
							else:
												_snake_case							:       List[Any]												= torch.Generator(device=__snake_case					).manual_seed(__snake_case					)
							_snake_case							:       Optional[int]												= {
							    "example_image": example_image,
							    "image": init_image,
							    "mask_image": mask_image,
							    "generator": generator,
							    "num_inference_steps": 2,
							    "guidance_scale": 6.0,
							    "output_type": "numpy",
							}
							return inputs







		def 							lowercase							(     self					)   ->   int:


							'''simple docstring'''

							_snake_case							:       Optional[Any]												= self.get_dummy_components()

							# make sure here that pndm scheduler skips prk
							_snake_case							:       int												= PaintByExamplePipeline(**__snake_case					)
							_snake_case							:       List[Any]												= pipe.to("cpu"					)
							pipe.set_progress_bar_config(disable=__snake_case					)

							_snake_case							:       List[str]												= self.get_dummy_inputs()
							_snake_case							:       int												= pipe(**__snake_case					)
							_snake_case							:       Dict												= output.images

							_snake_case							:       Optional[int]												= image[0, -3:, -3:, -1]

							assert image.shape == (1, 6_4, 6_4, 3)
							_snake_case							:       int												= np.array([0.46_86, 0.56_87, 0.40_07, 0.52_18, 0.57_41, 0.44_82, 0.49_40, 0.46_29, 0.45_03]					)

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







		def 							lowercase							(     self					)   ->   List[Any]:


							'''simple docstring'''

							_snake_case							:       Optional[int]												= "cpu"
							_snake_case							:       str												= self.get_dummy_inputs()
							inputs.pop("mask_image"					)
							_snake_case							:       Union[str, Any]												= self.convert_to_pt(inputs.pop("image"					)					)
							_snake_case							:       Union[str, Any]												= image.clamp(0			,    1					) / 2

							# make sure here that pndm scheduler skips prk
							_snake_case							:       Dict												= PaintByExamplePipeline(**self.get_dummy_components()					)
							_snake_case							:       Union[str, Any]												= pipe.to(__snake_case					)
							pipe.set_progress_bar_config(disable=__snake_case					)

							_snake_case							:       int												= pipe(image=__snake_case			,    mask_image=mask_image[:, 0]			,    **__snake_case					)
							_snake_case							:       Optional[int]												= output.images

							_snake_case							:       Any												= image.cpu().permute(0			,    2			,    3			,    1					)[0]
							_snake_case							:       List[str]												= mask_image.cpu().permute(0			,    2			,    3			,    1					)[0]

							_snake_case							:       Any												= Image.fromarray(np.uinta(__snake_case					)					).convert("RGB"					)
							_snake_case							:       int												= Image.fromarray(np.uinta(__snake_case					)					).convert("RGB"					)

							_snake_case							:       Optional[int]												= pipe(**self.get_dummy_inputs()					)
							_snake_case							:       Dict												= output.images

							assert out_a.shape == (1, 6_4, 6_4, 3)
							assert np.abs(out_a.flatten() - out_a.flatten()					).max() < 5E-2







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


							'''simple docstring'''

							super().test_inference_batch_single_identical(expected_max_diff=3E-3					)






@slow
@require_torch_gpu
class 							_a(     unittest.TestCase     ):







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


							'''simple docstring'''

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







		def 							lowercase							(     self					)   ->   int:


							'''simple docstring'''

							_snake_case							:       Tuple												= load_image(
							    "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
							    "/paint_by_example/dog_in_bucket.png"					)
							_snake_case							:       Optional[Any]												= load_image(
							    "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
							    "/paint_by_example/mask.png"					)
							_snake_case							:       Optional[int]												= load_image(
							    "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
							    "/paint_by_example/panda.jpg"					)

							_snake_case							:       Any												= PaintByExamplePipeline.from_pretrained("Fantasy-Studio/Paint-by-Example"					)
							_snake_case							:       Any												= pipe.to(__snake_case					)
							pipe.set_progress_bar_config(disable=__snake_case					)

							_snake_case							:       Union[str, Any]												= torch.manual_seed(3_2_1					)
							_snake_case							:       int												= pipe(
							    image=__snake_case			,    mask_image=__snake_case			,    example_image=__snake_case			,    generator=__snake_case			,    guidance_scale=5.0			,    num_inference_steps=5_0			,    output_type="np"			,    )

							_snake_case							:       str												= output.images

							_snake_case							:       Optional[int]												= image[0, -3:, -3:, -1]

							assert image.shape == (1, 5_1_2, 5_1_2, 3)
							_snake_case							:       int												= np.array([0.48_34, 0.48_11, 0.48_74, 0.51_22, 0.50_81, 0.51_44, 0.52_91, 0.52_90, 0.53_74]					)

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