






'''simple docstring'''





import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_video_inputs


if is_torch_available():
							import torch

if is_vision_available():
							from PIL import Image

							from transformers import VideoMAEImageProcessor





class 		SCREAMING_SNAKE_CASE__   (     unittest.TestCase    ):



		def __init__(    self:    Any				,   a:    int				,   a:    int=7				,   a:    Dict=3				,   a:    Optional[int]=10				,   a:    int=18				,   a:    Union[str, Any]=30				,   a:    List[Any]=4_00				,   a:    Any=True				,   a:    Tuple=None				,   a:    List[str]=True				,   a:    int=[0.5, 0.5, 0.5]				,   a:    List[str]=[0.5, 0.5, 0.5]				,   a:    Union[str, Any]=None				,   )  ->int:






								'''simple docstring'''


								a_				     =   size if size is not None else {"shortest_edge": 18}
								a_				     =   crop_size if crop_size is not None else {"height": 18, "width": 18}

								a_				     =   parent
								a_				     =   batch_size
								a_				     =   num_channels
								a_				     =   num_frames
								a_				     =   image_size
								a_				     =   min_resolution
								a_				     =   max_resolution
								a_				     =   do_resize
								a_				     =   size
								a_				     =   do_normalize
								a_				     =   image_mean
								a_				     =   image_std
								a_				     =   crop_size




		def _lowerCAmelCase     (    self:    Union[str, Any])  ->Optional[Any]:






								'''simple docstring'''


								return {
								    "image_mean": self.image_mean,
								    "image_std": self.image_std,
								    "do_normalize": self.do_normalize,
								    "do_resize": self.do_resize,
								    "size": self.size,
								    "crop_size": self.crop_size,
								}





@require_torch
@require_vision
class 		SCREAMING_SNAKE_CASE__   (     lowercase_		,		unittest.TestCase    ):
		_UpperCAmelCase         =VideoMAEImageProcessor if is_vision_available() else None



		def _lowerCAmelCase     (    self:    Union[str, Any])  ->Optional[Any]:






								'''simple docstring'''


								a_				     =   VideoMAEImageProcessingTester(self)



		@property
		def _lowerCAmelCase     (    self:    Tuple)  ->str:






								'''simple docstring'''


								return self.image_processor_tester.prepare_image_processor_dict()



		def _lowerCAmelCase     (    self:    List[Any])  ->List[Any]:






								'''simple docstring'''


								a_				     =   self.image_processing_class(**self.image_processor_dict)
								self.assertTrue(hasattr(a				,   "image_mean"))
								self.assertTrue(hasattr(a				,   "image_std"))
								self.assertTrue(hasattr(a				,   "do_normalize"))
								self.assertTrue(hasattr(a				,   "do_resize"))
								self.assertTrue(hasattr(a				,   "do_center_crop"))
								self.assertTrue(hasattr(a				,   "size"))



		def _lowerCAmelCase     (    self:    Optional[Any])  ->str:






								'''simple docstring'''


								a_				     =   self.image_processing_class.from_dict(self.image_processor_dict)
								self.assertEqual(image_processor.size				,   {"shortest_edge": 18})
								self.assertEqual(image_processor.crop_size				,   {"height": 18, "width": 18})

								a_				     =   self.image_processing_class.from_dict(self.image_processor_dict				,   size=42				,   crop_size=84)
								self.assertEqual(image_processor.size				,   {"shortest_edge": 42})
								self.assertEqual(image_processor.crop_size				,   {"height": 84, "width": 84})



		def _lowerCAmelCase     (    self:    Optional[int])  ->Union[str, Any]:






								'''simple docstring'''


								pass



		def _lowerCAmelCase     (    self:    Optional[int])  ->Optional[Any]:






								'''simple docstring'''


								a_				     =   self.image_processing_class(**self.image_processor_dict)
								# create random PIL videos
								a_				     =   prepare_video_inputs(self.image_processor_tester				,   equal_resolution=a)
								for video in video_inputs:
														self.assertIsInstance(a				,   a)
														self.assertIsInstance(video[0]				,   Image.Image)

								# Test not batched input
								a_				     =   image_processing(video_inputs[0]				,   return_tensors="pt").pixel_values
								self.assertEqual(
								    encoded_videos.shape				,   (
								        1,
								        self.image_processor_tester.num_frames,
								        self.image_processor_tester.num_channels,
								        self.image_processor_tester.crop_size["height"],
								        self.image_processor_tester.crop_size["width"],
								    )				,   )

								# Test batched
								a_				     =   image_processing(a				,   return_tensors="pt").pixel_values
								self.assertEqual(
								    encoded_videos.shape				,   (
								        self.image_processor_tester.batch_size,
								        self.image_processor_tester.num_frames,
								        self.image_processor_tester.num_channels,
								        self.image_processor_tester.crop_size["height"],
								        self.image_processor_tester.crop_size["width"],
								    )				,   )



		def _lowerCAmelCase     (    self:    Any)  ->List[Any]:






								'''simple docstring'''


								a_				     =   self.image_processing_class(**self.image_processor_dict)
								# create random numpy tensors
								a_				     =   prepare_video_inputs(self.image_processor_tester				,   equal_resolution=a				,   numpify=a)
								for video in video_inputs:
														self.assertIsInstance(a				,   a)
														self.assertIsInstance(video[0]				,   np.ndarray)

								# Test not batched input
								a_				     =   image_processing(video_inputs[0]				,   return_tensors="pt").pixel_values
								self.assertEqual(
								    encoded_videos.shape				,   (
								        1,
								        self.image_processor_tester.num_frames,
								        self.image_processor_tester.num_channels,
								        self.image_processor_tester.crop_size["height"],
								        self.image_processor_tester.crop_size["width"],
								    )				,   )

								# Test batched
								a_				     =   image_processing(a				,   return_tensors="pt").pixel_values
								self.assertEqual(
								    encoded_videos.shape				,   (
								        self.image_processor_tester.batch_size,
								        self.image_processor_tester.num_frames,
								        self.image_processor_tester.num_channels,
								        self.image_processor_tester.crop_size["height"],
								        self.image_processor_tester.crop_size["width"],
								    )				,   )




		def _lowerCAmelCase     (    self:    Union[str, Any])  ->int:






								'''simple docstring'''


								a_				     =   self.image_processing_class(**self.image_processor_dict)
								# create random PyTorch tensors
								a_				     =   prepare_video_inputs(self.image_processor_tester				,   equal_resolution=a				,   torchify=a)
								for video in video_inputs:
														self.assertIsInstance(a				,   a)
														self.assertIsInstance(video[0]				,   torch.Tensor)

								# Test not batched input
								a_				     =   image_processing(video_inputs[0]				,   return_tensors="pt").pixel_values
								self.assertEqual(
								    encoded_videos.shape				,   (
								        1,
								        self.image_processor_tester.num_frames,
								        self.image_processor_tester.num_channels,
								        self.image_processor_tester.crop_size["height"],
								        self.image_processor_tester.crop_size["width"],
								    )				,   )

								# Test batched
								a_				     =   image_processing(a				,   return_tensors="pt").pixel_values
								self.assertEqual(
								    encoded_videos.shape				,   (
								        self.image_processor_tester.batch_size,
								        self.image_processor_tester.num_frames,
								        self.image_processor_tester.num_channels,
								        self.image_processor_tester.crop_size["height"],
								        self.image_processor_tester.crop_size["width"],
								    )				,   )











'''simple docstring'''





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


a_      = get_tests_dir('fixtures/test_sentencepiece_bpe_char.model')





@require_torch
class 		SCREAMING_SNAKE_CASE__   (     unittest.TestCase    ):



		def _lowerCAmelCase     (    self:    str)  ->Optional[Any]:






								'''simple docstring'''


								a_				     =   tempfile.mkdtemp()

								a_				     =   SpeechTaTokenizer(a)
								tokenizer.save_pretrained(self.tmpdirname)

								a_				     =   {
								    "feature_size": 1,
								    "padding_value": 0.0,
								    "sampling_rate": 1_60_00,
								    "do_normalize": False,
								    "num_mel_bins": 80,
								    "hop_length": 16,
								    "win_length": 64,
								    "win_function": "hann_window",
								    "fmin": 80,
								    "fmax": 76_00,
								    "mel_floor": 1e-10,
								    "reduction_factor": 2,
								    "return_attention_mask": True,
								}

								a_				     =   os.path.join(self.tmpdirname				,   a)
								with open(self.feature_extraction_file				,   "w"				,   encoding="utf-8") as fp:
														fp.write(json.dumps(a) + "\n")



		def _lowerCAmelCase     (    self:    Any				,   **a:    List[Any])  ->Optional[Any]:






								'''simple docstring'''


								return SpeechTaTokenizer.from_pretrained(self.tmpdirname				,   **a)



		def _lowerCAmelCase     (    self:    Union[str, Any]				,   **a:    int)  ->str:






								'''simple docstring'''


								return SpeechTaFeatureExtractor.from_pretrained(self.tmpdirname				,   **a)



		def _lowerCAmelCase     (    self:    int)  ->Optional[Any]:






								'''simple docstring'''


								shutil.rmtree(self.tmpdirname)



		def _lowerCAmelCase     (    self:    Any)  ->str:






								'''simple docstring'''


								a_				     =   self.get_tokenizer()
								a_				     =   self.get_feature_extractor()

								a_				     =   SpeechTaProcessor(tokenizer=a				,   feature_extractor=a)

								processor.save_pretrained(self.tmpdirname)
								a_				     =   SpeechTaProcessor.from_pretrained(self.tmpdirname)

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

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



		def _lowerCAmelCase     (    self:    List[Any])  ->Optional[int]:






								'''simple docstring'''


								a_				     =   SpeechTaProcessor(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=a				,   padding_value=1.0)

								a_				     =   SpeechTaProcessor.from_pretrained(
								    self.tmpdirname				,   bos_token="(BOS)"				,   eos_token="(EOS)"				,   do_normalize=a				,   padding_value=1.0)

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

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



		def _lowerCAmelCase     (    self:    Any)  ->List[Any]:






								'''simple docstring'''


								a_				     =   self.get_feature_extractor()
								a_				     =   self.get_tokenizer()

								a_				     =   SpeechTaProcessor(tokenizer=a				,   feature_extractor=a)

								a_				     =   floats_list((3, 10_00))

								a_				     =   feature_extractor(audio=a				,   return_tensors="np")
								a_				     =   processor(audio=a				,   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:    Tuple)  ->Union[str, Any]:






								'''simple docstring'''


								a_				     =   self.get_feature_extractor()
								a_				     =   self.get_tokenizer()

								a_				     =   SpeechTaProcessor(tokenizer=a				,   feature_extractor=a)

								a_				     =   floats_list((3, 10_00))

								a_				     =   feature_extractor(audio_target=a				,   return_tensors="np")
								a_				     =   processor(audio_target=a				,   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:    str)  ->Dict:






								'''simple docstring'''


								a_				     =   self.get_feature_extractor()
								a_				     =   self.get_tokenizer()

								a_				     =   SpeechTaProcessor(tokenizer=a				,   feature_extractor=a)

								a_				     =   "This is a test string"

								a_				     =   processor(text=a)
								a_				     =   tokenizer(a)

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



		def _lowerCAmelCase     (    self:    List[Any])  ->Any:






								'''simple docstring'''


								a_				     =   self.get_feature_extractor()
								a_				     =   self.get_tokenizer()

								a_				     =   SpeechTaProcessor(tokenizer=a				,   feature_extractor=a)

								a_				     =   "This is a test string"

								a_				     =   processor(text_target=a)
								a_				     =   tokenizer(a)

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



		def _lowerCAmelCase     (    self:    List[Any])  ->Any:






								'''simple docstring'''


								a_				     =   self.get_feature_extractor()
								a_				     =   self.get_tokenizer()

								a_				     =   SpeechTaProcessor(tokenizer=a				,   feature_extractor=a)

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

								a_				     =   processor.batch_decode(a)
								a_				     =   tokenizer.batch_decode(a)

								self.assertListEqual(a				,   a)




		def _lowerCAmelCase     (    self:    Optional[int])  ->Optional[int]:






								'''simple docstring'''


								a_				     =   self.get_feature_extractor()
								a_				     =   self.get_tokenizer()

								a_				     =   SpeechTaProcessor(tokenizer=a				,   feature_extractor=a)

								self.assertListEqual(
								    processor.model_input_names				,   feature_extractor.model_input_names				,   msg="`processor` and `feature_extractor` model input names do not match"				,   )



