




import gc
import tempfile
import unittest

import numpy as np
import torch

from diffusers import VersatileDiffusionTextToImagePipeline
from diffusers.utils.testing_utils import nightly, require_torch_gpu, torch_device


__SCREAMING_SNAKE_CASE 						=False

class  __magic_name__						(  unittest.TestCase):
				'''simple docstring'''







				pass






@nightly
@require_torch_gpu
class  __magic_name__						(  unittest.TestCase):
				'''simple docstring'''







				def     _A							(							self:   Tuple		):
											# clean up the VRAM after each test
											super().tearDown()
											gc.collect()
											torch.cuda.empty_cache()


				def     _A							(							self:   List[Any]		):
											SCREAMING_SNAKE_CASE_   	=    VersatileDiffusionTextToImagePipeline.from_pretrained('''shi-labs/versatile-diffusion'''		)
											# remove text_unet
											pipe.remove_unused_weights()
											pipe.to(_lowerCamelCase		)
											pipe.set_progress_bar_config(disable=_lowerCamelCase		)

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

											with tempfile.TemporaryDirectory() as tmpdirname:
																		pipe.save_pretrained(_lowerCamelCase		)
																		SCREAMING_SNAKE_CASE_   	=    VersatileDiffusionTextToImagePipeline.from_pretrained(_lowerCamelCase		)
											pipe.to(_lowerCamelCase		)
											pipe.set_progress_bar_config(disable=_lowerCamelCase		)

											SCREAMING_SNAKE_CASE_   	=    generator.manual_seed(0		)
											SCREAMING_SNAKE_CASE_   	=    pipe(
											    prompt=_lowerCamelCase		,					generator=_lowerCamelCase		,					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     _A							(							self:   str		):
											SCREAMING_SNAKE_CASE_   	=    VersatileDiffusionTextToImagePipeline.from_pretrained(
											    '''shi-labs/versatile-diffusion'''		,					torch_dtype=torch.floataa		)
											pipe.to(_lowerCamelCase		)
											pipe.set_progress_bar_config(disable=_lowerCamelCase		)

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

											SCREAMING_SNAKE_CASE_   	=    image[0, 2_53:2_56, 2_53:2_56, -1]

											assert image.shape == (1, 5_12, 5_12, 3)
											SCREAMING_SNAKE_CASE_   	=    np.array([0.33_67, 0.31_69, 0.26_56, 0.38_70, 0.47_90, 0.37_96, 0.40_09, 0.48_78, 0.47_78]		)

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








import warnings
from contextlib import contextmanager

from ...processing_utils import ProcessorMixin
from .feature_extraction_wavaveca import WavaVecaFeatureExtractor
from .tokenization_wavaveca import WavaVecaCTCTokenizer






class  __magic_name__						(  __UpperCAmelCase):
				'''simple docstring'''







				SCREAMING_SNAKE_CASE__					:							List[str]	      =					"Wav2Vec2FeatureExtractor"
				SCREAMING_SNAKE_CASE__					:							List[Any]	      =					"AutoTokenizer"


				def __init__(							self:   Tuple		,					_lowerCamelCase:   str		,					_lowerCamelCase:   Optional[Any]		):
											super().__init__(_lowerCamelCase		,					_lowerCamelCase		)
											SCREAMING_SNAKE_CASE_   	=    self.feature_extractor
											SCREAMING_SNAKE_CASE_   	=    False


				@classmethod
				def     _A							(							cls:   List[Any]		,					_lowerCamelCase:   Tuple		,					**_lowerCamelCase:   List[str]		):
											try:
																		return super().from_pretrained(_lowerCamelCase		,					**_lowerCamelCase		)
											except OSError:
																		warnings.warn(
																		    f"Loading a tokenizer inside {cls.__name__} from a config that does not"
																		    ''' include a `tokenizer_class` attribute is deprecated and will be '''
																		    '''removed in v5. Please add `\'tokenizer_class\': \'Wav2Vec2CTCTokenizer\'`'''
																		    ''' attribute to either your `config.json` or `tokenizer_config.json` '''
																		    '''file to suppress this warning: '''		,					_lowerCamelCase		,					)

																		SCREAMING_SNAKE_CASE_   	=    WavaVecaFeatureExtractor.from_pretrained(_lowerCamelCase		,					**_lowerCamelCase		)
																		SCREAMING_SNAKE_CASE_   	=    WavaVecaCTCTokenizer.from_pretrained(_lowerCamelCase		,					**_lowerCamelCase		)

																		return cls(feature_extractor=_lowerCamelCase		,					tokenizer=_lowerCamelCase		)


				def __call__(							self:   Union[str, Any]		,					*_lowerCamelCase:   List[Any]		,					**_lowerCamelCase:   str		):
											# For backward compatibility
											if self._in_target_context_manager:
																		return self.current_processor(*_lowerCamelCase		,					**_lowerCamelCase		)

											if "raw_speech" in kwargs:
																		warnings.warn('''Using `raw_speech` as a keyword argument is deprecated. Use `audio` instead.'''		)
																		SCREAMING_SNAKE_CASE_   	=    kwargs.pop('''raw_speech'''		)
											else:
																		SCREAMING_SNAKE_CASE_   	=    kwargs.pop('''audio'''		,					_lowerCamelCase		)
											SCREAMING_SNAKE_CASE_   	=    kwargs.pop('''sampling_rate'''		,					_lowerCamelCase		)
											SCREAMING_SNAKE_CASE_   	=    kwargs.pop('''text'''		,					_lowerCamelCase		)
											if len(_lowerCamelCase		) > 0:
																		SCREAMING_SNAKE_CASE_   	=    args[0]
																		SCREAMING_SNAKE_CASE_   	=    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:
																		SCREAMING_SNAKE_CASE_   	=    self.feature_extractor(_lowerCamelCase		,					*_lowerCamelCase		,					sampling_rate=_lowerCamelCase		,					**_lowerCamelCase		)
											if text is not None:
																		SCREAMING_SNAKE_CASE_   	=    self.tokenizer(_lowerCamelCase		,					**_lowerCamelCase		)

											if text is None:
																		return inputs
											elif audio is None:
																		return encodings
											else:
																		SCREAMING_SNAKE_CASE_   	=    encodings['''input_ids''']
																		return inputs


				def     _A							(							self:   Optional[int]		,					*_lowerCamelCase:   str		,					**_lowerCamelCase:   List[Any]		):
											# For backward compatibility
											if self._in_target_context_manager:
																		return self.current_processor.pad(*_lowerCamelCase		,					**_lowerCamelCase		)

											SCREAMING_SNAKE_CASE_   	=    kwargs.pop('''input_features'''		,					_lowerCamelCase		)
											SCREAMING_SNAKE_CASE_   	=    kwargs.pop('''labels'''		,					_lowerCamelCase		)
											if len(_lowerCamelCase		) > 0:
																		SCREAMING_SNAKE_CASE_   	=    args[0]
																		SCREAMING_SNAKE_CASE_   	=    args[1:]

											if input_features is not None:
																		SCREAMING_SNAKE_CASE_   	=    self.feature_extractor.pad(_lowerCamelCase		,					*_lowerCamelCase		,					**_lowerCamelCase		)
											if labels is not None:
																		SCREAMING_SNAKE_CASE_   	=    self.tokenizer.pad(_lowerCamelCase		,					**_lowerCamelCase		)

											if labels is None:
																		return input_features
											elif input_features is None:
																		return labels
											else:
																		SCREAMING_SNAKE_CASE_   	=    labels['''input_ids''']
																		return input_features


				def     _A							(							self:   str		,					*_lowerCamelCase:   Dict		,					**_lowerCamelCase:   Dict		):
											return self.tokenizer.batch_decode(*_lowerCamelCase		,					**_lowerCamelCase		)


				def     _A							(							self:   Optional[int]		,					*_lowerCamelCase:   Optional[int]		,					**_lowerCamelCase:   Tuple		):
											return self.tokenizer.decode(*_lowerCamelCase		,					**_lowerCamelCase		)





				@contextmanager
				def     _A							(							self:   Tuple		):
											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.'''		)
											SCREAMING_SNAKE_CASE_   	=    True
											SCREAMING_SNAKE_CASE_   	=    self.tokenizer
											yield
											SCREAMING_SNAKE_CASE_   	=    self.feature_extractor
											SCREAMING_SNAKE_CASE_   	=    False


