





from typing import List, Optional, Union

import numpy as np
import torch
import torchaudio.compliance.kaldi as ta_kaldi

from ...feature_extraction_sequence_utils import SequenceFeatureExtractor
from ...feature_extraction_utils import BatchFeature
from ...utils import PaddingStrategy, TensorType, logging


lowercase_					     =	logging.get_logger(__name__)
class        SCREAMING_SNAKE_CASE     (UpperCAmelCase   ):
   _UpperCamelCase					:	Optional[Any]				      = ['input_features', 'attention_mask']


   def __init__(					self    :       Tuple    ,  a    :       List[str]=80    ,  a    :       List[Any]=16_000    ,  a    :       Any=80    ,  a    :       str=0.0    ,  a    :       List[Any]=True    ,  a    :       List[str]=True    ,  a    :       int=True    ,  **a    :       str    ,  )->     int:





     """simple docstring"""







     super().__init__(feature_size=a    ,  sampling_rate=a    ,  padding_value=a    ,  **a )
     lowercase__			    =						num_mel_bins
     lowercase__			    =						do_ceptral_normalize
     lowercase__			    =						normalize_means
     lowercase__			    =						normalize_vars
     lowercase__			    =						True


   def  SCREAMING_SNAKE_CASE_							(					self    :       Optional[int]    ,  a    :       np.ndarray    ,  )->     np.ndarray:





     """simple docstring"""







     lowercase__			    =						waveform * (2**15)  # Kaldi compliance: 16-bit signed integers
     lowercase__			    =						torch.from_numpy(a ).unsqueeze(0 )
     lowercase__			    =						ta_kaldi.fbank(a    ,  num_mel_bins=self.num_mel_bins    ,  sample_frequency=self.sampling_rate )
     return features.numpy()


   @staticmethod
   def  SCREAMING_SNAKE_CASE_							(					a    :       np.ndarray    ,  a    :       int    ,  a    :       Optional[bool] = True    ,  a    :       Optional[bool] = True    ,  a    :       float = 0.0    ,  )->     np.ndarray:





     """simple docstring"""







     if normalize_means:
       lowercase__			    =						x[:input_length].mean(axis=0 )
       lowercase__			    =						np.subtract(a    ,  a )
     if normalize_vars:
       lowercase__			    =						x[:input_length].std(axis=0 )
       lowercase__			    =						np.divide(a    ,  a )

     if input_length < x.shape[0]:
       lowercase__			    =						padding_value

     # make sure array is in float32
     lowercase__			    =						x.astype(np.floataa )

     return x


   def  SCREAMING_SNAKE_CASE_							(					self    :       Optional[int]    ,  a    :       List[np.ndarray]    ,  a    :       Optional[np.ndarray] = None )->     List[np.ndarray]:





     """simple docstring"""







     lowercase__			    =						attention_mask.sum(-1 ) if attention_mask is not None else [x.shape[0] for x in input_features]
     return [
         self.utterance_cmvn(a    ,  a    ,  self.normalize_means    ,  self.normalize_vars    ,  self.padding_value )
         for x, n in zip(a    ,  a )
     ]


   def __call__(					self    :       Tuple    ,  a    :       Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]]    ,  a    :       Union[bool, str, PaddingStrategy] = False    ,  a    :       Optional[int] = None    ,  a    :       bool = False    ,  a    :       Optional[int] = None    ,  a    :       Optional[Union[str, TensorType]] = None    ,  a    :       Optional[int] = None    ,  a    :       Optional[bool] = None    ,  **a    :       List[Any]    ,  )->     BatchFeature:





     """simple docstring"""







     if sampling_rate is not None:
       if sampling_rate != self.sampling_rate:
         raise ValueError(
             f"""The model corresponding to this feature extractor: {self} was trained using a sampling rate of"""
             f""" {self.sampling_rate}. Please make sure that the provided `raw_speech` input was sampled with"""
             f""" {self.sampling_rate} and not {sampling_rate}.""" )
     else:
       logger.warning(
           'It is strongly recommended to pass the `sampling_rate` argument to this function. '
           'Failing to do so can result in silent errors that might be hard to debug.' )

     lowercase__			    =						isinstance(a    ,  np.ndarray ) and len(raw_speech.shape ) > 1
     if is_batched_numpy and len(raw_speech.shape ) > 2:
       raise ValueError(f"""Only mono-channel audio is supported for input to {self}""" )
     lowercase__			    =						is_batched_numpy or (
         isinstance(a    ,  (list, tuple) ) and (isinstance(raw_speech[0]    ,  (np.ndarray, tuple, list) ))
     )

     if is_batched:
       lowercase__			    =						[np.asarray(a    ,  dtype=np.floataa ) for speech in raw_speech]
     elif not is_batched and not isinstance(a    ,  np.ndarray ):
       lowercase__			    =						np.asarray(a    ,  dtype=np.floataa )
     elif isinstance(a    ,  np.ndarray ) and raw_speech.dtype is np.dtype(np.floataa ):
       lowercase__			    =						raw_speech.astype(np.floataa )

     # always return batch
     if not is_batched:
       lowercase__			    =						[raw_speech]

     # extract fbank features
     lowercase__			    =						[self._extract_fbank_features(a ) for waveform in raw_speech]

     # convert into correct format for padding
     lowercase__			    =						BatchFeature({'input_features': features} )

     lowercase__			    =						self.pad(
         a    ,  padding=a    ,  max_length=a    ,  truncation=a    ,  pad_to_multiple_of=a    ,  return_attention_mask=a    ,  **a    ,  )

     # make sure list is in array format
     lowercase__			    =						padded_inputs.get('input_features' )
     if isinstance(input_features[0]    ,  a ):
       lowercase__			    =						[np.asarray(a    ,  dtype=np.floataa ) for feature in input_features]

     lowercase__			    =						padded_inputs.get('attention_mask' )
     if attention_mask is not None:
       lowercase__			    =						[np.asarray(a    ,  dtype=np.intaa ) for array in attention_mask]

     # Utterance-level cepstral mean and variance normalization
     if self.do_ceptral_normalize:
       lowercase__			    =						(
           np.array(a    ,  dtype=np.intaa )
           if self._get_padding_strategies(a    ,  max_length=a ) is not PaddingStrategy.DO_NOT_PAD
           else None
       )
       lowercase__			    =						self.normalize(
           padded_inputs['input_features']    ,  attention_mask=a )

     if return_tensors is not None:
       lowercase__			    =						padded_inputs.convert_to_tensors(a )

     return padded_inputs









import gc
import tempfile
import unittest

import numpy as np
import torch

from diffusers import VersatileDiffusionPipeline
from diffusers.utils.testing_utils import load_image, nightly, require_torch_gpu, torch_device


lowercase_					     =	False
class        SCREAMING_SNAKE_CASE     (unittest.TestCase   ):
   pass






@nightly
@require_torch_gpu
class        SCREAMING_SNAKE_CASE     (unittest.TestCase   ):


   def  SCREAMING_SNAKE_CASE_							(					self    :       str )->     Optional[int]:





     """simple docstring"""







     super().tearDown()
     gc.collect()
     torch.cuda.empty_cache()


   def  SCREAMING_SNAKE_CASE_							(					self    :       Optional[Any] )->     Union[str, Any]:





     """simple docstring"""







     lowercase__			    =						VersatileDiffusionPipeline.from_pretrained('shi-labs/versatile-diffusion'    ,  torch_dtype=torch.floataa )
     pipe.to(a )
     pipe.set_progress_bar_config(disable=a )

     lowercase__			    =						load_image(
         'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg' )

     lowercase__			    =						torch.manual_seed(0 )
     lowercase__			    =						pipe.dual_guided(
         prompt='first prompt'    ,  image=a    ,  text_to_image_strength=0.75    ,  generator=a    ,  guidance_scale=7.5    ,  num_inference_steps=2    ,  output_type='numpy'    ,  ).images

     with tempfile.TemporaryDirectory() as tmpdirname:
       pipe.save_pretrained(a )
       lowercase__			    =						VersatileDiffusionPipeline.from_pretrained(a    ,  torch_dtype=torch.floataa )
     pipe.to(a )
     pipe.set_progress_bar_config(disable=a )

     lowercase__			    =						generator.manual_seed(0 )
     lowercase__			    =						pipe.dual_guided(
         prompt='first prompt'    ,  image=a    ,  text_to_image_strength=0.75    ,  generator=a    ,  guidance_scale=7.5    ,  num_inference_steps=2    ,  output_type='numpy'    ,  ).images

     assert np.abs(image - new_image ).sum() < 1E-5, "Models don't have the same forward pass"


   def  SCREAMING_SNAKE_CASE_							(					self    :       Tuple )->     List[str]:





     """simple docstring"""







     lowercase__			    =						VersatileDiffusionPipeline.from_pretrained('shi-labs/versatile-diffusion'    ,  torch_dtype=torch.floataa )
     pipe.to(a )
     pipe.set_progress_bar_config(disable=a )

     lowercase__			    =						'cyberpunk 2077'
     lowercase__			    =						load_image(
         'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg' )
     lowercase__			    =						torch.manual_seed(0 )
     lowercase__			    =						pipe.dual_guided(
         prompt=a    ,  image=a    ,  text_to_image_strength=0.75    ,  generator=a    ,  guidance_scale=7.5    ,  num_inference_steps=50    ,  output_type='numpy'    ,  ).images

     lowercase__			    =						image[0, 253:256, 253:256, -1]

     assert image.shape == (1, 512, 512, 3)
     lowercase__			    =						np.array([0.1448, 0.1619, 0.1741, 0.1086, 0.1147, 0.1128, 0.1199, 0.1165, 0.1001] )

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

     lowercase__			    =						'A painting of a squirrel eating a burger '
     lowercase__			    =						torch.manual_seed(0 )
     lowercase__			    =						pipe.text_to_image(
         prompt=a    ,  generator=a    ,  guidance_scale=7.5    ,  num_inference_steps=50    ,  output_type='numpy' ).images

     lowercase__			    =						image[0, 253:256, 253:256, -1]

     assert image.shape == (1, 512, 512, 3)
     lowercase__			    =						np.array([0.3367, 0.3169, 0.2656, 0.3870, 0.4790, 0.3796, 0.4009, 0.4878, 0.4778] )

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

     lowercase__			    =						pipe.image_variation(a    ,  generator=a    ,  output_type='numpy' ).images

     lowercase__			    =						image[0, 253:256, 253:256, -1]

     assert image.shape == (1, 512, 512, 3)
     lowercase__			    =						np.array([0.3076, 0.3123, 0.3284, 0.3782, 0.3770, 0.3894, 0.4297, 0.4331, 0.4456] )

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


