




'''simple docstring'''




import shutil
import tempfile
import unittest
from pathlib import Path
from shutil import copyfile

from transformers import SpeechaTextTokenizer, is_speech_available
from transformers.models.speech_to_text.tokenization_speech_to_text import VOCAB_FILES_NAMES, save_json
from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_torch, require_torchaudio
from transformers.utils import FEATURE_EXTRACTOR_NAME

from .test_feature_extraction_speech_to_text import floats_list


if is_speech_available():
							from transformers import SpeechaTextFeatureExtractor, SpeechaTextProcessor


snake_case__          =						get_tests_dir("""fixtures/test_sentencepiece.model""")






@require_torch
@require_torchaudio
@require_sentencepiece
class     UpperCamelCase_     (unittest.TestCase    ):







						"""simple docstring"""


						def       _a    (     self      :       List[Any]    ):





											"""simple docstring"""







											A_ :     Optional[int] =							tempfile.mkdtemp()

											A_ :     Optional[Any] =							['''<s>''', '''<pad>''', '''</s>''', '''<unk>''', '''▁This''', '''▁is''', '''▁a''', '''▁t''', '''est''']
											A_ :     Optional[Any] =							dict(zip(_lowerCamelCase							, range(len(_lowerCamelCase    )    )    )    )
											A_ :     Any =							Path(self.tmpdirname    )
											save_json(_lowerCamelCase							, save_dir / VOCAB_FILES_NAMES['''vocab_file''']    )
											if not (save_dir / VOCAB_FILES_NAMES["spm_file"]).exists():
																copyfile(_lowerCamelCase							, save_dir / VOCAB_FILES_NAMES['''spm_file''']    )

											A_ :     Union[str, Any] =							SpeechaTextTokenizer.from_pretrained(self.tmpdirname    )
											tokenizer.save_pretrained(self.tmpdirname    )

											A_ :     List[Any] =							{
											    '''feature_size''': 24,
											    '''num_mel_bins''': 24,
											    '''padding_value''': 0.0,
											    '''sampling_rate''': 16000,
											    '''return_attention_mask''': False,
											    '''do_normalize''': True,
											}
											save_json(_lowerCamelCase							, save_dir / FEATURE_EXTRACTOR_NAME    )







						def       _a    (     self      :       List[Any]							, **_lowerCamelCase      :       List[str]    ):





											"""simple docstring"""







											return SpeechaTextTokenizer.from_pretrained(self.tmpdirname							, **_lowerCamelCase    )







						def       _a    (     self      :       List[Any]							, **_lowerCamelCase      :       str    ):





											"""simple docstring"""







											return SpeechaTextFeatureExtractor.from_pretrained(self.tmpdirname							, **_lowerCamelCase    )







						def       _a    (     self      :       Dict    ):





											"""simple docstring"""







											shutil.rmtree(self.tmpdirname    )







						def       _a    (     self      :       List[Any]    ):





											"""simple docstring"""







											A_ :     int =							self.get_tokenizer()
											A_ :     Dict =							self.get_feature_extractor()

											A_ :     Dict =							SpeechaTextProcessor(tokenizer=_lowerCamelCase							, feature_extractor=_lowerCamelCase    )

											processor.save_pretrained(self.tmpdirname    )
											A_ :     Optional[Any] =							SpeechaTextProcessor.from_pretrained(self.tmpdirname    )

											self.assertEqual(processor.tokenizer.get_vocab()							, tokenizer.get_vocab()    )
											self.assertIsInstance(processor.tokenizer							, _lowerCamelCase    )

											self.assertEqual(processor.feature_extractor.to_json_string()							, feature_extractor.to_json_string()    )
											self.assertIsInstance(processor.feature_extractor							, _lowerCamelCase    )







						def       _a    (     self      :       Union[str, Any]    ):





											"""simple docstring"""







											A_ :     Any =							SpeechaTextProcessor(
											    tokenizer=self.get_tokenizer()							, feature_extractor=self.get_feature_extractor()    )
											processor.save_pretrained(self.tmpdirname    )

											A_ :     Union[str, Any] =							self.get_tokenizer(bos_token='''(BOS)'''							, eos_token='''(EOS)'''    )
											A_ :     Optional[int] =							self.get_feature_extractor(do_normalize=_lowerCamelCase							, padding_value=1.0    )

											A_ :     Union[str, Any] =							SpeechaTextProcessor.from_pretrained(
											    self.tmpdirname							, bos_token='''(BOS)'''							, eos_token='''(EOS)'''							, do_normalize=_lowerCamelCase							, padding_value=1.0    )

											self.assertEqual(processor.tokenizer.get_vocab()							, tokenizer_add_kwargs.get_vocab()    )
											self.assertIsInstance(processor.tokenizer							, _lowerCamelCase    )

											self.assertEqual(processor.feature_extractor.to_json_string()							, feature_extractor_add_kwargs.to_json_string()    )
											self.assertIsInstance(processor.feature_extractor							, _lowerCamelCase    )







						def       _a    (     self      :       Optional[int]    ):





											"""simple docstring"""







											A_ :     List[Any] =							self.get_feature_extractor()
											A_ :     List[str] =							self.get_tokenizer()

											A_ :     Dict =							SpeechaTextProcessor(tokenizer=_lowerCamelCase							, feature_extractor=_lowerCamelCase    )

											A_ :     Dict =							floats_list((3, 1000)    )

											A_ :     List[Any] =							feature_extractor(_lowerCamelCase							, return_tensors='''np'''    )
											A_ :     Any =							processor(_lowerCamelCase							, return_tensors='''np'''    )

											for key in input_feat_extract.keys():
																self.assertAlmostEqual(input_feat_extract[key].sum()							, input_processor[key].sum()							, delta=1E-2    )







						def       _a    (     self      :       Optional[int]    ):





											"""simple docstring"""







											A_ :     Optional[int] =							self.get_feature_extractor()
											A_ :     Tuple =							self.get_tokenizer()

											A_ :     Dict =							SpeechaTextProcessor(tokenizer=_lowerCamelCase							, feature_extractor=_lowerCamelCase    )

											A_ :     str =							'''This is a test string'''

											A_ :     Union[str, Any] =							processor(text=_lowerCamelCase    )

											A_ :     Optional[Any] =							tokenizer(_lowerCamelCase    )

											for key in encoded_tok.keys():
																self.assertListEqual(encoded_tok[key]							, encoded_processor[key]    )







						def       _a    (     self      :       Optional[Any]    ):





											"""simple docstring"""







											A_ :     Any =							self.get_feature_extractor()
											A_ :     str =							self.get_tokenizer()

											A_ :     Any =							SpeechaTextProcessor(tokenizer=_lowerCamelCase							, feature_extractor=_lowerCamelCase    )

											A_ :     Dict =							[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

											A_ :     Optional[Any] =							processor.batch_decode(_lowerCamelCase    )
											A_ :     Dict =							tokenizer.batch_decode(_lowerCamelCase    )

											self.assertListEqual(_lowerCamelCase							, _lowerCamelCase    )







						def       _a    (     self      :       Optional[Any]    ):





											"""simple docstring"""







											A_ :     Any =							self.get_feature_extractor()
											A_ :     int =							self.get_tokenizer()

											A_ :     Optional[Any] =							SpeechaTextProcessor(tokenizer=_lowerCamelCase							, feature_extractor=_lowerCamelCase    )

											self.assertListEqual(
											    processor.model_input_names							, feature_extractor.model_input_names							, msg='''`processor` and `feature_extractor` model input names do not match'''							, )









'''simple docstring'''




import random
import unittest

import numpy as np
import torch

from diffusers import DDIMScheduler, LDMSuperResolutionPipeline, UNetaDModel, VQModel
from diffusers.utils import PIL_INTERPOLATION, floats_tensor, load_image, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch


enable_full_determinism()






class     UpperCamelCase_     (unittest.TestCase     ):







						"""simple docstring"""


						@property
						def       _a    (     self      :       int    ):





											"""simple docstring"""







											A_ :     Dict =							1
											A_ :     Any =							3
											A_ :     Any =							(32, 32)

											A_ :     Dict =							floats_tensor((batch_size, num_channels) + sizes							, rng=random.Random(0    )    ).to(_lowerCamelCase    )
											return image







						@property
						def       _a    (     self      :       Dict    ):





											"""simple docstring"""







											torch.manual_seed(0    )
											A_ :     Optional[int] =							UNetaDModel(
											    block_out_channels=(32, 64)							, layers_per_block=2							, sample_size=32							, in_channels=6							, out_channels=3							, down_block_types=('''DownBlock2D''', '''AttnDownBlock2D''')							, up_block_types=('''AttnUpBlock2D''', '''UpBlock2D''')							, )
											return model







						@property
						def       _a    (     self      :       Any    ):





											"""simple docstring"""







											torch.manual_seed(0    )
											A_ :     Dict =							VQModel(
											    block_out_channels=[32, 64]							, in_channels=3							, out_channels=3							, down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D''']							, up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D''']							, latent_channels=3							, )
											return model







						def       _a    (     self      :       List[Any]    ):





											"""simple docstring"""







											A_ :     Any =							'''cpu'''
											A_ :     Optional[Any] =							self.dummy_uncond_unet
											A_ :     str =							DDIMScheduler()
											A_ :     Optional[int] =							self.dummy_vq_model

											A_ :     Optional[Any] =							LDMSuperResolutionPipeline(unet=_lowerCamelCase							, vqvae=_lowerCamelCase							, scheduler=_lowerCamelCase    )
											ldm.to(_lowerCamelCase    )
											ldm.set_progress_bar_config(disable=_lowerCamelCase    )

											A_ :     Any =							self.dummy_image.to(_lowerCamelCase    )

											A_ :     str =							torch.Generator(device=_lowerCamelCase    ).manual_seed(0    )
											A_ :     List[Any] =							ldm(image=_lowerCamelCase							, generator=_lowerCamelCase							, num_inference_steps=2							, output_type='''numpy'''    ).images

											A_ :     str =							image[0, -3:, -3:, -1]

											assert image.shape == (1, 64, 64, 3)
											A_ :     List[Any] =							np.array([0.86_78, 0.82_45, 0.63_81, 0.68_30, 0.43_85, 0.55_99, 0.46_41, 0.62_01, 0.51_50]    )

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







						@unittest.skipIf(torch_device != '''cuda'''							, '''This test requires a GPU'''    )
						def       _a    (     self      :       Optional[int]    ):





											"""simple docstring"""







											A_ :     Tuple =							self.dummy_uncond_unet
											A_ :     List[str] =							DDIMScheduler()
											A_ :     str =							self.dummy_vq_model

											# put models in fp16
											A_ :     List[Any] =							unet.half()
											A_ :     Optional[int] =							vqvae.half()

											A_ :     Optional[Any] =							LDMSuperResolutionPipeline(unet=_lowerCamelCase							, vqvae=_lowerCamelCase							, scheduler=_lowerCamelCase    )
											ldm.to(_lowerCamelCase    )
											ldm.set_progress_bar_config(disable=_lowerCamelCase    )

											A_ :     Optional[Any] =							self.dummy_image.to(_lowerCamelCase    )

											A_ :     Optional[Any] =							ldm(_lowerCamelCase							, num_inference_steps=2							, output_type='''numpy'''    ).images

											assert image.shape == (1, 64, 64, 3)






@slow
@require_torch
class     UpperCamelCase_     (unittest.TestCase     ):







						"""simple docstring"""


						def       _a    (     self      :       int    ):





											"""simple docstring"""







											A_ :     Optional[int] =							load_image(
											    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'''
											    '''/vq_diffusion/teddy_bear_pool.png'''    )
											A_ :     List[str] =							init_image.resize((64, 64)							, resample=PIL_INTERPOLATION['''lanczos''']    )

											A_ :     List[str] =							LDMSuperResolutionPipeline.from_pretrained('''duongna/ldm-super-resolution'''							, device_map='''auto'''    )
											ldm.set_progress_bar_config(disable=_lowerCamelCase    )

											A_ :     List[str] =							torch.manual_seed(0    )
											A_ :     str =							ldm(image=_lowerCamelCase							, generator=_lowerCamelCase							, num_inference_steps=20							, output_type='''numpy'''    ).images

											A_ :     str =							image[0, -3:, -3:, -1]

											assert image.shape == (1, 256, 256, 3)
											A_ :     Optional[Any] =							np.array([0.76_44, 0.76_79, 0.76_42, 0.76_33, 0.76_66, 0.75_60, 0.74_25, 0.72_57, 0.69_07]    )

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



