'''simple docstring'''
a=  '''0.18.2'''

from .configuration_utils import ConfigMixin
from .utils import (
    OptionalDependencyNotAvailable,
    is_flax_available,
    is_inflect_available,
    is_invisible_watermark_available,
    is_k_diffusion_available,
    is_k_diffusion_version,
    is_librosa_available,
    is_note_seq_available,
    is_onnx_available,
    is_scipy_available,
    is_torch_available,
    is_torchsde_available,
    is_transformers_available,
    is_transformers_version,
    is_unidecode_available,
    logging,
)


try:
  if not is_onnx_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  from .utils.dummy_onnx_objects import *  # noqa F403
else:
  from .pipelines import OnnxRuntimeModel

try:
  if not is_torch_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  from .utils.dummy_pt_objects import *  # noqa F403
else:
  from .models import (
      AutoencoderKL,
      ControlNetModel,
      ModelMixin,
      PriorTransformer,
      TaFilmDecoder,
      TransformeraDModel,
      UNetaDModel,
      UNetaDConditionModel,
      UNetaDModel,
      UNetaDConditionModel,
      VQModel,
  )
  from .optimization import (
      get_constant_schedule,
      get_constant_schedule_with_warmup,
      get_cosine_schedule_with_warmup,
      get_cosine_with_hard_restarts_schedule_with_warmup,
      get_linear_schedule_with_warmup,
      get_polynomial_decay_schedule_with_warmup,
      get_scheduler,
  )
  from .pipelines import (
      AudioPipelineOutput,
      ConsistencyModelPipeline,
      DanceDiffusionPipeline,
      DDIMPipeline,
      DDPMPipeline,
      DiffusionPipeline,
      DiTPipeline,
      ImagePipelineOutput,
      KarrasVePipeline,
      LDMPipeline,
      LDMSuperResolutionPipeline,
      PNDMPipeline,
      RePaintPipeline,
      ScoreSdeVePipeline,
  )
  from .schedulers import (
      CMStochasticIterativeScheduler,
      DDIMInverseScheduler,
      DDIMParallelScheduler,
      DDIMScheduler,
      DDPMParallelScheduler,
      DDPMScheduler,
      DEISMultistepScheduler,
      DPMSolverMultistepInverseScheduler,
      DPMSolverMultistepScheduler,
      DPMSolverSinglestepScheduler,
      EulerAncestralDiscreteScheduler,
      EulerDiscreteScheduler,
      HeunDiscreteScheduler,
      IPNDMScheduler,
      KarrasVeScheduler,
      KDPMaAncestralDiscreteScheduler,
      KDPMaDiscreteScheduler,
      PNDMScheduler,
      RePaintScheduler,
      SchedulerMixin,
      ScoreSdeVeScheduler,
      UnCLIPScheduler,
      UniPCMultistepScheduler,
      VQDiffusionScheduler,
  )
  from .training_utils import EMAModel

try:
  if not (is_torch_available() and is_scipy_available()):
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  from .utils.dummy_torch_and_scipy_objects import *  # noqa F403
else:
  from .schedulers import LMSDiscreteScheduler

try:
  if not (is_torch_available() and is_torchsde_available()):
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  from .utils.dummy_torch_and_torchsde_objects import *  # noqa F403
else:
  from .schedulers import DPMSolverSDEScheduler

try:
  if not (is_torch_available() and is_transformers_available()):
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  from .utils.dummy_torch_and_transformers_objects import *  # noqa F403
else:
  from .pipelines import (
      AltDiffusionImgaImgPipeline,
      AltDiffusionPipeline,
      AudioLDMPipeline,
      CycleDiffusionPipeline,
      IFImgaImgPipeline,
      IFImgaImgSuperResolutionPipeline,
      IFInpaintingPipeline,
      IFInpaintingSuperResolutionPipeline,
      IFPipeline,
      IFSuperResolutionPipeline,
      ImageTextPipelineOutput,
      KandinskyImgaImgPipeline,
      KandinskyInpaintPipeline,
      KandinskyPipeline,
      KandinskyPriorPipeline,
      KandinskyVaaControlnetImgaImgPipeline,
      KandinskyVaaControlnetPipeline,
      KandinskyVaaImgaImgPipeline,
      KandinskyVaaInpaintPipeline,
      KandinskyVaaPipeline,
      KandinskyVaaPriorEmbaEmbPipeline,
      KandinskyVaaPriorPipeline,
      LDMTextToImagePipeline,
      PaintByExamplePipeline,
      SemanticStableDiffusionPipeline,
      ShapEImgaImgPipeline,
      ShapEPipeline,
      StableDiffusionAttendAndExcitePipeline,
      StableDiffusionControlNetImgaImgPipeline,
      StableDiffusionControlNetInpaintPipeline,
      StableDiffusionControlNetPipeline,
      StableDiffusionDepthaImgPipeline,
      StableDiffusionDiffEditPipeline,
      StableDiffusionImageVariationPipeline,
      StableDiffusionImgaImgPipeline,
      StableDiffusionInpaintPipeline,
      StableDiffusionInpaintPipelineLegacy,
      StableDiffusionInstructPixaPixPipeline,
      StableDiffusionLatentUpscalePipeline,
      StableDiffusionLDMaDPipeline,
      StableDiffusionModelEditingPipeline,
      StableDiffusionPanoramaPipeline,
      StableDiffusionParadigmsPipeline,
      StableDiffusionPipeline,
      StableDiffusionPipelineSafe,
      StableDiffusionPixaPixZeroPipeline,
      StableDiffusionSAGPipeline,
      StableDiffusionUpscalePipeline,
      StableUnCLIPImgaImgPipeline,
      StableUnCLIPPipeline,
      TextToVideoSDPipeline,
      TextToVideoZeroPipeline,
      UnCLIPImageVariationPipeline,
      UnCLIPPipeline,
      UniDiffuserModel,
      UniDiffuserPipeline,
      UniDiffuserTextDecoder,
      VersatileDiffusionDualGuidedPipeline,
      VersatileDiffusionImageVariationPipeline,
      VersatileDiffusionPipeline,
      VersatileDiffusionTextToImagePipeline,
      VideoToVideoSDPipeline,
      VQDiffusionPipeline,
  )

try:
  if not (is_torch_available() and is_transformers_available() and is_invisible_watermark_available()):
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  from .utils.dummy_torch_and_transformers_and_invisible_watermark_objects import *  # noqa F403
else:
  from .pipelines import StableDiffusionXLImgaImgPipeline, StableDiffusionXLPipeline

try:
  if not (is_torch_available() and is_transformers_available() and is_k_diffusion_available()):
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  from .utils.dummy_torch_and_transformers_and_k_diffusion_objects import *  # noqa F403
else:
  from .pipelines import StableDiffusionKDiffusionPipeline

try:
  if not (is_torch_available() and is_transformers_available() and is_onnx_available()):
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  from .utils.dummy_torch_and_transformers_and_onnx_objects import *  # noqa F403
else:
  from .pipelines import (
      OnnxStableDiffusionImgaImgPipeline,
      OnnxStableDiffusionInpaintPipeline,
      OnnxStableDiffusionInpaintPipelineLegacy,
      OnnxStableDiffusionPipeline,
      OnnxStableDiffusionUpscalePipeline,
      StableDiffusionOnnxPipeline,
  )

try:
  if not (is_torch_available() and is_librosa_available()):
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  from .utils.dummy_torch_and_librosa_objects import *  # noqa F403
else:
  from .pipelines import AudioDiffusionPipeline, Mel

try:
  if not (is_transformers_available() and is_torch_available() and is_note_seq_available()):
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  from .utils.dummy_transformers_and_torch_and_note_seq_objects import *  # noqa F403
else:
  from .pipelines import SpectrogramDiffusionPipeline

try:
  if not is_flax_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  from .utils.dummy_flax_objects import *  # noqa F403
else:
  from .models.controlnet_flax import FlaxControlNetModel
  from .models.modeling_flax_utils import FlaxModelMixin
  from .models.unet_ad_condition_flax import FlaxUNetaDConditionModel
  from .models.vae_flax import FlaxAutoencoderKL
  from .pipelines import FlaxDiffusionPipeline
  from .schedulers import (
      FlaxDDIMScheduler,
      FlaxDDPMScheduler,
      FlaxDPMSolverMultistepScheduler,
      FlaxKarrasVeScheduler,
      FlaxLMSDiscreteScheduler,
      FlaxPNDMScheduler,
      FlaxSchedulerMixin,
      FlaxScoreSdeVeScheduler,
  )


try:
  if not (is_flax_available() and is_transformers_available()):
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  from .utils.dummy_flax_and_transformers_objects import *  # noqa F403
else:
  from .pipelines import (
      FlaxStableDiffusionControlNetPipeline,
      FlaxStableDiffusionImgaImgPipeline,
      FlaxStableDiffusionInpaintPipeline,
      FlaxStableDiffusionPipeline,
  )

try:
  if not (is_note_seq_available()):
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  from .utils.dummy_note_seq_objects import *  # noqa F403
else:
  from .pipelines import MidiProcessor




'''simple docstring'''
import json
import os
from pathlib import Path
from shutil import copyfile
from typing import Any, Dict, List, Optional, Tuple, Union

import sentencepiece

from ...tokenization_utils import PreTrainedTokenizer
from ...utils import logging


a=  logging.get_logger(__name__)

a=  '''▁'''

a=  {
    '''vocab_file''': '''vocab.json''',
    '''spm_file''': '''sentencepiece.bpe.model''',
}

a=  {
    '''vocab_file''': {
        '''facebook/s2t-small-librispeech-asr''': (
            '''https://huggingface.co/facebook/s2t-small-librispeech-asr/resolve/main/vocab.json'''
        ),
    },
    '''spm_file''': {
        '''facebook/s2t-small-librispeech-asr''': (
            '''https://huggingface.co/facebook/s2t-small-librispeech-asr/resolve/main/sentencepiece.bpe.model'''
        )
    },
}

a=  {
    '''facebook/s2t-small-librispeech-asr''': 1_0_2_4,
}

a=  ['''pt''', '''fr''', '''ru''', '''nl''', '''ro''', '''it''', '''es''', '''de''']

a=  {'''mustc''': MUSTC_LANGS}






class 			__lowercase		(						_lowerCamelCase						):







     """simple docstring"""






     SCREAMING_SNAKE_CASE__		   =						VOCAB_FILES_NAMES
     SCREAMING_SNAKE_CASE__		   =						PRETRAINED_VOCAB_FILES_MAP
     SCREAMING_SNAKE_CASE__		   =						MAX_MODEL_INPUT_SIZES
     SCREAMING_SNAKE_CASE__		   =						['''input_ids''', '''attention_mask''']

     SCREAMING_SNAKE_CASE__		   =						[]







     def __init__(		self     ,			_lowerCamelCase     ,			_lowerCamelCase     ,			_lowerCamelCase="<s>"     ,			_lowerCamelCase="</s>"     ,			_lowerCamelCase="<pad>"     ,			_lowerCamelCase="<unk>"     ,			_lowerCamelCase=False     ,			_lowerCamelCase=False     ,			_lowerCamelCase=None     ,			_lowerCamelCase=None     ,			_lowerCamelCase = None     ,			**_lowerCamelCase     ,			):
         __UpperCamelCase	:	List[Any]		    =  {} if sp_model_kwargs is None else sp_model_kwargs

         super().__init__(
             bos_token=_lowerCamelCase     ,			eos_token=_lowerCamelCase     ,			unk_token=_lowerCamelCase     ,			pad_token=_lowerCamelCase     ,			do_upper_case=_lowerCamelCase     ,			do_lower_case=_lowerCamelCase     ,			tgt_lang=_lowerCamelCase     ,			lang_codes=_lowerCamelCase     ,			sp_model_kwargs=self.sp_model_kwargs     ,			**_lowerCamelCase     ,			)
         __UpperCamelCase	:	Union[str, Any]		    =  do_upper_case
         __UpperCamelCase	:	Dict		    =  do_lower_case

         __UpperCamelCase	:	List[str]		    =  load_json(_lowerCamelCase    )
         __UpperCamelCase	:	List[Any]		    =  {v: k for k, v in self.encoder.items()}
         __UpperCamelCase	:	int		    =  spm_file
         __UpperCamelCase	:	List[Any]		    =  load_spm(_lowerCamelCase     ,			self.sp_model_kwargs    )

         if lang_codes is not None:
             __UpperCamelCase	:	Any		    =  lang_codes
             __UpperCamelCase	:	Any		    =  LANGUAGES[lang_codes]
             __UpperCamelCase	:	str		    =  [f"""<lang:{lang}>""" for lang in self.langs]
             __UpperCamelCase	:	List[str]		    =  {lang: self.sp_model.PieceToId(f"""<lang:{lang}>"""    ) for lang in self.langs}

             __UpperCamelCase	:	str		    =  self.lang_tokens
             __UpperCamelCase	:	str		    =  tgt_lang if tgt_lang is not None else self.langs[0]

             self.set_tgt_lang_special_tokens(self._tgt_lang    )
         else:
             __UpperCamelCase	:	Dict		    =  {}







     @property
     def lowerCAmelCase     (		self    ):
         return len(self.encoder    )







     @property
     def lowerCAmelCase     (		self    ):
         return self._tgt_lang







     @tgt_lang.setter
     def lowerCAmelCase     (		self     ,			_lowerCamelCase    ):
         __UpperCamelCase	:	Optional[int]		    =  new_tgt_lang
         self.set_tgt_lang_special_tokens(_lowerCamelCase    )







     def lowerCAmelCase     (		self     ,			_lowerCamelCase    ):
         __UpperCamelCase	:	int		    =  self.lang_code_to_id[tgt_lang]
         __UpperCamelCase	:	List[str]		    =  [lang_code_id]







     def lowerCAmelCase     (		self     ,			_lowerCamelCase    ):
         return self.sp_model.encode(_lowerCamelCase     ,			out_type=_lowerCamelCase    )







     def lowerCAmelCase     (		self     ,			_lowerCamelCase    ):
         return self.encoder.get(_lowerCamelCase     ,			self.encoder[self.unk_token]    )







     def lowerCAmelCase     (		self     ,			_lowerCamelCase    ):
         return self.decoder.get(_lowerCamelCase     ,			self.unk_token    )







     def lowerCAmelCase     (		self     ,			_lowerCamelCase    ):
         __UpperCamelCase	:	Optional[Any]		    =  []
         __UpperCamelCase	:	List[str]		    =  ''
         for token in tokens:
             # make sure that special tokens are not decoded using sentencepiece model
             if token in self.all_special_tokens:
                 __UpperCamelCase	:	int		    =  self.sp_model.decode(_lowerCamelCase    )
                 out_string += (decoded.upper() if self.do_upper_case else decoded) + token + " "
                 __UpperCamelCase	:	int		    =  []
             else:
                 current_sub_tokens.append(_lowerCamelCase    )
         __UpperCamelCase	:	str		    =  self.sp_model.decode(_lowerCamelCase    )
         out_string += decoded.upper() if self.do_upper_case else decoded
         return out_string.strip()







     def lowerCAmelCase     (		self     ,			_lowerCamelCase     ,			_lowerCamelCase=None    ):
         if token_ids_a is None:
             return self.prefix_tokens + token_ids_a + [self.eos_token_id]
         # We don't expect to process pairs, but leave the pair logic for API consistency
         return self.prefix_tokens + token_ids_a + token_ids_a + [self.eos_token_id]







     def lowerCAmelCase     (		self     ,			_lowerCamelCase     ,			_lowerCamelCase = None     ,			_lowerCamelCase = False    ):

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

         __UpperCamelCase	:	Union[str, Any]		    =  [1] * len(self.prefix_tokens    )
         __UpperCamelCase	:	Any		    =  [1]
         if token_ids_a is None:
             return prefix_ones + ([0] * len(_lowerCamelCase    )) + suffix_ones
         return prefix_ones + ([0] * len(_lowerCamelCase    )) + ([0] * len(_lowerCamelCase    )) + suffix_ones







     def lowerCAmelCase     (		self    ):
         __UpperCamelCase	:	Union[str, Any]		    =  self.encoder.copy()
         vocab.update(self.added_tokens_encoder    )
         return vocab







     def __getstate__(		self    ):
         __UpperCamelCase	:	int		    =  self.__dict__.copy()
         __UpperCamelCase	:	Dict		    =  None
         return state







     def __setstate__(		self     ,			_lowerCamelCase    ):
         __UpperCamelCase	:	List[str]		    =  d

         # for backward compatibility
         if not hasattr(self     ,			'sp_model_kwargs'    ):
             __UpperCamelCase	:	Optional[int]		    =  {}

         __UpperCamelCase	:	Union[str, Any]		    =  load_spm(self.spm_file     ,			self.sp_model_kwargs    )







     def lowerCAmelCase     (		self     ,			_lowerCamelCase     ,			_lowerCamelCase = None    ):
         __UpperCamelCase	:	List[str]		    =  Path(_lowerCamelCase    )
         assert save_dir.is_dir(), f"""{save_directory} should be a directory"""
         __UpperCamelCase	:	Optional[int]		    =  save_dir / (
             (filename_prefix + '-' if filename_prefix else '') + self.vocab_files_names['vocab_file']
         )
         __UpperCamelCase	:	Union[str, Any]		    =  save_dir / (
             (filename_prefix + '-' if filename_prefix else '') + self.vocab_files_names['spm_file']
         )

         save_json(self.encoder     ,			_lowerCamelCase    )

         if os.path.abspath(self.spm_file    ) != os.path.abspath(_lowerCamelCase    ) and os.path.isfile(self.spm_file    ):
             copyfile(self.spm_file     ,			_lowerCamelCase    )
         elif not os.path.isfile(self.spm_file    ):
             with open(_lowerCamelCase     ,			'wb'    ) as fi:
                 __UpperCamelCase	:	Dict		    =  self.sp_model.serialized_model_proto()
                 fi.write(_lowerCamelCase    )

         return (str(_lowerCamelCase    ), str(_lowerCamelCase    ))







def  _UpperCamelCase (     _a	:	str				,     _a	:	Dict[str, Any]			):




    """simple docstring"""



    __UpperCamelCase	:	List[Any]		    =  sentencepiece.SentencePieceProcessor(**_a			)
    spm.Load(str(_a			)			)
    return spm







def  _UpperCamelCase (     _a	:	str			):




    """simple docstring"""



    with open(_a				,     'r'			) as f:
        return json.load(_a			)







def  _UpperCamelCase (     _a	:	Any				,     _a	:	str			):




    """simple docstring"""



    with open(_a				,     'w'			) as f:
        json.dump(_a				,     _a				,     indent=2			)



