code: 
  
  
  
  
  
  
  import json
  import os
  import shutil
  import tempfile
  import unittest
  
  from transformers import is_speech_available, is_torch_available
  from transformers.models.speechta import SpeechTaTokenizer
  from transformers.testing_utils import get_tests_dir, require_torch
  from transformers.utils import FEATURE_EXTRACTOR_NAME
  
  
  if is_speech_available() and is_torch_available():
     from transformers import SpeechTaFeatureExtractor, SpeechTaProcessor
  
     from .test_feature_extraction_speechta import floats_list
  
  
  lowerCamelCase										=  get_tests_dir('''fixtures/test_sentencepiece_bpe_char.model''')
  
  @require_torch
  class 					snake_case_							(       unittest.TestCase      ):
  
     def 			SCREAMING_SNAKE_CASE__				(    self:	List[str]	)  ->		Union[str, Any]:
  
  
         """simple docstring"""
  
  
  
         _lowercase     =	tempfile.mkdtemp()
  
         _lowercase     =	SpeechTaTokenizer(snake_case__	)
         tokenizer.save_pretrained(self.tmpdirname	)
  
         _lowercase     =	{
             'feature_size': 1,
             'padding_value': 0.0,
             'sampling_rate': 16_000,
             'do_normalize': False,
             'num_mel_bins': 80,
             'hop_length': 16,
             'win_length': 64,
             'win_function': 'hann_window',
             'fmin': 80,
             'fmax': 7_600,
             'mel_floor': 1E-10,
             'reduction_factor': 2,
             'return_attention_mask': True,
         }
  
         _lowercase     =	os.path.join(self.tmpdirname   ,       snake_case__	)
         with open(self.feature_extraction_file   ,       'w'   ,       encoding='utf-8'	) as fp:
             fp.write(json.dumps(snake_case__	) + '\n'	)
  
     def 			SCREAMING_SNAKE_CASE__				(    self:	Union[str, Any]   ,       **snake_case__:	Union[str, Any]	)  ->		Union[str, Any]:
  
  
         """simple docstring"""
  
  
  
         return SpeechTaTokenizer.from_pretrained(self.tmpdirname   ,       **snake_case__	)
  
     def 			SCREAMING_SNAKE_CASE__				(    self:	Optional[int]   ,       **snake_case__:	str	)  ->		int:
  
  
         """simple docstring"""
  
  
  
         return SpeechTaFeatureExtractor.from_pretrained(self.tmpdirname   ,       **snake_case__	)
  
     def 			SCREAMING_SNAKE_CASE__				(    self:	Optional[Any]	)  ->		Optional[int]:
  
  
         """simple docstring"""
  
  
  
         shutil.rmtree(self.tmpdirname	)
  
     def 			SCREAMING_SNAKE_CASE__				(    self:	int	)  ->		List[str]:
  
  
         """simple docstring"""
  
  
  
         _lowercase     =	self.get_tokenizer()
         _lowercase     =	self.get_feature_extractor()
  
         _lowercase     =	SpeechTaProcessor(tokenizer=snake_case__   ,       feature_extractor=snake_case__	)
  
         processor.save_pretrained(self.tmpdirname	)
         _lowercase     =	SpeechTaProcessor.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 			SCREAMING_SNAKE_CASE__				(    self:	int	)  ->		Any:
  
  
         """simple docstring"""
  
  
  
         _lowercase     =	SpeechTaProcessor(tokenizer=self.get_tokenizer()   ,       feature_extractor=self.get_feature_extractor()	)
         processor.save_pretrained(self.tmpdirname	)
  
         _lowercase     =	self.get_tokenizer(bos_token='(BOS)'   ,       eos_token='(EOS)'	)
         _lowercase     =	self.get_feature_extractor(do_normalize=snake_case__   ,       padding_value=1.0	)
  
         _lowercase     =	SpeechTaProcessor.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 			SCREAMING_SNAKE_CASE__				(    self:	Union[str, Any]	)  ->		int:
  
  
         """simple docstring"""
  
  
  
         _lowercase     =	self.get_feature_extractor()
         _lowercase     =	self.get_tokenizer()
  
         _lowercase     =	SpeechTaProcessor(tokenizer=snake_case__   ,       feature_extractor=snake_case__	)
  
         _lowercase     =	floats_list((3, 1_000)	)
  
         _lowercase     =	feature_extractor(audio=snake_case__   ,       return_tensors='np'	)
         _lowercase     =	processor(audio=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 			SCREAMING_SNAKE_CASE__				(    self:	Tuple	)  ->		List[str]:
  
  
         """simple docstring"""
  
  
  
         _lowercase     =	self.get_feature_extractor()
         _lowercase     =	self.get_tokenizer()
  
         _lowercase     =	SpeechTaProcessor(tokenizer=snake_case__   ,       feature_extractor=snake_case__	)
  
         _lowercase     =	floats_list((3, 1_000)	)
  
         _lowercase     =	feature_extractor(audio_target=snake_case__   ,       return_tensors='np'	)
         _lowercase     =	processor(audio_target=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 			SCREAMING_SNAKE_CASE__				(    self:	Dict	)  ->		Dict:
  
  
         """simple docstring"""
  
  
  
         _lowercase     =	self.get_feature_extractor()
         _lowercase     =	self.get_tokenizer()
  
         _lowercase     =	SpeechTaProcessor(tokenizer=snake_case__   ,       feature_extractor=snake_case__	)
  
         _lowercase     =	'This is a test string'
  
         _lowercase     =	processor(text=snake_case__	)
         _lowercase     =	tokenizer(snake_case__	)
  
         for key in encoded_tok.keys():
             self.assertListEqual(encoded_tok[key]   ,       encoded_processor[key]	)
  
     def 			SCREAMING_SNAKE_CASE__				(    self:	Optional[int]	)  ->		int:
  
  
         """simple docstring"""
  
  
  
         _lowercase     =	self.get_feature_extractor()
         _lowercase     =	self.get_tokenizer()
  
         _lowercase     =	SpeechTaProcessor(tokenizer=snake_case__   ,       feature_extractor=snake_case__	)
  
         _lowercase     =	'This is a test string'
  
         _lowercase     =	processor(text_target=snake_case__	)
         _lowercase     =	tokenizer(snake_case__	)
  
         for key in encoded_tok.keys():
             self.assertListEqual(encoded_tok[key]   ,       encoded_processor[key]	)
  
     def 			SCREAMING_SNAKE_CASE__				(    self:	Dict	)  ->		int:
  
  
         """simple docstring"""
  
  
  
         _lowercase     =	self.get_feature_extractor()
         _lowercase     =	self.get_tokenizer()
  
         _lowercase     =	SpeechTaProcessor(tokenizer=snake_case__   ,       feature_extractor=snake_case__	)
  
         _lowercase     =	[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]
  
         _lowercase     =	processor.batch_decode(snake_case__	)
         _lowercase     =	tokenizer.batch_decode(snake_case__	)
  
         self.assertListEqual(snake_case__   ,       snake_case__	)
  
     def 			SCREAMING_SNAKE_CASE__				(    self:	Optional[Any]	)  ->		List[Any]:
  
  
         """simple docstring"""
  
  
  
         _lowercase     =	self.get_feature_extractor()
         _lowercase     =	self.get_tokenizer()
  
         _lowercase     =	SpeechTaProcessor(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'   ,       )
  
style_context: 
  
  
  
  
  
  
  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
  
  
  lowerCamelCase										=  logging.get_logger(__name__)
  
  class 					snake_case_							(       lowercase_      ):
  
  
  
  
  
  
  
     """simple docstring"""
  
  
  
     __lowerCAmelCase      =						["""pixel_values"""]
  
     def __init__(    self:	int   ,       snake_case__:	bool = True   ,       snake_case__:	Dict[str, int] = None   ,       snake_case__:	PILImageResampling = PIL.Image.NEAREST   ,       snake_case__:	bool = False   ,       snake_case__:	Dict[str, int] = None   ,       snake_case__:	Union[int, float] = 1 / 255   ,       snake_case__:	bool = False   ,       snake_case__:	bool = True   ,       snake_case__:	bool = True   ,       snake_case__:	Optional[Union[float, List[float]]] = None   ,       snake_case__:	Optional[Union[float, List[float]]] = None   ,       snake_case__:	bool = True   ,       **snake_case__:	Optional[Any]   ,       )  ->		None:
  
  
         """simple docstring"""
  
  
  
         super().__init__(**snake_case__	)
         _lowercase     =	size if size is not None else {'height': 346, 'width': 346}
         _lowercase     =	get_size_dict(snake_case__	)
         _lowercase     =	crop_size if crop_size is not None else {'height': 289, 'width': 289}
         _lowercase     =	get_size_dict(snake_case__   ,       param_name='crop_size'	)
  
         _lowercase     =	do_resize
         _lowercase     =	size
         _lowercase     =	resample
         _lowercase     =	do_center_crop
         _lowercase     =	crop_size
         _lowercase     =	do_rescale
         _lowercase     =	rescale_factor
         _lowercase     =	rescale_offset
         _lowercase     =	do_normalize
         _lowercase     =	image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
         _lowercase     =	image_std if image_std is not None else IMAGENET_STANDARD_STD
         _lowercase     =	include_top
  
     def 			SCREAMING_SNAKE_CASE__				(    self:	str   ,       snake_case__:	np.ndarray   ,       snake_case__:	Dict[str, int]   ,       snake_case__:	PILImageResampling = PIL.Image.NEAREST   ,       snake_case__:	Optional[Union[str, ChannelDimension]] = None   ,       **snake_case__:	Optional[int]   ,       )  ->		np.ndarray:
  
  
         """simple docstring"""
  
  
  
         _lowercase     =	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 			SCREAMING_SNAKE_CASE__				(    self:	Optional[Any]   ,       snake_case__:	np.ndarray   ,       snake_case__:	Dict[str, int]   ,       snake_case__:	Optional[Union[str, ChannelDimension]] = None   ,       **snake_case__:	str   ,       )  ->		np.ndarray:
  
  
         """simple docstring"""
  
  
  
         _lowercase     =	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 			SCREAMING_SNAKE_CASE__				(    self:	Optional[Any]   ,       snake_case__:	np.ndarray   ,       snake_case__:	Union[int, float]   ,       snake_case__:	bool = True   ,       snake_case__:	Optional[Union[str, ChannelDimension]] = None   ,       **snake_case__:	List[str]   ,       )  ->		Optional[int]:
  
  
         """simple docstring"""
  
  
  
         if offset:
             _lowercase     =	(image - 1_27.5) * scale
             if data_format is not None:
                 _lowercase     =	to_channel_dimension_format(snake_case__   ,       snake_case__	)
             _lowercase     =	rescaled_image.astype(np.floataa	)
         else:
             _lowercase     =	rescale(snake_case__   ,       scale=snake_case__   ,       data_format=snake_case__   ,       **snake_case__	)
         return rescaled_image
  
     def 			SCREAMING_SNAKE_CASE__				(    self:	Dict   ,       snake_case__:	np.ndarray   ,       snake_case__:	Union[float, List[float]]   ,       snake_case__:	Union[float, List[float]]   ,       snake_case__:	Optional[Union[str, ChannelDimension]] = None   ,       **snake_case__:	Any   ,       )  ->		np.ndarray:
  
  
         """simple docstring"""
  
  
  
         return normalize(snake_case__   ,       mean=snake_case__   ,       std=snake_case__   ,       data_format=snake_case__   ,       **snake_case__	)
  
     def 			SCREAMING_SNAKE_CASE__				(    self:	Optional[int]   ,       snake_case__:	ImageInput   ,       snake_case__:	bool = None   ,       snake_case__:	Dict[str, int] = None   ,       snake_case__:	Union[str, Any]=None   ,       snake_case__:	bool = None   ,       snake_case__:	Dict[str, int] = None   ,       snake_case__:	bool = None   ,       snake_case__:	float = None   ,       snake_case__:	bool = None   ,       snake_case__:	bool = None   ,       snake_case__:	Optional[Union[float, List[float]]] = None   ,       snake_case__:	Optional[Union[float, List[float]]] = None   ,       snake_case__:	bool = None   ,       snake_case__:	Optional[Union[str, TensorType]] = None   ,       snake_case__:	ChannelDimension = ChannelDimension.FIRST   ,       **snake_case__:	int   ,       )  ->		PIL.Image.Image:
  
  
         """simple docstring"""
  
  
  
         _lowercase     =	do_resize if do_resize is not None else self.do_resize
         _lowercase     =	resample if resample is not None else self.resample
         _lowercase     =	do_center_crop if do_center_crop is not None else self.do_center_crop
         _lowercase     =	do_rescale if do_rescale is not None else self.do_rescale
         _lowercase     =	rescale_factor if rescale_factor is not None else self.rescale_factor
         _lowercase     =	rescale_offset if rescale_offset is not None else self.rescale_offset
         _lowercase     =	do_normalize if do_normalize is not None else self.do_normalize
         _lowercase     =	image_mean if image_mean is not None else self.image_mean
         _lowercase     =	image_std if image_std is not None else self.image_std
         _lowercase     =	include_top if include_top is not None else self.include_top
  
         _lowercase     =	size if size is not None else self.size
         _lowercase     =	get_size_dict(snake_case__	)
         _lowercase     =	crop_size if crop_size is not None else self.crop_size
         _lowercase     =	get_size_dict(snake_case__   ,       param_name='crop_size'	)
  
         _lowercase     =	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.
         _lowercase     =	[to_numpy_array(snake_case__	) for image in images]
  
         if do_resize:
             _lowercase     =	[self.resize(image=snake_case__   ,       size=snake_case__   ,       resample=snake_case__	) for image in images]
  
         if do_center_crop:
             _lowercase     =	[self.center_crop(image=snake_case__   ,       size=snake_case__	) for image in images]
  
         if do_rescale:
             _lowercase     =	[self.rescale(image=snake_case__   ,       scale=snake_case__   ,       offset=snake_case__	) for image in images]
  
         if do_normalize:
             _lowercase     =	[self.normalize(image=snake_case__   ,       mean=snake_case__   ,       std=snake_case__	) for image in images]
  
         if include_top:
             _lowercase     =	[self.normalize(image=snake_case__   ,       mean=[0, 0, 0]   ,       std=snake_case__	) for image in images]
  
         _lowercase     =	[to_channel_dimension_format(snake_case__   ,       snake_case__	) for image in images]
  
         _lowercase     =	{'pixel_values': images}
         return BatchFeature(data=snake_case__   ,       tensor_type=snake_case__	)
  