import subprocess
from typing import Union

import numpy as np
import requests

from ..utils import add_end_docstrings, is_torch_available, is_torchaudio_available, logging
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_torch_available():
    from ..models.auto.modeling_auto import MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING

lowerCAmelCase__      		=					logging.get_logger(__name__)






def  __lowerCamelCase						(		lowerCAmelCase__      ,    lowerCAmelCase__      ):
       lowerCAmelCase__	      =   F"""{sampling_rate}"""
       lowerCAmelCase__	      =   '1'
       lowerCAmelCase__	      =   'f32le'
       lowerCAmelCase__	      =   [
           'ffmpeg',
           '-i',
           'pipe:0',
           '-ac',
           ac,
           '-ar',
           ar,
           '-f',
           format_for_conversion,
           '-hide_banner',
           '-loglevel',
           'quiet',
           'pipe:1',
       ]

       try:
              lowerCAmelCase__	      =   subprocess.Popen(lowerCAmelCase__      ,    stdin=subprocess.PIPE      ,    stdout=subprocess.PIPE      )
       except FileNotFoundError:
              raise ValueError('ffmpeg was not found but is required to load audio files from filename'      )
       lowerCAmelCase__	      =   ffmpeg_process.communicate(lowerCAmelCase__      )
       lowerCAmelCase__	      =   output_stream[0]

       lowerCAmelCase__	      =   np.frombuffer(lowerCAmelCase__      ,    np.floataa      )
       if audio.shape[0] == 0:
              raise ValueError('Malformed soundfile'      )
       return audio



@add_end_docstrings(SCREAMING_SNAKE_CASE		)
class 							a_					(		SCREAMING_SNAKE_CASE		):






 '''simple docstring'''






 def __init__(     self   :    List[str]     ,       *lowercase__   :    List[str]     ,       **lowercase__   :    List[str]):







        '''simple docstring'''



        lowerCAmelCase__	      =   5
        super().__init__(*lowercase__     ,       **lowercase__)

        if self.framework != "pt":
               raise ValueError(F"""The {self.__class__} is only available in PyTorch.""")

        self.check_model_type(lowercase__)




 def __call__(     self   :    str     ,       lowercase__   :    Union[np.ndarray, bytes, str]     ,       **lowercase__   :    List[str]     ,       ):







        '''simple docstring'''



        return super().__call__(lowercase__     ,       **lowercase__)




 def 						__snake_case		(     self   :    int     ,       lowercase__   :    Union[str, Any]=None     ,       **lowercase__   :    int):







        '''simple docstring'''



        lowerCAmelCase__	      =   {}
        if top_k is not None:
               if top_k > self.model.config.num_labels:
                      lowerCAmelCase__	      =   self.model.config.num_labels
               lowerCAmelCase__	      =   top_k
        return {}, {}, postprocess_params




 def 						__snake_case		(     self   :    Any     ,       lowercase__   :    Tuple):







        '''simple docstring'''



        if isinstance(lowercase__     ,       lowercase__):
               if inputs.startswith('http://') or inputs.startswith('https://'):
                      # We need to actually check for a real protocol, otherwise it's impossible to use a local file
                      # like http_huggingface_co.png
                      lowerCAmelCase__	      =   requests.get(lowercase__).content
               else:
                      with open(lowercase__     ,       'rb') as f:
                             lowerCAmelCase__	      =   f.read()

        if isinstance(lowercase__     ,       lowercase__):
               lowerCAmelCase__	      =   ffmpeg_read(lowercase__     ,       self.feature_extractor.sampling_rate)

        if isinstance(lowercase__     ,       lowercase__):
               # Accepting `"array"` which is the key defined in `datasets` for
               # better integration
               if not ("sampling_rate" in inputs and ("raw" in inputs or "array" in inputs)):
                      raise ValueError(
                          'When passing a dictionary to AudioClassificationPipeline, the dict needs to contain a '
                          '"raw" key containing the numpy array representing the audio and a "sampling_rate" key, '
                          'containing the sampling_rate associated with that array')

               lowerCAmelCase__	      =   inputs.pop('raw'     ,       lowercase__)
               if _inputs is None:
                      # Remove path which will not be used from `datasets`.
                      inputs.pop('path'     ,       lowercase__)
                      lowerCAmelCase__	      =   inputs.pop('array'     ,       lowercase__)
               lowerCAmelCase__	      =   inputs.pop('sampling_rate')
               lowerCAmelCase__	      =   _inputs
               if in_sampling_rate != self.feature_extractor.sampling_rate:
                      import torch

                      if is_torchaudio_available():
                             from torchaudio import functional as F
                      else:
                             raise ImportError(
                                 'torchaudio is required to resample audio samples in AudioClassificationPipeline. '
                                 'The torchaudio package can be installed through: `pip install torchaudio`.')

                      lowerCAmelCase__	      =   F.resample(
                          torch.from_numpy(lowercase__)     ,       lowercase__     ,       self.feature_extractor.sampling_rate).numpy()

        if not isinstance(lowercase__     ,       np.ndarray):
               raise ValueError('We expect a numpy ndarray as input')
        if len(inputs.shape) != 1:
               raise ValueError('We expect a single channel audio input for AudioClassificationPipeline')

        lowerCAmelCase__	      =   self.feature_extractor(
            lowercase__     ,       sampling_rate=self.feature_extractor.sampling_rate     ,       return_tensors='pt')
        return processed




 def 						__snake_case		(     self   :    List[str]     ,       lowercase__   :    Optional[int]):







        '''simple docstring'''



        lowerCAmelCase__	      =   self.model(**lowercase__)
        return model_outputs




 def 						__snake_case		(     self   :    Union[str, Any]     ,       lowercase__   :    Tuple     ,       lowercase__   :    Any=5):







        '''simple docstring'''



        lowerCAmelCase__	      =   model_outputs.logits[0].softmax(-1)
        lowerCAmelCase__      ,   lowerCAmelCase__	      =   probs.topk(lowercase__)

        lowerCAmelCase__	      =   scores.tolist()
        lowerCAmelCase__	      =   ids.tolist()

        lowerCAmelCase__	      =   [{'score': score, 'label': self.model.config.idalabel[_id]} for score, _id in zip(lowercase__     ,       lowercase__)]

        return labels

import gc
import unittest

import numpy as np
import torch

from diffusers import DDPMScheduler, MidiProcessor, SpectrogramDiffusionPipeline
from diffusers.pipelines.spectrogram_diffusion import SpectrogramContEncoder, SpectrogramNotesEncoder, TaFilmDecoder
from diffusers.utils import require_torch_gpu, skip_mps, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_note_seq, require_onnxruntime

from ..pipeline_params import TOKENS_TO_AUDIO_GENERATION_BATCH_PARAMS, TOKENS_TO_AUDIO_GENERATION_PARAMS
from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()


lowerCAmelCase__      		=					'./tests/fixtures/elise_format0.mid'
@unittest.skip('The note-seq package currently throws an error on import'		)
class 							a_					(		SCREAMING_SNAKE_CASE			,	unittest.TestCase		):






 '''simple docstring'''






 UpperCAmelCase_      					=   SpectrogramDiffusionPipeline
 UpperCAmelCase_      					=   PipelineTesterMixin.required_optional_params - {
     'callback',
     'latents',
     'callback_steps',
     'output_type',
     'num_images_per_prompt',
 }
 UpperCAmelCase_      					=   False
 UpperCAmelCase_      					=   False
 UpperCAmelCase_      					=   TOKENS_TO_AUDIO_GENERATION_PARAMS
 UpperCAmelCase_      					=   TOKENS_TO_AUDIO_GENERATION_BATCH_PARAMS




 def 						__snake_case		(     self   :    List[Any]):







        '''simple docstring'''



        torch.manual_seed(0)
        lowerCAmelCase__	      =   SpectrogramNotesEncoder(
            max_length=2_048     ,       vocab_size=1_536     ,       d_model=768     ,       dropout_rate=0.1     ,       num_layers=1     ,       num_heads=1     ,       d_kv=4     ,       d_ff=2_048     ,       feed_forward_proj='gated-gelu'     ,       )

        lowerCAmelCase__	      =   SpectrogramContEncoder(
            input_dims=128     ,       targets_context_length=256     ,       d_model=768     ,       dropout_rate=0.1     ,       num_layers=1     ,       num_heads=1     ,       d_kv=4     ,       d_ff=2_048     ,       feed_forward_proj='gated-gelu'     ,       )

        lowerCAmelCase__	      =   TaFilmDecoder(
            input_dims=128     ,       targets_length=256     ,       max_decoder_noise_time=20_000.0     ,       d_model=768     ,       num_layers=1     ,       num_heads=1     ,       d_kv=4     ,       d_ff=2_048     ,       dropout_rate=0.1     ,       )

        lowerCAmelCase__	      =   DDPMScheduler()

        lowerCAmelCase__	      =   {
            'notes_encoder': notes_encoder.eval(),
            'continuous_encoder': continuous_encoder.eval(),
            'decoder': decoder.eval(),
            'scheduler': scheduler,
            'melgan': None,
        }
        return components




 def 						__snake_case		(     self   :    Optional[int]     ,       lowercase__   :    int     ,       lowercase__   :    List[str]=0):







        '''simple docstring'''



        if str(lowercase__).startswith('mps'):
               lowerCAmelCase__	      =   torch.manual_seed(lowercase__)
        else:
               lowerCAmelCase__	      =   torch.Generator(device=lowercase__).manual_seed(lowercase__)
        lowerCAmelCase__	      =   {
            'input_tokens': [
                [1_134, 90, 1_135, 1_133, 1_080, 112, 1_132, 1_080, 1_133, 1_079, 133, 1_132, 1_079, 1_133, 1] + [0] * 2_033
            ],
            'generator': generator,
            'num_inference_steps': 4,
            'output_type': 'mel',
        }
        return inputs




 def 						__snake_case		(     self   :    List[str]):







        '''simple docstring'''



        lowerCAmelCase__	      =   'cpu'  # ensure determinism for the device-dependent torch.Generator
        lowerCAmelCase__	      =   self.get_dummy_components()
        lowerCAmelCase__	      =   SpectrogramDiffusionPipeline(**lowercase__)
        lowerCAmelCase__	      =   pipe.to(lowercase__)
        pipe.set_progress_bar_config(disable=lowercase__)

        lowerCAmelCase__	      =   self.get_dummy_inputs(lowercase__)
        lowerCAmelCase__	      =   pipe(**lowercase__)
        lowerCAmelCase__	      =   output.audios

        lowerCAmelCase__	      =   mel[0, -3:, -3:]

        assert mel_slice.shape == (3, 3)
        lowerCAmelCase__	      =   np.array(
            [-11.512_925, -4.788_215, -0.46_172_905, -2.051_715, -10.539_147, -10.970_963, -9.091_634, 4.0, 4.0])
        assert np.abs(mel_slice.flatten() - expected_slice).max() < 1e-2




 @skip_mps
 def 						__snake_case		(     self   :    List[Any]):







        '''simple docstring'''



        return super().test_save_load_local()




 @skip_mps
 def 						__snake_case		(     self   :    Optional[Any]):







        '''simple docstring'''



        return super().test_dict_tuple_outputs_equivalent()




 @skip_mps
 def 						__snake_case		(     self   :    Dict):







        '''simple docstring'''



        return super().test_save_load_optional_components()




 @skip_mps
 def 						__snake_case		(     self   :    str):







        '''simple docstring'''



        return super().test_attention_slicing_forward_pass()




 def 						__snake_case		(     self   :    Optional[Any]):







        '''simple docstring'''



        pass




 def 						__snake_case		(     self   :    List[Any]):







        '''simple docstring'''



        pass




 @skip_mps
 def 						__snake_case		(     self   :    Optional[Any]):







        '''simple docstring'''



        return super().test_progress_bar()






@slow
@require_torch_gpu
@require_onnxruntime
@require_note_seq
class 							a_					(		unittest.TestCase		):






 '''simple docstring'''






 def 						__snake_case		(     self   :    Optional[int]):







        '''simple docstring'''



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




 def 						__snake_case		(     self   :    Optional[Any]):







        '''simple docstring'''



        lowerCAmelCase__	      =   torch_device

        lowerCAmelCase__	      =   SpectrogramDiffusionPipeline.from_pretrained('google/music-spectrogram-diffusion')
        lowerCAmelCase__	      =   pipe.melgan
        lowerCAmelCase__	      =   None

        lowerCAmelCase__	      =   pipe.to(lowercase__)
        pipe.set_progress_bar_config(disable=lowercase__)

        def callback(lowercase__   :    int     ,       lowercase__   :    List[Any]):
               # decode mel to audio
               lowerCAmelCase__	      =   melgan(input_features=mel_output.astype(np.floataa))[0]
               assert len(audio[0]) == 81_920 * (step + 1)
               # simulate that audio is played
               return audio

        lowerCAmelCase__	      =   MidiProcessor()
        lowerCAmelCase__	      =   processor(lowercase__)

        lowerCAmelCase__	      =   input_tokens[:3]
        lowerCAmelCase__	      =   torch.manual_seed(0)
        pipe(lowercase__     ,       num_inference_steps=5     ,       generator=lowercase__     ,       callback=lowercase__     ,       output_type='mel')




 def 						__snake_case		(     self   :    str):







        '''simple docstring'''



        lowerCAmelCase__	      =   torch_device

        lowerCAmelCase__	      =   SpectrogramDiffusionPipeline.from_pretrained('google/music-spectrogram-diffusion')
        lowerCAmelCase__	      =   pipe.to(lowercase__)
        pipe.set_progress_bar_config(disable=lowercase__)
        lowerCAmelCase__	      =   MidiProcessor()

        lowerCAmelCase__	      =   processor(lowercase__)
        # just run two denoising loops
        lowerCAmelCase__	      =   input_tokens[:2]

        lowerCAmelCase__	      =   torch.manual_seed(0)
        lowerCAmelCase__	      =   pipe(lowercase__     ,       num_inference_steps=2     ,       generator=lowercase__)

        lowerCAmelCase__	      =   output.audios[0]

        assert abs(np.abs(lowercase__).sum() - 3_612.841) < 1e-1




 def 						__snake_case		(     self   :    Dict):







        '''simple docstring'''



        lowerCAmelCase__	      =   torch_device

        lowerCAmelCase__	      =   SpectrogramDiffusionPipeline.from_pretrained('google/music-spectrogram-diffusion')
        lowerCAmelCase__	      =   pipe.to(lowercase__)
        pipe.set_progress_bar_config(disable=lowercase__)

        lowerCAmelCase__	      =   MidiProcessor()

        lowerCAmelCase__	      =   processor(lowercase__)

        # just run 4 denoising loops
        lowerCAmelCase__	      =   input_tokens[:4]

        lowerCAmelCase__	      =   torch.manual_seed(0)
        lowerCAmelCase__	      =   pipe(lowercase__     ,       num_inference_steps=100     ,       generator=lowercase__)

        lowerCAmelCase__	      =   output.audios[0]
        assert abs(np.abs(lowercase__).sum() - 9_389.1_111) < 5e-2
