




"""simple docstring"""

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 		=			50_358
__A 		=			50_362
@require_torch
@require_torchaudio
@require_sentencepiece
class 			lowerCamelCase__    (  unittest.TestCase			):


			def 							lowerCamelCase_				(     self							):



					"""simple docstring"""







					snake_case		:						List[str]								=				"openai/whisper-small.en"
					snake_case		:						Tuple								=				tempfile.mkdtemp()


			def 							lowerCamelCase_				(     self				, **SCREAMING_SNAKE_CASE							):



					"""simple docstring"""







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


			def 							lowerCamelCase_				(     self				, **SCREAMING_SNAKE_CASE							):



					"""simple docstring"""







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


			def 							lowerCamelCase_				(     self							):



					"""simple docstring"""







					shutil.rmtree(self.tmpdirname							)


			def 							lowerCamelCase_				(     self							):



					"""simple docstring"""







					snake_case		:						str								=				self.get_tokenizer()
					snake_case		:						Dict								=				self.get_feature_extractor()

					snake_case		:						Any								=				WhisperProcessor(tokenizer=SCREAMING_SNAKE_CASE				, feature_extractor=SCREAMING_SNAKE_CASE							)

					processor.save_pretrained(self.tmpdirname							)
					snake_case		:						List[Any]								=				WhisperProcessor.from_pretrained(self.tmpdirname							)

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

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


			def 							lowerCamelCase_				(     self							):



					"""simple docstring"""







					snake_case		:						Tuple								=				WhisperProcessor(tokenizer=self.get_tokenizer()				, feature_extractor=self.get_feature_extractor()							)
					processor.save_pretrained(self.tmpdirname							)

					snake_case		:						Union[str, Any]								=				self.get_tokenizer(bos_token="(BOS)"				, eos_token="(EOS)"							)
					snake_case		:						List[str]								=				self.get_feature_extractor(do_normalize=SCREAMING_SNAKE_CASE				, padding_value=1.0							)

					snake_case		:						Union[str, Any]								=				WhisperProcessor.from_pretrained(
					    self.tmpdirname				, bos_token="(BOS)"				, eos_token="(EOS)"				, do_normalize=SCREAMING_SNAKE_CASE				, padding_value=1.0							)

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

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


			def 							lowerCamelCase_				(     self							):



					"""simple docstring"""







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

					snake_case		:						int								=				WhisperProcessor(tokenizer=SCREAMING_SNAKE_CASE				, feature_extractor=SCREAMING_SNAKE_CASE							)

					snake_case		:						Any								=				floats_list((3, 1_000)							)

					snake_case		:						Dict								=				feature_extractor(SCREAMING_SNAKE_CASE				, return_tensors="np"							)
					snake_case		:						Dict								=				processor(SCREAMING_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 							lowerCamelCase_				(     self							):



					"""simple docstring"""







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

					snake_case		:						Dict								=				WhisperProcessor(tokenizer=SCREAMING_SNAKE_CASE				, feature_extractor=SCREAMING_SNAKE_CASE							)

					snake_case		:						Any								=				"This is a test string"

					snake_case		:						str								=				processor(text=SCREAMING_SNAKE_CASE							)

					snake_case		:						Union[str, Any]								=				tokenizer(SCREAMING_SNAKE_CASE							)

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


			def 							lowerCamelCase_				(     self							):



					"""simple docstring"""







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

					snake_case		:						Optional[Any]								=				WhisperProcessor(tokenizer=SCREAMING_SNAKE_CASE				, feature_extractor=SCREAMING_SNAKE_CASE							)

					snake_case		:						int								=				[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

					snake_case		:						Optional[int]								=				processor.batch_decode(SCREAMING_SNAKE_CASE							)
					snake_case		:						Optional[int]								=				tokenizer.batch_decode(SCREAMING_SNAKE_CASE							)

					self.assertListEqual(SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE							)


			def 							lowerCamelCase_				(     self							):



					"""simple docstring"""







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

					snake_case		:						Dict								=				WhisperProcessor(tokenizer=SCREAMING_SNAKE_CASE				, feature_extractor=SCREAMING_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 							lowerCamelCase_				(     self							):



					"""simple docstring"""







					snake_case		:						Dict								=				self.get_feature_extractor()
					snake_case		:						Any								=				self.get_tokenizer()

					snake_case		:						Dict								=				WhisperProcessor(tokenizer=SCREAMING_SNAKE_CASE				, feature_extractor=SCREAMING_SNAKE_CASE							)
					snake_case		:						Tuple								=				processor.get_decoder_prompt_ids(task="transcribe"				, no_timestamps=SCREAMING_SNAKE_CASE							)

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

					snake_case		:						int								=				[TRANSCRIBE, NOTIMESTAMPS]
					self.assertListEqual([ids[-1] for ids in forced_decoder_ids]				, SCREAMING_SNAKE_CASE							)


			def 							lowerCamelCase_				(     self							):



					"""simple docstring"""







					snake_case		:						List[str]								=				WhisperProcessor(tokenizer=self.get_tokenizer()				, feature_extractor=self.get_feature_extractor()							)
					snake_case		:						Any								=				processor.get_prompt_ids("Mr. Quilter"							)
					snake_case		:						int								=				processor.tokenizer.decode(SCREAMING_SNAKE_CASE							)

					self.assertListEqual(prompt_ids.tolist()				, [50_360, 1_770, 13, 2_264, 346, 353]							)
					self.assertEqual(SCREAMING_SNAKE_CASE				, "<|startofprev|> Mr. Quilter"							)


			def 							lowerCamelCase_				(     self							):



					"""simple docstring"""







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

					self.assertListEqual(prompt_ids.tolist()				, [50_360, 220]							)
					self.assertEqual(SCREAMING_SNAKE_CASE				, "<|startofprev|> "							)



			def 							lowerCamelCase_				(     self							):



					"""simple docstring"""







					snake_case		:						Optional[Any]								=				WhisperProcessor(tokenizer=self.get_tokenizer()				, feature_extractor=self.get_feature_extractor()							)

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









"""simple docstring"""

import unittest

import numpy as np
import PIL
import torch

from diffusers.image_processor import VaeImageProcessor
class 			lowerCamelCase__    (  unittest.TestCase			):


			@property
			def 							lowerCamelCase_				(     self							):



					"""simple docstring"""







					snake_case		:						int								=				1
					snake_case		:						Dict								=				3
					snake_case		:						int								=				8
					snake_case		:						int								=				8

					snake_case		:						List[str]								=				torch.rand((batch_size, num_channels, height, width)							)

					return sample


			def 							lowerCamelCase_				(     self				, SCREAMING_SNAKE_CASE							):



					"""simple docstring"""







					if isinstance(image[0]				, PIL.Image.Image							):
							return np.stack([np.array(SCREAMING_SNAKE_CASE							) for i in image]				, axis=0							)
					elif isinstance(SCREAMING_SNAKE_CASE				, torch.Tensor							):
							return image.cpu().numpy().transpose(0				, 2				, 3				, 1							)
					return image


			def 							lowerCamelCase_				(     self							):



					"""simple docstring"""







					snake_case		:						Optional[int]								=				VaeImageProcessor(do_resize=SCREAMING_SNAKE_CASE				, do_normalize=SCREAMING_SNAKE_CASE							)

					snake_case		:						Any								=				self.dummy_sample
					snake_case		:						Optional[Any]								=				self.to_np(SCREAMING_SNAKE_CASE							)

					for output_type in ["pt", "np", "pil"]:
							snake_case		:						Optional[Any]								=				image_processor.postprocess(
							    image_processor.preprocess(SCREAMING_SNAKE_CASE							)				, output_type=SCREAMING_SNAKE_CASE				, )
							snake_case		:						Optional[Any]								=				self.to_np(SCREAMING_SNAKE_CASE							)
							snake_case		:						Any								=				(input_np * 255).round() if output_type == "pil" else input_np
							assert (
							    np.abs(in_np - out_np							).max() < 1E-6
							), F'''decoded output does not match input for output_type {output_type}'''


			def 							lowerCamelCase_				(     self							):



					"""simple docstring"""







					snake_case		:						Dict								=				VaeImageProcessor(do_resize=SCREAMING_SNAKE_CASE				, do_normalize=SCREAMING_SNAKE_CASE							)
					snake_case		:						Tuple								=				self.dummy_sample.cpu().numpy().transpose(0				, 2				, 3				, 1							)

					for output_type in ["pt", "np", "pil"]:
							snake_case		:						Dict								=				image_processor.postprocess(image_processor.preprocess(SCREAMING_SNAKE_CASE							)				, output_type=SCREAMING_SNAKE_CASE							)

							snake_case		:						Union[str, Any]								=				self.to_np(SCREAMING_SNAKE_CASE							)
							snake_case		:						Tuple								=				(input_np * 255).round() if output_type == "pil" else input_np
							assert (
							    np.abs(in_np - out_np							).max() < 1E-6
							), F'''decoded output does not match input for output_type {output_type}'''


			def 							lowerCamelCase_				(     self							):



					"""simple docstring"""







					snake_case		:						Optional[Any]								=				VaeImageProcessor(do_resize=SCREAMING_SNAKE_CASE				, do_normalize=SCREAMING_SNAKE_CASE							)

					snake_case		:						str								=				self.dummy_sample.cpu().numpy().transpose(0				, 2				, 3				, 1							)
					snake_case		:						int								=				image_processor.numpy_to_pil(SCREAMING_SNAKE_CASE							)

					for output_type in ["pt", "np", "pil"]:
							snake_case		:						Dict								=				image_processor.postprocess(image_processor.preprocess(SCREAMING_SNAKE_CASE							)				, output_type=SCREAMING_SNAKE_CASE							)
							for i, o in zip(SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE							):
									snake_case		:						Dict								=				np.array(SCREAMING_SNAKE_CASE							)
									snake_case		:						Tuple								=				self.to_np(SCREAMING_SNAKE_CASE							) if output_type == "pil" else (self.to_np(SCREAMING_SNAKE_CASE							) * 255).round()
									assert (
									    np.abs(in_np - out_np							).max() < 1E-6
									), F'''decoded output does not match input for output_type {output_type}'''


			def 							lowerCamelCase_				(     self							):



					"""simple docstring"""







					snake_case		:						Tuple								=				VaeImageProcessor(do_resize=SCREAMING_SNAKE_CASE				, do_normalize=SCREAMING_SNAKE_CASE							)

					snake_case		:						List[str]								=				self.dummy_sample
					snake_case		:						Optional[int]								=				input_pt_ad.squeeze(0							)

					snake_case		:						Any								=				image_processor.postprocess(
					    image_processor.preprocess(SCREAMING_SNAKE_CASE							)				, output_type="np"				, )
					snake_case		:						Union[str, Any]								=				image_processor.postprocess(
					    image_processor.preprocess(SCREAMING_SNAKE_CASE							)				, output_type="np"				, )

					snake_case		:						Union[str, Any]								=				self.to_np(self.dummy_sample							)
					snake_case		:						Tuple								=				input_np_ad.squeeze(0							)

					snake_case		:						str								=				image_processor.postprocess(
					    image_processor.preprocess(SCREAMING_SNAKE_CASE							)				, output_type="np"				, )
					snake_case		:						Union[str, Any]								=				image_processor.postprocess(
					    image_processor.preprocess(SCREAMING_SNAKE_CASE							)				, output_type="np"				, )

					assert np.abs(out_pt_ad - out_pt_ad							).max() < 1E-6
					assert np.abs(out_np_ad - out_np_ad							).max() < 1E-6



			def 							lowerCamelCase_				(     self							):



					"""simple docstring"""







					snake_case		:						Optional[Any]								=				VaeImageProcessor(do_resize=SCREAMING_SNAKE_CASE				, do_normalize=SCREAMING_SNAKE_CASE							)

					snake_case		:						Union[str, Any]								=				self.dummy_sample
					snake_case		:						List[str]								=				list(SCREAMING_SNAKE_CASE							)

					snake_case		:						str								=				image_processor.postprocess(
					    image_processor.preprocess(SCREAMING_SNAKE_CASE							)				, output_type="np"				, )

					snake_case		:						Any								=				image_processor.postprocess(
					    image_processor.preprocess(SCREAMING_SNAKE_CASE							)				, output_type="np"				, )

					snake_case		:						Dict								=				self.to_np(self.dummy_sample							)
					list(SCREAMING_SNAKE_CASE							)

					snake_case		:						str								=				image_processor.postprocess(
					    image_processor.preprocess(SCREAMING_SNAKE_CASE							)				, output_type="np"				, )

					snake_case		:						Union[str, Any]								=				image_processor.postprocess(
					    image_processor.preprocess(SCREAMING_SNAKE_CASE							)				, output_type="np"				, )

					assert np.abs(out_pt_ad - out_pt_list							).max() < 1E-6
					assert np.abs(out_np_ad - out_np_list							).max() < 1E-6

