


from collections import Counter
from pathlib import Path
from typing import Optional, Tuple

import yaml
class 		__magic_name__						(yaml.SafeLoader				):





     '''simple docstring'''



     def 	SCREAMING_SNAKE_CASE__						(					self:Union[str, Any]     ,		_a:List[str]   ):
       snake_case__						= [self.constructed_objects[key_node] for key_node, _ in node.value]
       snake_case__						= [tuple(_a   ) if isinstance(_a     ,		_a   ) else key for key in keys]
       snake_case__						= Counter(_a   )
       snake_case__						= [key for key in counter if counter[key] > 1]
       if duplicate_keys:
         raise TypeError(F"""Got duplicate yaml keys: {duplicate_keys}"""   )







     def 	SCREAMING_SNAKE_CASE__						(					self:Dict     ,		_a:Any     ,		_a:Any=False   ):
       snake_case__						= super().construct_mapping(_a     ,		deep=_a   )
       self._check_no_duplicates_on_constructed_node(_a   )
       return mapping







def SCREAMING_SNAKE_CASE	(			__lowerCAmelCase				)    ->				Tuple[Optional[str], str]:
  snake_case__						= list(readme_content.splitlines()				)
  if full_content and full_content[0] == "---" and "---" in full_content[1:]:
    snake_case__						= full_content[1:].index('''---'''				) + 1
    snake_case__						= '''\n'''.join(full_content[1:sep_idx]				)
    return yamlblock, "\n".join(full_content[sep_idx + 1 :]				)

  return None, "\n".join(__lowerCAmelCase				)
class 		__magic_name__						(snake_case_				):





     '''simple docstring'''



     __lowercase				:							Tuple	   =       {'train_eval_index'}  # train-eval-index in the YAML metadata

     @classmethod
     def 	SCREAMING_SNAKE_CASE__						(					cls:Tuple     ,		_a:Path   ):
       with open(_a     ,		encoding='''utf-8'''   ) as readme_file:
         snake_case__				,				snake_case__						= _split_yaml_from_readme(readme_file.read()   )
       if yaml_string is not None:
         return cls.from_yaml_string(_a   )
       else:
         return cls()

     def 	SCREAMING_SNAKE_CASE__						(					self:List[str]     ,		_a:Path   ):
       if path.exists():
         with open(_a     ,		encoding='''utf-8'''   ) as readme_file:
           snake_case__						= readme_file.read()
       else:
         snake_case__						= None
       snake_case__						= self._to_readme(_a   )
       with open(_a     ,		'''w'''     ,		encoding='''utf-8'''   ) as readme_file:
         readme_file.write(_a   )

     def 	SCREAMING_SNAKE_CASE__						(					self:Dict     ,		_a:Optional[str] = None   ):
       if readme_content is not None:
         snake_case__				,				snake_case__						= _split_yaml_from_readme(_a   )
         snake_case__						= '''---\n''' + self.to_yaml_string() + '''---\n''' + content
       else:
         snake_case__						= '''---\n''' + self.to_yaml_string() + '''---\n'''
       return full_content

     @classmethod
     def 	SCREAMING_SNAKE_CASE__						(					cls:str     ,		_a:str   ):
       snake_case__						= yaml.load(_a     ,		Loader=_NoDuplicateSafeLoader   ) or {}

       # Convert the YAML keys to DatasetMetadata fields
       snake_case__						= {
           (key.replace('''-'''     ,		'''_'''   ) if key.replace('''-'''     ,		'''_'''   ) in cls._FIELDS_WITH_DASHES else key): value
           for key, value in metadata_dict.items()
       }
       return cls(**_a   )







     def 	SCREAMING_SNAKE_CASE__						(					self:str   ):
       return yaml.safe_dump(
           {
               (key.replace('''_'''     ,		'''-'''   ) if key in self._FIELDS_WITH_DASHES else key): value
               for key, value in self.items()
           }     ,		sort_keys=_a     ,		allow_unicode=_a     ,		encoding='''utf-8'''     ,		).decode('''utf-8'''   )




lowerCamelCase__							:						List[str]							     =   {
    """image-classification""": [],
    """translation""": [],
    """image-segmentation""": [],
    """fill-mask""": [],
    """automatic-speech-recognition""": [],
    """token-classification""": [],
    """sentence-similarity""": [],
    """audio-classification""": [],
    """question-answering""": [],
    """summarization""": [],
    """zero-shot-classification""": [],
    """table-to-text""": [],
    """feature-extraction""": [],
    """other""": [],
    """multiple-choice""": [],
    """text-classification""": [],
    """text-to-image""": [],
    """text2text-generation""": [],
    """zero-shot-image-classification""": [],
    """tabular-classification""": [],
    """tabular-regression""": [],
    """image-to-image""": [],
    """tabular-to-text""": [],
    """unconditional-image-generation""": [],
    """text-retrieval""": [],
    """text-to-speech""": [],
    """object-detection""": [],
    """audio-to-audio""": [],
    """text-generation""": [],
    """conversational""": [],
    """table-question-answering""": [],
    """visual-question-answering""": [],
    """image-to-text""": [],
    """reinforcement-learning""": [],
    """voice-activity-detection""": [],
    """time-series-forecasting""": [],
    """document-question-answering""": [],
}


if __name__ == "__main__":
   from argparse import ArgumentParser

   lowerCamelCase__							:						str							     =   ArgumentParser(usage="""Validate the yaml metadata block of a README.md file.""")
   ap.add_argument("""readme_filepath""")
   lowerCamelCase__							:						Optional[Any]							     =   ap.parse_args()

   lowerCamelCase__							:						List[Any]							     =   Path(args.readme_filepath)
   lowerCamelCase__							:						Optional[Any]							     =   DatasetMetadata.from_readme(readme_filepath)
   print(dataset_metadata)
   dataset_metadata.to_readme(readme_filepath)



import gc
import unittest

from parameterized import parameterized

from diffusers import FlaxUNetaDConditionModel
from diffusers.utils import is_flax_available
from diffusers.utils.testing_utils import load_hf_numpy, require_flax, slow


if is_flax_available():
		import jax
		import jax.numpy as jnp







@slow
@require_flax
class 							lowerCAmelCase__   ( unittest.TestCase						):

						def 			A_					(      self							,				a							,				a					)       ->   Optional[int]:


								'''simple docstring'''

								return F'gaussian_noise_s={seed}_shape={"_".join([str(a					) for s in shape]					)}.npy'

						def 			A_					(      self					)       ->   List[Any]:


								'''simple docstring'''

								super().tearDown()
								gc.collect()

						def 			A_					(      self							,				a=0							,				a=(4, 4, 64, 64)							,				a=False					)       ->   Optional[int]:


								'''simple docstring'''

								_UpperCamelCase         =						jnp.bfloataa if fpaa else jnp.floataa
								_UpperCamelCase         =						jnp.array(load_hf_numpy(self.get_file_format(a							,				a					)					)							,				dtype=a					)
								return image

						def 			A_					(      self							,				a=False							,				a="CompVis/stable-diffusion-v1-4"					)       ->   Any:


								'''simple docstring'''

								_UpperCamelCase         =						jnp.bfloataa if fpaa else jnp.floataa
								_UpperCamelCase         =						"""bf16""" if fpaa else None

								_UpperCamelCase     ,						_UpperCamelCase         =						FlaxUNetaDConditionModel.from_pretrained(
								    a							,				subfolder="""unet"""							,				dtype=a							,				revision=a					)
								return model, params

						def 			A_					(      self							,				a=0							,				a=(4, 77, 7_68)							,				a=False					)       ->   Tuple:


								'''simple docstring'''

								_UpperCamelCase         =						jnp.bfloataa if fpaa else jnp.floataa
								_UpperCamelCase         =						jnp.array(load_hf_numpy(self.get_file_format(a							,				a					)					)							,				dtype=a					)
								return hidden_states

						@parameterized.expand(
						    [
						        # fmt: off
						        [83, 4, [-0.2323, -0.1304, 0.0813, -0.3093, -0.0919, -0.1571, -0.1125, -0.5806]],
						        [17, 0.55, [-0.0831, -0.2443, 0.0901, -0.0919, 0.3396, 0.0103, -0.3743, 0.0701]],
						        [8, 0.89, [-0.4863, 0.0859, 0.0875, -0.1658, 0.9199, -0.0114, 0.4839, 0.4639]],
						        [3, 10_00, [-0.5649, 0.2402, -0.5518, 0.1248, 1.1328, -0.2443, -0.0325, -1.0078]],
						        # fmt: on
						    ]					)
						def 			A_					(      self							,				a							,				a							,				a					)       ->   Any:


								'''simple docstring'''

								_UpperCamelCase     ,						_UpperCamelCase         =						self.get_unet_model(model_id="""CompVis/stable-diffusion-v1-4"""							,				fpaa=a					)
								_UpperCamelCase         =						self.get_latents(a							,				fpaa=a					)
								_UpperCamelCase         =						self.get_encoder_hidden_states(a							,				fpaa=a					)

								_UpperCamelCase         =						model.apply(
								    {"""params""": params}							,				a							,				jnp.array(a							,				dtype=jnp.intaa					)							,				encoder_hidden_states=a							,				).sample

								assert sample.shape == latents.shape

								_UpperCamelCase         =						jnp.asarray(jax.device_get((sample[-1, -2:, -2:, :2].flatten())					)							,				dtype=jnp.floataa					)
								_UpperCamelCase         =						jnp.array(a							,				dtype=jnp.floataa					)

								# Found torch (float16) and flax (bfloat16) outputs to be within this tolerance, in the same hardware
								assert jnp.allclose(a							,				a							,				atol=1e-2					)







						@parameterized.expand(
						    [
						        # fmt: off
						        [83, 4, [0.1514, 0.0807, 0.1624, 0.1016, -0.1896, 0.0263, 0.0677, 0.2310]],
						        [17, 0.55, [0.1164, -0.0216, 0.0170, 0.1589, -0.3120, 0.1005, -0.0581, -0.1458]],
						        [8, 0.89, [-0.1758, -0.0169, 0.1004, -0.1411, 0.1312, 0.1103, -0.1996, 0.2139]],
						        [3, 10_00, [0.1214, 0.0352, -0.0731, -0.1562, -0.0994, -0.0906, -0.2340, -0.0539]],
						        # fmt: on
						    ]					)
						def 			A_					(      self							,				a							,				a							,				a					)       ->   Any:


								'''simple docstring'''

								_UpperCamelCase     ,						_UpperCamelCase         =						self.get_unet_model(model_id="""stabilityai/stable-diffusion-2"""							,				fpaa=a					)
								_UpperCamelCase         =						self.get_latents(a							,				shape=(4, 4, 96, 96)							,				fpaa=a					)
								_UpperCamelCase         =						self.get_encoder_hidden_states(a							,				shape=(4, 77, 10_24)							,				fpaa=a					)

								_UpperCamelCase         =						model.apply(
								    {"""params""": params}							,				a							,				jnp.array(a							,				dtype=jnp.intaa					)							,				encoder_hidden_states=a							,				).sample

								assert sample.shape == latents.shape

								_UpperCamelCase         =						jnp.asarray(jax.device_get((sample[-1, -2:, -2:, :2].flatten())					)							,				dtype=jnp.floataa					)
								_UpperCamelCase         =						jnp.array(a							,				dtype=jnp.floataa					)

								# Found torch (float16) and flax (bfloat16) outputs to be within this tolerance, on the same hardware
								assert jnp.allclose(a							,				a							,				atol=1e-2					)



