





'''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, 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, is_vision_available, logging


if is_vision_available():
      import PIL


a__			:    Union[str, Any]			      =		logging.get_logger(__name__)





class UpperCAmelCase_							(   A__   ):
  __lowerCAmelCase      :	str  	=      ['''pixel_values''']


  def __init__(       self   ,__snake_case = True   ,__snake_case = None   ,__snake_case = PIL.Image.NEAREST   ,__snake_case = False   ,__snake_case = None   ,__snake_case = 1 / 2_5_5   ,__snake_case = False   ,__snake_case = True   ,__snake_case = True   ,__snake_case = None   ,__snake_case = None   ,__snake_case = True   ,**__snake_case   ,):



       """simple docstring"""





       super().__init__(**__snake_case   )
       A_					      =					size if size is not None else {"height": 3_4_6, "width": 3_4_6}
       A_					      =					get_size_dict(__snake_case   )
       A_					      =					crop_size if crop_size is not None else {"height": 2_8_9, "width": 2_8_9}
       A_					      =					get_size_dict(__snake_case   ,param_name='''crop_size'''   )

       A_					      =					do_resize
       A_					      =					size
       A_					      =					resample
       A_					      =					do_center_crop
       A_					      =					crop_size
       A_					      =					do_rescale
       A_					      =					rescale_factor
       A_					      =					rescale_offset
       A_					      =					do_normalize
       A_					      =					image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
       A_					      =					image_std if image_std is not None else IMAGENET_STANDARD_STD
       A_					      =					include_top


  def 	__UpperCAmelCase    (       self   ,__snake_case   ,__snake_case   ,__snake_case = PIL.Image.NEAREST   ,__snake_case = None   ,**__snake_case   ,):



       """simple docstring"""





       A_					      =					get_size_dict(__snake_case   )
       if "height" not in size or "width" not in size:
            raise ValueError(f'The size dictionary must have keys \'height\' and \'width\'. Got {size.keys()}'   )
       return resize(
           __snake_case   ,size=(size['''height'''], size['''width'''])   ,resample=__snake_case   ,data_format=__snake_case   ,**__snake_case   )


  def 	__UpperCAmelCase    (       self   ,__snake_case   ,__snake_case   ,__snake_case = None   ,**__snake_case   ,):



       """simple docstring"""





       A_					      =					get_size_dict(__snake_case   )
       if "height" not in size or "width" not in size:
            raise ValueError(f'The size dictionary must have keys \'height\' and \'width\'. Got {size.keys()}'   )
       return center_crop(__snake_case   ,size=(size['''height'''], size['''width'''])   ,data_format=__snake_case   ,**__snake_case   )


  def 	__UpperCAmelCase    (       self   ,__snake_case   ,__snake_case   ,__snake_case = True   ,__snake_case = None   ,**__snake_case   ,):



       """simple docstring"""





       if offset:
            A_					      =					(image - 1_2_7.5) * scale
            if data_format is not None:
                 A_					      =					to_channel_dimension_format(__snake_case   ,__snake_case   )
            A_					      =					rescaled_image.astype(np.floataa   )
       else:
            A_					      =					rescale(__snake_case   ,scale=__snake_case   ,data_format=__snake_case   ,**__snake_case   )
       return rescaled_image


  def 	__UpperCAmelCase    (       self   ,__snake_case   ,__snake_case   ,__snake_case   ,__snake_case = None   ,**__snake_case   ,):



       """simple docstring"""





       return normalize(__snake_case   ,mean=__snake_case   ,std=__snake_case   ,data_format=__snake_case   ,**__snake_case   )




  def 	__UpperCAmelCase    (       self   ,__snake_case   ,__snake_case = None   ,__snake_case = None   ,__snake_case=None   ,__snake_case = None   ,__snake_case = None   ,__snake_case = None   ,__snake_case = None   ,__snake_case = None   ,__snake_case = None   ,__snake_case = None   ,__snake_case = None   ,__snake_case = None   ,__snake_case = None   ,__snake_case = ChannelDimension.FIRST   ,**__snake_case   ,):



       """simple docstring"""





       A_					      =					do_resize if do_resize is not None else self.do_resize
       A_					      =					resample if resample is not None else self.resample
       A_					      =					do_center_crop if do_center_crop is not None else self.do_center_crop
       A_					      =					do_rescale if do_rescale is not None else self.do_rescale
       A_					      =					rescale_factor if rescale_factor is not None else self.rescale_factor
       A_					      =					rescale_offset if rescale_offset is not None else self.rescale_offset
       A_					      =					do_normalize if do_normalize is not None else self.do_normalize
       A_					      =					image_mean if image_mean is not None else self.image_mean
       A_					      =					image_std if image_std is not None else self.image_std
       A_					      =					include_top if include_top is not None else self.include_top

       A_					      =					size if size is not None else self.size
       A_					      =					get_size_dict(__snake_case   )
       A_					      =					crop_size if crop_size is not None else self.crop_size
       A_					      =					get_size_dict(__snake_case   ,param_name='''crop_size'''   )

       A_					      =					make_list_of_images(__snake_case   )

       if not valid_images(__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 or resample is None:
            raise ValueError('''Size and resample 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.'''   )

       # All transformations expect numpy arrays.
       A_					      =					[to_numpy_array(__snake_case   ) for image in images]

       if do_resize:
            A_					      =					[self.resize(image=__snake_case   ,size=__snake_case   ,resample=__snake_case   ) for image in images]

       if do_center_crop:
            A_					      =					[self.center_crop(image=__snake_case   ,size=__snake_case   ) for image in images]

       if do_rescale:
            A_					      =					[self.rescale(image=__snake_case   ,scale=__snake_case   ,offset=__snake_case   ) for image in images]

       if do_normalize:
            A_					      =					[self.normalize(image=__snake_case   ,mean=__snake_case   ,std=__snake_case   ) for image in images]

       if include_top:
            A_					      =					[self.normalize(image=__snake_case   ,mean=[0, 0, 0]   ,std=__snake_case   ) for image in images]

       A_					      =					[to_channel_dimension_format(__snake_case   ,__snake_case   ) for image in images]

       A_					      =					{"pixel_values": images}
       return BatchFeature(data=__snake_case   ,tensor_type=__snake_case   )










import shutil
import tempfile
import unittest
from pathlib import Path
from shutil import copyfile

from transformers import SpeechaTextTokenizer, is_speech_available
from transformers.models.speech_to_text.tokenization_speech_to_text import VOCAB_FILES_NAMES, save_json
from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_torch, require_torchaudio
from transformers.utils import FEATURE_EXTRACTOR_NAME

from .test_feature_extraction_speech_to_text import floats_list


if is_speech_available():
      from transformers import SpeechaTextFeatureExtractor, SpeechaTextProcessor


a__			:    Optional[Any]			      =		get_tests_dir('fixtures/test_sentencepiece.model')





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


  def 	__UpperCAmelCase    (       self   ):



       """simple docstring"""





       A_					      =					tempfile.mkdtemp()

       A_					      =					['''<s>''', '''<pad>''', '''</s>''', '''<unk>''', '''▁This''', '''▁is''', '''▁a''', '''▁t''', '''est''']
       A_					      =					dict(zip(__snake_case   ,range(len(__snake_case   )   )   )   )
       A_					      =					Path(self.tmpdirname   )
       save_json(__snake_case   ,save_dir / VOCAB_FILES_NAMES['''vocab_file''']   )
       if not (save_dir / VOCAB_FILES_NAMES["spm_file"]).exists():
            copyfile(__snake_case   ,save_dir / VOCAB_FILES_NAMES['''spm_file''']   )

       A_					      =					SpeechaTextTokenizer.from_pretrained(self.tmpdirname   )
       tokenizer.save_pretrained(self.tmpdirname   )

       A_					      =					{
           '''feature_size''': 2_4,
           '''num_mel_bins''': 2_4,
           '''padding_value''': 0.0,
           '''sampling_rate''': 1_6_0_0_0,
           '''return_attention_mask''': False,
           '''do_normalize''': True,
       }
       save_json(__snake_case   ,save_dir / FEATURE_EXTRACTOR_NAME   )


  def 	__UpperCAmelCase    (       self   ,**__snake_case   ):



       """simple docstring"""





       return SpeechaTextTokenizer.from_pretrained(self.tmpdirname   ,**__snake_case   )


  def 	__UpperCAmelCase    (       self   ,**__snake_case   ):



       """simple docstring"""





       return SpeechaTextFeatureExtractor.from_pretrained(self.tmpdirname   ,**__snake_case   )


  def 	__UpperCAmelCase    (       self   ):



       """simple docstring"""





       shutil.rmtree(self.tmpdirname   )


  def 	__UpperCAmelCase    (       self   ):



       """simple docstring"""





       A_					      =					self.get_tokenizer()
       A_					      =					self.get_feature_extractor()

       A_					      =					SpeechaTextProcessor(tokenizer=__snake_case   ,feature_extractor=__snake_case   )

       processor.save_pretrained(self.tmpdirname   )
       A_					      =					SpeechaTextProcessor.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   ):



       """simple docstring"""





       A_					      =					SpeechaTextProcessor(
           tokenizer=self.get_tokenizer()   ,feature_extractor=self.get_feature_extractor()   )
       processor.save_pretrained(self.tmpdirname   )

       A_					      =					self.get_tokenizer(bos_token='''(BOS)'''   ,eos_token='''(EOS)'''   )
       A_					      =					self.get_feature_extractor(do_normalize=__snake_case   ,padding_value=1.0   )

       A_					      =					SpeechaTextProcessor.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   ):



       """simple docstring"""





       A_					      =					self.get_feature_extractor()
       A_					      =					self.get_tokenizer()

       A_					      =					SpeechaTextProcessor(tokenizer=__snake_case   ,feature_extractor=__snake_case   )

       A_					      =					floats_list((3, 1_0_0_0)   )

       A_					      =					feature_extractor(__snake_case   ,return_tensors='''np'''   )
       A_					      =					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   ):



       """simple docstring"""





       A_					      =					self.get_feature_extractor()
       A_					      =					self.get_tokenizer()

       A_					      =					SpeechaTextProcessor(tokenizer=__snake_case   ,feature_extractor=__snake_case   )

       A_					      =					'''This is a test string'''

       A_					      =					processor(text=__snake_case   )

       A_					      =					tokenizer(__snake_case   )

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


  def 	__UpperCAmelCase    (       self   ):



       """simple docstring"""





       A_					      =					self.get_feature_extractor()
       A_					      =					self.get_tokenizer()

       A_					      =					SpeechaTextProcessor(tokenizer=__snake_case   ,feature_extractor=__snake_case   )

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

       A_					      =					processor.batch_decode(__snake_case   )
       A_					      =					tokenizer.batch_decode(__snake_case   )

       self.assertListEqual(__snake_case   ,__snake_case   )




  def 	__UpperCAmelCase    (       self   ):



       """simple docstring"""





       A_					      =					self.get_feature_extractor()
       A_					      =					self.get_tokenizer()

       A_					      =					SpeechaTextProcessor(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'''   ,)



