

'''simple docstring'''







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


_lowerCamelCase    =  get_tests_dir('fixtures/test_sentencepiece.model')
@require_torch
@require_torchaudio
@require_sentencepiece
class 					__a   (		unittest.TestCase  ):

						def 	_UpperCAmelCase							(		self	:						Optional[int])						->Tuple:

							"""simple docstring"""



							_lowercase							=     tempfile.mkdtemp()

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

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

							_lowercase							=     {
							    """feature_size""": 24,
							    """num_mel_bins""": 24,
							    """padding_value""": 0.0,
							    """sampling_rate""": 1_60_00,
							    """return_attention_mask""": False,
							    """do_normalize""": True,
							}
							save_json(__UpperCAmelCase    ,			save_dir / FEATURE_EXTRACTOR_NAME)

						def 	_UpperCAmelCase							(		self	:						Dict    ,			**lowercase__	:						Union[str, Any])						->Any:

							"""simple docstring"""



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

						def 	_UpperCAmelCase							(		self	:						Any    ,			**lowercase__	:						Optional[Any])						->str:

							"""simple docstring"""



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

						def 	_UpperCAmelCase							(		self	:						Any)						->Any:

							"""simple docstring"""



							shutil.rmtree(self.tmpdirname)

						def 	_UpperCAmelCase							(		self	:						Any)						->Tuple:

							"""simple docstring"""



							_lowercase							=     self.get_tokenizer()
							_lowercase							=     self.get_feature_extractor()

							_lowercase							=     SpeechaTextProcessor(tokenizer=__UpperCAmelCase    ,			feature_extractor=__UpperCAmelCase)

							processor.save_pretrained(self.tmpdirname)
							_lowercase							=     SpeechaTextProcessor.from_pretrained(self.tmpdirname)

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

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

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

							"""simple docstring"""



							_lowercase							=     SpeechaTextProcessor(
							    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=__UpperCAmelCase    ,			padding_value=1.0)

							_lowercase							=     SpeechaTextProcessor.from_pretrained(
							    self.tmpdirname    ,			bos_token="""(BOS)"""    ,			eos_token="""(EOS)"""    ,			do_normalize=__UpperCAmelCase    ,			padding_value=1.0)

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

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

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

							"""simple docstring"""



							_lowercase							=     self.get_feature_extractor()
							_lowercase							=     self.get_tokenizer()

							_lowercase							=     SpeechaTextProcessor(tokenizer=__UpperCAmelCase    ,			feature_extractor=__UpperCAmelCase)

							_lowercase							=     floats_list((3, 10_00))

							_lowercase							=     feature_extractor(__UpperCAmelCase    ,			return_tensors="""np""")
							_lowercase							=     processor(__UpperCAmelCase    ,			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	:						Optional[int])						->List[Any]:

							"""simple docstring"""



							_lowercase							=     self.get_feature_extractor()
							_lowercase							=     self.get_tokenizer()

							_lowercase							=     SpeechaTextProcessor(tokenizer=__UpperCAmelCase    ,			feature_extractor=__UpperCAmelCase)

							_lowercase							=     """This is a test string"""

							_lowercase							=     processor(text=__UpperCAmelCase)

							_lowercase							=     tokenizer(__UpperCAmelCase)

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

						def 	_UpperCAmelCase							(		self	:						str)						->Optional[int]:

							"""simple docstring"""



							_lowercase							=     self.get_feature_extractor()
							_lowercase							=     self.get_tokenizer()

							_lowercase							=     SpeechaTextProcessor(tokenizer=__UpperCAmelCase    ,			feature_extractor=__UpperCAmelCase)

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

							_lowercase							=     processor.batch_decode(__UpperCAmelCase)
							_lowercase							=     tokenizer.batch_decode(__UpperCAmelCase)

							self.assertListEqual(__UpperCAmelCase    ,			__UpperCAmelCase)


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

							"""simple docstring"""



							_lowercase							=     self.get_feature_extractor()
							_lowercase							=     self.get_tokenizer()

							_lowercase							=     SpeechaTextProcessor(tokenizer=__UpperCAmelCase    ,			feature_extractor=__UpperCAmelCase)

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






"""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


if is_torch_available():
      import torch

if is_vision_available():
      from PIL import Image

      from transformers import TvltImageProcessor
def    UpperCamelCase						(					UpperCAmelCase ,   UpperCAmelCase=10 ,   UpperCAmelCase=10 ,   UpperCAmelCase=False ,   UpperCAmelCase=False     )						->Union[str, Any]:

   """simple docstring"""



   a_						   =     []
   for i in range(image_processor_tester.num_frames     ):
      video.append(np.random.randint(255 ,   size=(image_processor_tester.num_channels, width, height) ,   dtype=np.uinta     )     )

   if not numpify and not torchify:
      # PIL expects the channel dimension as last dimension
      a_						   =     [Image.fromarray(np.moveaxis(UpperCAmelCase ,   0 ,   -1     )     ) for frame in video]

   if torchify:
      a_						   =     [torch.from_numpy(UpperCAmelCase     ) for frame in video]

   return video
def    UpperCamelCase						(					UpperCAmelCase ,   UpperCAmelCase=False ,   UpperCAmelCase=False ,   UpperCAmelCase=False     )						->Optional[Any]:

   """simple docstring"""



   assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

   a_						   =     []
   for i in range(image_processor_tester.batch_size     ):
      if equal_resolution:
         a_						   =     a_						   =     image_processor_tester.max_resolution
      else:
         a_   ,			a_						   =     np.random.choice(
             np.arange(image_processor_tester.min_resolution ,   image_processor_tester.max_resolution     ) ,   2     )
         a_						   =     prepare_video(
             image_processor_tester=UpperCAmelCase ,   width=UpperCAmelCase ,   height=UpperCAmelCase ,   numpify=UpperCAmelCase ,   torchify=UpperCAmelCase ,   )
      video_inputs.append(UpperCAmelCase     )

   return video_inputs





class 							snake_case       (     unittest.TestCase		):







   def __init__( self       ,							__UpperCAmelCase       ,							__UpperCAmelCase=7       ,							__UpperCAmelCase=3       ,							__UpperCAmelCase=4       ,							__UpperCAmelCase=18       ,							__UpperCAmelCase=30       ,							__UpperCAmelCase=4_00       ,							__UpperCAmelCase=True       ,							__UpperCAmelCase=None       ,							__UpperCAmelCase=True       ,							__UpperCAmelCase=[0.5, 0.5, 0.5]       ,							__UpperCAmelCase=[0.5, 0.5, 0.5]       ,							__UpperCAmelCase=True       ,							__UpperCAmelCase=None       ,							)   ->str:
      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_						   =     do_center_crop
      a_						   =     crop_size







   def    UpperCAmelCase__	( self)   ->Tuple:
      return {
          "image_mean": self.image_mean,
          "image_std": self.image_std,
          "do_normalize": self.do_normalize,
          "do_resize": self.do_resize,
          "size": self.size,
          "do_center_crop": self.do_center_crop,
          "crop_size": self.crop_size,
      }






@require_torch
@require_vision
class 							snake_case       (     SCREAMING_SNAKE_CASE_							,       unittest.TestCase		):
   a_					:		int     					=     TvltImageProcessor if is_vision_available() else None







   def    UpperCAmelCase__	( self)   ->Optional[Any]:
      a_						   =     TvltImageProcessorTester(self)







   @property
   def    UpperCAmelCase__	( self)   ->int:
      return self.image_processor_tester.prepare_image_processor_dict()







   def    UpperCAmelCase__	( self)   ->int:
      a_						   =     self.image_processing_class(**self.image_processor_dict)
      self.assertTrue(hasattr(__UpperCAmelCase       ,							"image_mean"))
      self.assertTrue(hasattr(__UpperCAmelCase       ,							"image_std"))
      self.assertTrue(hasattr(__UpperCAmelCase       ,							"do_normalize"))
      self.assertTrue(hasattr(__UpperCAmelCase       ,							"do_resize"))
      self.assertTrue(hasattr(__UpperCAmelCase       ,							"do_center_crop"))
      self.assertTrue(hasattr(__UpperCAmelCase       ,							"size"))







   def    UpperCAmelCase__	( self)   ->Any:
      # Initialize image_processor
      a_						   =     self.image_processing_class(**self.image_processor_dict)
      # create random PIL videos
      a_						   =     prepare_video_inputs(self.image_processor_tester       ,							equal_resolution=__UpperCAmelCase)
      for video in video_inputs:
         self.assertIsInstance(__UpperCAmelCase       ,							__UpperCAmelCase)
         self.assertIsInstance(video[0]       ,							Image.Image)

      # Test not batched input
      a_						   =     image_processor(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_processor(__UpperCAmelCase       ,							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    UpperCAmelCase__	( self)   ->int:
      # Initialize image_processor
      a_						   =     self.image_processing_class(**self.image_processor_dict)
      # create random numpy tensors
      a_						   =     prepare_video_inputs(self.image_processor_tester       ,							equal_resolution=__UpperCAmelCase       ,							numpify=__UpperCAmelCase)
      for video in video_inputs:
         self.assertIsInstance(__UpperCAmelCase       ,							__UpperCAmelCase)
         self.assertIsInstance(video[0]       ,							np.ndarray)

      # Test not batched input
      a_						   =     image_processor(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_processor(__UpperCAmelCase       ,							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    UpperCAmelCase__	( self)   ->Tuple:
      # Initialize image_processor
      a_						   =     self.image_processing_class(**self.image_processor_dict)
      # create random PyTorch tensors
      a_						   =     prepare_video_inputs(self.image_processor_tester       ,							equal_resolution=__UpperCAmelCase       ,							torchify=__UpperCAmelCase)
      for video in video_inputs:
         self.assertIsInstance(__UpperCAmelCase       ,							__UpperCAmelCase)
         self.assertIsInstance(video[0]       ,							torch.Tensor)

      # Test not batched input
      a_						   =     image_processor(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_processor(__UpperCAmelCase       ,							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"],
          )       ,							)