





import unittest

from transformers import MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING, is_vision_available, pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    require_torch,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
  from PIL import Image
else:






  class     lowercase__       :

     '''simple docstring'''


     @staticmethod
     def       UpperCamelCase__  (    *__magic_name__,		**__magic_name__							) ->      Optional[int]:






         """simple docstring"""
         pass






@is_pipeline_test
@require_vision
@require_torch
class     lowercase__       (	unittest.TestCase				):

   '''simple docstring'''


   a						:							Dict   		=		MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING




   def       UpperCamelCase__  (    self,		__magic_name__,		__magic_name__,		__magic_name__							) ->      Union[str, Any]:






       """simple docstring"""
       UpperCamelCase__							:				Any          =      pipeline(
           '''zero-shot-object-detection''',		model='''hf-internal-testing/tiny-random-owlvit-object-detection'''							)

       UpperCamelCase__							:				Union[str, Any]          =      [
           {
               '''image''': '''./tests/fixtures/tests_samples/COCO/000000039769.png''',
               '''candidate_labels''': ['''cat''', '''remote''', '''couch'''],
           }
       ]
       return object_detector, examples




   def       UpperCamelCase__  (    self,		__magic_name__,		__magic_name__							) ->      str:






       """simple docstring"""
       UpperCamelCase__							:				str          =      object_detector(examples[0],		threshold=0.0							)

       UpperCamelCase__							:				List[str]          =      len(__magic_name__							)
       self.assertGreater(__magic_name__,		0							)
       self.assertEqual(
           __magic_name__,		[
               {
                   '''score''': ANY(__magic_name__							),
                   '''label''': ANY(__magic_name__							),
                   '''box''': {'''xmin''': ANY(__magic_name__							), '''ymin''': ANY(__magic_name__							), '''xmax''': ANY(__magic_name__							), '''ymax''': ANY(__magic_name__							)},
               }
               for i in range(__magic_name__							)
           ],		)




   @require_tf
   @unittest.skip('''Zero Shot Object Detection not implemented in TF'''							)
   def       UpperCamelCase__  (    self							) ->      Any:






       """simple docstring"""
       pass




   @require_torch
   def       UpperCamelCase__  (    self							) ->      List[Any]:






       """simple docstring"""
       UpperCamelCase__							:				str          =      pipeline(
           '''zero-shot-object-detection''',		model='''hf-internal-testing/tiny-random-owlvit-object-detection'''							)

       UpperCamelCase__							:				Any          =      object_detector(
           '''./tests/fixtures/tests_samples/COCO/000000039769.png''',		candidate_labels=['''cat''', '''remote''', '''couch'''],		threshold=0.64,		)

       self.assertEqual(
           nested_simplify(__magic_name__,		decimals=4							),		[
               {'''score''': 0.7235, '''label''': '''cat''', '''box''': {'''xmin''': 204, '''ymin''': 167, '''xmax''': 232, '''ymax''': 190}},
               {'''score''': 0.7218, '''label''': '''remote''', '''box''': {'''xmin''': 204, '''ymin''': 167, '''xmax''': 232, '''ymax''': 190}},
               {'''score''': 0.7184, '''label''': '''couch''', '''box''': {'''xmin''': 204, '''ymin''': 167, '''xmax''': 232, '''ymax''': 190}},
               {'''score''': 0.6748, '''label''': '''remote''', '''box''': {'''xmin''': 571, '''ymin''': 83, '''xmax''': 598, '''ymax''': 103}},
               {'''score''': 0.6656, '''label''': '''cat''', '''box''': {'''xmin''': 571, '''ymin''': 83, '''xmax''': 598, '''ymax''': 103}},
               {'''score''': 0.6614, '''label''': '''couch''', '''box''': {'''xmin''': 571, '''ymin''': 83, '''xmax''': 598, '''ymax''': 103}},
               {'''score''': 0.6456, '''label''': '''remote''', '''box''': {'''xmin''': 494, '''ymin''': 105, '''xmax''': 521, '''ymax''': 127}},
               {'''score''': 0.642, '''label''': '''remote''', '''box''': {'''xmin''': 67, '''ymin''': 274, '''xmax''': 93, '''ymax''': 297}},
               {'''score''': 0.6419, '''label''': '''cat''', '''box''': {'''xmin''': 494, '''ymin''': 105, '''xmax''': 521, '''ymax''': 127}},
           ],		)

       UpperCamelCase__							:				int          =      object_detector(
           [
               {
                   '''image''': '''./tests/fixtures/tests_samples/COCO/000000039769.png''',
                   '''candidate_labels''': ['''cat''', '''remote''', '''couch'''],
               }
           ],		threshold=0.64,		)

       self.assertEqual(
           nested_simplify(__magic_name__,		decimals=4							),		[
               [
                   {'''score''': 0.7235, '''label''': '''cat''', '''box''': {'''xmin''': 204, '''ymin''': 167, '''xmax''': 232, '''ymax''': 190}},
                   {'''score''': 0.7218, '''label''': '''remote''', '''box''': {'''xmin''': 204, '''ymin''': 167, '''xmax''': 232, '''ymax''': 190}},
                   {'''score''': 0.7184, '''label''': '''couch''', '''box''': {'''xmin''': 204, '''ymin''': 167, '''xmax''': 232, '''ymax''': 190}},
                   {'''score''': 0.6748, '''label''': '''remote''', '''box''': {'''xmin''': 571, '''ymin''': 83, '''xmax''': 598, '''ymax''': 103}},
                   {'''score''': 0.6656, '''label''': '''cat''', '''box''': {'''xmin''': 571, '''ymin''': 83, '''xmax''': 598, '''ymax''': 103}},
                   {'''score''': 0.6614, '''label''': '''couch''', '''box''': {'''xmin''': 571, '''ymin''': 83, '''xmax''': 598, '''ymax''': 103}},
                   {'''score''': 0.6456, '''label''': '''remote''', '''box''': {'''xmin''': 494, '''ymin''': 105, '''xmax''': 521, '''ymax''': 127}},
                   {'''score''': 0.642, '''label''': '''remote''', '''box''': {'''xmin''': 67, '''ymin''': 274, '''xmax''': 93, '''ymax''': 297}},
                   {'''score''': 0.6419, '''label''': '''cat''', '''box''': {'''xmin''': 494, '''ymin''': 105, '''xmax''': 521, '''ymax''': 127}},
               ]
           ],		)




   @require_torch
   @slow
   def       UpperCamelCase__  (    self							) ->      Any:






       """simple docstring"""
       UpperCamelCase__							:				List[Any]          =      pipeline('''zero-shot-object-detection'''							)

       UpperCamelCase__							:				Dict          =      object_detector(
           '''http://images.cocodataset.org/val2017/000000039769.jpg''',		candidate_labels=['''cat''', '''remote''', '''couch'''],		)
       self.assertEqual(
           nested_simplify(__magic_name__,		decimals=4							),		[
               {'''score''': 0.2868, '''label''': '''cat''', '''box''': {'''xmin''': 324, '''ymin''': 20, '''xmax''': 640, '''ymax''': 373}},
               {'''score''': 0.277, '''label''': '''remote''', '''box''': {'''xmin''': 40, '''ymin''': 72, '''xmax''': 177, '''ymax''': 115}},
               {'''score''': 0.2537, '''label''': '''cat''', '''box''': {'''xmin''': 1, '''ymin''': 55, '''xmax''': 315, '''ymax''': 472}},
               {'''score''': 0.1474, '''label''': '''remote''', '''box''': {'''xmin''': 335, '''ymin''': 74, '''xmax''': 371, '''ymax''': 187}},
               {'''score''': 0.1208, '''label''': '''couch''', '''box''': {'''xmin''': 4, '''ymin''': 0, '''xmax''': 642, '''ymax''': 476}},
           ],		)

       UpperCamelCase__							:				Optional[Any]          =      object_detector(
           [
               {
                   '''image''': '''http://images.cocodataset.org/val2017/000000039769.jpg''',
                   '''candidate_labels''': ['''cat''', '''remote''', '''couch'''],
               },
               {
                   '''image''': '''http://images.cocodataset.org/val2017/000000039769.jpg''',
                   '''candidate_labels''': ['''cat''', '''remote''', '''couch'''],
               },
           ],		)
       self.assertEqual(
           nested_simplify(__magic_name__,		decimals=4							),		[
               [
                   {'''score''': 0.2868, '''label''': '''cat''', '''box''': {'''xmin''': 324, '''ymin''': 20, '''xmax''': 640, '''ymax''': 373}},
                   {'''score''': 0.277, '''label''': '''remote''', '''box''': {'''xmin''': 40, '''ymin''': 72, '''xmax''': 177, '''ymax''': 115}},
                   {'''score''': 0.2537, '''label''': '''cat''', '''box''': {'''xmin''': 1, '''ymin''': 55, '''xmax''': 315, '''ymax''': 472}},
                   {'''score''': 0.1474, '''label''': '''remote''', '''box''': {'''xmin''': 335, '''ymin''': 74, '''xmax''': 371, '''ymax''': 187}},
                   {'''score''': 0.1208, '''label''': '''couch''', '''box''': {'''xmin''': 4, '''ymin''': 0, '''xmax''': 642, '''ymax''': 476}},
               ],
               [
                   {'''score''': 0.2868, '''label''': '''cat''', '''box''': {'''xmin''': 324, '''ymin''': 20, '''xmax''': 640, '''ymax''': 373}},
                   {'''score''': 0.277, '''label''': '''remote''', '''box''': {'''xmin''': 40, '''ymin''': 72, '''xmax''': 177, '''ymax''': 115}},
                   {'''score''': 0.2537, '''label''': '''cat''', '''box''': {'''xmin''': 1, '''ymin''': 55, '''xmax''': 315, '''ymax''': 472}},
                   {'''score''': 0.1474, '''label''': '''remote''', '''box''': {'''xmin''': 335, '''ymin''': 74, '''xmax''': 371, '''ymax''': 187}},
                   {'''score''': 0.1208, '''label''': '''couch''', '''box''': {'''xmin''': 4, '''ymin''': 0, '''xmax''': 642, '''ymax''': 476}},
               ],
           ],		)




   @require_tf
   @unittest.skip('''Zero Shot Object Detection not implemented in TF'''							)
   def       UpperCamelCase__  (    self							) ->      str:






       """simple docstring"""
       pass




   @require_torch
   @slow
   def       UpperCamelCase__  (    self							) ->      Dict:






       """simple docstring"""
       UpperCamelCase__							:				Optional[Any]          =      0.2
       UpperCamelCase__							:				Optional[int]          =      pipeline('''zero-shot-object-detection'''							)

       UpperCamelCase__							:				Union[str, Any]          =      object_detector(
           '''http://images.cocodataset.org/val2017/000000039769.jpg''',		candidate_labels=['''cat''', '''remote''', '''couch'''],		threshold=__magic_name__,		)
       self.assertEqual(
           nested_simplify(__magic_name__,		decimals=4							),		[
               {'''score''': 0.2868, '''label''': '''cat''', '''box''': {'''xmin''': 324, '''ymin''': 20, '''xmax''': 640, '''ymax''': 373}},
               {'''score''': 0.277, '''label''': '''remote''', '''box''': {'''xmin''': 40, '''ymin''': 72, '''xmax''': 177, '''ymax''': 115}},
               {'''score''': 0.2537, '''label''': '''cat''', '''box''': {'''xmin''': 1, '''ymin''': 55, '''xmax''': 315, '''ymax''': 472}},
           ],		)




   @require_torch
   @slow
   def       UpperCamelCase__  (    self							) ->      Optional[int]:






       """simple docstring"""
       UpperCamelCase__							:				Optional[Any]          =      2
       UpperCamelCase__							:				Any          =      pipeline('''zero-shot-object-detection'''							)

       UpperCamelCase__							:				List[str]          =      object_detector(
           '''http://images.cocodataset.org/val2017/000000039769.jpg''',		candidate_labels=['''cat''', '''remote''', '''couch'''],		top_k=__magic_name__,		)
       self.assertEqual(
           nested_simplify(__magic_name__,		decimals=4							),		[
               {'''score''': 0.2868, '''label''': '''cat''', '''box''': {'''xmin''': 324, '''ymin''': 20, '''xmax''': 640, '''ymax''': 373}},
               {'''score''': 0.277, '''label''': '''remote''', '''box''': {'''xmin''': 40, '''ymin''': 72, '''xmax''': 177, '''ymax''': 115}},
           ],		)








import math
from typing import Any, Callable, List, Optional, Tuple, Union

import numpy as np
import torch

from ...models import TaFilmDecoder
from ...schedulers import DDPMScheduler
from ...utils import is_onnx_available, logging, randn_tensor


if is_onnx_available():
  from ..onnx_utils import OnnxRuntimeModel

from ..pipeline_utils import AudioPipelineOutput, DiffusionPipeline
from .continous_encoder import SpectrogramContEncoder
from .notes_encoder import SpectrogramNotesEncoder


UpperCAmelCase_     							= logging.get_logger(__name__)  # pylint: disable=invalid-name

UpperCAmelCase_     							= 256






class     lowercase__       (	__lowerCamelCase				):

   '''simple docstring'''


   a						:							List[str]   		=		["melgan"]




   def __init__(    self,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		) ->      None:






       """simple docstring"""
       super().__init__()

       # From MELGAN
       UpperCamelCase__							:				Optional[int]          =      math.log(1E-5							)  # Matches MelGAN training.
       UpperCamelCase__							:				int          =      4.0  # Largest value for most examples
       UpperCamelCase__							:				Optional[int]          =      128

       self.register_modules(
           notes_encoder=__magic_name__,		continuous_encoder=__magic_name__,		decoder=__magic_name__,		scheduler=__magic_name__,		melgan=__magic_name__,		)




   def       UpperCamelCase__  (    self,		__magic_name__,		__magic_name__=(-1.0, 1.0),		__magic_name__=False							) ->      Any:






       """simple docstring"""
       UpperCamelCase__   ,UpperCamelCase__							:				str          =      output_range
       if clip:
           UpperCamelCase__							:				Union[str, Any]          =      torch.clip(__magic_name__,		self.min_value,		self.max_value							)
       # Scale to [0, 1].
       UpperCamelCase__							:				List[str]          =      (features - self.min_value) / (self.max_value - self.min_value)
       # Scale to [min_out, max_out].
       return zero_one * (max_out - min_out) + min_out




   def       UpperCamelCase__  (    self,		__magic_name__,		__magic_name__=(-1.0, 1.0),		__magic_name__=False							) ->      Optional[int]:






       """simple docstring"""
       UpperCamelCase__   ,UpperCamelCase__							:				List[str]          =      input_range
       UpperCamelCase__							:				Any          =      torch.clip(__magic_name__,		__magic_name__,		__magic_name__							) if clip else outputs
       # Scale to [0, 1].
       UpperCamelCase__							:				Any          =      (outputs - min_out) / (max_out - min_out)
       # Scale to [self.min_value, self.max_value].
       return zero_one * (self.max_value - self.min_value) + self.min_value




   def       UpperCamelCase__  (    self,		__magic_name__,		__magic_name__,		__magic_name__							) ->      List[str]:






       """simple docstring"""
       UpperCamelCase__							:				Union[str, Any]          =      input_tokens > 0
       UpperCamelCase__   ,UpperCamelCase__							:				Any          =      self.notes_encoder(
           encoder_input_tokens=__magic_name__,		encoder_inputs_mask=__magic_name__							)

       UpperCamelCase__   ,UpperCamelCase__							:				Optional[Any]          =      self.continuous_encoder(
           encoder_inputs=__magic_name__,		encoder_inputs_mask=__magic_name__							)

       return [(tokens_encoded, tokens_mask), (continuous_encoded, continuous_mask)]




   def       UpperCamelCase__  (    self,		__magic_name__,		__magic_name__,		__magic_name__							) ->      str:






       """simple docstring"""
       UpperCamelCase__							:				Any          =      noise_time
       if not torch.is_tensor(__magic_name__							):
           UpperCamelCase__							:				Tuple          =      torch.tensor([timesteps],		dtype=torch.long,		device=input_tokens.device							)
       elif torch.is_tensor(__magic_name__							) and len(timesteps.shape							) == 0:
           UpperCamelCase__							:				Union[str, Any]          =      timesteps[None].to(input_tokens.device							)

       # broadcast to batch dimension in a way that's compatible with ONNX/Core ML
       UpperCamelCase__							:				Dict          =      timesteps * torch.ones(input_tokens.shape[0],		dtype=timesteps.dtype,		device=timesteps.device							)

       UpperCamelCase__							:				List[str]          =      self.decoder(
           encodings_and_masks=__magic_name__,		decoder_input_tokens=__magic_name__,		decoder_noise_time=__magic_name__							)
       return logits




   @torch.no_grad()
   def __call__(    self,		__magic_name__,		__magic_name__ = None,		__magic_name__ = 100,		__magic_name__ = True,		__magic_name__ = "numpy",		__magic_name__ = None,		__magic_name__ = 1,		) ->      Union[AudioPipelineOutput, Tuple]:






       """simple docstring"""
       if (callback_steps is None) or (
           callback_steps is not None and (not isinstance(__magic_name__,		__magic_name__							) or callback_steps <= 0)
       ):
           raise ValueError(
               f"`callback_steps` has to be a positive integer but is {callback_steps} of type"
               f" {type(__magic_name__							)}."							)

       UpperCamelCase__							:				Dict          =      np.zeros([1, TARGET_FEATURE_LENGTH, self.n_dims],		dtype=np.floataa							)
       UpperCamelCase__							:				Tuple          =      np.zeros([1, 0, self.n_dims],		np.floataa							)
       UpperCamelCase__							:				List[Any]          =      torch.ones((1, TARGET_FEATURE_LENGTH),		dtype=__magic_name__,		device=self.device							)

       for i, encoder_input_tokens in enumerate(__magic_name__							):
           if i == 0:
               UpperCamelCase__							:				str          =      torch.from_numpy(pred_mel[:1].copy()							).to(
                   device=self.device,		dtype=self.decoder.dtype							)
               # The first chunk has no previous context.
               UpperCamelCase__							:				Any          =      torch.zeros((1, TARGET_FEATURE_LENGTH),		dtype=__magic_name__,		device=self.device							)
           else:
               # The full song pipeline does not feed in a context feature, so the mask
               # will be all 0s after the feature converter. Because we know we're
               # feeding in a full context chunk from the previous prediction, set it
               # to all 1s.
               UpperCamelCase__							:				List[str]          =      ones

           UpperCamelCase__							:				int          =      self.scale_features(
               __magic_name__,		output_range=[-1.0, 1.0],		clip=__magic_name__							)

           UpperCamelCase__							:				Union[str, Any]          =      self.encode(
               input_tokens=torch.IntTensor([encoder_input_tokens]							).to(device=self.device							),		continuous_inputs=__magic_name__,		continuous_mask=__magic_name__,		)

           # Sample encoder_continuous_inputs shaped gaussian noise to begin loop
           UpperCamelCase__							:				Optional[int]          =      randn_tensor(
               shape=encoder_continuous_inputs.shape,		generator=__magic_name__,		device=self.device,		dtype=self.decoder.dtype,		)

           # set step values
           self.scheduler.set_timesteps(__magic_name__							)

           # Denoising diffusion loop
           for j, t in enumerate(self.progress_bar(self.scheduler.timesteps							)							):
               UpperCamelCase__							:				Union[str, Any]          =      self.decode(
                   encodings_and_masks=__magic_name__,		input_tokens=__magic_name__,		noise_time=t / self.scheduler.config.num_train_timesteps,		)

               # Compute previous output: x_t -> x_t-1
               UpperCamelCase__							:				List[Any]          =      self.scheduler.step(__magic_name__,		__magic_name__,		__magic_name__,		generator=__magic_name__							).prev_sample

           UpperCamelCase__							:				List[Any]          =      self.scale_to_features(__magic_name__,		input_range=[-1.0, 1.0]							)
           UpperCamelCase__							:				List[Any]          =      mel[:1]
           UpperCamelCase__							:				int          =      mel.cpu().float().numpy()

           UpperCamelCase__							:				Union[str, Any]          =      np.concatenate([full_pred_mel, pred_mel[:1]],		axis=1							)

           # call the callback, if provided
           if callback is not None and i % callback_steps == 0:
               callback(__magic_name__,		__magic_name__							)

           logger.info('''Generated segment''',		__magic_name__							)

       if output_type == "numpy" and not is_onnx_available():
           raise ValueError(
               '''Cannot return output in \'np\' format if ONNX is not available. Make sure to have ONNX installed or set \'output_type\' to \'mel\'.'''							)
       elif output_type == "numpy" and self.melgan is None:
           raise ValueError(
               '''Cannot return output in \'np\' format if melgan component is not defined. Make sure to define `self.melgan` or set \'output_type\' to \'mel\'.'''							)

       if output_type == "numpy":
           UpperCamelCase__							:				Optional[int]          =      self.melgan(input_features=full_pred_mel.astype(np.floataa							)							)
       else:
           UpperCamelCase__							:				Any          =      full_pred_mel

       if not return_dict:
           return (output,)

       return AudioPipelineOutput(audios=__magic_name__							)

