





'''simple docstring'''

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


lowerCAmelCase__      :   Tuple      	=			random.Random()


def 						__UpperCamelCase						(			_UpperCAmelCase,       _UpperCAmelCase=1.0,       _UpperCAmelCase=None,       _UpperCAmelCase=None  ):
		if rng is None:
				__UpperCAmelCase	:							Any     =     global_rng

		__UpperCAmelCase	:							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      SCREAMING_SNAKE_CASE__      (   unittest.TestCase		):







					"""simple docstring"""






					def 		lowerCamelCase_   (   self       :      Any		):







							"""simple docstring"""
							__UpperCAmelCase	:							Any     =     "facebook/musicgen-small"
							__UpperCAmelCase	:							Any     =     tempfile.mkdtemp()
					def 		lowerCamelCase_   (   self       :      int		,	**UpperCAmelCase_       :      Optional[int]		):







							"""simple docstring"""
							return TaTokenizer.from_pretrained(self.checkpoint		,	**UpperCAmelCase_		)
					def 		lowerCamelCase_   (   self       :      Dict		,	**UpperCAmelCase_       :      List[Any]		):







							"""simple docstring"""
							return EncodecFeatureExtractor.from_pretrained(self.checkpoint		,	**UpperCAmelCase_		)
					def 		lowerCamelCase_   (   self       :      List[Any]		):







							"""simple docstring"""
							shutil.rmtree(self.tmpdirname		)
					def 		lowerCamelCase_   (   self       :      Dict		):







							"""simple docstring"""
							__UpperCAmelCase	:							Tuple     =     self.get_tokenizer()
							__UpperCAmelCase	:							Tuple     =     self.get_feature_extractor()

							__UpperCAmelCase	:							Optional[int]     =     MusicgenProcessor(tokenizer=UpperCAmelCase_		,	feature_extractor=UpperCAmelCase_		)

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







							"""simple docstring"""
							__UpperCAmelCase	:							Any     =     MusicgenProcessor(tokenizer=self.get_tokenizer()		,	feature_extractor=self.get_feature_extractor()		)
							processor.save_pretrained(self.tmpdirname		)

							__UpperCAmelCase	:							Optional[int]     =     self.get_tokenizer(bos_token="(BOS)"		,	eos_token="(EOS)"		)
							__UpperCAmelCase	:							Optional[Any]     =     self.get_feature_extractor(do_normalize=UpperCAmelCase_		,	padding_value=1.0		)

							__UpperCAmelCase	:							List[str]     =     MusicgenProcessor.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 		lowerCamelCase_   (   self       :      Optional[Any]		):







							"""simple docstring"""
							__UpperCAmelCase	:							Optional[Any]     =     self.get_feature_extractor()
							__UpperCAmelCase	:							str     =     self.get_tokenizer()

							__UpperCAmelCase	:							int     =     MusicgenProcessor(tokenizer=UpperCAmelCase_		,	feature_extractor=UpperCAmelCase_		)

							__UpperCAmelCase	:							Tuple     =     floats_list((3, 1_000)		)

							__UpperCAmelCase	:							List[str]     =     feature_extractor(UpperCAmelCase_		,	return_tensors="np"		)
							__UpperCAmelCase	:							Union[str, Any]     =     processor(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 		lowerCamelCase_   (   self       :      List[str]		):







							"""simple docstring"""
							__UpperCAmelCase	:							Optional[Any]     =     self.get_feature_extractor()
							__UpperCAmelCase	:							Optional[Any]     =     self.get_tokenizer()

							__UpperCAmelCase	:							str     =     MusicgenProcessor(tokenizer=UpperCAmelCase_		,	feature_extractor=UpperCAmelCase_		)

							__UpperCAmelCase	:							Any     =     "This is a test string"

							__UpperCAmelCase	:							Any     =     processor(text=UpperCAmelCase_		)

							__UpperCAmelCase	:							int     =     tokenizer(UpperCAmelCase_		)

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







							"""simple docstring"""
							__UpperCAmelCase	:							Any     =     self.get_feature_extractor()
							__UpperCAmelCase	:							Union[str, Any]     =     self.get_tokenizer()

							__UpperCAmelCase	:							Union[str, Any]     =     MusicgenProcessor(tokenizer=UpperCAmelCase_		,	feature_extractor=UpperCAmelCase_		)

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

							__UpperCAmelCase	:							int     =     processor.batch_decode(sequences=UpperCAmelCase_		)
							__UpperCAmelCase	:							str     =     tokenizer.batch_decode(UpperCAmelCase_		)

							self.assertListEqual(UpperCAmelCase_		,	UpperCAmelCase_		)
					def 		lowerCamelCase_   (   self       :      List[str]		):







							"""simple docstring"""
							__UpperCAmelCase	:							str     =     self.get_feature_extractor()
							__UpperCAmelCase	:							Union[str, Any]     =     self.get_tokenizer()

							__UpperCAmelCase	:							Optional[int]     =     MusicgenProcessor(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"		,	)






					def 		lowerCamelCase_   (   self       :      List[str]		):







							"""simple docstring"""
							__UpperCAmelCase	:							List[str]     =     self.get_feature_extractor(padding_side="left"		)
							__UpperCAmelCase	:							str     =     self.get_tokenizer()

							__UpperCAmelCase	:							Optional[int]     =     MusicgenProcessor(tokenizer=UpperCAmelCase_		,	feature_extractor=UpperCAmelCase_		)

							__UpperCAmelCase	:							List[Any]     =     [floats_list((1, x)		)[0] for x in range(5		,	20		,	5		)]
							__UpperCAmelCase	:							Any     =     processor(UpperCAmelCase_		).padding_mask

							__UpperCAmelCase	:							Dict     =     np.asarray(floats_list((3, 20)		)		)[:, None, :]
							__UpperCAmelCase	:							List[Any]     =     processor.batch_decode(UpperCAmelCase_		,	padding_mask=UpperCAmelCase_		)

							self.assertIsInstance(UpperCAmelCase_		,	UpperCAmelCase_		)

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











'''simple docstring'''

from typing import List

import PIL
import torch
from PIL import Image

from ...configuration_utils import ConfigMixin
from ...models.modeling_utils import ModelMixin
from ...utils import PIL_INTERPOLATION


class      SCREAMING_SNAKE_CASE__      (   snake_case__							,snake_case__		):







					"""simple docstring"""






					def __init__(   self       :      Any		):







							"""simple docstring"""
							super().__init__()

							self.register_buffer("watermark_image"		,	torch.zeros((62, 62, 4)		)		)
							__UpperCAmelCase	:							Optional[int]     =     None






					def 		lowerCamelCase_   (   self       :      List[Any]		,	UpperCAmelCase_       :      List[PIL.Image.Image]		,	UpperCAmelCase_       :      int=None		):







							"""simple docstring"""
							# copied from https://github.com/deep-floyd/IF/blob/b77482e36ca2031cb94dbca1001fc1e6400bf4ab/deepfloyd_if/modules/base.py#L287

							__UpperCAmelCase	:							int     =     images[0].height
							__UpperCAmelCase	:							Any     =     images[0].width

							__UpperCAmelCase	:							Union[str, Any]     =     sample_size or h

							__UpperCAmelCase	:							Dict     =     min(h / sample_size		,	w / sample_size		)
							__UpperCAmelCase  ,			__UpperCAmelCase	:							Optional[int]     =     (int(h / coef		), int(w / coef		)) if coef < 1 else (h, w)

							__UpperCAmelCase  ,			__UpperCAmelCase	:							Union[str, Any]     =     1_024**2, img_w * img_h
							__UpperCAmelCase	:							Optional[int]     =     (Sa / Sa) ** 0.5
							__UpperCAmelCase  ,			__UpperCAmelCase  ,			__UpperCAmelCase	:							str     =     int(K * 62		), img_w - int(14 * K		), img_h - int(14 * K		)

							if self.watermark_image_as_pil is None:
									__UpperCAmelCase	:							int     =     self.watermark_image.to(torch.uinta		).cpu().numpy()
									__UpperCAmelCase	:							Optional[int]     =     Image.fromarray(UpperCAmelCase_		,	mode="RGBA"		)
									__UpperCAmelCase	:							Optional[int]     =     watermark_image

							__UpperCAmelCase	:							List[str]     =     self.watermark_image_as_pil.resize(
							    (wm_size, wm_size)		,	PIL_INTERPOLATION["bicubic"]		,	reducing_gap=UpperCAmelCase_		)

							for pil_img in images:
									pil_img.paste(UpperCAmelCase_		,	box=(wm_x - wm_size, wm_y - wm_size, wm_x, wm_y)		,	mask=wm_img.split()[-1]		)

							return images




