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


snake_case__	:     Union[str, Any]      =    get_tests_dir('''fixtures/test_sentencepiece_bpe_char.model''')






@require_torch
class 	__SCREAMING_SNAKE_CASE		(			unittest.TestCase			):







  '''simple docstring'''






  def _UpperCamelCase	(					self		):




       '''simple docstring'''

       UpperCAmelCase_				:  List[Any]  = tempfile.mkdtemp()

       UpperCAmelCase_				:  Any  = SpeechTaTokenizer(snake_case_		)
       tokenizer.save_pretrained(self.tmpdirname		)

       UpperCAmelCase_				:  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,
       }

       UpperCAmelCase_				:  List[Any]  = 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 _UpperCamelCase	(					self ,						**snake_case_		):




       '''simple docstring'''

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


  def _UpperCamelCase	(					self ,						**snake_case_		):




       '''simple docstring'''

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


  def _UpperCamelCase	(					self		):




       '''simple docstring'''

       shutil.rmtree(self.tmpdirname		)


  def _UpperCamelCase	(					self		):




       '''simple docstring'''

       UpperCAmelCase_				:  Dict  = self.get_tokenizer()
       UpperCAmelCase_				:  str  = self.get_feature_extractor()

       UpperCAmelCase_				:  Optional[Any]  = SpeechTaProcessor(tokenizer=snake_case_ ,						feature_extractor=snake_case_		)

       processor.save_pretrained(self.tmpdirname		)
       UpperCAmelCase_				:  Union[str, Any]  = 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 _UpperCamelCase	(					self		):




       '''simple docstring'''

       UpperCAmelCase_				:  Dict  = SpeechTaProcessor(tokenizer=self.get_tokenizer() ,						feature_extractor=self.get_feature_extractor()		)
       processor.save_pretrained(self.tmpdirname		)

       UpperCAmelCase_				:  List[Any]  = self.get_tokenizer(bos_token='(BOS)' ,						eos_token='(EOS)'		)
       UpperCAmelCase_				:  Any  = self.get_feature_extractor(do_normalize=snake_case_ ,						padding_value=1.0		)

       UpperCAmelCase_				:  int  = 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 _UpperCamelCase	(					self		):




       '''simple docstring'''

       UpperCAmelCase_				:  Optional[int]  = self.get_feature_extractor()
       UpperCAmelCase_				:  str  = self.get_tokenizer()

       UpperCAmelCase_				:  List[str]  = SpeechTaProcessor(tokenizer=snake_case_ ,						feature_extractor=snake_case_		)

       UpperCAmelCase_				:  Dict  = floats_list((3, 1_0_0_0)		)

       UpperCAmelCase_				:  Tuple  = feature_extractor(audio=snake_case_ ,						return_tensors='np'		)
       UpperCAmelCase_				:  Dict  = 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 _UpperCamelCase	(					self		):




       '''simple docstring'''

       UpperCAmelCase_				:  List[str]  = self.get_feature_extractor()
       UpperCAmelCase_				:  str  = self.get_tokenizer()

       UpperCAmelCase_				:  List[str]  = SpeechTaProcessor(tokenizer=snake_case_ ,						feature_extractor=snake_case_		)

       UpperCAmelCase_				:  Optional[Any]  = floats_list((3, 1_0_0_0)		)

       UpperCAmelCase_				:  List[Any]  = feature_extractor(audio_target=snake_case_ ,						return_tensors='np'		)
       UpperCAmelCase_				:  Optional[int]  = 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 _UpperCamelCase	(					self		):




       '''simple docstring'''

       UpperCAmelCase_				:  List[Any]  = self.get_feature_extractor()
       UpperCAmelCase_				:  Optional[int]  = self.get_tokenizer()

       UpperCAmelCase_				:  Any  = SpeechTaProcessor(tokenizer=snake_case_ ,						feature_extractor=snake_case_		)

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

       UpperCAmelCase_				:  Dict  = processor(text=snake_case_		)
       UpperCAmelCase_				:  Union[str, Any]  = tokenizer(snake_case_		)

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


  def _UpperCamelCase	(					self		):




       '''simple docstring'''

       UpperCAmelCase_				:  Optional[Any]  = self.get_feature_extractor()
       UpperCAmelCase_				:  Dict  = self.get_tokenizer()

       UpperCAmelCase_				:  Tuple  = SpeechTaProcessor(tokenizer=snake_case_ ,						feature_extractor=snake_case_		)

       UpperCAmelCase_				:  Optional[int]  = 'This is a test string'

       UpperCAmelCase_				:  int  = processor(text_target=snake_case_		)
       UpperCAmelCase_				:  Optional[int]  = tokenizer(snake_case_		)

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


  def _UpperCamelCase	(					self		):




       '''simple docstring'''

       UpperCAmelCase_				:  int  = self.get_feature_extractor()
       UpperCAmelCase_				:  Tuple  = self.get_tokenizer()

       UpperCAmelCase_				:  List[str]  = SpeechTaProcessor(tokenizer=snake_case_ ,						feature_extractor=snake_case_		)

       UpperCAmelCase_				:  int  = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

       UpperCAmelCase_				:  List[Any]  = processor.batch_decode(snake_case_		)
       UpperCAmelCase_				:  str  = tokenizer.batch_decode(snake_case_		)

       self.assertListEqual(snake_case_ ,						snake_case_		)






  def _UpperCamelCase	(					self		):




       '''simple docstring'''

       UpperCAmelCase_				:  List[Any]  = self.get_feature_extractor()
       UpperCAmelCase_				:  Dict  = self.get_tokenizer()

       UpperCAmelCase_				:  Optional[Any]  = 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' ,						)










'''simple docstring'''




import importlib.metadata
import sys

from transformers.testing_utils import TestCasePlus
from transformers.utils.versions import require_version, require_version_core


_lowerCAmelCase	:		Optional[int]		  =						importlib.metadata.version('numpy')
_lowerCAmelCase	:		List[Any]		  =						'.'.join([str(x) for x in sys.version_info[:3]])



class 			lowerCAmelCase					(					a				):



	def 					lowercase  (       self     ):
		# lt + different version strings
		require_version_core('numpy<1000.4.5'     )
		require_version_core('numpy<1000.4'     )
		require_version_core('numpy<1000'     )

		# le
		require_version_core('numpy<=1000.4.5'     )
		require_version_core(f"numpy<={numpy_ver}"     )

		# eq
		require_version_core(f"numpy=={numpy_ver}"     )

		# ne
		require_version_core('numpy!=1000.4.5'     )

		# ge
		require_version_core('numpy>=1.0'     )
		require_version_core('numpy>=1.0.0'     )
		require_version_core(f"numpy>={numpy_ver}"     )

		# gt
		require_version_core('numpy>1.0.0'     )

		# mix
		require_version_core('numpy>1.0.0,<1000'     )

		# requirement w/o version
		require_version_core('numpy'     )

		# unmet requirements due to version conflict
		for req in ["numpy==1.0.0", "numpy>=1000.0.0", f"numpy<{numpy_ver}"]:
			try:
				require_version_core(snake_case__     )
			except ImportError as e:
				self.assertIn(f"{req} is required"			,       str(snake_case__     )     )
				self.assertIn('but found'			,       str(snake_case__     )     )

        # unmet requirements due to missing module
		for req in ["numpipypie>1", "numpipypie2"]:
			try:
				require_version_core(snake_case__     )
			except importlib.metadata.PackageNotFoundError as e:
				self.assertIn(f"The '{req}' distribution was not found and is required by this application"			,       str(snake_case__     )     )
				self.assertIn('Try: pip install transformers -U'			,       str(snake_case__     )     )

        # bogus requirements formats:
        # 1. whole thing
		for req in ["numpy??1.0.0", "numpy1.0.0"]:
			try:
				require_version_core(snake_case__     )
			except ValueError as e:
				self.assertIn('requirement needs to be in the pip package format'			,       str(snake_case__     )     )
        # 2. only operators
		for req in ["numpy=1.0.0", "numpy == 1.00", "numpy<>1.0.0", "numpy><1.00", "numpy>>1.0.0"]:
			try:
				require_version_core(snake_case__     )
			except ValueError as e:
				self.assertIn('need one of '			,       str(snake_case__     )     )



	def 					lowercase  (       self     ):
		# matching requirement
		require_version('python>=3.6.0'     )

		# not matching requirements
		for req in ["python>9.9.9", "python<3.0.0"]:
			try:
				require_version_core(snake_case__     )
			except ImportError as e:
				self.assertIn(f"{req} is required"			,       str(snake_case__     )     )
				self.assertIn(f"but found python=={python_ver}"			,       str(snake_case__     )     )
