
from typing import Any, Callable, Dict, List, Optional, Union

import PIL.Image
import torch
from transformers import CLIPImageProcessor, CLIPTextModel, CLIPTokenizer

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    DiffusionPipeline,
    LMSDiscreteScheduler,
    PNDMScheduler,
    StableDiffusionImgaImgPipeline,
    StableDiffusionInpaintPipelineLegacy,
    StableDiffusionPipeline,
    UNetaDConditionModel,
)
from diffusers.configuration_utils import FrozenDict
from diffusers.pipelines.stable_diffusion.safety_checker import StableDiffusionSafetyChecker
from diffusers.utils import deprecate, logging


lowercase_											=	logging.get_logger(__name__)  # pylint: disable=invalid-name





class   __UpperCamelCase				(    lowerCAmelCase__   ):






     """simple docstring"""







     lowerCAmelCase_     =	['''safety_checker''', '''feature_extractor''']



     def __init__(							self    :    str      ,		_A    :    AutoencoderKL      ,		_A    :    CLIPTextModel      ,		_A    :    CLIPTokenizer      ,		_A    :    UNetaDConditionModel      ,		_A    :    Union[DDIMScheduler, PNDMScheduler, LMSDiscreteScheduler]      ,		_A    :    StableDiffusionSafetyChecker      ,		_A    :    CLIPImageProcessor      ,		_A    :    bool = True      ,		):







       """simple docstring"""




       super().__init__()
       if hasattr(scheduler.config      ,		'''steps_offset'''				) and scheduler.config.steps_offset != 1:
         __SCREAMING_SNAKE_CASE     :					List[Any]			=						(
             F'''The configuration file of this scheduler: {scheduler} is outdated. `steps_offset`'''
             F''' should be set to 1 instead of {scheduler.config.steps_offset}. Please make sure '''
             '''to update the config accordingly as leaving `steps_offset` might led to incorrect results'''
             ''' in future versions. If you have downloaded this checkpoint from the Hugging Face Hub,'''
             ''' it would be very nice if you could open a Pull request for the `scheduler/scheduler_config.json`'''
             ''' file'''
         )
         deprecate('''steps_offset!=1'''      ,		'''1.0.0'''      ,		_A      ,		standard_warn=_A				)
         __SCREAMING_SNAKE_CASE     :					Any			=						dict(scheduler.config				)
         __SCREAMING_SNAKE_CASE     :					Union[str, Any]			=						1
         __SCREAMING_SNAKE_CASE     :					List[str]			=						FrozenDict(_A				)

       self.register_modules(
           vae=_A      ,		text_encoder=_A      ,		tokenizer=_A      ,		unet=_A      ,		scheduler=_A      ,		safety_checker=_A      ,		feature_extractor=_A      ,		)
       self.register_to_config(requires_safety_checker=_A				)



     @property
     def 				UpperCAmelCase__    (							self    :    Optional[int]				):







       """simple docstring"""




       return {k: getattr(self      ,		_A				) for k in self.config.keys() if not k.startswith('''_'''				)}



     def 				UpperCAmelCase__    (							self    :    Optional[int]      ,		_A    :    Optional[Union[str, int]] = "auto"				):







       """simple docstring"""




       if slice_size == "auto":
         # half the attention head size is usually a good trade-off between
         # speed and memory
         __SCREAMING_SNAKE_CASE     :					Optional[Any]			=						self.unet.config.attention_head_dim // 2
       self.unet.set_attention_slice(_A				)



     def 				UpperCAmelCase__    (							self    :    Any				):







       """simple docstring"""




       self.enable_attention_slicing(_A				)



     @torch.no_grad()
     def 				UpperCAmelCase__    (							self    :    Optional[Any]      ,		_A    :    Union[str, List[str]]      ,		_A    :    Union[torch.FloatTensor, PIL.Image.Image]      ,		_A    :    Union[torch.FloatTensor, PIL.Image.Image]      ,		_A    :    float = 0.8      ,		_A    :    Optional[int] = 50      ,		_A    :    Optional[float] = 7.5      ,		_A    :    Optional[Union[str, List[str]]] = None      ,		_A    :    Optional[int] = 1      ,		_A    :    Optional[float] = 0.0      ,		_A    :    Optional[torch.Generator] = None      ,		_A    :    Optional[str] = "pil"      ,		_A    :    bool = True      ,		_A    :    Optional[Callable[[int, int, torch.FloatTensor], None]] = None      ,		_A    :    int = 1      ,		):







       """simple docstring"""




       return StableDiffusionInpaintPipelineLegacy(**self.components				)(
           prompt=_A      ,		image=_A      ,		mask_image=_A      ,		strength=_A      ,		num_inference_steps=_A      ,		guidance_scale=_A      ,		negative_prompt=_A      ,		num_images_per_prompt=_A      ,		eta=_A      ,		generator=_A      ,		output_type=_A      ,		return_dict=_A      ,		callback=_A      ,		)



     @torch.no_grad()
     def 				UpperCAmelCase__    (							self    :    List[str]      ,		_A    :    Union[str, List[str]]      ,		_A    :    Union[torch.FloatTensor, PIL.Image.Image]      ,		_A    :    float = 0.8      ,		_A    :    Optional[int] = 50      ,		_A    :    Optional[float] = 7.5      ,		_A    :    Optional[Union[str, List[str]]] = None      ,		_A    :    Optional[int] = 1      ,		_A    :    Optional[float] = 0.0      ,		_A    :    Optional[torch.Generator] = None      ,		_A    :    Optional[str] = "pil"      ,		_A    :    bool = True      ,		_A    :    Optional[Callable[[int, int, torch.FloatTensor], None]] = None      ,		_A    :    int = 1      ,		**_A    :    List[str]      ,		):







       """simple docstring"""




       return StableDiffusionImgaImgPipeline(**self.components				)(
           prompt=_A      ,		image=_A      ,		strength=_A      ,		num_inference_steps=_A      ,		guidance_scale=_A      ,		negative_prompt=_A      ,		num_images_per_prompt=_A      ,		eta=_A      ,		generator=_A      ,		output_type=_A      ,		return_dict=_A      ,		callback=_A      ,		callback_steps=_A      ,		)




     @torch.no_grad()
     def 				UpperCAmelCase__    (							self    :    Optional[int]      ,		_A    :    Union[str, List[str]]      ,		_A    :    int = 512      ,		_A    :    int = 512      ,		_A    :    int = 50      ,		_A    :    float = 7.5      ,		_A    :    Optional[Union[str, List[str]]] = None      ,		_A    :    Optional[int] = 1      ,		_A    :    float = 0.0      ,		_A    :    Optional[torch.Generator] = None      ,		_A    :    Optional[torch.FloatTensor] = None      ,		_A    :    Optional[str] = "pil"      ,		_A    :    bool = True      ,		_A    :    Optional[Callable[[int, int, torch.FloatTensor], None]] = None      ,		_A    :    int = 1      ,		):







       """simple docstring"""




       return StableDiffusionPipeline(**self.components				)(
           prompt=_A      ,		height=_A      ,		width=_A      ,		num_inference_steps=_A      ,		guidance_scale=_A      ,		negative_prompt=_A      ,		num_images_per_prompt=_A      ,		eta=_A      ,		generator=_A      ,		latents=_A      ,		output_type=_A      ,		return_dict=_A      ,		callback=_A      ,		callback_steps=_A      ,		)


import json
from typing import TYPE_CHECKING, List, Optional, Tuple

from tokenizers import pre_tokenizers

from ...tokenization_utils_base import AddedToken, BatchEncoding
from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_deberta import DebertaTokenizer


if TYPE_CHECKING:
   from transformers.pipelines.conversational import Conversation


lowercase_											=	logging.get_logger(__name__)

lowercase_											=	{"""vocab_file""": """vocab.json""", """merges_file""": """merges.txt""", """tokenizer_file""": """tokenizer.json"""}

lowercase_											=	{
    """vocab_file""": {
        """microsoft/deberta-base""": """https://huggingface.co/microsoft/deberta-base/resolve/main/vocab.json""",
        """microsoft/deberta-large""": """https://huggingface.co/microsoft/deberta-large/resolve/main/vocab.json""",
        """microsoft/deberta-xlarge""": """https://huggingface.co/microsoft/deberta-xlarge/resolve/main/vocab.json""",
        """microsoft/deberta-base-mnli""": """https://huggingface.co/microsoft/deberta-base-mnli/resolve/main/vocab.json""",
        """microsoft/deberta-large-mnli""": """https://huggingface.co/microsoft/deberta-large-mnli/resolve/main/vocab.json""",
        """microsoft/deberta-xlarge-mnli""": (
            """https://huggingface.co/microsoft/deberta-xlarge-mnli/resolve/main/vocab.json"""
        ),
    },
    """merges_file""": {
        """microsoft/deberta-base""": """https://huggingface.co/microsoft/deberta-base/resolve/main/merges.txt""",
        """microsoft/deberta-large""": """https://huggingface.co/microsoft/deberta-large/resolve/main/merges.txt""",
        """microsoft/deberta-xlarge""": """https://huggingface.co/microsoft/deberta-xlarge/resolve/main/merges.txt""",
        """microsoft/deberta-base-mnli""": """https://huggingface.co/microsoft/deberta-base-mnli/resolve/main/merges.txt""",
        """microsoft/deberta-large-mnli""": """https://huggingface.co/microsoft/deberta-large-mnli/resolve/main/merges.txt""",
        """microsoft/deberta-xlarge-mnli""": (
            """https://huggingface.co/microsoft/deberta-xlarge-mnli/resolve/main/merges.txt"""
        ),
    },
}

lowercase_											=	{
    """microsoft/deberta-base""": 512,
    """microsoft/deberta-large""": 512,
    """microsoft/deberta-xlarge""": 512,
    """microsoft/deberta-base-mnli""": 512,
    """microsoft/deberta-large-mnli""": 512,
    """microsoft/deberta-xlarge-mnli""": 512,
}

lowercase_											=	{
    """microsoft/deberta-base""": {"""do_lower_case""": False},
    """microsoft/deberta-large""": {"""do_lower_case""": False},
}





class   __UpperCamelCase				(    lowerCAmelCase__   ):






     """simple docstring"""







     lowerCAmelCase_     =	VOCAB_FILES_NAMES
     lowerCAmelCase_     =	PRETRAINED_VOCAB_FILES_MAP
     lowerCAmelCase_     =	PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
     lowerCAmelCase_     =	['''input_ids''', '''attention_mask''', '''token_type_ids''']
     lowerCAmelCase_     =	DebertaTokenizer



     def __init__(							self    :    List[str]      ,		_A    :    Tuple=None      ,		_A    :    List[Any]=None      ,		_A    :    Optional[int]=None      ,		_A    :    List[str]="replace"      ,		_A    :    List[Any]="[CLS]"      ,		_A    :    Optional[Any]="[SEP]"      ,		_A    :    Union[str, Any]="[SEP]"      ,		_A    :    List[str]="[CLS]"      ,		_A    :    Optional[Any]="[UNK]"      ,		_A    :    str="[PAD]"      ,		_A    :    List[str]="[MASK]"      ,		_A    :    Dict=False      ,		**_A    :    Optional[int]      ,		):







       """simple docstring"""




       super().__init__(
           _A      ,		_A      ,		tokenizer_file=_A      ,		errors=_A      ,		bos_token=_A      ,		eos_token=_A      ,		unk_token=_A      ,		sep_token=_A      ,		cls_token=_A      ,		pad_token=_A      ,		mask_token=_A      ,		add_prefix_space=_A      ,		**_A      ,		)
       __SCREAMING_SNAKE_CASE     :					List[str]			=						kwargs.pop('''add_bos_token'''      ,		_A				)

       __SCREAMING_SNAKE_CASE     :					int			=						json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__()				)
       if pre_tok_state.get('''add_prefix_space'''      ,		_A				) != add_prefix_space:
         __SCREAMING_SNAKE_CASE     :					Optional[Any]			=						getattr(_A      ,		pre_tok_state.pop('''type'''				)				)
         __SCREAMING_SNAKE_CASE     :					Dict			=						add_prefix_space
         __SCREAMING_SNAKE_CASE     :					Any			=						pre_tok_class(**_A				)

       __SCREAMING_SNAKE_CASE     :					Optional[Any]			=						add_prefix_space



     @property
     def 				UpperCAmelCase__    (							self    :    List[Any]				):







       """simple docstring"""




       if self._mask_token is None:
         if self.verbose:
           logger.error('''Using mask_token, but it is not set yet.'''				)
         return None
       return str(self._mask_token				)



     @mask_token.setter
     def 				UpperCAmelCase__    (							self    :    Any      ,		_A    :    Union[str, Any]				):







       """simple docstring"""




       __SCREAMING_SNAKE_CASE     :					Dict			=						AddedToken(_A      ,		lstrip=_A      ,		rstrip=_A				) if isinstance(_A      ,		_A				) else value
       __SCREAMING_SNAKE_CASE     :					Tuple			=						value



     def 				UpperCAmelCase__    (							self    :    List[Any]      ,		_A    :    List[int]      ,		_A    :    Optional[List[int]] = None				):







       """simple docstring"""




       if token_ids_a is None:
         return [self.cls_token_id] + token_ids_a + [self.sep_token_id]
       __SCREAMING_SNAKE_CASE     :					List[str]			=						[self.cls_token_id]
       __SCREAMING_SNAKE_CASE     :					List[Any]			=						[self.sep_token_id]
       return cls + token_ids_a + sep + token_ids_a + sep



     def 				UpperCAmelCase__    (							self    :    int      ,		_A    :    List[int]      ,		_A    :    Optional[List[int]] = None				):







       """simple docstring"""




       __SCREAMING_SNAKE_CASE     :					Tuple			=						[self.sep_token_id]
       __SCREAMING_SNAKE_CASE     :					List[str]			=						[self.cls_token_id]

       if token_ids_a is None:
         return len(cls + token_ids_a + sep				) * [0]
       return len(cls + token_ids_a + sep				) * [0] + len(token_ids_a + sep				) * [1]



     def 				UpperCAmelCase__    (							self    :    Tuple      ,		*_A    :    Optional[Any]      ,		**_A    :    Optional[Any]				):







       """simple docstring"""




       __SCREAMING_SNAKE_CASE     :					List[str]			=						kwargs.get('''is_split_into_words'''      ,		_A				)
       assert self.add_prefix_space or not is_split_into_words, (
           F'''You need to instantiate {self.__class__.__name__} with add_prefix_space=True '''
           "to use it with pretokenized inputs."
       )

       return super()._batch_encode_plus(*_A      ,		**_A				)



     def 				UpperCAmelCase__    (							self    :    Tuple      ,		*_A    :    Tuple      ,		**_A    :    int				):







       """simple docstring"""




       __SCREAMING_SNAKE_CASE     :					Dict			=						kwargs.get('''is_split_into_words'''      ,		_A				)

       assert self.add_prefix_space or not is_split_into_words, (
           F'''You need to instantiate {self.__class__.__name__} with add_prefix_space=True '''
           "to use it with pretokenized inputs."
       )

       return super()._encode_plus(*_A      ,		**_A				)



     def 				UpperCAmelCase__    (							self    :    Tuple      ,		_A    :    str      ,		_A    :    Optional[str] = None				):







       """simple docstring"""




       __SCREAMING_SNAKE_CASE     :					Optional[int]			=						self._tokenizer.model.save(_A      ,		name=_A				)
       return tuple(_A				)




     def 				UpperCAmelCase__    (							self    :    Tuple      ,		_A    :    "Conversation"				):







       """simple docstring"""




       __SCREAMING_SNAKE_CASE     :					List[Any]			=						[]
       for is_user, text in conversation.iter_texts():
         input_ids.extend(self.encode(_A      ,		add_special_tokens=_A				) + [self.eos_token_id]				)

       if len(_A				) > self.model_max_length:
         __SCREAMING_SNAKE_CASE     :					Any			=						input_ids[-self.model_max_length :]
       return input_ids
