


import warnings

from transformers import AutoTokenizer
from transformers.utils import is_torch_available
from transformers.utils.generic import ExplicitEnum

from ...processing_utils import ProcessorMixin


if is_torch_available():
   import torch






class     UpperCAmelCase__       (					A_		):



    """simple docstring"""






    UpperCAmelCase__						:		Optional[int]      =  "char"
    UpperCAmelCase__						:		List[str]      =  "bpe"
    UpperCAmelCase__						:		Tuple      =  "wp"
_A		       =						(DecodeType.CHARACTER, DecodeType.BPE, DecodeType.WORDPIECE)






class     UpperCAmelCase__       (					A_		):



    """simple docstring"""






    UpperCAmelCase__						:		Optional[int]      =  ["image_processor", "char_tokenizer"]
    UpperCAmelCase__						:		str      =  "ViTImageProcessor"
    UpperCAmelCase__						:		Optional[Any]      =  "MgpstrTokenizer"






    def __init__(			self					,   A_=None					,   A_=None					,   **A_    )  ->       Union[str, Any]:
         __UpperCamelCase    							=None
         if "feature_extractor" in kwargs:
              warnings.warn(
                  'The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`'
                  ' instead.'					,   A_					,   )
              __UpperCamelCase    							=kwargs.pop('feature_extractor'    )

         __UpperCamelCase    							=image_processor if image_processor is not None else feature_extractor
         if image_processor is None:
              raise ValueError('You need to specify an `image_processor`.'    )
         if tokenizer is None:
              raise ValueError('You need to specify a `tokenizer`.'    )

         __UpperCamelCase    							=tokenizer
         __UpperCamelCase    							=AutoTokenizer.from_pretrained('gpt2'    )
         __UpperCamelCase    							=AutoTokenizer.from_pretrained('bert-base-uncased'    )

         super().__init__(A_					,   A_    )






    def __call__(			self					,   A_=None					,   A_=None					,   A_=None					,   **A_    )  ->       Any:
         if images is None and text is None:
              raise ValueError('You need to specify either an `images` or `text` input to process.'    )

         if images is not None:
              __UpperCamelCase    							=self.image_processor(A_					,   return_tensors=A_					,   **A_    )
         if text is not None:
              __UpperCamelCase    							=self.char_tokenizer(A_					,   return_tensors=A_					,   **A_    )

         if text is None:
              return inputs
         elif images is None:
              return encodings
         else:
              __UpperCamelCase    							=encodings['input_ids']
              return inputs






    def _a					(			self					,   A_    )  ->       int:
         __UpperCamelCase							,      __UpperCamelCase							,      __UpperCamelCase    							=sequences
         __UpperCamelCase    							=char_preds.size(0    )

         __UpperCamelCase							,      __UpperCamelCase    							=self._decode_helper(A_					,   'char'    )
         __UpperCamelCase							,      __UpperCamelCase    							=self._decode_helper(A_					,   'bpe'    )
         __UpperCamelCase							,      __UpperCamelCase    							=self._decode_helper(A_					,   'wp'    )

         __UpperCamelCase    							=[]
         __UpperCamelCase    							=[]
         for i in range(A_    ):
              __UpperCamelCase    							=[char_scores[i], bpe_scores[i], wp_scores[i]]
              __UpperCamelCase    							=[char_strs[i], bpe_strs[i], wp_strs[i]]
              __UpperCamelCase    							=scores.index(max(A_    )    )
              final_strs.append(strs[max_score_index]    )
              final_scores.append(scores[max_score_index]    )

         __UpperCamelCase    							={}
         __UpperCamelCase    							=final_strs
         __UpperCamelCase    							=final_scores
         __UpperCamelCase    							=char_strs
         __UpperCamelCase    							=bpe_strs
         __UpperCamelCase    							=wp_strs
         return out






    def _a					(			self					,   A_					,   A_    )  ->       str:
         if format == DecodeType.CHARACTER:
              __UpperCamelCase    							=self.char_decode
              __UpperCamelCase    							=1
              __UpperCamelCase    							='[s]'
         elif format == DecodeType.BPE:
              __UpperCamelCase    							=self.bpe_decode
              __UpperCamelCase    							=2
              __UpperCamelCase    							='#'
         elif format == DecodeType.WORDPIECE:
              __UpperCamelCase    							=self.wp_decode
              __UpperCamelCase    							=102
              __UpperCamelCase    							='[SEP]'
         else:
              raise ValueError(f'Format {format} is not supported.'    )

         __UpperCamelCase							,      __UpperCamelCase    							=[], []
         __UpperCamelCase    							=pred_logits.size(0    )
         __UpperCamelCase    							=pred_logits.size(1    )
         __UpperCamelCase							,      __UpperCamelCase    							=pred_logits.topk(1					,   dim=-1					,   largest=A_					,   sorted=A_    )
         __UpperCamelCase    							=preds_index.view(-1					,   A_    )[:, 1:]
         __UpperCamelCase    							=decoder(A_    )
         __UpperCamelCase							,      __UpperCamelCase    							=torch.nn.functional.softmax(A_					,   dim=2    ).max(dim=2    )
         __UpperCamelCase    							=preds_max_prob[:, 1:]

         for index in range(A_    ):
              __UpperCamelCase    							=preds_str[index].find(A_    )
              __UpperCamelCase    							=preds_str[index][:pred_eos]
              __UpperCamelCase    							=preds_index[index].cpu().tolist()
              __UpperCamelCase    							=pred_index.index(A_    ) if eos_token in pred_index else -1
              __UpperCamelCase    							=preds_max_prob[index][: pred_eos_index + 1]
              __UpperCamelCase    							=pred_max_prob.cumprod(dim=0    )[-1] if pred_max_prob.nelement() != 0 else 0.0
              dec_strs.append(A_    )
              conf_scores.append(A_    )

         return dec_strs, conf_scores






    def _a					(			self					,   A_    )  ->       int:
         __UpperCamelCase    							=[seq.replace(' '					,   ''    ) for seq in self.char_tokenizer.batch_decode(A_    )]
         return decode_strs






    def _a					(			self					,   A_    )  ->       str:
         return self.bpe_tokenizer.batch_decode(A_    )






    def _a					(			self					,   A_    )  ->       Optional[int]:
         __UpperCamelCase    							=[seq.replace(' '					,   ''    ) for seq in self.wp_tokenizer.batch_decode(A_    )]
         return decode_strs












'''simple docstring'''




__SCREAMING_SNAKE_CASE     :List[str]       =				'''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




