





import json
import os
import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers import BertTokenizer, BertTokenizerFast
from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES
from transformers.testing_utils import require_vision
from transformers.utils import FEATURE_EXTRACTOR_NAME, is_vision_available


if is_vision_available():
    from PIL import Image

    from transformers import ChineseCLIPImageProcessor, ChineseCLIPProcessor

@require_vision
class      A       (	unittest.TestCase						):



      def 	lowerCAmelCase__    (	self:						Dict       )						->    str:
         '''simple docstring'''

         UpperCAmelCase_											=tempfile.mkdtemp()

         UpperCAmelCase_											=[
             "[UNK]",
             "[CLS]",
             "[SEP]",
             "[PAD]",
             "[MASK]",
             "的",
             "价",
             "格",
             "是",
             "15",
             "便",
             "alex",
             "##andra",
             "，",
             "。",
             "-",
             "t",
             "shirt",
         ]
         UpperCAmelCase_											=os.path.join(self.tmpdirname		,  VOCAB_FILES_NAMES["vocab_file"]       )
         with open(self.vocab_file		,  "w"		,  encoding="utf-8"       ) as vocab_writer:
            vocab_writer.write("".join([x + "\n" for x in vocab_tokens]       )       )

         UpperCAmelCase_											={
             "do_resize": True,
             "size": {"height": 224, "width": 224},
             "do_center_crop": True,
             "crop_size": {"height": 18, "width": 18},
             "do_normalize": True,
             "image_mean": [0.48_14_54_66, 0.4_57_82_75, 0.40_82_10_73],
             "image_std": [0.26_86_29_54, 0.26_13_02_58, 0.27_57_77_11],
             "do_convert_rgb": True,
         }
         UpperCAmelCase_											=os.path.join(self.tmpdirname		,  _lowerCAmelCase       )
         with open(self.image_processor_file		,  "w"		,  encoding="utf-8"       ) as fp:
            json.dump(_lowerCAmelCase		,  _lowerCAmelCase       )



      def 	lowerCAmelCase__    (	self:						List[str]		,  **_lowerCAmelCase:						List[Any]       )						->    Optional[int]:
         '''simple docstring'''

         return BertTokenizer.from_pretrained(self.tmpdirname		,  **_lowerCAmelCase       )



      def 	lowerCAmelCase__    (	self:						List[Any]		,  **_lowerCAmelCase:						Optional[int]       )						->    List[str]:
         '''simple docstring'''

         return BertTokenizerFast.from_pretrained(self.tmpdirname		,  **_lowerCAmelCase       )



      def 	lowerCAmelCase__    (	self:						List[Any]		,  **_lowerCAmelCase:						Optional[Any]       )						->    str:
         '''simple docstring'''

         return ChineseCLIPImageProcessor.from_pretrained(self.tmpdirname		,  **_lowerCAmelCase       )



      def 	lowerCAmelCase__    (	self:						List[str]       )						->    List[Any]:
         '''simple docstring'''

         shutil.rmtree(self.tmpdirname       )



      def 	lowerCAmelCase__    (	self:						List[Any]       )						->    str:
         '''simple docstring'''

         UpperCAmelCase_											=[np.random.randint(255		,  size=(3, 30, 400)		,  dtype=np.uinta       )]

         UpperCAmelCase_											=[Image.fromarray(np.moveaxis(_lowerCAmelCase		,  0		,  -1       )       ) for x in image_inputs]

         return image_inputs



      def 	lowerCAmelCase__    (	self:						Any       )						->    Dict:
         '''simple docstring'''

         UpperCAmelCase_											=self.get_tokenizer()
         UpperCAmelCase_											=self.get_rust_tokenizer()
         UpperCAmelCase_											=self.get_image_processor()

         UpperCAmelCase_											=ChineseCLIPProcessor(tokenizer=_lowerCAmelCase		,  image_processor=_lowerCAmelCase       )
         processor_slow.save_pretrained(self.tmpdirname       )
         UpperCAmelCase_											=ChineseCLIPProcessor.from_pretrained(self.tmpdirname		,  use_fast=_lowerCAmelCase       )

         UpperCAmelCase_											=ChineseCLIPProcessor(tokenizer=_lowerCAmelCase		,  image_processor=_lowerCAmelCase       )
         processor_fast.save_pretrained(self.tmpdirname       )
         UpperCAmelCase_											=ChineseCLIPProcessor.from_pretrained(self.tmpdirname       )

         self.assertEqual(processor_slow.tokenizer.get_vocab()		,  tokenizer_slow.get_vocab()       )
         self.assertEqual(processor_fast.tokenizer.get_vocab()		,  tokenizer_fast.get_vocab()       )
         self.assertEqual(tokenizer_slow.get_vocab()		,  tokenizer_fast.get_vocab()       )
         self.assertIsInstance(processor_slow.tokenizer		,  _lowerCAmelCase       )
         self.assertIsInstance(processor_fast.tokenizer		,  _lowerCAmelCase       )

         self.assertEqual(processor_slow.image_processor.to_json_string()		,  image_processor.to_json_string()       )
         self.assertEqual(processor_fast.image_processor.to_json_string()		,  image_processor.to_json_string()       )
         self.assertIsInstance(processor_slow.image_processor		,  _lowerCAmelCase       )
         self.assertIsInstance(processor_fast.image_processor		,  _lowerCAmelCase       )



      def 	lowerCAmelCase__    (	self:						List[str]       )						->    Any:
         '''simple docstring'''

         UpperCAmelCase_											=ChineseCLIPProcessor(tokenizer=self.get_tokenizer()		,  image_processor=self.get_image_processor()       )
         processor.save_pretrained(self.tmpdirname       )

         UpperCAmelCase_											=self.get_tokenizer(cls_token="(CLS)"		,  sep_token="(SEP)"       )
         UpperCAmelCase_											=self.get_image_processor(do_normalize=_lowerCAmelCase       )

         UpperCAmelCase_											=ChineseCLIPProcessor.from_pretrained(
             self.tmpdirname		,  cls_token="(CLS)"		,  sep_token="(SEP)"		,  do_normalize=_lowerCAmelCase       )

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

         self.assertEqual(processor.image_processor.to_json_string()		,  image_processor_add_kwargs.to_json_string()       )
         self.assertIsInstance(processor.image_processor		,  _lowerCAmelCase       )



      def 	lowerCAmelCase__    (	self:						Any       )						->    int:
         '''simple docstring'''

         UpperCAmelCase_											=self.get_image_processor()
         UpperCAmelCase_											=self.get_tokenizer()

         UpperCAmelCase_											=ChineseCLIPProcessor(tokenizer=_lowerCAmelCase		,  image_processor=_lowerCAmelCase       )

         UpperCAmelCase_											=self.prepare_image_inputs()

         UpperCAmelCase_											=image_processor(_lowerCAmelCase		,  return_tensors="np"       )
         UpperCAmelCase_											=processor(images=_lowerCAmelCase		,  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:						str       )						->    Dict:
         '''simple docstring'''

         UpperCAmelCase_											=self.get_image_processor()
         UpperCAmelCase_											=self.get_tokenizer()

         UpperCAmelCase_											=ChineseCLIPProcessor(tokenizer=_lowerCAmelCase		,  image_processor=_lowerCAmelCase       )

         UpperCAmelCase_											="Alexandra，T-shirt的价格是15便士。"

         UpperCAmelCase_											=processor(text=_lowerCAmelCase       )

         UpperCAmelCase_											=tokenizer(_lowerCAmelCase       )

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



      def 	lowerCAmelCase__    (	self:						Dict       )						->    Union[str, Any]:
         '''simple docstring'''

         UpperCAmelCase_											=self.get_image_processor()
         UpperCAmelCase_											=self.get_tokenizer()

         UpperCAmelCase_											=ChineseCLIPProcessor(tokenizer=_lowerCAmelCase		,  image_processor=_lowerCAmelCase       )

         UpperCAmelCase_											="Alexandra，T-shirt的价格是15便士。"
         UpperCAmelCase_											=self.prepare_image_inputs()

         UpperCAmelCase_											=processor(text=_lowerCAmelCase		,  images=_lowerCAmelCase       )

         self.assertListEqual(list(inputs.keys()       )		,  ["input_ids", "token_type_ids", "attention_mask", "pixel_values"]       )

         # test if it raises when no input is passed
         with pytest.raises(_lowerCAmelCase       ):
            processor()



      def 	lowerCAmelCase__    (	self:						str       )						->    int:
         '''simple docstring'''

         UpperCAmelCase_											=self.get_image_processor()
         UpperCAmelCase_											=self.get_tokenizer()

         UpperCAmelCase_											=ChineseCLIPProcessor(tokenizer=_lowerCAmelCase		,  image_processor=_lowerCAmelCase       )

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

         UpperCAmelCase_											=processor.batch_decode(_lowerCAmelCase       )
         UpperCAmelCase_											=tokenizer.batch_decode(_lowerCAmelCase       )

         self.assertListEqual(_lowerCAmelCase		,  _lowerCAmelCase       )






      def 	lowerCAmelCase__    (	self:						Dict       )						->    Optional[int]:
         '''simple docstring'''

         UpperCAmelCase_											=self.get_image_processor()
         UpperCAmelCase_											=self.get_tokenizer()

         UpperCAmelCase_											=ChineseCLIPProcessor(tokenizer=_lowerCAmelCase		,  image_processor=_lowerCAmelCase       )

         UpperCAmelCase_											="Alexandra，T-shirt的价格是15便士。"
         UpperCAmelCase_											=self.prepare_image_inputs()

         UpperCAmelCase_											=processor(text=_lowerCAmelCase		,  images=_lowerCAmelCase       )

         self.assertListEqual(list(inputs.keys()       )		,  processor.model_input_names       )











import gc
import unittest

import numpy as np
import torch

from diffusers import DanceDiffusionPipeline, IPNDMScheduler, UNetaDModel
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, skip_mps

from ..pipeline_params import UNCONDITIONAL_AUDIO_GENERATION_BATCH_PARAMS, UNCONDITIONAL_AUDIO_GENERATION_PARAMS
from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()

class      A       (	__lowercase		,      unittest.TestCase						):
      _snake_case							     =DanceDiffusionPipeline
      _snake_case							     =UNCONDITIONAL_AUDIO_GENERATION_PARAMS
      _snake_case							     =PipelineTesterMixin.required_optional_params - {
          '''callback''',
          '''latents''',
          '''callback_steps''',
          '''output_type''',
          '''num_images_per_prompt''',
      }
      _snake_case							     =UNCONDITIONAL_AUDIO_GENERATION_BATCH_PARAMS
      _snake_case							     =False
      _snake_case							     =False



      def 	lowerCAmelCase__    (	self:						Tuple       )						->    Tuple:
         '''simple docstring'''

         torch.manual_seed(0       )
         UpperCAmelCase_											=UNetaDModel(
             block_out_channels=(32, 32, 64)		,  extra_in_channels=16		,  sample_size=512		,  sample_rate=1_6000		,  in_channels=2		,  out_channels=2		,  flip_sin_to_cos=_lowerCAmelCase		,  use_timestep_embedding=_lowerCAmelCase		,  time_embedding_type="fourier"		,  mid_block_type="UNetMidBlock1D"		,  down_block_types=("DownBlock1DNoSkip", "DownBlock1D", "AttnDownBlock1D")		,  up_block_types=("AttnUpBlock1D", "UpBlock1D", "UpBlock1DNoSkip")		,  )
         UpperCAmelCase_											=IPNDMScheduler()

         UpperCAmelCase_											={
             "unet": unet,
             "scheduler": scheduler,
         }
         return components



      def 	lowerCAmelCase__    (	self:						str		,  _lowerCAmelCase:						Tuple		,  _lowerCAmelCase:						List[str]=0       )						->    str:
         '''simple docstring'''

         if str(_lowerCAmelCase       ).startswith("mps"       ):
            UpperCAmelCase_											=torch.manual_seed(_lowerCAmelCase       )
         else:
            UpperCAmelCase_											=torch.Generator(device=_lowerCAmelCase       ).manual_seed(_lowerCAmelCase       )
         UpperCAmelCase_											={
             "batch_size": 1,
             "generator": generator,
             "num_inference_steps": 4,
         }
         return inputs



      def 	lowerCAmelCase__    (	self:						Dict       )						->    Tuple:
         '''simple docstring'''

         UpperCAmelCase_											="cpu"  # ensure determinism for the device-dependent torch.Generator
         UpperCAmelCase_											=self.get_dummy_components()
         UpperCAmelCase_											=DanceDiffusionPipeline(**_lowerCAmelCase       )
         UpperCAmelCase_											=pipe.to(_lowerCAmelCase       )
         pipe.set_progress_bar_config(disable=_lowerCAmelCase       )

         UpperCAmelCase_											=self.get_dummy_inputs(_lowerCAmelCase       )
         UpperCAmelCase_											=pipe(**_lowerCAmelCase       )
         UpperCAmelCase_											=output.audios

         UpperCAmelCase_											=audio[0, -3:, -3:]

         assert audio.shape == (1, 2, components["unet"].sample_size)
         UpperCAmelCase_											=np.array([-0.72_65, 1.00_00, -0.83_88, 0.11_75, 0.94_98, -1.00_00]       )
         assert np.abs(audio_slice.flatten() - expected_slice       ).max() < 1e-2



      @skip_mps
      def 	lowerCAmelCase__    (	self:						Optional[int]       )						->    Any:
         '''simple docstring'''

         return super().test_save_load_local()



      @skip_mps
      def 	lowerCAmelCase__    (	self:						Union[str, Any]       )						->    int:
         '''simple docstring'''

         return super().test_dict_tuple_outputs_equivalent(expected_max_difference=3e-3       )



      @skip_mps
      def 	lowerCAmelCase__    (	self:						List[str]       )						->    Optional[int]:
         '''simple docstring'''

         return super().test_save_load_optional_components()



      @skip_mps
      def 	lowerCAmelCase__    (	self:						Optional[Any]       )						->    Union[str, Any]:
         '''simple docstring'''

         return super().test_attention_slicing_forward_pass()






      def 	lowerCAmelCase__    (	self:						Optional[int]       )						->    Optional[Any]:
         '''simple docstring'''

         super().test_inference_batch_single_identical(expected_max_diff=3e-3       )

@slow
@require_torch_gpu
class      A       (	unittest.TestCase						):



      def 	lowerCAmelCase__    (	self:						Any       )						->    Optional[Any]:
         '''simple docstring'''

         super().tearDown()
         gc.collect()
         torch.cuda.empty_cache()



      def 	lowerCAmelCase__    (	self:						Union[str, Any]       )						->    Tuple:
         '''simple docstring'''

         UpperCAmelCase_											=torch_device

         UpperCAmelCase_											=DanceDiffusionPipeline.from_pretrained("harmonai/maestro-150k"       )
         UpperCAmelCase_											=pipe.to(_lowerCAmelCase       )
         pipe.set_progress_bar_config(disable=_lowerCAmelCase       )

         UpperCAmelCase_											=torch.manual_seed(0       )
         UpperCAmelCase_											=pipe(generator=_lowerCAmelCase		,  num_inference_steps=100		,  audio_length_in_s=4.0_96       )
         UpperCAmelCase_											=output.audios

         UpperCAmelCase_											=audio[0, -3:, -3:]

         assert audio.shape == (1, 2, pipe.unet.sample_size)
         UpperCAmelCase_											=np.array([-0.01_92, -0.02_31, -0.03_18, -0.00_59, 0.00_02, -0.00_20]       )

         assert np.abs(audio_slice.flatten() - expected_slice       ).max() < 1e-2






      def 	lowerCAmelCase__    (	self:						Tuple       )						->    Any:
         '''simple docstring'''

         UpperCAmelCase_											=torch_device

         UpperCAmelCase_											=DanceDiffusionPipeline.from_pretrained("harmonai/maestro-150k"		,  torch_dtype=torch.floataa       )
         UpperCAmelCase_											=pipe.to(_lowerCAmelCase       )
         pipe.set_progress_bar_config(disable=_lowerCAmelCase       )

         UpperCAmelCase_											=torch.manual_seed(0       )
         UpperCAmelCase_											=pipe(generator=_lowerCAmelCase		,  num_inference_steps=100		,  audio_length_in_s=4.0_96       )
         UpperCAmelCase_											=output.audios

         UpperCAmelCase_											=audio[0, -3:, -3:]

         assert audio.shape == (1, 2, pipe.unet.sample_size)
         UpperCAmelCase_											=np.array([-0.03_67, -0.04_88, -0.07_71, -0.05_25, -0.04_44, -0.03_41]       )

         assert np.abs(audio_slice.flatten() - expected_slice       ).max() < 1e-2




