





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

import yaml




class 	UpperCamelCase_							(  yaml.SafeLoader     ):


							def   _snake_case	(				self     :List[str]				,					__A     :List[Any]		) -> Optional[int]:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								=   [self.constructed_objects[key_node] for key_node, _ in node.value]
									SCREAMING_SNAKE_CASE__								=   [tuple(__A		) if isinstance(__A				,					__A		) else key for key in keys]
									SCREAMING_SNAKE_CASE__								=   Counter(__A		)
									SCREAMING_SNAKE_CASE__								=   [key for key in counter if counter[key] > 1]
									if duplicate_keys:
											raise TypeError(f'''Got duplicate yaml keys: {duplicate_keys}'''		)


							def   _snake_case	(				self     :Optional[int]				,					__A     :List[Any]				,					__A     :Union[str, Any]=False		) -> int:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								=   super().construct_mapping(__A				,					deep=__A		)
									self._check_no_duplicates_on_constructed_node(__A		)
									return mapping






def 					SCREAMING_SNAKE_CASE__				(							UpperCamelCase__:				str     ):
		SCREAMING_SNAKE_CASE__								=   list(readme_content.splitlines()     )
		if full_content and full_content[0] == "---" and "---" in full_content[1:]:
				SCREAMING_SNAKE_CASE__								=   full_content[1:].index("""---"""     ) + 1
				SCREAMING_SNAKE_CASE__								=   """\n""".join(full_content[1:sep_idx]     )
				return yamlblock, "\n".join(full_content[sep_idx + 1 :]     )

		return None, "\n".join(UpperCamelCase__     )




class 	UpperCamelCase_							(  UpperCamelCase__     ):
							# class attributes
							lowerCamelCase_							     =       {"train_eval_index"}  # train-eval-index in the YAML metadata


							@classmethod
							def   _snake_case	(				cls     :int				,					__A     :Path		) -> "DatasetMetadata":



									"""simple docstring"""



									with open(__A				,					encoding="""utf-8"""		) as readme_file:
											SCREAMING_SNAKE_CASE__						,    SCREAMING_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   _snake_case	(				self     :int				,					__A     :Path		) -> str:



									"""simple docstring"""



									if path.exists():
											with open(__A				,					encoding="""utf-8"""		) as readme_file:
													SCREAMING_SNAKE_CASE__								=   readme_file.read()
									else:
											SCREAMING_SNAKE_CASE__								=   None
									SCREAMING_SNAKE_CASE__								=   self._to_readme(__A		)
									with open(__A				,					"""w"""				,					encoding="""utf-8"""		) as readme_file:
											readme_file.write(__A		)


							def   _snake_case	(				self     :int				,					__A     :Optional[str] = None		) -> str:



									"""simple docstring"""



									if readme_content is not None:
											SCREAMING_SNAKE_CASE__						,    SCREAMING_SNAKE_CASE__								=   _split_yaml_from_readme(__A		)
											SCREAMING_SNAKE_CASE__								=   """---\n""" + self.to_yaml_string() + """---\n""" + content
									else:
											SCREAMING_SNAKE_CASE__								=   """---\n""" + self.to_yaml_string() + """---\n"""
									return full_content


							@classmethod
							def   _snake_case	(				cls     :Tuple				,					__A     :str		) -> "DatasetMetadata":



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								=   yaml.load(__A				,					Loader=_NoDuplicateSafeLoader		) or {}

									# Convert the YAML keys to DatasetMetadata fields
									SCREAMING_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   _snake_case	(				self     :Union[str, Any]		) -> str:



									"""simple docstring"""



									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 					=      {
    '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 					=      ArgumentParser(usage='Validate the yaml metadata block of a README.md file.')
	ap.add_argument('readme_filepath')
	_lowerCamelCase 					=      ap.parse_args()

	_lowerCamelCase 					=      Path(args.readme_filepath)
	_lowerCamelCase 					=      DatasetMetadata.from_readme(readme_filepath)
	print(dataset_metadata)
	dataset_metadata.to_readme(readme_filepath)






import warnings
from typing import List

import numpy as np

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding
from ...utils import is_flax_available, is_tf_available, is_torch_available




class 	UpperCamelCase_							(  UpperCamelCase__     ):
							lowerCamelCase_							     =       ["image_processor", "tokenizer"]
							lowerCamelCase_							     =       "OwlViTImageProcessor"
							lowerCamelCase_							     =       ("CLIPTokenizer", "CLIPTokenizerFast")


							def __init__(				self     :Optional[Any]				,					__A     :int=None				,					__A     :Optional[int]=None				,					**__A     :str		) -> Optional[int]:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								=   None
									if "feature_extractor" in kwargs:
											warnings.warn(
											    """The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`"""
											    """ instead."""				,					__A				,					)
											SCREAMING_SNAKE_CASE__								=   kwargs.pop("""feature_extractor"""		)

									SCREAMING_SNAKE_CASE__								=   image_processor if image_processor is not None else feature_extractor
									if image_processor is None:
											raise ValueError("""You need to specify an `image_processor`."""		)
									if tokenizer is None:
											raise ValueError("""You need to specify a `tokenizer`."""		)

									super().__init__(__A				,					__A		)


							def __call__(				self     :str				,					__A     :Dict=None				,					__A     :List[str]=None				,					__A     :str=None				,					__A     :Optional[int]="max_length"				,					__A     :Tuple="np"				,					**__A     :int		) -> Tuple:



									"""simple docstring"""



									if text is None and query_images is None and images is None:
											raise ValueError(
											    """You have to specify at least one text or query image or image. All three cannot be none."""		)

									if text is not None:
											if isinstance(__A				,					__A		) or (isinstance(__A				,					__A		) and not isinstance(text[0]				,					__A		)):
													SCREAMING_SNAKE_CASE__								=   [self.tokenizer(__A				,					padding=__A				,					return_tensors=__A				,					**__A		)]

											elif isinstance(__A				,					__A		) and isinstance(text[0]				,					__A		):
													SCREAMING_SNAKE_CASE__								=   []

													# Maximum number of queries across batch
													SCREAMING_SNAKE_CASE__								=   max([len(__A		) for t in text]		)

													# Pad all batch samples to max number of text queries
													for t in text:
															if len(__A		) != max_num_queries:
																	SCREAMING_SNAKE_CASE__								=   t + [""" """] * (max_num_queries - len(__A		))

															SCREAMING_SNAKE_CASE__								=   self.tokenizer(__A				,					padding=__A				,					return_tensors=__A				,					**__A		)
															encodings.append(__A		)
											else:
													raise TypeError("""Input text should be a string, a list of strings or a nested list of strings"""		)

											if return_tensors == "np":
													SCREAMING_SNAKE_CASE__								=   np.concatenate([encoding["""input_ids"""] for encoding in encodings]				,					axis=0		)
													SCREAMING_SNAKE_CASE__								=   np.concatenate([encoding["""attention_mask"""] for encoding in encodings]				,					axis=0		)

											elif return_tensors == "jax" and is_flax_available():
													import jax.numpy as jnp

													SCREAMING_SNAKE_CASE__								=   jnp.concatenate([encoding["""input_ids"""] for encoding in encodings]				,					axis=0		)
													SCREAMING_SNAKE_CASE__								=   jnp.concatenate([encoding["""attention_mask"""] for encoding in encodings]				,					axis=0		)

											elif return_tensors == "pt" and is_torch_available():
													import torch

													SCREAMING_SNAKE_CASE__								=   torch.cat([encoding["""input_ids"""] for encoding in encodings]				,					dim=0		)
													SCREAMING_SNAKE_CASE__								=   torch.cat([encoding["""attention_mask"""] for encoding in encodings]				,					dim=0		)

											elif return_tensors == "tf" and is_tf_available():
													import tensorflow as tf

													SCREAMING_SNAKE_CASE__								=   tf.stack([encoding["""input_ids"""] for encoding in encodings]				,					axis=0		)
													SCREAMING_SNAKE_CASE__								=   tf.stack([encoding["""attention_mask"""] for encoding in encodings]				,					axis=0		)

											else:
													raise ValueError("""Target return tensor type could not be returned"""		)

											SCREAMING_SNAKE_CASE__								=   BatchEncoding()
											SCREAMING_SNAKE_CASE__								=   input_ids
											SCREAMING_SNAKE_CASE__								=   attention_mask

									if query_images is not None:
											SCREAMING_SNAKE_CASE__								=   BatchEncoding()
											SCREAMING_SNAKE_CASE__								=   self.image_processor(
											    __A				,					return_tensors=__A				,					**__A		).pixel_values
											SCREAMING_SNAKE_CASE__								=   query_pixel_values

									if images is not None:
											SCREAMING_SNAKE_CASE__								=   self.image_processor(__A				,					return_tensors=__A				,					**__A		)

									if text is not None and images is not None:
											SCREAMING_SNAKE_CASE__								=   image_features.pixel_values
											return encoding
									elif query_images is not None and images is not None:
											SCREAMING_SNAKE_CASE__								=   image_features.pixel_values
											return encoding
									elif text is not None or query_images is not None:
											return encoding
									else:
											return BatchEncoding(data=dict(**__A		)				,					tensor_type=__A		)


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



									"""simple docstring"""



									return self.image_processor.post_process(*__A				,					**__A		)


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



									"""simple docstring"""



									return self.image_processor.post_process_object_detection(*__A				,					**__A		)


							def   _snake_case	(				self     :str				,					*__A     :List[str]				,					**__A     :Union[str, Any]		) -> Any:



									"""simple docstring"""



									return self.image_processor.post_process_image_guided_detection(*__A				,					**__A		)


							def   _snake_case	(				self     :Dict				,					*__A     :List[str]				,					**__A     :List[str]		) -> int:



									"""simple docstring"""



									return self.tokenizer.batch_decode(*__A				,					**__A		)


							def   _snake_case	(				self     :Dict				,					*__A     :Dict				,					**__A     :List[str]		) -> str:



									"""simple docstring"""



									return self.tokenizer.decode(*__A				,					**__A		)


							@property
							def   _snake_case	(				self     :List[Any]		) -> Optional[int]:



									"""simple docstring"""



									warnings.warn(
									    """`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead."""				,					__A				,					)
									return self.image_processor_class


							@property
							def   _snake_case	(				self     :Any		) -> Optional[Any]:



									"""simple docstring"""



									warnings.warn(
									    """`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead."""				,					__A				,					)
									return self.image_processor