

'''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 (
    center_crop,
    convert_to_rgb,
    get_resize_output_image_size,
    normalize,
    rescale,
    resize,
    to_channel_dimension_format,
)
from ...image_utils import (
    OPENAI_CLIP_MEAN,
    OPENAI_CLIP_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_vision_available, logging


A_      =							logging.get_logger(__name__)


if is_vision_available():
    import PIL





class 							_snake_case	(    _a  ):
   _A    :       List[str]      				=   ['''pixel_values''']
   def __init__(	self     : int       ,SCREAMING_SNAKE_CASE__     : bool = True       ,SCREAMING_SNAKE_CASE__     : Dict[str, int] = None       ,SCREAMING_SNAKE_CASE__     : PILImageResampling = PILImageResampling.BICUBIC       ,SCREAMING_SNAKE_CASE__     : bool = True       ,SCREAMING_SNAKE_CASE__     : Dict[str, int] = None       ,SCREAMING_SNAKE_CASE__     : bool = True       ,SCREAMING_SNAKE_CASE__     : Union[int, float] = 1 / 255       ,SCREAMING_SNAKE_CASE__     : bool = True       ,SCREAMING_SNAKE_CASE__     : Optional[Union[float, List[float]]] = None       ,SCREAMING_SNAKE_CASE__     : Optional[Union[float, List[float]]] = None       ,SCREAMING_SNAKE_CASE__     : bool = True       ,**SCREAMING_SNAKE_CASE__     : int       ,):
     super().__init__(**SCREAMING_SNAKE_CASE__							)
     SCREAMING_SNAKE_CASE:Optional[int]   =   size if size is not None else {"shortest_edge": 224}
     SCREAMING_SNAKE_CASE:str   =   get_size_dict(SCREAMING_SNAKE_CASE__       ,default_to_square=SCREAMING_SNAKE_CASE__							)
     SCREAMING_SNAKE_CASE:Tuple   =   crop_size if crop_size is not None else {"height": 224, "width": 224}
     SCREAMING_SNAKE_CASE:Any   =   get_size_dict(SCREAMING_SNAKE_CASE__							)

     SCREAMING_SNAKE_CASE:Tuple   =   do_resize
     SCREAMING_SNAKE_CASE:Any   =   size
     SCREAMING_SNAKE_CASE:Any   =   resample
     SCREAMING_SNAKE_CASE:Tuple   =   do_center_crop
     SCREAMING_SNAKE_CASE:List[str]   =   crop_size
     SCREAMING_SNAKE_CASE:Dict   =   do_rescale
     SCREAMING_SNAKE_CASE:Optional[Any]   =   rescale_factor
     SCREAMING_SNAKE_CASE:Union[str, Any]   =   do_normalize
     SCREAMING_SNAKE_CASE:List[str]   =   image_mean if image_mean is not None else OPENAI_CLIP_MEAN
     SCREAMING_SNAKE_CASE:Dict   =   image_std if image_std is not None else OPENAI_CLIP_STD
     SCREAMING_SNAKE_CASE:Any   =   do_convert_rgb
   def       __UpperCamelCase  (	self     : int       ,SCREAMING_SNAKE_CASE__     : np.ndarray       ,SCREAMING_SNAKE_CASE__     : Dict[str, int]       ,SCREAMING_SNAKE_CASE__     : PILImageResampling = PILImageResampling.BICUBIC       ,SCREAMING_SNAKE_CASE__     : Optional[Union[str, ChannelDimension]] = None       ,**SCREAMING_SNAKE_CASE__     : str       ,):
     SCREAMING_SNAKE_CASE:List[str]   =   get_size_dict(SCREAMING_SNAKE_CASE__       ,default_to_square=SCREAMING_SNAKE_CASE__							)
     SCREAMING_SNAKE_CASE:str   =   get_resize_output_image_size(
         SCREAMING_SNAKE_CASE__       ,size=(size["height"], size["width"])       ,default_to_square=SCREAMING_SNAKE_CASE__							)
     return resize(SCREAMING_SNAKE_CASE__       ,size=SCREAMING_SNAKE_CASE__       ,resample=SCREAMING_SNAKE_CASE__       ,data_format=SCREAMING_SNAKE_CASE__       ,**SCREAMING_SNAKE_CASE__							)
   def       __UpperCamelCase  (	self     : Union[str, Any]       ,SCREAMING_SNAKE_CASE__     : np.ndarray       ,SCREAMING_SNAKE_CASE__     : Dict[str, int]       ,SCREAMING_SNAKE_CASE__     : Optional[Union[str, ChannelDimension]] = None       ,**SCREAMING_SNAKE_CASE__     : Optional[Any]       ,):
     SCREAMING_SNAKE_CASE:str   =   get_size_dict(SCREAMING_SNAKE_CASE__							)
     return center_crop(SCREAMING_SNAKE_CASE__       ,size=(size["height"], size["width"])       ,data_format=SCREAMING_SNAKE_CASE__       ,**SCREAMING_SNAKE_CASE__							)
   def       __UpperCamelCase  (	self     : Dict       ,SCREAMING_SNAKE_CASE__     : np.ndarray       ,SCREAMING_SNAKE_CASE__     : Union[int, float]       ,SCREAMING_SNAKE_CASE__     : Optional[Union[str, ChannelDimension]] = None       ,**SCREAMING_SNAKE_CASE__     : Optional[int]       ,):
     return rescale(SCREAMING_SNAKE_CASE__       ,scale=SCREAMING_SNAKE_CASE__       ,data_format=SCREAMING_SNAKE_CASE__       ,**SCREAMING_SNAKE_CASE__							)
   def       __UpperCamelCase  (	self     : Tuple       ,SCREAMING_SNAKE_CASE__     : np.ndarray       ,SCREAMING_SNAKE_CASE__     : Union[float, List[float]]       ,SCREAMING_SNAKE_CASE__     : Union[float, List[float]]       ,SCREAMING_SNAKE_CASE__     : Optional[Union[str, ChannelDimension]] = None       ,**SCREAMING_SNAKE_CASE__     : Any       ,):
     return normalize(SCREAMING_SNAKE_CASE__       ,mean=SCREAMING_SNAKE_CASE__       ,std=SCREAMING_SNAKE_CASE__       ,data_format=SCREAMING_SNAKE_CASE__       ,**SCREAMING_SNAKE_CASE__							)







   def       __UpperCamelCase  (	self     : List[str]       ,SCREAMING_SNAKE_CASE__     : ImageInput       ,SCREAMING_SNAKE_CASE__     : bool = None       ,SCREAMING_SNAKE_CASE__     : Dict[str, int] = None       ,SCREAMING_SNAKE_CASE__     : PILImageResampling = None       ,SCREAMING_SNAKE_CASE__     : bool = None       ,SCREAMING_SNAKE_CASE__     : int = None       ,SCREAMING_SNAKE_CASE__     : bool = None       ,SCREAMING_SNAKE_CASE__     : float = None       ,SCREAMING_SNAKE_CASE__     : bool = None       ,SCREAMING_SNAKE_CASE__     : Optional[Union[float, List[float]]] = None       ,SCREAMING_SNAKE_CASE__     : Optional[Union[float, List[float]]] = None       ,SCREAMING_SNAKE_CASE__     : bool = None       ,SCREAMING_SNAKE_CASE__     : Optional[Union[str, TensorType]] = None       ,SCREAMING_SNAKE_CASE__     : Optional[ChannelDimension] = ChannelDimension.FIRST       ,**SCREAMING_SNAKE_CASE__     : Any       ,):
     SCREAMING_SNAKE_CASE:List[Any]   =   do_resize if do_resize is not None else self.do_resize
     SCREAMING_SNAKE_CASE:int   =   size if size is not None else self.size
     SCREAMING_SNAKE_CASE:Dict   =   get_size_dict(SCREAMING_SNAKE_CASE__       ,default_to_square=SCREAMING_SNAKE_CASE__							)
     SCREAMING_SNAKE_CASE:int   =   resample if resample is not None else self.resample
     SCREAMING_SNAKE_CASE:Optional[int]   =   do_center_crop if do_center_crop is not None else self.do_center_crop
     SCREAMING_SNAKE_CASE:Any   =   crop_size if crop_size is not None else self.crop_size
     SCREAMING_SNAKE_CASE:Dict   =   get_size_dict(SCREAMING_SNAKE_CASE__							)
     SCREAMING_SNAKE_CASE:Optional[int]   =   do_rescale if do_rescale is not None else self.do_rescale
     SCREAMING_SNAKE_CASE:Any   =   rescale_factor if rescale_factor is not None else self.rescale_factor
     SCREAMING_SNAKE_CASE:Tuple   =   do_normalize if do_normalize is not None else self.do_normalize
     SCREAMING_SNAKE_CASE:Tuple   =   image_mean if image_mean is not None else self.image_mean
     SCREAMING_SNAKE_CASE:Dict   =   image_std if image_std is not None else self.image_std
     SCREAMING_SNAKE_CASE:Union[str, Any]   =   do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb

     SCREAMING_SNAKE_CASE:Any   =   make_list_of_images(SCREAMING_SNAKE_CASE__							)

     if not valid_images(SCREAMING_SNAKE_CASE__							):
       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_center_crop and crop_size is None:
       raise ValueError("Crop size must be specified if do_center_crop is True."							)

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

     if do_normalize and (image_mean is None or image_std is None):
       raise ValueError("Image mean and std must be specified if do_normalize is True."							)

     # PIL RGBA images are converted to RGB
     if do_convert_rgb:
       SCREAMING_SNAKE_CASE:Any   =   [convert_to_rgb(SCREAMING_SNAKE_CASE__							) for image in images]

     # All transformations expect numpy arrays.
     SCREAMING_SNAKE_CASE:Tuple   =   [to_numpy_array(SCREAMING_SNAKE_CASE__							) for image in images]

     if do_resize:
       SCREAMING_SNAKE_CASE:Dict   =   [self.resize(image=SCREAMING_SNAKE_CASE__       ,size=SCREAMING_SNAKE_CASE__       ,resample=SCREAMING_SNAKE_CASE__							) for image in images]

     if do_center_crop:
       SCREAMING_SNAKE_CASE:Union[str, Any]   =   [self.center_crop(image=SCREAMING_SNAKE_CASE__       ,size=SCREAMING_SNAKE_CASE__							) for image in images]

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

     if do_normalize:
       SCREAMING_SNAKE_CASE:List[Any]   =   [self.normalize(image=SCREAMING_SNAKE_CASE__       ,mean=SCREAMING_SNAKE_CASE__       ,std=SCREAMING_SNAKE_CASE__							) for image in images]

     SCREAMING_SNAKE_CASE:Union[str, Any]   =   [to_channel_dimension_format(SCREAMING_SNAKE_CASE__       ,SCREAMING_SNAKE_CASE__							) for image in images]

     SCREAMING_SNAKE_CASE:int   =   {"pixel_values": images}
     return BatchFeature(data=SCREAMING_SNAKE_CASE__       ,tensor_type=SCREAMING_SNAKE_CASE__							)









'''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_      =							5_03_58
A_      =							5_03_62





@require_torch
@require_torchaudio
@require_sentencepiece
class 							_snake_case	(    unittest.TestCase  ):
   def       __UpperCamelCase  (	self     : Any							):
     SCREAMING_SNAKE_CASE:int   =   "openai/whisper-small.en"
     SCREAMING_SNAKE_CASE:Optional[Any]   =   tempfile.mkdtemp()
   def       __UpperCamelCase  (	self     : Any       ,**SCREAMING_SNAKE_CASE__     : List[str]							):
     return WhisperTokenizer.from_pretrained(self.checkpoint       ,**SCREAMING_SNAKE_CASE__							)
   def       __UpperCamelCase  (	self     : List[str]       ,**SCREAMING_SNAKE_CASE__     : Dict							):
     return WhisperFeatureExtractor.from_pretrained(self.checkpoint       ,**SCREAMING_SNAKE_CASE__							)
   def       __UpperCamelCase  (	self     : str							):
     shutil.rmtree(self.tmpdirname							)
   def       __UpperCamelCase  (	self     : Any							):
     SCREAMING_SNAKE_CASE:Dict   =   self.get_tokenizer()
     SCREAMING_SNAKE_CASE:Optional[int]   =   self.get_feature_extractor()

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

     processor.save_pretrained(self.tmpdirname							)
     SCREAMING_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       __UpperCamelCase  (	self     : Union[str, Any]							):
     SCREAMING_SNAKE_CASE:int   =   WhisperProcessor(tokenizer=self.get_tokenizer()       ,feature_extractor=self.get_feature_extractor()							)
     processor.save_pretrained(self.tmpdirname							)

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

     SCREAMING_SNAKE_CASE: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       __UpperCamelCase  (	self     : List[Any]							):
     SCREAMING_SNAKE_CASE:Any   =   self.get_feature_extractor()
     SCREAMING_SNAKE_CASE:List[Any]   =   self.get_tokenizer()

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

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

     SCREAMING_SNAKE_CASE:Any   =   feature_extractor(SCREAMING_SNAKE_CASE__       ,return_tensors="np"							)
     SCREAMING_SNAKE_CASE:List[str]   =   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       __UpperCamelCase  (	self     : List[Any]							):
     SCREAMING_SNAKE_CASE:int   =   self.get_feature_extractor()
     SCREAMING_SNAKE_CASE:Tuple   =   self.get_tokenizer()

     SCREAMING_SNAKE_CASE:Union[str, Any]   =   WhisperProcessor(tokenizer=SCREAMING_SNAKE_CASE__       ,feature_extractor=SCREAMING_SNAKE_CASE__							)

     SCREAMING_SNAKE_CASE:str   =   "This is a test string"

     SCREAMING_SNAKE_CASE:int   =   processor(text=SCREAMING_SNAKE_CASE__							)

     SCREAMING_SNAKE_CASE:str   =   tokenizer(SCREAMING_SNAKE_CASE__							)

     for key in encoded_tok.keys():
       self.assertListEqual(encoded_tok[key]       ,encoded_processor[key]							)
   def       __UpperCamelCase  (	self     : str							):
     SCREAMING_SNAKE_CASE:List[Any]   =   self.get_feature_extractor()
     SCREAMING_SNAKE_CASE:List[str]   =   self.get_tokenizer()

     SCREAMING_SNAKE_CASE:Union[str, Any]   =   WhisperProcessor(tokenizer=SCREAMING_SNAKE_CASE__       ,feature_extractor=SCREAMING_SNAKE_CASE__							)

     SCREAMING_SNAKE_CASE:Optional[Any]   =   [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

     SCREAMING_SNAKE_CASE:Any   =   processor.batch_decode(SCREAMING_SNAKE_CASE__							)
     SCREAMING_SNAKE_CASE:Union[str, Any]   =   tokenizer.batch_decode(SCREAMING_SNAKE_CASE__							)

     self.assertListEqual(SCREAMING_SNAKE_CASE__       ,SCREAMING_SNAKE_CASE__							)
   def       __UpperCamelCase  (	self     : int							):
     SCREAMING_SNAKE_CASE:Tuple   =   self.get_feature_extractor()
     SCREAMING_SNAKE_CASE:Optional[int]   =   self.get_tokenizer()

     SCREAMING_SNAKE_CASE:Union[str, Any]   =   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       __UpperCamelCase  (	self     : int							):
     SCREAMING_SNAKE_CASE:Tuple   =   self.get_feature_extractor()
     SCREAMING_SNAKE_CASE:Any   =   self.get_tokenizer()

     SCREAMING_SNAKE_CASE:Any   =   WhisperProcessor(tokenizer=SCREAMING_SNAKE_CASE__       ,feature_extractor=SCREAMING_SNAKE_CASE__							)
     SCREAMING_SNAKE_CASE:Optional[int]   =   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)							)

     SCREAMING_SNAKE_CASE:Any   =   [TRANSCRIBE, NOTIMESTAMPS]
     self.assertListEqual([ids[-1] for ids in forced_decoder_ids]       ,SCREAMING_SNAKE_CASE__							)
   def       __UpperCamelCase  (	self     : Dict							):
     SCREAMING_SNAKE_CASE:Tuple   =   WhisperProcessor(tokenizer=self.get_tokenizer()       ,feature_extractor=self.get_feature_extractor()							)
     SCREAMING_SNAKE_CASE:Dict   =   processor.get_prompt_ids("Mr. Quilter"							)
     SCREAMING_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       __UpperCamelCase  (	self     : Union[str, Any]							):
     SCREAMING_SNAKE_CASE:Dict   =   WhisperProcessor(tokenizer=self.get_tokenizer()       ,feature_extractor=self.get_feature_extractor()							)
     SCREAMING_SNAKE_CASE:List[Any]   =   processor.get_prompt_ids(""							)
     SCREAMING_SNAKE_CASE:Tuple   =   processor.tokenizer.decode(SCREAMING_SNAKE_CASE__							)

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







   def       __UpperCamelCase  (	self     : Union[str, Any]							):
     SCREAMING_SNAKE_CASE:Tuple   =   WhisperProcessor(tokenizer=self.get_tokenizer()       ,feature_extractor=self.get_feature_extractor()							)

     def _test_prompt_error_raised_helper(SCREAMING_SNAKE_CASE__     : int       ,SCREAMING_SNAKE_CASE__     : List[str]							):
       with pytest.raises(SCREAMING_SNAKE_CASE__							) as excinfo:
         processor.get_prompt_ids(SCREAMING_SNAKE_CASE__							)
       SCREAMING_SNAKE_CASE:List[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|>"							)






