





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


UpperCAmelCase__  :	Union[str, Any]     =5_03_58
UpperCAmelCase__  :	Any     =5_03_62

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


				def _snake_case				(						self							):
								lowerCamelCase		       ="""openai/whisper-small.en"""
								lowerCamelCase		       =tempfile.mkdtemp()


				def _snake_case				(						self      ,       **UpperCAmelCase_							):
								return WhisperTokenizer.from_pretrained(self.checkpoint      ,       **UpperCAmelCase_							)


				def _snake_case				(						self      ,       **UpperCAmelCase_							):
								return WhisperFeatureExtractor.from_pretrained(self.checkpoint      ,       **UpperCAmelCase_							)


				def _snake_case				(						self							):
								shutil.rmtree(self.tmpdirname							)


				def _snake_case				(						self							):
								lowerCamelCase		       =self.get_tokenizer()
								lowerCamelCase		       =self.get_feature_extractor()

								lowerCamelCase		       =WhisperProcessor(tokenizer=UpperCAmelCase_      ,       feature_extractor=UpperCAmelCase_							)

								processor.save_pretrained(self.tmpdirname							)
								lowerCamelCase		       =WhisperProcessor.from_pretrained(self.tmpdirname							)

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

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


				def _snake_case				(						self							):
								lowerCamelCase		       =WhisperProcessor(tokenizer=self.get_tokenizer()      ,       feature_extractor=self.get_feature_extractor()							)
								processor.save_pretrained(self.tmpdirname							)

								lowerCamelCase		       =self.get_tokenizer(bos_token="""(BOS)"""      ,       eos_token="""(EOS)"""							)
								lowerCamelCase		       =self.get_feature_extractor(do_normalize=UpperCAmelCase_      ,       padding_value=1.0							)

								lowerCamelCase		       =WhisperProcessor.from_pretrained(
								    self.tmpdirname      ,       bos_token="""(BOS)"""      ,       eos_token="""(EOS)"""      ,       do_normalize=UpperCAmelCase_      ,       padding_value=1.0							)

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

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


				def _snake_case				(						self							):
								lowerCamelCase		       =self.get_feature_extractor()
								lowerCamelCase		       =self.get_tokenizer()

								lowerCamelCase		       =WhisperProcessor(tokenizer=UpperCAmelCase_      ,       feature_extractor=UpperCAmelCase_							)

								lowerCamelCase		       =floats_list((3, 1000)							)

								lowerCamelCase		       =feature_extractor(UpperCAmelCase_      ,       return_tensors="""np"""							)
								lowerCamelCase		       =processor(UpperCAmelCase_      ,       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 _snake_case				(						self							):
								lowerCamelCase		       =self.get_feature_extractor()
								lowerCamelCase		       =self.get_tokenizer()

								lowerCamelCase		       =WhisperProcessor(tokenizer=UpperCAmelCase_      ,       feature_extractor=UpperCAmelCase_							)

								lowerCamelCase		       ="""This is a test string"""

								lowerCamelCase		       =processor(text=UpperCAmelCase_							)

								lowerCamelCase		       =tokenizer(UpperCAmelCase_							)

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


				def _snake_case				(						self							):
								lowerCamelCase		       =self.get_feature_extractor()
								lowerCamelCase		       =self.get_tokenizer()

								lowerCamelCase		       =WhisperProcessor(tokenizer=UpperCAmelCase_      ,       feature_extractor=UpperCAmelCase_							)

								lowerCamelCase		       =[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

								lowerCamelCase		       =processor.batch_decode(UpperCAmelCase_							)
								lowerCamelCase		       =tokenizer.batch_decode(UpperCAmelCase_							)

								self.assertListEqual(UpperCAmelCase_      ,       UpperCAmelCase_							)


				def _snake_case				(						self							):
								lowerCamelCase		       =self.get_feature_extractor()
								lowerCamelCase		       =self.get_tokenizer()

								lowerCamelCase		       =WhisperProcessor(tokenizer=UpperCAmelCase_      ,       feature_extractor=UpperCAmelCase_							)

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


				def _snake_case				(						self							):
								lowerCamelCase		       =self.get_feature_extractor()
								lowerCamelCase		       =self.get_tokenizer()

								lowerCamelCase		       =WhisperProcessor(tokenizer=UpperCAmelCase_      ,       feature_extractor=UpperCAmelCase_							)
								lowerCamelCase		       =processor.get_decoder_prompt_ids(task="""transcribe"""      ,       no_timestamps=UpperCAmelCase_							)

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

								lowerCamelCase		       =[TRANSCRIBE, NOTIMESTAMPS]
								self.assertListEqual([ids[-1] for ids in forced_decoder_ids]      ,       UpperCAmelCase_							)


				def _snake_case				(						self							):
								lowerCamelCase		       =WhisperProcessor(tokenizer=self.get_tokenizer()      ,       feature_extractor=self.get_feature_extractor()							)
								lowerCamelCase		       =processor.get_prompt_ids("""Mr. Quilter"""							)
								lowerCamelCase		       =processor.tokenizer.decode(UpperCAmelCase_							)

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


				def _snake_case				(						self							):
								lowerCamelCase		       =WhisperProcessor(tokenizer=self.get_tokenizer()      ,       feature_extractor=self.get_feature_extractor()							)
								lowerCamelCase		       =processor.get_prompt_ids(""""""							)
								lowerCamelCase		       =processor.tokenizer.decode(UpperCAmelCase_							)

								self.assertListEqual(prompt_ids.tolist()      ,       [50360, 220]							)
								self.assertEqual(UpperCAmelCase_      ,       """<|startofprev|> """							)


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

								def _test_prompt_error_raised_helper(UpperCAmelCase_      ,       UpperCAmelCase_							):
												with pytest.raises(UpperCAmelCase_							) as excinfo:
																processor.get_prompt_ids(UpperCAmelCase_							)
												lowerCamelCase		       =f"""Encountered text in the prompt corresponding to disallowed special token: {special_token}."""
												self.assertEqual(UpperCAmelCase_      ,       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|>"""							)












from dataclasses import dataclass
from typing import List, Optional, Union

import numpy as np
import PIL

from ...utils import BaseOutput, is_invisible_watermark_available, is_torch_available, is_transformers_available

@dataclass
class 					__A   (  a			):
				__A            = 42


if is_transformers_available() and is_torch_available() and is_invisible_watermark_available():
							from .pipeline_stable_diffusion_xl import StableDiffusionXLPipeline
							from .pipeline_stable_diffusion_xl_imgaimg import StableDiffusionXLImgaImgPipeline





