

"""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


__UpperCamelCase						:   Any							   =	5_0_3_5_8
__UpperCamelCase						:   int							   =	5_0_3_6_2







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






					"""simple docstring"""



					def  __lowerCAmelCase				( self    :       List[Any]	):
						lowerCAmelCase__			:		int						       =			'''openai/whisper-small.en'''
						lowerCAmelCase__			:		Optional[Any]						       =			tempfile.mkdtemp()







					def  __lowerCAmelCase				( self    :       Dict     ,**lowercase_    :       Dict	):
						return WhisperTokenizer.from_pretrained(self.checkpoint     ,**lowercase_	)







					def  __lowerCAmelCase				( self    :       List[str]     ,**lowercase_    :       Optional[Any]	):
						return WhisperFeatureExtractor.from_pretrained(self.checkpoint     ,**lowercase_	)







					def  __lowerCAmelCase				( self    :       Union[str, Any]	):
						shutil.rmtree(self.tmpdirname	)







					def  __lowerCAmelCase				( self    :       List[Any]	):
						lowerCAmelCase__			:		Optional[int]						       =			self.get_tokenizer()
						lowerCAmelCase__			:		Optional[int]						       =			self.get_feature_extractor()

						lowerCAmelCase__			:		Tuple						       =			WhisperProcessor(tokenizer=lowercase_     ,feature_extractor=lowercase_	)

						processor.save_pretrained(self.tmpdirname	)
						lowerCAmelCase__			:		int						       =			WhisperProcessor.from_pretrained(self.tmpdirname	)

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

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







					def  __lowerCAmelCase				( self    :       List[Any]	):
						lowerCAmelCase__			:		int						       =			WhisperProcessor(tokenizer=self.get_tokenizer()     ,feature_extractor=self.get_feature_extractor()	)
						processor.save_pretrained(self.tmpdirname	)

						lowerCAmelCase__			:		Optional[int]						       =			self.get_tokenizer(bos_token='''(BOS)'''     ,eos_token='''(EOS)'''	)
						lowerCAmelCase__			:		Dict						       =			self.get_feature_extractor(do_normalize=lowercase_     ,padding_value=1.0	)

						lowerCAmelCase__			:		Dict						       =			WhisperProcessor.from_pretrained(
						    self.tmpdirname     ,bos_token='''(BOS)'''     ,eos_token='''(EOS)'''     ,do_normalize=lowercase_     ,padding_value=1.0	)

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

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







					def  __lowerCAmelCase				( self    :       Union[str, Any]	):
						lowerCAmelCase__			:		str						       =			self.get_feature_extractor()
						lowerCAmelCase__			:		Optional[Any]						       =			self.get_tokenizer()

						lowerCAmelCase__			:		int						       =			WhisperProcessor(tokenizer=lowercase_     ,feature_extractor=lowercase_	)

						lowerCAmelCase__			:		Optional[Any]						       =			floats_list((3, 1_0_0_0)	)

						lowerCAmelCase__			:		Optional[int]						       =			feature_extractor(lowercase_     ,return_tensors='''np'''	)
						lowerCAmelCase__			:		Tuple						       =			processor(lowercase_     ,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    :       Dict	):
						lowerCAmelCase__			:		int						       =			self.get_feature_extractor()
						lowerCAmelCase__			:		Any						       =			self.get_tokenizer()

						lowerCAmelCase__			:		int						       =			WhisperProcessor(tokenizer=lowercase_     ,feature_extractor=lowercase_	)

						lowerCAmelCase__			:		Union[str, Any]						       =			'''This is a test string'''

						lowerCAmelCase__			:		Union[str, Any]						       =			processor(text=lowercase_	)

						lowerCAmelCase__			:		Any						       =			tokenizer(lowercase_	)

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







					def  __lowerCAmelCase				( self    :       str	):
						lowerCAmelCase__			:		List[Any]						       =			self.get_feature_extractor()
						lowerCAmelCase__			:		List[Any]						       =			self.get_tokenizer()

						lowerCAmelCase__			:		List[Any]						       =			WhisperProcessor(tokenizer=lowercase_     ,feature_extractor=lowercase_	)

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

						lowerCAmelCase__			:		Any						       =			processor.batch_decode(lowercase_	)
						lowerCAmelCase__			:		List[str]						       =			tokenizer.batch_decode(lowercase_	)

						self.assertListEqual(lowercase_     ,lowercase_	)







					def  __lowerCAmelCase				( self    :       int	):
						lowerCAmelCase__			:		Dict						       =			self.get_feature_extractor()
						lowerCAmelCase__			:		Tuple						       =			self.get_tokenizer()

						lowerCAmelCase__			:		Optional[Any]						       =			WhisperProcessor(tokenizer=lowercase_     ,feature_extractor=lowercase_	)

						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    :       str	):
						lowerCAmelCase__			:		int						       =			self.get_feature_extractor()
						lowerCAmelCase__			:		Dict						       =			self.get_tokenizer()

						lowerCAmelCase__			:		Any						       =			WhisperProcessor(tokenizer=lowercase_     ,feature_extractor=lowercase_	)
						lowerCAmelCase__			:		Dict						       =			processor.get_decoder_prompt_ids(task='''transcribe'''     ,no_timestamps=lowercase_	)

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

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







					def  __lowerCAmelCase				( self    :       List[str]	):
						lowerCAmelCase__			:		str						       =			WhisperProcessor(tokenizer=self.get_tokenizer()     ,feature_extractor=self.get_feature_extractor()	)
						lowerCAmelCase__			:		str						       =			processor.get_prompt_ids('''Mr. Quilter'''	)
						lowerCAmelCase__			:		Any						       =			processor.tokenizer.decode(lowercase_	)

						self.assertListEqual(prompt_ids.tolist()     ,[5_0_3_6_0, 1_7_7_0, 1_3, 2_2_6_4, 3_4_6, 3_5_3]	)
						self.assertEqual(lowercase_     ,'''<|startofprev|> Mr. Quilter'''	)







					def  __lowerCAmelCase				( self    :       List[str]	):
						lowerCAmelCase__			:		Union[str, Any]						       =			WhisperProcessor(tokenizer=self.get_tokenizer()     ,feature_extractor=self.get_feature_extractor()	)
						lowerCAmelCase__			:		List[Any]						       =			processor.get_prompt_ids(''''''	)
						lowerCAmelCase__			:		str						       =			processor.tokenizer.decode(lowercase_	)

						self.assertListEqual(prompt_ids.tolist()     ,[5_0_3_6_0, 2_2_0]	)
						self.assertEqual(lowercase_     ,'''<|startofprev|> '''	)







					def  __lowerCAmelCase				( self    :       Union[str, Any]	):
						lowerCAmelCase__			:		Any						       =			WhisperProcessor(tokenizer=self.get_tokenizer()     ,feature_extractor=self.get_feature_extractor()	)

						def _test_prompt_error_raised_helper(lowercase_    :       List[Any]     ,lowercase_    :       Tuple	):
							with pytest.raises(lowercase_	) as excinfo:
								processor.get_prompt_ids(lowercase_	)
							lowerCAmelCase__			:		Optional[int]						       =			F'Encountered text in the prompt corresponding to disallowed special token: {special_token}.'
							self.assertEqual(lowercase_     ,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"""





from typing import Dict, List, Optional, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import normalize, rescale, resize, to_channel_dimension_format
from ...image_utils import (
    IMAGENET_STANDARD_MEAN,
    IMAGENET_STANDARD_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, logging


__UpperCamelCase						:   List[Any]							   =	logging.get_logger(__name__)







class        SCREAMING_SNAKE_CASE      (	a_ ):






					"""simple docstring"""



					lowercase__       				= ["pixel_values"]







					def __init__( self    :       Tuple     ,lowercase_    :       bool = True     ,lowercase_    :       Optional[Dict[str, int]] = None     ,lowercase_    :       PILImageResampling = PILImageResampling.BILINEAR     ,lowercase_    :       bool = True     ,lowercase_    :       Union[int, float] = 1 / 2_5_5     ,lowercase_    :       bool = True     ,lowercase_    :       Optional[Union[float, List[float]]] = None     ,lowercase_    :       Optional[Union[float, List[float]]] = None     ,**lowercase_    :       Tuple     ,):
						super().__init__(**lowercase_	)
						lowerCAmelCase__			:		Any						       =			size if size is not None else {'''height''': 2_2_4, '''width''': 2_2_4}
						lowerCAmelCase__			:		Tuple						       =			get_size_dict(lowercase_	)
						lowerCAmelCase__			:		Tuple						       =			do_resize
						lowerCAmelCase__			:		Tuple						       =			do_rescale
						lowerCAmelCase__			:		Union[str, Any]						       =			do_normalize
						lowerCAmelCase__			:		Optional[int]						       =			size
						lowerCAmelCase__			:		List[Any]						       =			resample
						lowerCAmelCase__			:		int						       =			rescale_factor
						lowerCAmelCase__			:		int						       =			image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
						lowerCAmelCase__			:		Any						       =			image_std if image_std is not None else IMAGENET_STANDARD_STD







					def  __lowerCAmelCase				( self    :       List[Any]     ,lowercase_    :       np.ndarray     ,lowercase_    :       Dict[str, int]     ,lowercase_    :       PILImageResampling = PILImageResampling.BILINEAR     ,lowercase_    :       Optional[Union[str, ChannelDimension]] = None     ,**lowercase_    :       str     ,):
						lowerCAmelCase__			:		Union[str, Any]						       =			get_size_dict(lowercase_	)
						if "height" not in size or "width" not in size:
							raise ValueError(F'The `size` dictionary must contain the keys `height` and `width`. Got {size.keys()}'	)
						return resize(
						    lowercase_     ,size=(size['''height'''], size['''width'''])     ,resample=lowercase_     ,data_format=lowercase_     ,**lowercase_	)







					def  __lowerCAmelCase				( self    :       List[Any]     ,lowercase_    :       np.ndarray     ,lowercase_    :       float     ,lowercase_    :       Optional[Union[str, ChannelDimension]] = None     ,**lowercase_    :       Tuple	):
						return rescale(lowercase_     ,scale=lowercase_     ,data_format=lowercase_     ,**lowercase_	)







					def  __lowerCAmelCase				( self    :       Dict     ,lowercase_    :       np.ndarray     ,lowercase_    :       Union[float, List[float]]     ,lowercase_    :       Union[float, List[float]]     ,lowercase_    :       Optional[Union[str, ChannelDimension]] = None     ,**lowercase_    :       Union[str, Any]     ,):
						return normalize(lowercase_     ,mean=lowercase_     ,std=lowercase_     ,data_format=lowercase_     ,**lowercase_	)







					def  __lowerCAmelCase				( self    :       List[str]     ,lowercase_    :       ImageInput     ,lowercase_    :       Optional[bool] = None     ,lowercase_    :       Dict[str, int] = None     ,lowercase_    :       PILImageResampling = None     ,lowercase_    :       Optional[bool] = None     ,lowercase_    :       Optional[float] = None     ,lowercase_    :       Optional[bool] = None     ,lowercase_    :       Optional[Union[float, List[float]]] = None     ,lowercase_    :       Optional[Union[float, List[float]]] = None     ,lowercase_    :       Optional[Union[str, TensorType]] = None     ,lowercase_    :       Union[str, ChannelDimension] = ChannelDimension.FIRST     ,**lowercase_    :       List[Any]     ,):
						lowerCAmelCase__			:		Optional[int]						       =			do_resize if do_resize is not None else self.do_resize
						lowerCAmelCase__			:		Any						       =			do_rescale if do_rescale is not None else self.do_rescale
						lowerCAmelCase__			:		Dict						       =			do_normalize if do_normalize is not None else self.do_normalize
						lowerCAmelCase__			:		Dict						       =			resample if resample is not None else self.resample
						lowerCAmelCase__			:		Dict						       =			rescale_factor if rescale_factor is not None else self.rescale_factor
						lowerCAmelCase__			:		Optional[Any]						       =			image_mean if image_mean is not None else self.image_mean
						lowerCAmelCase__			:		str						       =			image_std if image_std is not None else self.image_std

						lowerCAmelCase__			:		Any						       =			size if size is not None else self.size
						lowerCAmelCase__			:		Dict						       =			get_size_dict(lowercase_	)

						lowerCAmelCase__			:		Union[str, Any]						       =			make_list_of_images(lowercase_	)

						if not valid_images(lowercase_	):
							raise ValueError(
							    '''Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, '''
							    '''torch.Tensor, tf.Tensor or jax.ndarray.'''	)

						if do_resize and size is None:
							raise ValueError('''Size must be specified if do_resize is True.'''	)

						if do_rescale and rescale_factor is None:
							raise ValueError('''Rescale factor must be specified if do_rescale is True.'''	)

						# All transformations expect numpy arrays.
						lowerCAmelCase__			:		Dict						       =			[to_numpy_array(lowercase_	) for image in images]

						if do_resize:
							lowerCAmelCase__			:		Optional[Any]						       =			[self.resize(image=lowercase_     ,size=lowercase_     ,resample=lowercase_	) for image in images]

						if do_rescale:
							lowerCAmelCase__			:		List[Any]						       =			[self.rescale(image=lowercase_     ,scale=lowercase_	) for image in images]

						if do_normalize:
							lowerCAmelCase__			:		Dict						       =			[self.normalize(image=lowercase_     ,mean=lowercase_     ,std=lowercase_	) for image in images]

						lowerCAmelCase__			:		Tuple						       =			[to_channel_dimension_format(lowercase_     ,lowercase_	) for image in images]

						lowerCAmelCase__			:		int						       =			{'''pixel_values''': images}
						return BatchFeature(data=lowercase_     ,tensor_type=lowercase_	)




