
"""simple docstring"""





import unittest

from transformers import (
    MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
    TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
    Pipeline,
    ZeroShotClassificationPipeline,
    pipeline,
)
from transformers.testing_utils import is_pipeline_test, nested_simplify, require_tf, require_torch, slow

from .test_pipelines_common import ANY


# These 2 model types require different inputs than those of the usual text models.
_a								=						{"""LayoutLMv2Config""", """LayoutLMv3Config"""}






@is_pipeline_test
class        _UpperCAmelCase(       unittest.TestCase      ):
		lowercase__         = MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING
		lowercase__         = TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING

		if model_mapping is not None:
				lowercase__         = {config: model for config, model in model_mapping.items() if config.__name__ not in _TO_SKIP}
		if tf_model_mapping is not None:
				lowercase__         = {
				    config: model for config, model in tf_model_mapping.items() if config.__name__ not in _TO_SKIP
				}



		def 		UpperCAmelCase				(   self							,						__a							,						__a							,						__a)			->			str:






							'''simple docstring'''
							_UpperCamelCase									=    ZeroShotClassificationPipeline(
							    model=__a							,						tokenizer=__a							,						candidate_labels=['''polics''', '''health'''])
							return classifier, ["Who are you voting for in 2020?", "My stomach hurts."]



		def 		UpperCAmelCase				(   self							,						__a							,						__a)			->			Optional[int]:






							'''simple docstring'''
							_UpperCamelCase									=    classifier('''Who are you voting for in 2020?'''							,						candidate_labels='''politics''')
							self.assertEqual(__a							,						{'''sequence''': ANY(__a), '''labels''': [ANY(__a)], '''scores''': [ANY(__a)]})

							# No kwarg
							_UpperCamelCase									=    classifier('''Who are you voting for in 2020?'''							,						['''politics'''])
							self.assertEqual(__a							,						{'''sequence''': ANY(__a), '''labels''': [ANY(__a)], '''scores''': [ANY(__a)]})

							_UpperCamelCase									=    classifier('''Who are you voting for in 2020?'''							,						candidate_labels=['''politics'''])
							self.assertEqual(__a							,						{'''sequence''': ANY(__a), '''labels''': [ANY(__a)], '''scores''': [ANY(__a)]})

							_UpperCamelCase									=    classifier('''Who are you voting for in 2020?'''							,						candidate_labels='''politics, public health''')
							self.assertEqual(
							    __a							,						{'''sequence''': ANY(__a), '''labels''': [ANY(__a), ANY(__a)], '''scores''': [ANY(__a), ANY(__a)]})
							self.assertAlmostEqual(sum(nested_simplify(outputs['''scores''']))							,						1.0)

							_UpperCamelCase									=    classifier('''Who are you voting for in 2020?'''							,						candidate_labels=['''politics''', '''public health'''])
							self.assertEqual(
							    __a							,						{'''sequence''': ANY(__a), '''labels''': [ANY(__a), ANY(__a)], '''scores''': [ANY(__a), ANY(__a)]})
							self.assertAlmostEqual(sum(nested_simplify(outputs['''scores''']))							,						1.0)

							_UpperCamelCase									=    classifier(
							    '''Who are you voting for in 2020?'''							,						candidate_labels='''politics'''							,						hypothesis_template='''This text is about {}''')
							self.assertEqual(__a							,						{'''sequence''': ANY(__a), '''labels''': [ANY(__a)], '''scores''': [ANY(__a)]})

							# https://github.com/huggingface/transformers/issues/13846
							_UpperCamelCase									=    classifier(['''I am happy''']							,						['''positive''', '''negative'''])
							self.assertEqual(
							    __a							,						[
							        {'''sequence''': ANY(__a), '''labels''': [ANY(__a), ANY(__a)], '''scores''': [ANY(__a), ANY(__a)]}
							        for i in range(1)
							    ]							,						)
							_UpperCamelCase									=    classifier(['''I am happy''', '''I am sad''']							,						['''positive''', '''negative'''])
							self.assertEqual(
							    __a							,						[
							        {'''sequence''': ANY(__a), '''labels''': [ANY(__a), ANY(__a)], '''scores''': [ANY(__a), ANY(__a)]}
							        for i in range(2)
							    ]							,						)

							with self.assertRaises(__a):
												classifier(''''''							,						candidate_labels='''politics''')

							with self.assertRaises(__a):
												classifier(__a							,						candidate_labels='''politics''')

							with self.assertRaises(__a):
												classifier('''Who are you voting for in 2020?'''							,						candidate_labels='''''')

							with self.assertRaises(__a):
												classifier('''Who are you voting for in 2020?'''							,						candidate_labels=__a)

							with self.assertRaises(__a):
												classifier(
												    '''Who are you voting for in 2020?'''							,						candidate_labels='''politics'''							,						hypothesis_template='''Not formatting template'''							,						)

							with self.assertRaises(__a):
												classifier(
												    '''Who are you voting for in 2020?'''							,						candidate_labels='''politics'''							,						hypothesis_template=__a							,						)

							self.run_entailment_id(__a)



		def 		UpperCAmelCase				(   self							,						__a)			->			List[Any]:






							'''simple docstring'''
							_UpperCamelCase									=    zero_shot_classifier.model.config
							_UpperCamelCase									=    config.labelaid
							_UpperCamelCase									=    zero_shot_classifier.entailment_id

							_UpperCamelCase									=    {'''LABEL_0''': 0, '''LABEL_1''': 1, '''LABEL_2''': 2}
							self.assertEqual(zero_shot_classifier.entailment_id							,						-1)

							_UpperCamelCase									=    {'''entailment''': 0, '''neutral''': 1, '''contradiction''': 2}
							self.assertEqual(zero_shot_classifier.entailment_id							,						0)

							_UpperCamelCase									=    {'''ENTAIL''': 0, '''NON-ENTAIL''': 1}
							self.assertEqual(zero_shot_classifier.entailment_id							,						0)

							_UpperCamelCase									=    {'''ENTAIL''': 2, '''NEUTRAL''': 1, '''CONTR''': 0}
							self.assertEqual(zero_shot_classifier.entailment_id							,						2)

							_UpperCamelCase									=    original_labelaid
							self.assertEqual(__a							,						zero_shot_classifier.entailment_id)



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






							'''simple docstring'''
							_UpperCamelCase									=    pipeline(
							    '''zero-shot-classification'''							,						model='''sshleifer/tiny-distilbert-base-cased-distilled-squad'''							,						framework='''pt'''							,						)
							# There was a regression in 4.10 for this
							# Adding a test so we don't make the mistake again.
							# https://github.com/huggingface/transformers/issues/13381#issuecomment-912343499
							zero_shot_classifier(
							    '''Who are you voting for in 2020?''' * 1_00							,						candidate_labels=['''politics''', '''public health''', '''science'''])



		@require_torch
		def 		UpperCAmelCase				(   self)			->			str:






							'''simple docstring'''
							_UpperCamelCase									=    pipeline(
							    '''zero-shot-classification'''							,						model='''sshleifer/tiny-distilbert-base-cased-distilled-squad'''							,						framework='''pt'''							,						)
							_UpperCamelCase									=    zero_shot_classifier(
							    '''Who are you voting for in 2020?'''							,						candidate_labels=['''politics''', '''public health''', '''science'''])

							self.assertEqual(
							    nested_simplify(__a)							,						{
							        '''sequence''': '''Who are you voting for in 2020?''',
							        '''labels''': ['''science''', '''public health''', '''politics'''],
							        '''scores''': [0.333, 0.333, 0.333],
							    }							,						)



		@require_tf
		def 		UpperCAmelCase				(   self)			->			Dict:






							'''simple docstring'''
							_UpperCamelCase									=    pipeline(
							    '''zero-shot-classification'''							,						model='''sshleifer/tiny-distilbert-base-cased-distilled-squad'''							,						framework='''tf'''							,						)
							_UpperCamelCase									=    zero_shot_classifier(
							    '''Who are you voting for in 2020?'''							,						candidate_labels=['''politics''', '''public health''', '''science'''])

							self.assertEqual(
							    nested_simplify(__a)							,						{
							        '''sequence''': '''Who are you voting for in 2020?''',
							        '''labels''': ['''science''', '''public health''', '''politics'''],
							        '''scores''': [0.333, 0.333, 0.333],
							    }							,						)



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






							'''simple docstring'''
							_UpperCamelCase									=    pipeline('''zero-shot-classification'''							,						model='''roberta-large-mnli'''							,						framework='''pt''')
							_UpperCamelCase									=    zero_shot_classifier(
							    '''Who are you voting for in 2020?'''							,						candidate_labels=['''politics''', '''public health''', '''science'''])

							self.assertEqual(
							    nested_simplify(__a)							,						{
							        '''sequence''': '''Who are you voting for in 2020?''',
							        '''labels''': ['''politics''', '''public health''', '''science'''],
							        '''scores''': [0.976, 0.015, 0.009],
							    }							,						)
							_UpperCamelCase									=    zero_shot_classifier(
							    '''The dominant sequence transduction models are based on complex recurrent or convolutional neural networks'''
							    ''' in an encoder-decoder configuration. The best performing models also connect the encoder and decoder'''
							    ''' through an attention mechanism. We propose a new simple network architecture, the Transformer, based'''
							    ''' solely on attention mechanisms, dispensing with recurrence and convolutions entirely. Experiments on two'''
							    ''' machine translation tasks show these models to be superior in quality while being more parallelizable'''
							    ''' and requiring significantly less time to train. Our model achieves 28.4 BLEU on the WMT 2014'''
							    ''' English-to-German translation task, improving over the existing best results, including ensembles by'''
							    ''' over 2 BLEU. On the WMT 2014 English-to-French translation task, our model establishes a new'''
							    ''' single-model state-of-the-art BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small'''
							    ''' fraction of the training costs of the best models from the literature. We show that the Transformer'''
							    ''' generalizes well to other tasks by applying it successfully to English constituency parsing both with'''
							    ''' large and limited training data.'''							,						candidate_labels=['''machine learning''', '''statistics''', '''translation''', '''vision''']							,						multi_label=__a							,						)
							self.assertEqual(
							    nested_simplify(__a)							,						{
							        '''sequence''': (
							            '''The dominant sequence transduction models are based on complex recurrent or convolutional neural'''
							            ''' networks in an encoder-decoder configuration. The best performing models also connect the'''
							            ''' encoder and decoder through an attention mechanism. We propose a new simple network'''
							            ''' architecture, the Transformer, based solely on attention mechanisms, dispensing with recurrence'''
							            ''' and convolutions entirely. Experiments on two machine translation tasks show these models to be'''
							            ''' superior in quality while being more parallelizable and requiring significantly less time to'''
							            ''' train. Our model achieves 28.4 BLEU on the WMT 2014 English-to-German translation task,'''
							            ''' improving over the existing best results, including ensembles by over 2 BLEU. On the WMT 2014'''
							            ''' English-to-French translation task, our model establishes a new single-model state-of-the-art'''
							            ''' BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small fraction of the training'''
							            ''' costs of the best models from the literature. We show that the Transformer generalizes well to'''
							            ''' other tasks by applying it successfully to English constituency parsing both with large and'''
							            ''' limited training data.'''
							        ),
							        '''labels''': ['''translation''', '''machine learning''', '''vision''', '''statistics'''],
							        '''scores''': [0.817, 0.713, 0.018, 0.018],
							    }							,						)




		@slow
		@require_tf
		def 		UpperCAmelCase				(   self)			->			Optional[Any]:






							'''simple docstring'''
							_UpperCamelCase									=    pipeline('''zero-shot-classification'''							,						model='''roberta-large-mnli'''							,						framework='''tf''')
							_UpperCamelCase									=    zero_shot_classifier(
							    '''Who are you voting for in 2020?'''							,						candidate_labels=['''politics''', '''public health''', '''science'''])

							self.assertEqual(
							    nested_simplify(__a)							,						{
							        '''sequence''': '''Who are you voting for in 2020?''',
							        '''labels''': ['''politics''', '''public health''', '''science'''],
							        '''scores''': [0.976, 0.015, 0.009],
							    }							,						)
							_UpperCamelCase									=    zero_shot_classifier(
							    '''The dominant sequence transduction models are based on complex recurrent or convolutional neural networks'''
							    ''' in an encoder-decoder configuration. The best performing models also connect the encoder and decoder'''
							    ''' through an attention mechanism. We propose a new simple network architecture, the Transformer, based'''
							    ''' solely on attention mechanisms, dispensing with recurrence and convolutions entirely. Experiments on two'''
							    ''' machine translation tasks show these models to be superior in quality while being more parallelizable'''
							    ''' and requiring significantly less time to train. Our model achieves 28.4 BLEU on the WMT 2014'''
							    ''' English-to-German translation task, improving over the existing best results, including ensembles by'''
							    ''' over 2 BLEU. On the WMT 2014 English-to-French translation task, our model establishes a new'''
							    ''' single-model state-of-the-art BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small'''
							    ''' fraction of the training costs of the best models from the literature. We show that the Transformer'''
							    ''' generalizes well to other tasks by applying it successfully to English constituency parsing both with'''
							    ''' large and limited training data.'''							,						candidate_labels=['''machine learning''', '''statistics''', '''translation''', '''vision''']							,						multi_label=__a							,						)
							self.assertEqual(
							    nested_simplify(__a)							,						{
							        '''sequence''': (
							            '''The dominant sequence transduction models are based on complex recurrent or convolutional neural'''
							            ''' networks in an encoder-decoder configuration. The best performing models also connect the'''
							            ''' encoder and decoder through an attention mechanism. We propose a new simple network'''
							            ''' architecture, the Transformer, based solely on attention mechanisms, dispensing with recurrence'''
							            ''' and convolutions entirely. Experiments on two machine translation tasks show these models to be'''
							            ''' superior in quality while being more parallelizable and requiring significantly less time to'''
							            ''' train. Our model achieves 28.4 BLEU on the WMT 2014 English-to-German translation task,'''
							            ''' improving over the existing best results, including ensembles by over 2 BLEU. On the WMT 2014'''
							            ''' English-to-French translation task, our model establishes a new single-model state-of-the-art'''
							            ''' BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small fraction of the training'''
							            ''' costs of the best models from the literature. We show that the Transformer generalizes well to'''
							            ''' other tasks by applying it successfully to English constituency parsing both with large and'''
							            ''' limited training data.'''
							        ),
							        '''labels''': ['''translation''', '''machine learning''', '''vision''', '''statistics'''],
							        '''scores''': [0.817, 0.713, 0.018, 0.018],
							    }							,						)








"""simple docstring"""





from math import acos, sin
from typing import List, Tuple, Union

import numpy as np
import torch
from PIL import Image

from ...models import AutoencoderKL, UNetaDConditionModel
from ...schedulers import DDIMScheduler, DDPMScheduler
from ...utils import randn_tensor
from ..pipeline_utils import AudioPipelineOutput, BaseOutput, DiffusionPipeline, ImagePipelineOutput
from .mel import Mel






class        _UpperCAmelCase(       lowerCamelCase      ):
		lowercase__         = ['vqvae']



		def __init__(   self							,						__a							,						__a							,						__a							,						__a							,						)			->			List[str]:






							'''simple docstring'''
							super().__init__()
							self.register_modules(unet=__a							,						scheduler=__a							,						mel=__a							,						vqvae=__a)



		def 		UpperCAmelCase				(   self)			->			int:






							'''simple docstring'''
							return 50 if isinstance(self.scheduler							,						__a) else 10_00



		@torch.no_grad()
		def __call__(   self							,						__a = 1							,						__a = None							,						__a = None							,						__a = 0							,						__a = 0							,						__a = None							,						__a = None							,						__a = 0							,						__a = 0							,						__a = None							,						__a = 0							,						__a = None							,						__a = None							,						__a=True							,						)			->			Union[
		    Union[AudioPipelineOutput, ImagePipelineOutput],
		    Tuple[List[Image.Image], Tuple[int, List[np.ndarray]]],
		]:






							'''simple docstring'''
							_UpperCamelCase									=    steps or self.get_default_steps()
							self.scheduler.set_timesteps(__a)
							_UpperCamelCase									=    step_generator or generator
							# For backwards compatibility
							if type(self.unet.config.sample_size) == int:
												_UpperCamelCase									=    (self.unet.config.sample_size, self.unet.config.sample_size)
							if noise is None:
												_UpperCamelCase									=    randn_tensor(
												    (
												        batch_size,
												        self.unet.config.in_channels,
												        self.unet.config.sample_size[0],
												        self.unet.config.sample_size[1],
												    )							,						generator=__a							,						device=self.device							,						)
							_UpperCamelCase									=    noise
							_UpperCamelCase									=    None

							if audio_file is not None or raw_audio is not None:
												self.mel.load_audio(__a							,						__a)
												_UpperCamelCase									=    self.mel.audio_slice_to_image(__a)
												_UpperCamelCase									=    np.frombuffer(input_image.tobytes()							,						dtype='''uint8''').reshape(
												    (input_image.height, input_image.width))
												_UpperCamelCase									=    (input_image / 2_55) * 2 - 1
												_UpperCamelCase									=    torch.tensor(input_image[np.newaxis, :, :]							,						dtype=torch.float).to(self.device)

												if self.vqvae is not None:
																	_UpperCamelCase									=    self.vqvae.encode(torch.unsqueeze(__a							,						0)).latent_dist.sample(
																	    generator=__a)[0]
																	_UpperCamelCase									=    self.vqvae.config.scaling_factor * input_images

												if start_step > 0:
																	_UpperCamelCase									=    self.scheduler.add_noise(__a							,						__a							,						self.scheduler.timesteps[start_step - 1])

												_UpperCamelCase									=    (
												    self.unet.config.sample_size[1] * self.mel.get_sample_rate() / self.mel.x_res / self.mel.hop_length
												)
												_UpperCamelCase									=    int(mask_start_secs * pixels_per_second)
												_UpperCamelCase									=    int(mask_end_secs * pixels_per_second)
												_UpperCamelCase									=    self.scheduler.add_noise(__a							,						__a							,						torch.tensor(self.scheduler.timesteps[start_step:]))

							for step, t in enumerate(self.progress_bar(self.scheduler.timesteps[start_step:])):
												if isinstance(self.unet							,						__a):
																	_UpperCamelCase									=    self.unet(__a							,						__a							,						__a)['''sample''']
												else:
																	_UpperCamelCase									=    self.unet(__a							,						__a)['''sample''']

												if isinstance(self.scheduler							,						__a):
																	_UpperCamelCase									=    self.scheduler.step(
																	    model_output=__a							,						timestep=__a							,						sample=__a							,						eta=__a							,						generator=__a							,						)['''prev_sample''']
												else:
																	_UpperCamelCase									=    self.scheduler.step(
																	    model_output=__a							,						timestep=__a							,						sample=__a							,						generator=__a							,						)['''prev_sample''']

												if mask is not None:
																	if mask_start > 0:
																						_UpperCamelCase									=    mask[:, step, :, :mask_start]
																	if mask_end > 0:
																						_UpperCamelCase									=    mask[:, step, :, -mask_end:]

							if self.vqvae is not None:
												# 0.18215 was scaling factor used in training to ensure unit variance
												_UpperCamelCase									=    1 / self.vqvae.config.scaling_factor * images
												_UpperCamelCase									=    self.vqvae.decode(__a)['''sample''']

							_UpperCamelCase									=    (images / 2 + 0.5).clamp(0							,						1)
							_UpperCamelCase									=    images.cpu().permute(0							,						2							,						3							,						1).numpy()
							_UpperCamelCase									=    (images * 2_55).round().astype('''uint8''')
							_UpperCamelCase									=    list(
							    (Image.fromarray(_[:, :, 0]) for _ in images)
							    if images.shape[3] == 1
							    else (Image.fromarray(__a							,						mode='''RGB''').convert('''L''') for _ in images))

							_UpperCamelCase									=    [self.mel.image_to_audio(__a) for _ in images]
							if not return_dict:
												return images, (self.mel.get_sample_rate(), audios)

							return BaseOutput(**AudioPipelineOutput(np.array(__a)[:, np.newaxis, :])							,						**ImagePipelineOutput(__a))



		@torch.no_grad()
		def 		UpperCAmelCase				(   self							,						__a							,						__a = 50)			->			np.ndarray:






							'''simple docstring'''
							assert isinstance(self.scheduler							,						__a)
							self.scheduler.set_timesteps(__a)
							_UpperCamelCase									=    np.array(
							    [np.frombuffer(image.tobytes()							,						dtype='''uint8''').reshape((1, image.height, image.width)) for image in images])
							_UpperCamelCase									=    (sample / 2_55) * 2 - 1
							_UpperCamelCase									=    torch.Tensor(__a).to(self.device)

							for t in self.progress_bar(torch.flip(self.scheduler.timesteps							,						(0,))):
												_UpperCamelCase									=    t - self.scheduler.config.num_train_timesteps // self.scheduler.num_inference_steps
												_UpperCamelCase									=    self.scheduler.alphas_cumprod[t]
												_UpperCamelCase									=    (
												    self.scheduler.alphas_cumprod[prev_timestep]
												    if prev_timestep >= 0
												    else self.scheduler.final_alpha_cumprod
												)
												_UpperCamelCase									=    1 - alpha_prod_t
												_UpperCamelCase									=    self.unet(__a							,						__a)['''sample''']
												_UpperCamelCase									=    (1 - alpha_prod_t_prev) ** 0.5 * model_output
												_UpperCamelCase									=    (sample - pred_sample_direction) * alpha_prod_t_prev ** (-0.5)
												_UpperCamelCase									=    sample * alpha_prod_t ** 0.5 + beta_prod_t ** 0.5 * model_output

							return sample




		@staticmethod
		def 		UpperCAmelCase				(   __a							,						__a							,						__a)			->			torch.Tensor:






							'''simple docstring'''
							_UpperCamelCase									=    acos(torch.dot(torch.flatten(__a)							,						torch.flatten(__a)) / torch.norm(__a) / torch.norm(__a))
							return sin((1 - alpha) * theta) * xa / sin(__a) + sin(alpha * theta) * xa / sin(__a)






