import os
from shutil import copyfile
from typing import Any, Dict, List, Optional, Tuple

import sentencepiece as spm

from ...tokenization_utils import AddedToken, PreTrainedTokenizer
from ...utils import logging


UpperCamelCase__    							= logging.get_logger(__name__)

UpperCamelCase__    							= {'vocab_file': 'sentencepiece.bpe.model'}

UpperCamelCase__    							= {
    'vocab_file': {
        'moussaKam/mbarthez': 'https://huggingface.co/moussaKam/mbarthez/resolve/main/sentencepiece.bpe.model',
        'moussaKam/barthez': 'https://huggingface.co/moussaKam/barthez/resolve/main/sentencepiece.bpe.model',
        'moussaKam/barthez-orangesum-title': (
            'https://huggingface.co/moussaKam/barthez-orangesum-title/resolve/main/sentencepiece.bpe.model'
        ),
    },
}

UpperCamelCase__    							= {
    'moussaKam/mbarthez': 1_0_2_4,
    'moussaKam/barthez': 1_0_2_4,
    'moussaKam/barthez-orangesum-title': 1_0_2_4,
}

UpperCamelCase__    							= '▁'





class 					A							(    UpperCAmelCase_					):
   __UpperCAmelCase			:			Dict              =      VOCAB_FILES_NAMES
   __UpperCAmelCase			:			Any              =      PRETRAINED_VOCAB_FILES_MAP
   __UpperCAmelCase			:			Any              =      PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
   __UpperCAmelCase			:			int              =      ['input_ids', 'attention_mask']


   def __init__(self					:						int	,  __UpperCAmelCase					:						Union[str, Any]	,  __UpperCAmelCase					:						Optional[Any]="<s>"	,  __UpperCAmelCase					:						Tuple="</s>"	,  __UpperCAmelCase					:						Tuple="</s>"	,  __UpperCAmelCase					:						int="<s>"	,  __UpperCAmelCase					:						Any="<unk>"	,  __UpperCAmelCase					:						List[str]="<pad>"	,  __UpperCAmelCase					:						int="<mask>"	,  __UpperCAmelCase					:						Optional[Dict[str, Any]] = None	,  **__UpperCAmelCase					:						str	,  )						->    None:
         """simple docstring"""



         UpperCAmelCase__								=  AddedToken(__UpperCAmelCase	,  lstrip=__UpperCAmelCase	,  rstrip=__UpperCAmelCase		) if isinstance(__UpperCAmelCase	,  __UpperCAmelCase		) else mask_token

         UpperCAmelCase__								=  {} if sp_model_kwargs is None else sp_model_kwargs

         super().__init__(
             bos_token=__UpperCAmelCase	,  eos_token=__UpperCAmelCase	,  unk_token=__UpperCAmelCase	,  sep_token=__UpperCAmelCase	,  cls_token=__UpperCAmelCase	,  pad_token=__UpperCAmelCase	,  mask_token=__UpperCAmelCase	,  sp_model_kwargs=self.sp_model_kwargs	,  **__UpperCAmelCase	,  )

         UpperCAmelCase__								=  vocab_file
         UpperCAmelCase__								=  spm.SentencePieceProcessor(**self.sp_model_kwargs		)
         self.sp_model.Load(str(__UpperCAmelCase		)		)

         UpperCAmelCase__								=  {"<s>": 0, "<pad>": 1, "</s>": 2, "<unk>": 3}

         UpperCAmelCase__								=  len(self.sp_model		) - 1
         UpperCAmelCase__								=  {v: k for k, v in self.fairseq_tokens_to_ids.items()}


   def       lowercase_				(self					:						Any	,  __UpperCAmelCase					:						List[int]	,  __UpperCAmelCase					:						Optional[List[int]] = None		)						->    List[int]:
         """simple docstring"""



         if token_ids_a is None:
               return [self.cls_token_id] + token_ids_a + [self.sep_token_id]
         UpperCAmelCase__								=  [self.cls_token_id]
         UpperCAmelCase__								=  [self.sep_token_id]
         return cls + token_ids_a + sep + sep + token_ids_a + sep


   def       lowercase_				(self					:						Dict	,  __UpperCAmelCase					:						List[int]	,  __UpperCAmelCase					:						Optional[List[int]] = None	,  __UpperCAmelCase					:						bool = False		)						->    List[int]:
         """simple docstring"""



         if already_has_special_tokens:
               return super().get_special_tokens_mask(
                   token_ids_a=__UpperCAmelCase	,  token_ids_a=__UpperCAmelCase	,  already_has_special_tokens=__UpperCAmelCase		)

         if token_ids_a is None:
               return [1] + ([0] * len(__UpperCAmelCase		)) + [1]
         return [1] + ([0] * len(__UpperCAmelCase		)) + [1, 1] + ([0] * len(__UpperCAmelCase		)) + [1]


   def       lowercase_				(self					:						List[Any]	,  __UpperCAmelCase					:						List[int]	,  __UpperCAmelCase					:						Optional[List[int]] = None		)						->    List[int]:
         """simple docstring"""



         UpperCAmelCase__								=  [self.sep_token_id]
         UpperCAmelCase__								=  [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 + sep + token_ids_a + sep		) * [0]


   @property
   def       lowercase_				(self					:						Optional[int]		)						->    int:
         """simple docstring"""



         return len(self.sp_model		)


   def       lowercase_				(self					:						Dict		)						->    str:
         """simple docstring"""



         UpperCAmelCase__								=  {self.convert_ids_to_tokens(__UpperCAmelCase		): i for i in range(self.vocab_size		)}
         vocab.update(self.added_tokens_encoder		)
         return vocab


   def       lowercase_				(self					:						int	,  __UpperCAmelCase					:						str		)						->    List[str]:
         """simple docstring"""



         return self.sp_model.encode(__UpperCAmelCase	,  out_type=__UpperCAmelCase		)


   def       lowercase_				(self					:						List[Any]	,  __UpperCAmelCase					:						Tuple		)						->    Any:
         """simple docstring"""



         if token in self.fairseq_tokens_to_ids:
               return self.fairseq_tokens_to_ids[token]
         UpperCAmelCase__								=  self.sp_model.PieceToId(__UpperCAmelCase		)

         return spm_id if spm_id else self.unk_token_id


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



         if index in self.fairseq_ids_to_tokens:
               return self.fairseq_ids_to_tokens[index]
         return self.sp_model.IdToPiece(__UpperCAmelCase		)


   def       lowercase_				(self					:						Dict	,  __UpperCAmelCase					:						Tuple		)						->    Optional[Any]:
         """simple docstring"""



         UpperCAmelCase__								=  []
         UpperCAmelCase__								=  ""
         UpperCAmelCase__								=  False
         for token in tokens:
               # make sure that special tokens are not decoded using sentencepiece model
               if token in self.all_special_tokens:
                     if not prev_is_special:
                           out_string += " "
                     out_string += self.sp_model.decode(__UpperCAmelCase		) + token
                     UpperCAmelCase__								=  True
                     UpperCAmelCase__								=  []
               else:
                     current_sub_tokens.append(__UpperCAmelCase		)
                     UpperCAmelCase__								=  False
         out_string += self.sp_model.decode(__UpperCAmelCase		)
         return out_string.strip()


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



         UpperCAmelCase__								=  self.__dict__.copy()
         UpperCAmelCase__								=  None
         return state


   def __setstate__(self					:						int	,  __UpperCAmelCase					:						Optional[Any]		)						->    int:
         """simple docstring"""



         UpperCAmelCase__								=  d

         # for backward compatibility
         if not hasattr(self	,  "sp_model_kwargs"		):
               UpperCAmelCase__								=  {}

         UpperCAmelCase__								=  spm.SentencePieceProcessor(**self.sp_model_kwargs		)
         self.sp_model.Load(self.vocab_file		)





   def       lowercase_				(self					:						List[str]	,  __UpperCAmelCase					:						str	,  __UpperCAmelCase					:						Optional[str] = None		)						->    Tuple[str]:
         """simple docstring"""



         if not os.path.isdir(__UpperCAmelCase		):
               logger.error(f"""Vocabulary path ({save_directory}) should be a directory"""		)
               return
         UpperCAmelCase__								=  os.path.join(
             __UpperCAmelCase	,  (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"]		)

         if os.path.abspath(self.vocab_file		) != os.path.abspath(__UpperCAmelCase		) and os.path.isfile(self.vocab_file		):
               copyfile(self.vocab_file	,  __UpperCAmelCase		)
         elif not os.path.isfile(self.vocab_file		):
               with open(__UpperCAmelCase	,  "wb"		) as fi:
                     UpperCAmelCase__								=  self.sp_model.serialized_model_proto()
                     fi.write(__UpperCAmelCase		)

         return (out_vocab_file,)




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

import torch
from transformers import CLIPImageProcessor, CLIPTextModel, CLIPTokenizer

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    DiffusionPipeline,
    LMSDiscreteScheduler,
    PNDMScheduler,
    StableDiffusionPipeline,
    UNetaDConditionModel,
)
from diffusers.pipelines.stable_diffusion import StableDiffusionPipelineOutput
from diffusers.pipelines.stable_diffusion.safety_checker import StableDiffusionSafetyChecker


UpperCamelCase__    							= 'CompVis/stable-diffusion-v1-1'
UpperCamelCase__    							= 'CompVis/stable-diffusion-v1-2'
UpperCamelCase__    							= 'CompVis/stable-diffusion-v1-3'
UpperCamelCase__    							= 'CompVis/stable-diffusion-v1-4'





class 					A							(    UpperCAmelCase_					):


   def __init__(self					:						Union[str, Any]	,  __UpperCAmelCase					:						AutoencoderKL	,  __UpperCAmelCase					:						CLIPTextModel	,  __UpperCAmelCase					:						CLIPTokenizer	,  __UpperCAmelCase					:						UNetaDConditionModel	,  __UpperCAmelCase					:						Union[DDIMScheduler, PNDMScheduler, LMSDiscreteScheduler]	,  __UpperCAmelCase					:						StableDiffusionSafetyChecker	,  __UpperCAmelCase					:						CLIPImageProcessor	,  __UpperCAmelCase					:						bool = True	,  )						->    Tuple:
         """simple docstring"""



         super()._init_()

         UpperCAmelCase__								=  StableDiffusionPipeline.from_pretrained(__UpperCAmelCase		)
         UpperCAmelCase__								=  StableDiffusionPipeline.from_pretrained(__UpperCAmelCase		)
         UpperCAmelCase__								=  StableDiffusionPipeline.from_pretrained(__UpperCAmelCase		)
         UpperCAmelCase__								=  StableDiffusionPipeline(
             vae=__UpperCAmelCase	,  text_encoder=__UpperCAmelCase	,  tokenizer=__UpperCAmelCase	,  unet=__UpperCAmelCase	,  scheduler=__UpperCAmelCase	,  safety_checker=__UpperCAmelCase	,  feature_extractor=__UpperCAmelCase	,  requires_safety_checker=__UpperCAmelCase	,  )

         self.register_modules(pipelinea=self.pipea	,  pipelinea=self.pipea	,  pipelinea=self.pipea	,  pipelinea=self.pipea		)


   @property
   def       lowercase_				(self					:						int		)						->    Dict[str, Any]:
         """simple docstring"""



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


   def       lowercase_				(self					:						Union[str, Any]	,  __UpperCAmelCase					:						Optional[Union[str, int]] = "auto"		)						->    Optional[int]:
         """simple docstring"""



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


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



         self.enable_attention_slicing(__UpperCAmelCase		)


   @torch.no_grad()
   def       lowercase_				(self					:						Optional[int]	,  __UpperCAmelCase					:						Union[str, List[str]]	,  __UpperCAmelCase					:						int = 5_1_2	,  __UpperCAmelCase					:						int = 5_1_2	,  __UpperCAmelCase					:						int = 5_0	,  __UpperCAmelCase					:						float = 7.5	,  __UpperCAmelCase					:						Optional[Union[str, List[str]]] = None	,  __UpperCAmelCase					:						Optional[int] = 1	,  __UpperCAmelCase					:						float = 0.0	,  __UpperCAmelCase					:						Optional[torch.Generator] = None	,  __UpperCAmelCase					:						Optional[torch.FloatTensor] = None	,  __UpperCAmelCase					:						Optional[str] = "pil"	,  __UpperCAmelCase					:						bool = True	,  __UpperCAmelCase					:						Optional[Callable[[int, int, torch.FloatTensor], None]] = None	,  __UpperCAmelCase					:						int = 1	,  **__UpperCAmelCase					:						Any	,  )						->    Dict:
         """simple docstring"""



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


   @torch.no_grad()
   def       lowercase_				(self					:						List[str]	,  __UpperCAmelCase					:						Union[str, List[str]]	,  __UpperCAmelCase					:						int = 5_1_2	,  __UpperCAmelCase					:						int = 5_1_2	,  __UpperCAmelCase					:						int = 5_0	,  __UpperCAmelCase					:						float = 7.5	,  __UpperCAmelCase					:						Optional[Union[str, List[str]]] = None	,  __UpperCAmelCase					:						Optional[int] = 1	,  __UpperCAmelCase					:						float = 0.0	,  __UpperCAmelCase					:						Optional[torch.Generator] = None	,  __UpperCAmelCase					:						Optional[torch.FloatTensor] = None	,  __UpperCAmelCase					:						Optional[str] = "pil"	,  __UpperCAmelCase					:						bool = True	,  __UpperCAmelCase					:						Optional[Callable[[int, int, torch.FloatTensor], None]] = None	,  __UpperCAmelCase					:						int = 1	,  **__UpperCAmelCase					:						Optional[Any]	,  )						->    Any:
         """simple docstring"""



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


   @torch.no_grad()
   def       lowercase_				(self					:						List[str]	,  __UpperCAmelCase					:						Union[str, List[str]]	,  __UpperCAmelCase					:						int = 5_1_2	,  __UpperCAmelCase					:						int = 5_1_2	,  __UpperCAmelCase					:						int = 5_0	,  __UpperCAmelCase					:						float = 7.5	,  __UpperCAmelCase					:						Optional[Union[str, List[str]]] = None	,  __UpperCAmelCase					:						Optional[int] = 1	,  __UpperCAmelCase					:						float = 0.0	,  __UpperCAmelCase					:						Optional[torch.Generator] = None	,  __UpperCAmelCase					:						Optional[torch.FloatTensor] = None	,  __UpperCAmelCase					:						Optional[str] = "pil"	,  __UpperCAmelCase					:						bool = True	,  __UpperCAmelCase					:						Optional[Callable[[int, int, torch.FloatTensor], None]] = None	,  __UpperCAmelCase					:						int = 1	,  **__UpperCAmelCase					:						Any	,  )						->    Dict:
         """simple docstring"""



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


   @torch.no_grad()
   def       lowercase_				(self					:						Union[str, Any]	,  __UpperCAmelCase					:						Union[str, List[str]]	,  __UpperCAmelCase					:						int = 5_1_2	,  __UpperCAmelCase					:						int = 5_1_2	,  __UpperCAmelCase					:						int = 5_0	,  __UpperCAmelCase					:						float = 7.5	,  __UpperCAmelCase					:						Optional[Union[str, List[str]]] = None	,  __UpperCAmelCase					:						Optional[int] = 1	,  __UpperCAmelCase					:						float = 0.0	,  __UpperCAmelCase					:						Optional[torch.Generator] = None	,  __UpperCAmelCase					:						Optional[torch.FloatTensor] = None	,  __UpperCAmelCase					:						Optional[str] = "pil"	,  __UpperCAmelCase					:						bool = True	,  __UpperCAmelCase					:						Optional[Callable[[int, int, torch.FloatTensor], None]] = None	,  __UpperCAmelCase					:						int = 1	,  **__UpperCAmelCase					:						Union[str, Any]	,  )						->    List[str]:
         """simple docstring"""



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





   @torch.no_grad()
   def       lowercase_				(self					:						int	,  __UpperCAmelCase					:						Union[str, List[str]]	,  __UpperCAmelCase					:						int = 5_1_2	,  __UpperCAmelCase					:						int = 5_1_2	,  __UpperCAmelCase					:						int = 5_0	,  __UpperCAmelCase					:						float = 7.5	,  __UpperCAmelCase					:						Optional[Union[str, List[str]]] = None	,  __UpperCAmelCase					:						Optional[int] = 1	,  __UpperCAmelCase					:						float = 0.0	,  __UpperCAmelCase					:						Optional[torch.Generator] = None	,  __UpperCAmelCase					:						Optional[torch.FloatTensor] = None	,  __UpperCAmelCase					:						Optional[str] = "pil"	,  __UpperCAmelCase					:						bool = True	,  __UpperCAmelCase					:						Optional[Callable[[int, int, torch.FloatTensor], None]] = None	,  __UpperCAmelCase					:						int = 1	,  **__UpperCAmelCase					:						Any	,  )						->    Union[str, Any]:
         """simple docstring"""



         UpperCAmelCase__								=  "cuda" if torch.cuda.is_available() else "cpu"
         self.to(__UpperCAmelCase		)

         # Checks if the height and width are divisible by 8 or not
         if height % 8 != 0 or width % 8 != 0:
               raise ValueError(f"""`height` and `width` must be divisible by 8 but are {height} and {width}."""		)

         # Get first result from Stable Diffusion Checkpoint v1.1
         UpperCAmelCase__								=  self.textaimg_sda_a(
             prompt=__UpperCAmelCase	,  height=__UpperCAmelCase	,  width=__UpperCAmelCase	,  num_inference_steps=__UpperCAmelCase	,  guidance_scale=__UpperCAmelCase	,  negative_prompt=__UpperCAmelCase	,  num_images_per_prompt=__UpperCAmelCase	,  eta=__UpperCAmelCase	,  generator=__UpperCAmelCase	,  latents=__UpperCAmelCase	,  output_type=__UpperCAmelCase	,  return_dict=__UpperCAmelCase	,  callback=__UpperCAmelCase	,  callback_steps=__UpperCAmelCase	,  **__UpperCAmelCase	,  )

         # Get first result from Stable Diffusion Checkpoint v1.2
         UpperCAmelCase__								=  self.textaimg_sda_a(
             prompt=__UpperCAmelCase	,  height=__UpperCAmelCase	,  width=__UpperCAmelCase	,  num_inference_steps=__UpperCAmelCase	,  guidance_scale=__UpperCAmelCase	,  negative_prompt=__UpperCAmelCase	,  num_images_per_prompt=__UpperCAmelCase	,  eta=__UpperCAmelCase	,  generator=__UpperCAmelCase	,  latents=__UpperCAmelCase	,  output_type=__UpperCAmelCase	,  return_dict=__UpperCAmelCase	,  callback=__UpperCAmelCase	,  callback_steps=__UpperCAmelCase	,  **__UpperCAmelCase	,  )

         # Get first result from Stable Diffusion Checkpoint v1.3
         UpperCAmelCase__								=  self.textaimg_sda_a(
             prompt=__UpperCAmelCase	,  height=__UpperCAmelCase	,  width=__UpperCAmelCase	,  num_inference_steps=__UpperCAmelCase	,  guidance_scale=__UpperCAmelCase	,  negative_prompt=__UpperCAmelCase	,  num_images_per_prompt=__UpperCAmelCase	,  eta=__UpperCAmelCase	,  generator=__UpperCAmelCase	,  latents=__UpperCAmelCase	,  output_type=__UpperCAmelCase	,  return_dict=__UpperCAmelCase	,  callback=__UpperCAmelCase	,  callback_steps=__UpperCAmelCase	,  **__UpperCAmelCase	,  )

         # Get first result from Stable Diffusion Checkpoint v1.4
         UpperCAmelCase__								=  self.textaimg_sda_a(
             prompt=__UpperCAmelCase	,  height=__UpperCAmelCase	,  width=__UpperCAmelCase	,  num_inference_steps=__UpperCAmelCase	,  guidance_scale=__UpperCAmelCase	,  negative_prompt=__UpperCAmelCase	,  num_images_per_prompt=__UpperCAmelCase	,  eta=__UpperCAmelCase	,  generator=__UpperCAmelCase	,  latents=__UpperCAmelCase	,  output_type=__UpperCAmelCase	,  return_dict=__UpperCAmelCase	,  callback=__UpperCAmelCase	,  callback_steps=__UpperCAmelCase	,  **__UpperCAmelCase	,  )

         # Get all result images into a single list and pass it via StableDiffusionPipelineOutput for final result
         return StableDiffusionPipelineOutput([resa[0], resa[0], resa[0], resa[0]]		)



