





"""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       snake_case		(       unittest.TestCase       ):

      def __init__(      self       :Any						,							_lowerCamelCase       :int						,							_lowerCamelCase       :Optional[Any]=7						,							_lowerCamelCase       :List[Any]=3						,							_lowerCamelCase       :Dict=1_0						,							_lowerCamelCase       :Union[str, Any]=1_8						,							_lowerCamelCase       :List[str]=3_0						,							_lowerCamelCase       :List[Any]=4_0_0						,							_lowerCamelCase       :Optional[int]=True						,							_lowerCamelCase       :Dict=None						,							_lowerCamelCase       :Dict=True						,							_lowerCamelCase       :str=[0.5, 0.5, 0.5]						,							_lowerCamelCase       :str=[0.5, 0.5, 0.5]						,							_lowerCamelCase       :List[Any]=None						,							):
       __SCREAMING_SNAKE_CASE			:      Tuple 							=	size if size is not None else {'''shortest_edge''': 1_8}
       __SCREAMING_SNAKE_CASE			:      Dict 							=	crop_size if crop_size is not None else {'''height''': 1_8, '''width''': 1_8}

       __SCREAMING_SNAKE_CASE			:      Optional[Any] 							=	parent
       __SCREAMING_SNAKE_CASE			:      Dict 							=	batch_size
       __SCREAMING_SNAKE_CASE			:      int 							=	num_channels
       __SCREAMING_SNAKE_CASE			:      List[str] 							=	num_frames
       __SCREAMING_SNAKE_CASE			:      List[Any] 							=	image_size
       __SCREAMING_SNAKE_CASE			:      List[Any] 							=	min_resolution
       __SCREAMING_SNAKE_CASE			:      Any 							=	max_resolution
       __SCREAMING_SNAKE_CASE			:      Union[str, Any] 							=	do_resize
       __SCREAMING_SNAKE_CASE			:      Tuple 							=	size
       __SCREAMING_SNAKE_CASE			:      List[str] 							=	do_normalize
       __SCREAMING_SNAKE_CASE			:      Any 							=	image_mean
       __SCREAMING_SNAKE_CASE			:      int 							=	image_std
       __SCREAMING_SNAKE_CASE			:      Tuple 							=	crop_size



      def 						SCREAMING_SNAKE_CASE_      (      self       :str						):
       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       snake_case		(       __UpperCAmelCase			,					unittest.TestCase       ):
      lowerCamelCase__      =			VideoMAEImageProcessor if is_vision_available() else None

      def 						SCREAMING_SNAKE_CASE_      (      self       :Optional[Any]						):
       __SCREAMING_SNAKE_CASE			:      List[Any] 							=	VideoMAEImageProcessingTester(self						)

      @property
      def 						SCREAMING_SNAKE_CASE_      (      self       :Optional[Any]						):
       return self.image_processor_tester.prepare_image_processor_dict()

      def 						SCREAMING_SNAKE_CASE_      (      self       :Tuple						):
       __SCREAMING_SNAKE_CASE			:      Optional[Any] 							=	self.image_processing_class(**self.image_processor_dict						)
       self.assertTrue(hasattr(_lowerCamelCase						,							'''image_mean'''						)						)
       self.assertTrue(hasattr(_lowerCamelCase						,							'''image_std'''						)						)
       self.assertTrue(hasattr(_lowerCamelCase						,							'''do_normalize'''						)						)
       self.assertTrue(hasattr(_lowerCamelCase						,							'''do_resize'''						)						)
       self.assertTrue(hasattr(_lowerCamelCase						,							'''do_center_crop'''						)						)
       self.assertTrue(hasattr(_lowerCamelCase						,							'''size'''						)						)

      def 						SCREAMING_SNAKE_CASE_      (      self       :int						):
       __SCREAMING_SNAKE_CASE			:      Tuple 							=	self.image_processing_class.from_dict(self.image_processor_dict						)
       self.assertEqual(image_processor.size						,							{'''shortest_edge''': 1_8}						)
       self.assertEqual(image_processor.crop_size						,							{'''height''': 1_8, '''width''': 1_8}						)

       __SCREAMING_SNAKE_CASE			:      int 							=	self.image_processing_class.from_dict(self.image_processor_dict						,							size=4_2						,							crop_size=8_4						)
       self.assertEqual(image_processor.size						,							{'''shortest_edge''': 4_2}						)
       self.assertEqual(image_processor.crop_size						,							{'''height''': 8_4, '''width''': 8_4}						)

      def 						SCREAMING_SNAKE_CASE_      (      self       :Optional[Any]						):
       pass

      def 						SCREAMING_SNAKE_CASE_      (      self       :List[str]						):
       # Initialize image_processing
       __SCREAMING_SNAKE_CASE			:      Any 							=	self.image_processing_class(**self.image_processor_dict						)
       # create random PIL videos
       __SCREAMING_SNAKE_CASE			:      Union[str, Any] 							=	prepare_video_inputs(self.image_processor_tester						,							equal_resolution=_lowerCamelCase						)
       for video in video_inputs:
        self.assertIsInstance(_lowerCamelCase						,							_lowerCamelCase						)
        self.assertIsInstance(video[0]						,							Image.Image						)

       # Test not batched input
       __SCREAMING_SNAKE_CASE			:      Optional[int] 							=	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
       __SCREAMING_SNAKE_CASE			:      Dict 							=	image_processing(_lowerCamelCase						,							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 						SCREAMING_SNAKE_CASE_      (      self       :Optional[Any]						):
       # Initialize image_processing
       __SCREAMING_SNAKE_CASE			:      Optional[int] 							=	self.image_processing_class(**self.image_processor_dict						)
       # create random numpy tensors
       __SCREAMING_SNAKE_CASE			:      Dict 							=	prepare_video_inputs(self.image_processor_tester						,							equal_resolution=_lowerCamelCase						,							numpify=_lowerCamelCase						)
       for video in video_inputs:
        self.assertIsInstance(_lowerCamelCase						,							_lowerCamelCase						)
        self.assertIsInstance(video[0]						,							np.ndarray						)

       # Test not batched input
       __SCREAMING_SNAKE_CASE			:      Optional[Any] 							=	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
       __SCREAMING_SNAKE_CASE			:      List[str] 							=	image_processing(_lowerCamelCase						,							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 						SCREAMING_SNAKE_CASE_      (      self       :Any						):
       # Initialize image_processing
       __SCREAMING_SNAKE_CASE			:      int 							=	self.image_processing_class(**self.image_processor_dict						)
       # create random PyTorch tensors
       __SCREAMING_SNAKE_CASE			:      Optional[Any] 							=	prepare_video_inputs(self.image_processor_tester						,							equal_resolution=_lowerCamelCase						,							torchify=_lowerCamelCase						)
       for video in video_inputs:
        self.assertIsInstance(_lowerCamelCase						,							_lowerCamelCase						)
        self.assertIsInstance(video[0]						,							torch.Tensor						)

       # Test not batched input
       __SCREAMING_SNAKE_CASE			:      Dict 							=	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
       __SCREAMING_SNAKE_CASE			:      int 							=	image_processing(_lowerCamelCase						,							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


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





@require_torch
class       snake_case		(       unittest.TestCase       ):

      def 						SCREAMING_SNAKE_CASE_      (      self       :Union[str, Any]						):
       __SCREAMING_SNAKE_CASE			:      Union[str, Any] 							=	tempfile.mkdtemp()

       __SCREAMING_SNAKE_CASE			:      Optional[int] 							=	SpeechTaTokenizer(_lowerCamelCase						)
       tokenizer.save_pretrained(self.tmpdirname						)

       __SCREAMING_SNAKE_CASE			:      Optional[Any] 							=	{
           '''feature_size''': 1,
           '''padding_value''': 0.0,
           '''sampling_rate''': 1_6_0_0_0,
           '''do_normalize''': False,
           '''num_mel_bins''': 8_0,
           '''hop_length''': 1_6,
           '''win_length''': 6_4,
           '''win_function''': '''hann_window''',
           '''fmin''': 8_0,
           '''fmax''': 7_6_0_0,
           '''mel_floor''': 1e-10,
           '''reduction_factor''': 2,
           '''return_attention_mask''': True,
       }

       __SCREAMING_SNAKE_CASE			:      Any 							=	os.path.join(self.tmpdirname						,							_lowerCamelCase						)
       with open(self.feature_extraction_file						,							'''w'''						,							encoding='''utf-8'''						) as fp:
        fp.write(json.dumps(_lowerCamelCase						) + '''\n'''						)

      def 						SCREAMING_SNAKE_CASE_      (      self       :str						,							**_lowerCamelCase       :str						):
       return SpeechTaTokenizer.from_pretrained(self.tmpdirname						,							**_lowerCamelCase						)

      def 						SCREAMING_SNAKE_CASE_      (      self       :Dict						,							**_lowerCamelCase       :Dict						):
       return SpeechTaFeatureExtractor.from_pretrained(self.tmpdirname						,							**_lowerCamelCase						)

      def 						SCREAMING_SNAKE_CASE_      (      self       :List[str]						):
       shutil.rmtree(self.tmpdirname						)

      def 						SCREAMING_SNAKE_CASE_      (      self       :Union[str, Any]						):
       __SCREAMING_SNAKE_CASE			:      Any 							=	self.get_tokenizer()
       __SCREAMING_SNAKE_CASE			:      int 							=	self.get_feature_extractor()

       __SCREAMING_SNAKE_CASE			:      Any 							=	SpeechTaProcessor(tokenizer=_lowerCamelCase						,							feature_extractor=_lowerCamelCase						)

       processor.save_pretrained(self.tmpdirname						)
       __SCREAMING_SNAKE_CASE			:      Optional[Any] 							=	SpeechTaProcessor.from_pretrained(self.tmpdirname						)

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

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

      def 						SCREAMING_SNAKE_CASE_      (      self       :Union[str, Any]						):
       __SCREAMING_SNAKE_CASE			:      Union[str, Any] 							=	SpeechTaProcessor(tokenizer=self.get_tokenizer()						,							feature_extractor=self.get_feature_extractor()						)
       processor.save_pretrained(self.tmpdirname						)

       __SCREAMING_SNAKE_CASE			:      Tuple 							=	self.get_tokenizer(bos_token='''(BOS)'''						,							eos_token='''(EOS)'''						)
       __SCREAMING_SNAKE_CASE			:      List[str] 							=	self.get_feature_extractor(do_normalize=_lowerCamelCase						,							padding_value=1.0						)

       __SCREAMING_SNAKE_CASE			:      Any 							=	SpeechTaProcessor.from_pretrained(
           self.tmpdirname						,							bos_token='''(BOS)'''						,							eos_token='''(EOS)'''						,							do_normalize=_lowerCamelCase						,							padding_value=1.0						)

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

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

      def 						SCREAMING_SNAKE_CASE_      (      self       :str						):
       __SCREAMING_SNAKE_CASE			:      Optional[int] 							=	self.get_feature_extractor()
       __SCREAMING_SNAKE_CASE			:      Tuple 							=	self.get_tokenizer()

       __SCREAMING_SNAKE_CASE			:      Union[str, Any] 							=	SpeechTaProcessor(tokenizer=_lowerCamelCase						,							feature_extractor=_lowerCamelCase						)

       __SCREAMING_SNAKE_CASE			:      int 							=	floats_list((3, 1_0_0_0)						)

       __SCREAMING_SNAKE_CASE			:      int 							=	feature_extractor(audio=_lowerCamelCase						,							return_tensors='''np'''						)
       __SCREAMING_SNAKE_CASE			:      List[str] 							=	processor(audio=_lowerCamelCase						,							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						):
       __SCREAMING_SNAKE_CASE			:      Any 							=	self.get_feature_extractor()
       __SCREAMING_SNAKE_CASE			:      int 							=	self.get_tokenizer()

       __SCREAMING_SNAKE_CASE			:      Any 							=	SpeechTaProcessor(tokenizer=_lowerCamelCase						,							feature_extractor=_lowerCamelCase						)

       __SCREAMING_SNAKE_CASE			:      Union[str, Any] 							=	floats_list((3, 1_0_0_0)						)

       __SCREAMING_SNAKE_CASE			:      Union[str, Any] 							=	feature_extractor(audio_target=_lowerCamelCase						,							return_tensors='''np'''						)
       __SCREAMING_SNAKE_CASE			:      List[Any] 							=	processor(audio_target=_lowerCamelCase						,							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       :List[Any]						):
       __SCREAMING_SNAKE_CASE			:      Optional[Any] 							=	self.get_feature_extractor()
       __SCREAMING_SNAKE_CASE			:      Union[str, Any] 							=	self.get_tokenizer()

       __SCREAMING_SNAKE_CASE			:      Union[str, Any] 							=	SpeechTaProcessor(tokenizer=_lowerCamelCase						,							feature_extractor=_lowerCamelCase						)

       __SCREAMING_SNAKE_CASE			:      int 							=	'''This is a test string'''

       __SCREAMING_SNAKE_CASE			:      str 							=	processor(text=_lowerCamelCase						)
       __SCREAMING_SNAKE_CASE			:      Union[str, Any] 							=	tokenizer(_lowerCamelCase						)

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

      def 						SCREAMING_SNAKE_CASE_      (      self       :Optional[int]						):
       __SCREAMING_SNAKE_CASE			:      int 							=	self.get_feature_extractor()
       __SCREAMING_SNAKE_CASE			:      str 							=	self.get_tokenizer()

       __SCREAMING_SNAKE_CASE			:      Tuple 							=	SpeechTaProcessor(tokenizer=_lowerCamelCase						,							feature_extractor=_lowerCamelCase						)

       __SCREAMING_SNAKE_CASE			:      Dict 							=	'''This is a test string'''

       __SCREAMING_SNAKE_CASE			:      str 							=	processor(text_target=_lowerCamelCase						)
       __SCREAMING_SNAKE_CASE			:      Dict 							=	tokenizer(_lowerCamelCase						)

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

      def 						SCREAMING_SNAKE_CASE_      (      self       :Optional[Any]						):
       __SCREAMING_SNAKE_CASE			:      Dict 							=	self.get_feature_extractor()
       __SCREAMING_SNAKE_CASE			:      Tuple 							=	self.get_tokenizer()

       __SCREAMING_SNAKE_CASE			:      Optional[Any] 							=	SpeechTaProcessor(tokenizer=_lowerCamelCase						,							feature_extractor=_lowerCamelCase						)

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

       __SCREAMING_SNAKE_CASE			:      List[Any] 							=	processor.batch_decode(_lowerCamelCase						)
       __SCREAMING_SNAKE_CASE			:      List[Any] 							=	tokenizer.batch_decode(_lowerCamelCase						)

       self.assertListEqual(_lowerCamelCase						,							_lowerCamelCase						)



      def 						SCREAMING_SNAKE_CASE_      (      self       :Optional[Any]						):
       __SCREAMING_SNAKE_CASE			:      Optional[Any] 							=	self.get_feature_extractor()
       __SCREAMING_SNAKE_CASE			:      str 							=	self.get_tokenizer()

       __SCREAMING_SNAKE_CASE			:      str 							=	SpeechTaProcessor(tokenizer=_lowerCamelCase						,							feature_extractor=_lowerCamelCase						)

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

