


"""simple docstring"""



import warnings
from contextlib import contextmanager

from ...processing_utils import ProcessorMixin





class _lowerCAmelCase    (				snake_case_     ):
   __UpperCAmelCase			:	Dict							  =  '''Speech2TextFeatureExtractor'''
   __UpperCAmelCase			:	Optional[Any]							  =  '''Speech2TextTokenizer'''







   def __init__(     self						,     UpperCamelCase__						,     UpperCamelCase__						)     ->		Tuple:

       '''simple docstring'''




       super().__init__(UpperCamelCase__						,     UpperCamelCase__						)
       snake_case   :				List[str]				 =   self.feature_extractor
       snake_case   :				str				 =   False







   def __call__(     self						,     *UpperCamelCase__						,     **UpperCamelCase__						)     ->		int:

       '''simple docstring'''




       if self._in_target_context_manager:
           return self.current_processor(*UpperCamelCase__						,     **UpperCamelCase__						)

       if "raw_speech" in kwargs:
           warnings.warn("Using `raw_speech` as a keyword argument is deprecated. Use `audio` instead."						)
           snake_case   :				List[str]				 =   kwargs.pop("raw_speech"						)
       else:
           snake_case   :				Any				 =   kwargs.pop("audio"						,     UpperCamelCase__						)
       snake_case   :				str				 =   kwargs.pop("sampling_rate"						,     UpperCamelCase__						)
       snake_case   :				Union[str, Any]				 =   kwargs.pop("text"						,     UpperCamelCase__						)
       if len(UpperCamelCase__						) > 0:
           snake_case   :				str				 =   args[0]
           snake_case   :				List[str]				 =   args[1:]

       if audio is None and text is None:
           raise ValueError("You need to specify either an `audio` or `text` input to process."						)

       if audio is not None:
           snake_case   :				str				 =   self.feature_extractor(UpperCamelCase__						,     *UpperCamelCase__						,     sampling_rate=UpperCamelCase__						,     **UpperCamelCase__						)
       if text is not None:
           snake_case   :				Tuple				 =   self.tokenizer(UpperCamelCase__						,     **UpperCamelCase__						)

       if text is None:
           return inputs
       elif audio is None:
           return encodings
       else:
           snake_case   :				List[Any]				 =   encodings["input_ids"]
           return inputs







   def 			lowerCamelCase		(     self						,     *UpperCamelCase__						,     **UpperCamelCase__						)     ->		Tuple:

       '''simple docstring'''




       return self.tokenizer.batch_decode(*UpperCamelCase__						,     **UpperCamelCase__						)







   def 			lowerCamelCase		(     self						,     *UpperCamelCase__						,     **UpperCamelCase__						)     ->		Any:

       '''simple docstring'''




       return self.tokenizer.decode(*UpperCamelCase__						,     **UpperCamelCase__						)







   @contextmanager
   def 			lowerCamelCase		(     self						)     ->		List[str]:

       '''simple docstring'''




       warnings.warn(
           "`as_target_processor` is deprecated and will be removed in v5 of Transformers. You can process your "
           "labels by using the argument `text` of the regular `__call__` method (either in the same call as "
           "your audio inputs, or in a separate call."						)
       snake_case   :				Optional[int]				 =   True
       snake_case   :				List[Any]				 =   self.tokenizer
       yield
       snake_case   :				int				 =   self.feature_extractor
       snake_case   :				Dict				 =   False









"""simple docstring"""



import unittest

import numpy as np
import torch

from diffusers import KarrasVePipeline, KarrasVeScheduler, UNetaDModel
from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device


enable_full_determinism()







class _lowerCAmelCase    (				unittest.TestCase     ):







   @property
   def 			lowerCamelCase		(     self						)     ->		str:

       '''simple docstring'''




       torch.manual_seed(0						)
       snake_case   :				int				 =   UNetaDModel(
           block_out_channels=(32, 64)						,     layers_per_block=2						,     sample_size=32						,     in_channels=3						,     out_channels=3						,     down_block_types=("DownBlock2D", "AttnDownBlock2D")						,     up_block_types=("AttnUpBlock2D", "UpBlock2D")						,     )
       return model







   def 			lowerCamelCase		(     self						)     ->		Any:

       '''simple docstring'''




       snake_case   :				Any				 =   self.dummy_uncond_unet
       snake_case   :				Tuple				 =   KarrasVeScheduler()

       snake_case   :				int				 =   KarrasVePipeline(unet=UpperCamelCase__						,     scheduler=UpperCamelCase__						)
       pipe.to(UpperCamelCase__						)
       pipe.set_progress_bar_config(disable=UpperCamelCase__						)

       snake_case   :				Optional[Any]				 =   torch.manual_seed(0						)
       snake_case   :				List[Any]				 =   pipe(num_inference_steps=2						,     generator=UpperCamelCase__						,     output_type="numpy"						).images

       snake_case   :				Dict				 =   torch.manual_seed(0						)
       snake_case   :				Dict				 =   pipe(num_inference_steps=2						,     generator=UpperCamelCase__						,     output_type="numpy"						,     return_dict=UpperCamelCase__						)[0]

       snake_case   :				Tuple				 =   image[0, -3:, -3:, -1]
       snake_case   :				Tuple				 =   image_from_tuple[0, -3:, -3:, -1]

       assert image.shape == (1, 32, 32, 3)
       snake_case   :				int				 =   np.array([0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0]						)

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







@slow
@require_torch
class _lowerCAmelCase    (				unittest.TestCase     ):







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

       '''simple docstring'''




       snake_case   :				Optional[Any]				 =   "google/ncsnpp-celebahq-256"
       snake_case   :				List[str]				 =   UNetaDModel.from_pretrained(UpperCamelCase__						)
       snake_case   :				Optional[Any]				 =   KarrasVeScheduler()

       snake_case   :				Optional[int]				 =   KarrasVePipeline(unet=UpperCamelCase__						,     scheduler=UpperCamelCase__						)
       pipe.to(UpperCamelCase__						)
       pipe.set_progress_bar_config(disable=UpperCamelCase__						)

       snake_case   :				Dict				 =   torch.manual_seed(0						)
       snake_case   :				Union[str, Any]				 =   pipe(num_inference_steps=20						,     generator=UpperCamelCase__						,     output_type="numpy"						).images

       snake_case   :				int				 =   image[0, -3:, -3:, -1]
       assert image.shape == (1, 256, 256, 3)
       snake_case   :				Any				 =   np.array([0.578, 0.5811, 0.5924, 0.5809, 0.587, 0.5886, 0.5861, 0.5802, 0.586]						)

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





