






import random
import shutil
import tempfile
import unittest

import numpy as np

from transformers import TaTokenizer, TaTokenizerFast
from transformers.testing_utils import require_sentencepiece, require_torch
from transformers.utils.import_utils import is_speech_available, is_torch_available


if is_torch_available():
       pass

if is_speech_available():
       from transformers import EncodecFeatureExtractor, MusicgenProcessor


UpperCamelCase											=      random.Random()




def       _a   (							lowerCamelCase__ ,			lowerCamelCase__=1.0 ,			lowerCamelCase__=None ,			lowerCamelCase__=None				)  ->      Optional[Any]:
 if rng is None:
  lowerCamelCase_							:    Optional[Any]							=     global_rng

 lowerCamelCase_							:    int							=     []
 for batch_idx in range(shape[0]				):
  values.append([]				)
  for _ in range(shape[1]				):
   values[-1].append(rng.random() * scale				)

 return values





@require_torch
@require_sentencepiece
class 	lowerCamelCase__				(							unittest.TestCase ):






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


       """simple docstring"""






       lowerCamelCase_							:    Union[str, Any]							=     'facebook/musicgen-small'
       lowerCamelCase_							:    Tuple							=     tempfile.mkdtemp()






      def      UpperCAmelCase_     (self					:    Optional[int]		,		**_snake_case					:    Any		)       ->   Optional[Any]:


       """simple docstring"""






       return TaTokenizer.from_pretrained(self.checkpoint		,		**_snake_case		)






      def      UpperCAmelCase_     (self					:    Optional[Any]		,		**_snake_case					:    List[Any]		)       ->   Optional[int]:


       """simple docstring"""






       return EncodecFeatureExtractor.from_pretrained(self.checkpoint		,		**_snake_case		)






      def      UpperCAmelCase_     (self					:    str		)       ->   Any:


       """simple docstring"""






       shutil.rmtree(self.tmpdirname		)






      def      UpperCAmelCase_     (self					:    List[str]		)       ->   int:


       """simple docstring"""






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

       lowerCamelCase_							:    Tuple							=     MusicgenProcessor(tokenizer=_snake_case		,		feature_extractor=_snake_case		)

       processor.save_pretrained(self.tmpdirname		)
       lowerCamelCase_							:    str							=     MusicgenProcessor.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					:    Optional[int]		)       ->   Optional[int]:


       """simple docstring"""






       lowerCamelCase_							:    Tuple							=     MusicgenProcessor(tokenizer=self.get_tokenizer()		,		feature_extractor=self.get_feature_extractor()		)
       processor.save_pretrained(self.tmpdirname		)

       lowerCamelCase_							:    Dict							=     self.get_tokenizer(bos_token='(BOS)'		,		eos_token='(EOS)'		)
       lowerCamelCase_							:    Union[str, Any]							=     self.get_feature_extractor(do_normalize=_snake_case		,		padding_value=1.0		)

       lowerCamelCase_							:    List[str]							=     MusicgenProcessor.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					:    int		)       ->   Optional[int]:


       """simple docstring"""






       lowerCamelCase_							:    Tuple							=     self.get_feature_extractor()
       lowerCamelCase_							:    List[Any]							=     self.get_tokenizer()

       lowerCamelCase_							:    Tuple							=     MusicgenProcessor(tokenizer=_snake_case		,		feature_extractor=_snake_case		)

       lowerCamelCase_							:    Optional[int]							=     floats_list((3, 1000)		)

       lowerCamelCase_							:    int							=     feature_extractor(_snake_case		,		return_tensors='np'		)
       lowerCamelCase_							:    List[Any]							=     processor(_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					:    List[Any]		)       ->   Any:


       """simple docstring"""






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

       lowerCamelCase_							:    Dict							=     MusicgenProcessor(tokenizer=_snake_case		,		feature_extractor=_snake_case		)

       lowerCamelCase_							:    Optional[Any]							=     'This is a test string'

       lowerCamelCase_							:    Any							=     processor(text=_snake_case		)

       lowerCamelCase_							:    List[Any]							=     tokenizer(_snake_case		)

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






      def      UpperCAmelCase_     (self					:    str		)       ->   int:


       """simple docstring"""






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

       lowerCamelCase_							:    Optional[Any]							=     MusicgenProcessor(tokenizer=_snake_case		,		feature_extractor=_snake_case		)

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

       lowerCamelCase_							:    int							=     processor.batch_decode(sequences=_snake_case		)
       lowerCamelCase_							:    Union[str, Any]							=     tokenizer.batch_decode(_snake_case		)

       self.assertListEqual(_snake_case		,		_snake_case		)






      def      UpperCAmelCase_     (self					:    str		)       ->   Tuple:


       """simple docstring"""






       lowerCamelCase_							:    Dict							=     self.get_feature_extractor()
       lowerCamelCase_							:    Dict							=     self.get_tokenizer()

       lowerCamelCase_							:    int							=     MusicgenProcessor(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'		,		)






      def      UpperCAmelCase_     (self					:    Tuple		)       ->   int:


       """simple docstring"""






       lowerCamelCase_							:    Optional[int]							=     self.get_feature_extractor(padding_side='left'		)
       lowerCamelCase_							:    List[str]							=     self.get_tokenizer()

       lowerCamelCase_							:    Optional[Any]							=     MusicgenProcessor(tokenizer=_snake_case		,		feature_extractor=_snake_case		)

       lowerCamelCase_							:    Union[str, Any]							=     [floats_list((1, x)		)[0] for x in range(5		,		20		,		5		)]
       lowerCamelCase_							:    Union[str, Any]							=     processor(_snake_case		).padding_mask

       lowerCamelCase_							:    Any							=     np.asarray(floats_list((3, 20)		)		)[:, None, :]
       lowerCamelCase_							:    Union[str, Any]							=     processor.batch_decode(_snake_case		,		padding_mask=_snake_case		)

       self.assertIsInstance(_snake_case		,		_snake_case		)

       for audio in decoded_audios:
        self.assertIsInstance(_snake_case		,		np.ndarray		)

       self.assertTrue(decoded_audios[0].shape == (1, 10)		)
       self.assertTrue(decoded_audios[1].shape == (1, 15)		)
       self.assertTrue(decoded_audios[2].shape == (1, 20)		)








from collections import namedtuple

UpperCamelCase											=      namedtuple('''from_to''', '''from_ to''')

UpperCamelCase											=      {
    '''millimeter''': '''mm''',
    '''centimeter''': '''cm''',
    '''meter''': '''m''',
    '''kilometer''': '''km''',
    '''inch''': '''in''',
    '''inche''': '''in''',  # Trailing 's' has been stripped off
    '''feet''': '''ft''',
    '''foot''': '''ft''',
    '''yard''': '''yd''',
    '''mile''': '''mi''',
}

UpperCamelCase											=      {
    '''mm''': from_to(0.001, 1_0_0_0),
    '''cm''': from_to(0.01, 1_0_0),
    '''m''': from_to(1, 1),
    '''km''': from_to(1_0_0_0, 0.001),
    '''in''': from_to(0.0254, 39.3701),
    '''ft''': from_to(0.3048, 3.2_8084),
    '''yd''': from_to(0.9144, 1.0_9361),
    '''mi''': from_to(1609.34, 0.0_0062_1371),
}




def       _a   (							lowerCamelCase__ ,			lowerCamelCase__ ,			lowerCamelCase__				)  ->      float:
 lowerCamelCase_							:    List[Any]							=     from_type.lower().rstrip('s'				)
 lowerCamelCase_							:    Optional[Any]							=     TYPE_CONVERSION.get(lowerCamelCase__ ,			lowerCamelCase__				)
 lowerCamelCase_							:    List[Any]							=     to_type.lower().rstrip('s'				)
 lowerCamelCase_							:    Optional[int]							=     TYPE_CONVERSION.get(lowerCamelCase__ ,			lowerCamelCase__				)
 if new_from not in METRIC_CONVERSION:
  lowerCamelCase_							:    List[Any]							=     (
      F'Invalid \'from_type\' value: {from_type!r}.\n'
      F'Conversion abbreviations are: {", ".join(lowerCamelCase__				)}'
  )
  raise ValueError(lowerCamelCase__				)
 if new_to not in METRIC_CONVERSION:
  lowerCamelCase_							:    List[str]							=     (
      F'Invalid \'to_type\' value: {to_type!r}.\n'
      F'Conversion abbreviations are: {", ".join(lowerCamelCase__				)}'
  )
  raise ValueError(lowerCamelCase__				)
 return value * METRIC_CONVERSION[new_from].from_ * METRIC_CONVERSION[new_to].to


if __name__ == "__main__":
       import doctest

       doctest.testmod()
