


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     :			List[str]      =get_tests_dir('fixtures/test_sentencepiece_bpe_char.model')
@require_torch
class 	_lowercase				(      unittest.TestCase	):






				'''simple docstring'''



				def 		__magic_name__(	self      :Optional[int]     )		->      Optional[Any]:
										__SCREAMING_SNAKE_CASE						:					int		      =		tempfile.mkdtemp()

										__SCREAMING_SNAKE_CASE						:					str		      =		SpeechTaTokenizer(lowerCAmelCase__     )
										tokenizer.save_pretrained(self.tmpdirname     )

										__SCREAMING_SNAKE_CASE						:					Optional[Any]		      =		{
										    '''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-1_0,
										    '''reduction_factor''': 2,
										    '''return_attention_mask''': True,
										}

										__SCREAMING_SNAKE_CASE						:					Tuple		      =		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 		__magic_name__(	self      :Optional[Any]      ,      **lowerCAmelCase__      :str     )		->      Dict:
										return SpeechTaTokenizer.from_pretrained(self.tmpdirname      ,      **lowerCAmelCase__     )


				def 		__magic_name__(	self      :int      ,      **lowerCAmelCase__      :Any     )		->      Optional[int]:
										return SpeechTaFeatureExtractor.from_pretrained(self.tmpdirname      ,      **lowerCAmelCase__     )


				def 		__magic_name__(	self      :int     )		->      Optional[Any]:
										shutil.rmtree(self.tmpdirname     )


				def 		__magic_name__(	self      :Union[str, Any]     )		->      Tuple:
										__SCREAMING_SNAKE_CASE						:					Tuple		      =		self.get_tokenizer()
										__SCREAMING_SNAKE_CASE						:					int		      =		self.get_feature_extractor()

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

										processor.save_pretrained(self.tmpdirname     )
										__SCREAMING_SNAKE_CASE						:					Tuple		      =		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 		__magic_name__(	self      :str     )		->      int:
										__SCREAMING_SNAKE_CASE						:					Dict		      =		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						:					str		      =		self.get_feature_extractor(do_normalize=lowerCAmelCase__      ,      padding_value=1.0     )

										__SCREAMING_SNAKE_CASE						:					Optional[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 		__magic_name__(	self      :Optional[Any]     )		->      Union[str, Any]:
										__SCREAMING_SNAKE_CASE						:					List[Any]		      =		self.get_feature_extractor()
										__SCREAMING_SNAKE_CASE						:					Optional[Any]		      =		self.get_tokenizer()

										__SCREAMING_SNAKE_CASE						:					int		      =		SpeechTaProcessor(tokenizer=lowerCAmelCase__      ,      feature_extractor=lowerCAmelCase__     )

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

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

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

										__SCREAMING_SNAKE_CASE						:					Optional[int]		      =		floats_list((3, 1_000)     )

										__SCREAMING_SNAKE_CASE						:					List[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 		__magic_name__(	self      :Dict     )		->      List[str]:
										__SCREAMING_SNAKE_CASE						:					Any		      =		self.get_feature_extractor()
										__SCREAMING_SNAKE_CASE						:					List[Any]		      =		self.get_tokenizer()

										__SCREAMING_SNAKE_CASE						:					List[str]		      =		SpeechTaProcessor(tokenizer=lowerCAmelCase__      ,      feature_extractor=lowerCAmelCase__     )

										__SCREAMING_SNAKE_CASE						:					List[Any]		      =		'''This is a test string'''

										__SCREAMING_SNAKE_CASE						:					int		      =		processor(text=lowerCAmelCase__     )
										__SCREAMING_SNAKE_CASE						:					List[str]		      =		tokenizer(lowerCAmelCase__     )

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


				def 		__magic_name__(	self      :Optional[int]     )		->      List[Any]:
										__SCREAMING_SNAKE_CASE						:					Optional[int]		      =		self.get_feature_extractor()
										__SCREAMING_SNAKE_CASE						:					Optional[Any]		      =		self.get_tokenizer()

										__SCREAMING_SNAKE_CASE						:					Optional[int]		      =		SpeechTaProcessor(tokenizer=lowerCAmelCase__      ,      feature_extractor=lowerCAmelCase__     )

										__SCREAMING_SNAKE_CASE						:					Union[str, Any]		      =		'''This is a test string'''

										__SCREAMING_SNAKE_CASE						:					Optional[Any]		      =		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 		__magic_name__(	self      :Dict     )		->      List[str]:
										__SCREAMING_SNAKE_CASE						:					Tuple		      =		self.get_feature_extractor()
										__SCREAMING_SNAKE_CASE						:					Dict		      =		self.get_tokenizer()

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

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

										__SCREAMING_SNAKE_CASE						:					Dict		      =		processor.batch_decode(lowerCAmelCase__     )
										__SCREAMING_SNAKE_CASE						:					List[str]		      =		tokenizer.batch_decode(lowerCAmelCase__     )

										self.assertListEqual(lowerCAmelCase__      ,      lowerCAmelCase__     )



				def 		__magic_name__(	self      :int     )		->      Tuple:
										__SCREAMING_SNAKE_CASE						:					Tuple		      =		self.get_feature_extractor()
										__SCREAMING_SNAKE_CASE						:					List[Any]		      =		self.get_tokenizer()

										__SCREAMING_SNAKE_CASE						:					Union[str, Any]		      =		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'''      ,      )




from __future__ import annotations


def 	_UpperCamelCase							(				lowercase__				):
						__SCREAMING_SNAKE_CASE						:					List[Any]		      =		sorted(lowercase__				)
						__SCREAMING_SNAKE_CASE						:					str		      =		len(lowercase__				)
						__SCREAMING_SNAKE_CASE						:					Union[str, Any]		      =		length >> 1
						return (
						    (sorted_list[mid_index] + sorted_list[mid_index - 1]) / 2
						    if length % 2 == 0
						    else sorted_list[mid_index]
						)


def 	_UpperCamelCase							(				):
						import doctest

						doctest.testmod()


if __name__ == "__main__":
						main()
