






import shutil
import tempfile
import unittest

import pytest

from transformers import WhisperTokenizer, is_speech_available
from transformers.testing_utils import require_sentencepiece, require_torch, require_torchaudio

from .test_feature_extraction_whisper import floats_list


if is_speech_available():
  from transformers import WhisperFeatureExtractor, WhisperProcessor


a__      =		5_03_58
a__      =		5_03_62







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



   '''simple docstring'''






   def        UpperCamelCase_					(   self							:     Tuple)			->			List[Any]:

    """simple docstring"""







    _snake_case      :					Dict					=   """openai/whisper-small.en"""
    _snake_case      :					Optional[Any]					=   tempfile.mkdtemp()

   def        UpperCamelCase_					(   self							:     str			,						**lowerCAmelCase							:     List[str])			->			List[str]:

    """simple docstring"""







    return WhisperTokenizer.from_pretrained(self.checkpoint			,						**snake_case_)

   def        UpperCamelCase_					(   self							:     Optional[Any]			,						**lowerCAmelCase							:     str)			->			List[Any]:

    """simple docstring"""







    return WhisperFeatureExtractor.from_pretrained(self.checkpoint			,						**snake_case_)

   def        UpperCamelCase_					(   self							:     List[str])			->			Optional[Any]:

    """simple docstring"""







    shutil.rmtree(self.tmpdirname)

   def        UpperCamelCase_					(   self							:     Optional[int])			->			List[str]:

    """simple docstring"""







    _snake_case      :					int					=   self.get_tokenizer()
    _snake_case      :					List[Any]					=   self.get_feature_extractor()

    _snake_case      :					List[str]					=   WhisperProcessor(tokenizer=snake_case_			,						feature_extractor=snake_case_)

    processor.save_pretrained(self.tmpdirname)
    _snake_case      :					Union[str, Any]					=   WhisperProcessor.from_pretrained(self.tmpdirname)

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

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

   def        UpperCamelCase_					(   self							:     List[str])			->			str:

    """simple docstring"""







    _snake_case      :					List[Any]					=   WhisperProcessor(tokenizer=self.get_tokenizer()			,						feature_extractor=self.get_feature_extractor())
    processor.save_pretrained(self.tmpdirname)

    _snake_case      :					Tuple					=   self.get_tokenizer(bos_token="""(BOS)"""			,						eos_token="""(EOS)""")
    _snake_case      :					Any					=   self.get_feature_extractor(do_normalize=snake_case_			,						padding_value=1.0)

    _snake_case      :					int					=   WhisperProcessor.from_pretrained(
        self.tmpdirname			,						bos_token="""(BOS)"""			,						eos_token="""(EOS)"""			,						do_normalize=snake_case_			,						padding_value=1.0)

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

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

   def        UpperCamelCase_					(   self							:     Optional[int])			->			Any:

    """simple docstring"""







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

    _snake_case      :					List[Any]					=   WhisperProcessor(tokenizer=snake_case_			,						feature_extractor=snake_case_)

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

    _snake_case      :					List[Any]					=   feature_extractor(snake_case_			,						return_tensors="""np""")
    _snake_case      :					Optional[int]					=   processor(snake_case_			,						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        UpperCamelCase_					(   self							:     Optional[Any])			->			Optional[Any]:

    """simple docstring"""







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

    _snake_case      :					List[Any]					=   WhisperProcessor(tokenizer=snake_case_			,						feature_extractor=snake_case_)

    _snake_case      :					Tuple					=   """This is a test string"""

    _snake_case      :					Union[str, Any]					=   processor(text=snake_case_)

    _snake_case      :					Tuple					=   tokenizer(snake_case_)

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

   def        UpperCamelCase_					(   self							:     Union[str, Any])			->			Dict:

    """simple docstring"""







    _snake_case      :					Union[str, Any]					=   self.get_feature_extractor()
    _snake_case      :					Any					=   self.get_tokenizer()

    _snake_case      :					int					=   WhisperProcessor(tokenizer=snake_case_			,						feature_extractor=snake_case_)

    _snake_case      :					Tuple					=   [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

    _snake_case      :					List[str]					=   processor.batch_decode(snake_case_)
    _snake_case      :					str					=   tokenizer.batch_decode(snake_case_)

    self.assertListEqual(snake_case_			,						snake_case_)

   def        UpperCamelCase_					(   self							:     List[Any])			->			str:

    """simple docstring"""







    _snake_case      :					Any					=   self.get_feature_extractor()
    _snake_case      :					Union[str, Any]					=   self.get_tokenizer()

    _snake_case      :					Any					=   WhisperProcessor(tokenizer=snake_case_			,						feature_extractor=snake_case_)

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

   def        UpperCamelCase_					(   self							:     Optional[Any])			->			List[Any]:

    """simple docstring"""







    _snake_case      :					str					=   self.get_feature_extractor()
    _snake_case      :					Optional[Any]					=   self.get_tokenizer()

    _snake_case      :					Union[str, Any]					=   WhisperProcessor(tokenizer=snake_case_			,						feature_extractor=snake_case_)
    _snake_case      :					List[Any]					=   processor.get_decoder_prompt_ids(task="""transcribe"""			,						no_timestamps=snake_case_)

    self.assertIsInstance(snake_case_			,						snake_case_)
    for ids in forced_decoder_ids:
     self.assertIsInstance(snake_case_			,						(list, tuple))

    _snake_case      :					Optional[Any]					=   [TRANSCRIBE, NOTIMESTAMPS]
    self.assertListEqual([ids[-1] for ids in forced_decoder_ids]			,						snake_case_)

   def        UpperCamelCase_					(   self							:     int)			->			Optional[int]:

    """simple docstring"""







    _snake_case      :					str					=   WhisperProcessor(tokenizer=self.get_tokenizer()			,						feature_extractor=self.get_feature_extractor())
    _snake_case      :					Union[str, Any]					=   processor.get_prompt_ids("""Mr. Quilter""")
    _snake_case      :					int					=   processor.tokenizer.decode(snake_case_)

    self.assertListEqual(prompt_ids.tolist()			,						[5_0360, 1770, 13, 2264, 346, 353])
    self.assertEqual(snake_case_			,						"""<|startofprev|> Mr. Quilter""")

   def        UpperCamelCase_					(   self							:     Any)			->			Any:

    """simple docstring"""







    _snake_case      :					Optional[Any]					=   WhisperProcessor(tokenizer=self.get_tokenizer()			,						feature_extractor=self.get_feature_extractor())
    _snake_case      :					int					=   processor.get_prompt_ids("""""")
    _snake_case      :					Tuple					=   processor.tokenizer.decode(snake_case_)

    self.assertListEqual(prompt_ids.tolist()			,						[5_0360, 220])
    self.assertEqual(snake_case_			,						"""<|startofprev|> """)


   def        UpperCamelCase_					(   self							:     Tuple)			->			Any:

    """simple docstring"""







    _snake_case      :					Any					=   WhisperProcessor(tokenizer=self.get_tokenizer()			,						feature_extractor=self.get_feature_extractor())

    def _test_prompt_error_raised_helper(lowerCAmelCase							:     List[str]			,						lowerCAmelCase							:     List[str]):
     with pytest.raises(snake_case_) as excinfo:
      processor.get_prompt_ids(snake_case_)
     _snake_case      :					Union[str, Any]					=   F'''Encountered text in the prompt corresponding to disallowed special token: {special_token}.'''
     self.assertEqual(snake_case_			,						str(excinfo.value))

    _test_prompt_error_raised_helper("""<|startofprev|> test"""			,						"""<|startofprev|>""")
    _test_prompt_error_raised_helper("""test <|notimestamps|>"""			,						"""<|notimestamps|>""")
    _test_prompt_error_raised_helper("""test <|zh|> test <|transcribe|>"""			,						"""<|zh|>""")





import unittest

import numpy as np
import torch

from diffusers import DDPMPipeline, DDPMScheduler, UNetaDModel
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, slow, torch_device


enable_full_determinism()







class 						snake_case  (		unittest.TestCase					):



   '''simple docstring'''






   @property
   def        UpperCamelCase_					(   self							:     Union[str, Any])			->			int:

    """simple docstring"""







    torch.manual_seed(0)
    _snake_case      :					Any					=   UNetaDModel(
        block_out_channels=(32, 64)			,						layers_per_block=2			,						sample_size=32			,						in_channels=3			,						out_channels=3			,						down_block_types=("""DownBlock2D""", """AttnDownBlock2D""")			,						up_block_types=("""AttnUpBlock2D""", """UpBlock2D""")			,						)
    return model

   def        UpperCamelCase_					(   self							:     List[Any])			->			Optional[Any]:

    """simple docstring"""







    _snake_case      :					List[str]					=   """cpu"""
    _snake_case      :					Union[str, Any]					=   self.dummy_uncond_unet
    _snake_case      :					Union[str, Any]					=   DDPMScheduler()

    _snake_case      :					Dict					=   DDPMPipeline(unet=lowerCAmelCase			,						scheduler=lowerCAmelCase)
    ddpm.to(lowerCAmelCase)
    ddpm.set_progress_bar_config(disable=lowerCAmelCase)

    _snake_case      :					Optional[Any]					=   torch.Generator(device=lowerCAmelCase).manual_seed(0)
    _snake_case      :					Optional[int]					=   ddpm(generator=lowerCAmelCase			,						num_inference_steps=2			,						output_type="""numpy""").images

    _snake_case      :					Optional[int]					=   torch.Generator(device=lowerCAmelCase).manual_seed(0)
    _snake_case      :					Union[str, Any]					=   ddpm(generator=lowerCAmelCase			,						num_inference_steps=2			,						output_type="""numpy"""			,						return_dict=lowerCAmelCase)[0]

    _snake_case      :					Any					=   image[0, -3:, -3:, -1]
    _snake_case      :					str					=   image_from_tuple[0, -3:, -3:, -1]

    assert image.shape == (1, 32, 32, 3)
    _snake_case      :					Optional[int]					=   np.array(
        [9.956E-01, 5.785E-01, 4.675E-01, 9.930E-01, 0.0, 1.000, 1.199E-03, 2.648E-04, 5.101E-04])

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


   def        UpperCamelCase_					(   self							:     List[Any])			->			Optional[int]:

    """simple docstring"""







    _snake_case      :					int					=   self.dummy_uncond_unet
    _snake_case      :					int					=   DDPMScheduler(prediction_type="""sample""")

    _snake_case      :					Union[str, Any]					=   DDPMPipeline(unet=lowerCAmelCase			,						scheduler=lowerCAmelCase)
    ddpm.to(lowerCAmelCase)
    ddpm.set_progress_bar_config(disable=lowerCAmelCase)

    _snake_case      :					Tuple					=   torch.manual_seed(0)
    _snake_case      :					Union[str, Any]					=   ddpm(generator=lowerCAmelCase			,						num_inference_steps=2			,						output_type="""numpy""").images

    _snake_case      :					Any					=   torch.manual_seed(0)
    _snake_case      :					Dict					=   ddpm(generator=lowerCAmelCase			,						num_inference_steps=2			,						output_type="""numpy""")[0]

    _snake_case      :					str					=   image[0, -3:, -3:, -1]
    _snake_case      :					List[Any]					=   image_eps[0, -3:, -3:, -1]

    assert image.shape == (1, 32, 32, 3)
    _snake_case      :					Any					=   1E-2 if torch_device != """mps""" else 3E-2
    assert np.abs(image_slice.flatten() - image_eps_slice.flatten()).max() < tolerance







@slow
@require_torch_gpu
class 						snake_case  (		unittest.TestCase					):



   '''simple docstring'''






   def        UpperCamelCase_					(   self							:     Dict)			->			Optional[Any]:

    """simple docstring"""







    _snake_case      :					Dict					=   """google/ddpm-cifar10-32"""

    _snake_case      :					List[Any]					=   UNetaDModel.from_pretrained(lowerCAmelCase)
    _snake_case      :					Optional[Any]					=   DDPMScheduler.from_pretrained(lowerCAmelCase)

    _snake_case      :					Dict					=   DDPMPipeline(unet=lowerCAmelCase			,						scheduler=lowerCAmelCase)
    ddpm.to(lowerCAmelCase)
    ddpm.set_progress_bar_config(disable=lowerCAmelCase)

    _snake_case      :					Optional[Any]					=   torch.manual_seed(0)
    _snake_case      :					Any					=   ddpm(generator=lowerCAmelCase			,						output_type="""numpy""").images

    _snake_case      :					Optional[Any]					=   image[0, -3:, -3:, -1]

    assert image.shape == (1, 32, 32, 3)
    _snake_case      :					Union[str, Any]					=   np.array([0.4_200, 0.3_588, 0.1_939, 0.3_847, 0.3_382, 0.2_647, 0.4_155, 0.3_582, 0.3_385])
    assert np.abs(image_slice.flatten() - expected_slice).max() < 1E-2
