

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


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







@require_torch
class     a__    (				unittest.TestCase    ):







  """simple docstring"""




  def 	_lowercase (     self      :	int						)   ->Optional[Any]:
        """simple docstring"""







        SCREAMING_SNAKE_CASE	: Union[str, Any]					       =				tempfile.mkdtemp()

        SCREAMING_SNAKE_CASE	: Any					       =				SpeechTaTokenizer(UpperCAmelCase__						)
        tokenizer.save_pretrained(self.tmpdirname						)

        SCREAMING_SNAKE_CASE	: str					       =				{
            """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	: Tuple					       =				os.path.join(self.tmpdirname			,			UpperCAmelCase__						)
        with open(self.feature_extraction_file			,			"""w"""			,			encoding="""utf-8"""						) as fp:
              fp.write(json.dumps(UpperCAmelCase__						) + """\n"""						)


  def 	_lowercase (     self      :	List[str]			,			**UpperCAmelCase__      :	Optional[int]						)   ->Dict:
        """simple docstring"""







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


  def 	_lowercase (     self      :	Dict			,			**UpperCAmelCase__      :	List[str]						)   ->Any:
        """simple docstring"""







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


  def 	_lowercase (     self      :	List[Any]						)   ->Any:
        """simple docstring"""







        shutil.rmtree(self.tmpdirname						)


  def 	_lowercase (     self      :	Optional[Any]						)   ->Optional[int]:
        """simple docstring"""







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

        SCREAMING_SNAKE_CASE	: Any					       =				SpeechTaProcessor(tokenizer=UpperCAmelCase__			,			feature_extractor=UpperCAmelCase__						)

        processor.save_pretrained(self.tmpdirname						)
        SCREAMING_SNAKE_CASE	: List[Any]					       =				SpeechTaProcessor.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 	_lowercase (     self      :	Tuple						)   ->Tuple:
        """simple docstring"""







        SCREAMING_SNAKE_CASE	: Optional[int]					       =				SpeechTaProcessor(tokenizer=self.get_tokenizer()			,			feature_extractor=self.get_feature_extractor()						)
        processor.save_pretrained(self.tmpdirname						)

        SCREAMING_SNAKE_CASE	: List[str]					       =				self.get_tokenizer(bos_token="""(BOS)"""			,			eos_token="""(EOS)"""						)
        SCREAMING_SNAKE_CASE	: Dict					       =				self.get_feature_extractor(do_normalize=UpperCAmelCase__			,			padding_value=1.0						)

        SCREAMING_SNAKE_CASE	: Union[str, Any]					       =				SpeechTaProcessor.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 	_lowercase (     self      :	str						)   ->Union[str, Any]:
        """simple docstring"""







        SCREAMING_SNAKE_CASE	: Any					       =				self.get_feature_extractor()
        SCREAMING_SNAKE_CASE	: Union[str, Any]					       =				self.get_tokenizer()

        SCREAMING_SNAKE_CASE	: Dict					       =				SpeechTaProcessor(tokenizer=UpperCAmelCase__			,			feature_extractor=UpperCAmelCase__						)

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

        SCREAMING_SNAKE_CASE	: List[str]					       =				feature_extractor(audio=UpperCAmelCase__			,			return_tensors="""np"""						)
        SCREAMING_SNAKE_CASE	: Dict					       =				processor(audio=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 	_lowercase (     self      :	Union[str, Any]						)   ->Optional[Any]:
        """simple docstring"""







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

        SCREAMING_SNAKE_CASE	: Union[str, Any]					       =				SpeechTaProcessor(tokenizer=UpperCAmelCase__			,			feature_extractor=UpperCAmelCase__						)

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

        SCREAMING_SNAKE_CASE	: Union[str, Any]					       =				feature_extractor(audio_target=UpperCAmelCase__			,			return_tensors="""np"""						)
        SCREAMING_SNAKE_CASE	: int					       =				processor(audio_target=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 	_lowercase (     self      :	str						)   ->Any:
        """simple docstring"""







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

        SCREAMING_SNAKE_CASE	: Optional[Any]					       =				SpeechTaProcessor(tokenizer=UpperCAmelCase__			,			feature_extractor=UpperCAmelCase__						)

        SCREAMING_SNAKE_CASE	: str					       =				"""This is a test string"""

        SCREAMING_SNAKE_CASE	: Optional[Any]					       =				processor(text=UpperCAmelCase__						)
        SCREAMING_SNAKE_CASE	: Tuple					       =				tokenizer(UpperCAmelCase__						)

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


  def 	_lowercase (     self      :	Dict						)   ->Dict:
        """simple docstring"""







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

        SCREAMING_SNAKE_CASE	: int					       =				SpeechTaProcessor(tokenizer=UpperCAmelCase__			,			feature_extractor=UpperCAmelCase__						)

        SCREAMING_SNAKE_CASE	: int					       =				"""This is a test string"""

        SCREAMING_SNAKE_CASE	: Union[str, Any]					       =				processor(text_target=UpperCAmelCase__						)
        SCREAMING_SNAKE_CASE	: int					       =				tokenizer(UpperCAmelCase__						)

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


  def 	_lowercase (     self      :	List[str]						)   ->Tuple:
        """simple docstring"""







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

        SCREAMING_SNAKE_CASE	: Union[str, Any]					       =				SpeechTaProcessor(tokenizer=UpperCAmelCase__			,			feature_extractor=UpperCAmelCase__						)

        SCREAMING_SNAKE_CASE	: List[str]					       =				[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

        SCREAMING_SNAKE_CASE	: Optional[Any]					       =				processor.batch_decode(UpperCAmelCase__						)
        SCREAMING_SNAKE_CASE	: str					       =				tokenizer.batch_decode(UpperCAmelCase__						)

        self.assertListEqual(UpperCAmelCase__			,			UpperCAmelCase__						)




  def 	_lowercase (     self      :	List[str]						)   ->Tuple:
        """simple docstring"""







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

        SCREAMING_SNAKE_CASE	: Any					       =				SpeechTaProcessor(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"""			,			)







UpperCAmelCase__	:       Union[str, Any]			      =      """
# Installation de Transformers
! pip install transformers datasets
# Pour installer à partir du code source au lieu de la dernière version, commentez la commande ci-dessus et décommentez la suivante.
# ! pip install git+https://github.com/huggingface/transformers.git
"""

UpperCAmelCase__	:       List[Any]			      =      [{"""type""": """code""", """content""": INSTALL_CONTENT}]
UpperCAmelCase__	:       List[Any]			      =      {
    """{processor_class}""": """FakeProcessorClass""",
    """{model_class}""": """FakeModelClass""",
    """{object_class}""": """FakeObjectClass""",
}




