
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


lowercase_											=	random.Random()


def     a__			(    snake_case   ,	snake_case=1.0   ,	snake_case=None   ,	snake_case=None    ):





  """simple docstring"""
  if rng is None:
    __SCREAMING_SNAKE_CASE     :					Optional[Any]			=						global_rng

  __SCREAMING_SNAKE_CASE     :					List[Any]			=						[]
  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   __UpperCamelCase				(    unittest.TestCase   ):






     """simple docstring"""







     def 				UpperCAmelCase__    (							self    :    Tuple				):







       """simple docstring"""




       __SCREAMING_SNAKE_CASE     :					Dict			=						'''facebook/musicgen-small'''
       __SCREAMING_SNAKE_CASE     :					Optional[int]			=						tempfile.mkdtemp()



     def 				UpperCAmelCase__    (							self    :    Dict      ,		**_A    :    int				):







       """simple docstring"""




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



     def 				UpperCAmelCase__    (							self    :    int      ,		**_A    :    List[str]				):







       """simple docstring"""




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



     def 				UpperCAmelCase__    (							self    :    Union[str, Any]				):







       """simple docstring"""




       shutil.rmtree(self.tmpdirname				)



     def 				UpperCAmelCase__    (							self    :    Optional[Any]				):







       """simple docstring"""




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

       __SCREAMING_SNAKE_CASE     :					Dict			=						MusicgenProcessor(tokenizer=_A      ,		feature_extractor=_A				)

       processor.save_pretrained(self.tmpdirname				)
       __SCREAMING_SNAKE_CASE     :					Union[str, Any]			=						MusicgenProcessor.from_pretrained(self.tmpdirname				)

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

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



     def 				UpperCAmelCase__    (							self    :    str				):







       """simple docstring"""




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

       __SCREAMING_SNAKE_CASE     :					int			=						self.get_tokenizer(bos_token='''(BOS)'''      ,		eos_token='''(EOS)'''				)
       __SCREAMING_SNAKE_CASE     :					Optional[Any]			=						self.get_feature_extractor(do_normalize=_A      ,		padding_value=1.0				)

       __SCREAMING_SNAKE_CASE     :					Optional[Any]			=						MusicgenProcessor.from_pretrained(
           self.tmpdirname      ,		bos_token='''(BOS)'''      ,		eos_token='''(EOS)'''      ,		do_normalize=_A      ,		padding_value=1.0				)

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

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



     def 				UpperCAmelCase__    (							self    :    Optional[Any]				):







       """simple docstring"""




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

       __SCREAMING_SNAKE_CASE     :					List[str]			=						MusicgenProcessor(tokenizer=_A      ,		feature_extractor=_A				)

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

       __SCREAMING_SNAKE_CASE     :					List[Any]			=						feature_extractor(_A      ,		return_tensors='''np'''				)
       __SCREAMING_SNAKE_CASE     :					Optional[int]			=						processor(_A      ,		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]				):







       """simple docstring"""




       __SCREAMING_SNAKE_CASE     :					Tuple			=						self.get_feature_extractor()
       __SCREAMING_SNAKE_CASE     :					Tuple			=						self.get_tokenizer()

       __SCREAMING_SNAKE_CASE     :					List[str]			=						MusicgenProcessor(tokenizer=_A      ,		feature_extractor=_A				)

       __SCREAMING_SNAKE_CASE     :					Any			=						'''This is a test string'''

       __SCREAMING_SNAKE_CASE     :					List[Any]			=						processor(text=_A				)

       __SCREAMING_SNAKE_CASE     :					Dict			=						tokenizer(_A				)

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



     def 				UpperCAmelCase__    (							self    :    Union[str, Any]				):







       """simple docstring"""




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

       __SCREAMING_SNAKE_CASE     :					Any			=						MusicgenProcessor(tokenizer=_A      ,		feature_extractor=_A				)

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

       __SCREAMING_SNAKE_CASE     :					int			=						processor.batch_decode(sequences=_A				)
       __SCREAMING_SNAKE_CASE     :					Any			=						tokenizer.batch_decode(_A				)

       self.assertListEqual(_A      ,		_A				)



     def 				UpperCAmelCase__    (							self    :    Any				):







       """simple docstring"""




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

       __SCREAMING_SNAKE_CASE     :					Any			=						MusicgenProcessor(tokenizer=_A      ,		feature_extractor=_A				)

       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    :    str				):







       """simple docstring"""




       __SCREAMING_SNAKE_CASE     :					str			=						self.get_feature_extractor(padding_side='''left'''				)
       __SCREAMING_SNAKE_CASE     :					int			=						self.get_tokenizer()

       __SCREAMING_SNAKE_CASE     :					str			=						MusicgenProcessor(tokenizer=_A      ,		feature_extractor=_A				)

       __SCREAMING_SNAKE_CASE     :					Tuple			=						[floats_list((1, x)				)[0] for x in range(5      ,		20      ,		5				)]
       __SCREAMING_SNAKE_CASE     :					Tuple			=						processor(_A				).padding_mask

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

       self.assertIsInstance(_A      ,		_A				)

       for audio in decoded_audios:
         self.assertIsInstance(_A      ,		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)				)


import unittest

from transformers import load_tool
from transformers.tools.agent_types import AGENT_TYPE_MAPPING

from .test_tools_common import ToolTesterMixin, output_types





class   __UpperCamelCase				(    unittest.TestCase				,		lowerCAmelCase__   ):






     """simple docstring"""







     def 				UpperCAmelCase__    (							self    :    Optional[Any]				):







       """simple docstring"""




       __SCREAMING_SNAKE_CASE     :					str			=						load_tool('''translation'''				)
       self.tool.setup()
       __SCREAMING_SNAKE_CASE     :					List[Any]			=						load_tool('''translation'''      ,		remote=_A				)



     def 				UpperCAmelCase__    (							self    :    List[Any]				):







       """simple docstring"""




       __SCREAMING_SNAKE_CASE     :					Any			=						self.tool('''Hey, what\'s up?'''      ,		src_lang='''English'''      ,		tgt_lang='''French'''				)
       self.assertEqual(_A      ,		'''- Hé, comment ça va?'''				)



     def 				UpperCAmelCase__    (							self    :    List[str]				):







       """simple docstring"""




       __SCREAMING_SNAKE_CASE     :					List[str]			=						self.remote_tool('''Hey, what\'s up?'''      ,		src_lang='''English'''      ,		tgt_lang='''French'''				)
       self.assertEqual(_A      ,		'''- Hé, comment ça va?'''				)



     def 				UpperCAmelCase__    (							self    :    Optional[Any]				):







       """simple docstring"""




       __SCREAMING_SNAKE_CASE     :					int			=						self.tool(text='''Hey, what\'s up?'''      ,		src_lang='''English'''      ,		tgt_lang='''French'''				)
       self.assertEqual(_A      ,		'''- Hé, comment ça va?'''				)



     def 				UpperCAmelCase__    (							self    :    int				):







       """simple docstring"""




       __SCREAMING_SNAKE_CASE     :					List[Any]			=						self.remote_tool(text='''Hey, what\'s up?'''      ,		src_lang='''English'''      ,		tgt_lang='''French'''				)
       self.assertEqual(_A      ,		'''- Hé, comment ça va?'''				)



     def 				UpperCAmelCase__    (							self    :    Union[str, Any]				):







       """simple docstring"""




       __SCREAMING_SNAKE_CASE     :					Optional[int]			=						['''Hey, what\'s up?''', '''English''', '''Spanish''']
       __SCREAMING_SNAKE_CASE     :					List[Any]			=						self.tool(*_A				)

       # There is a single output
       if len(self.tool.outputs				) == 1:
         __SCREAMING_SNAKE_CASE     :					Any			=						[outputs]

       self.assertListEqual(output_types(_A				)      ,		self.tool.outputs				)



     def 				UpperCAmelCase__    (							self    :    Union[str, Any]				):







       """simple docstring"""




       __SCREAMING_SNAKE_CASE     :					int			=						['''Hey, what\'s up?''', '''English''', '''Spanish''']
       __SCREAMING_SNAKE_CASE     :					List[str]			=						self.tool(*_A				)

       if not isinstance(_A      ,		_A				):
         __SCREAMING_SNAKE_CASE     :					Dict			=						[outputs]

       self.assertEqual(len(_A				)      ,		len(self.tool.outputs				)				)

       for output, output_type in zip(_A      ,		self.tool.outputs				):
         __SCREAMING_SNAKE_CASE     :					List[Any]			=						AGENT_TYPE_MAPPING[output_type]
         self.assertTrue(isinstance(_A      ,		_A				)				)




     def 				UpperCAmelCase__    (							self    :    List[str]				):







       """simple docstring"""




       __SCREAMING_SNAKE_CASE     :					Dict			=						['''Hey, what\'s up?''', '''English''', '''Spanish''']

       __SCREAMING_SNAKE_CASE     :					Optional[Any]			=						[]

       for _input, input_type in zip(_A      ,		self.tool.inputs				):
         if isinstance(_A      ,		_A				):
           _inputs.append([AGENT_TYPE_MAPPING[_input_type](_input				) for _input_type in input_type]				)
         else:
           _inputs.append(AGENT_TYPE_MAPPING[input_type](_input				)				)

        # Should not raise an error
       __SCREAMING_SNAKE_CASE     :					Union[str, Any]			=						self.tool(*_A				)

       if not isinstance(_A      ,		_A				):
         __SCREAMING_SNAKE_CASE     :					Dict			=						[outputs]

       self.assertEqual(len(_A				)      ,		len(self.tool.outputs				)				)
