
import json
import os
import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers import CLIPTokenizer, CLIPTokenizerFast
from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES
from transformers.testing_utils import require_vision
from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available


if is_vision_available():
     from PIL import Image

     from transformers import OwlViTImageProcessor, OwlViTProcessor



@require_vision
class    _snake_case (						unittest.TestCase				):


 def   lowercase__  (       self):


   '''simple docstring'''




   lowercase__	: Dict     		=   tempfile.mkdtemp()

   # fmt: off
   lowercase__	: Dict     		=   ["""""", """l""", """o""", """w""", """e""", """r""", """s""", """t""", """i""", """d""", """n""", """lo""", """l</w>""", """w</w>""", """r</w>""", """t</w>""", """low</w>""", """er</w>""", """lowest</w>""", """newer</w>""", """wider""", """<unk>""", """<|startoftext|>""", """<|endoftext|>"""]
   # fmt: on
   lowercase__	: Optional[int]     		=   dict(zip(__lowerCamelCase       ,	range(len(__lowerCamelCase))))
   lowercase__	: Dict     		=   ["""#version: 0.2""", """l o""", """lo w</w>""", """e r</w>""", """"""]
   lowercase__	: Any     		=   {"""unk_token""": """<unk>"""}

   lowercase__	: Dict     		=   os.path.join(self.tmpdirname       ,	VOCAB_FILES_NAMES["""vocab_file"""])
   lowercase__	: List[Any]     		=   os.path.join(self.tmpdirname       ,	VOCAB_FILES_NAMES["""merges_file"""])
   with open(self.vocab_file       ,	"""w"""       ,	encoding="""utf-8""") as fp:
     fp.write(json.dumps(__lowerCamelCase) + """\n""")
   with open(self.merges_file       ,	"""w"""       ,	encoding="""utf-8""") as fp:
     fp.write("""\n""".join(__lowerCamelCase))

   lowercase__	: Optional[int]     		=   {
       """do_resize""": True,
       """size""": 20,
       """do_center_crop""": True,
       """crop_size""": 18,
       """do_normalize""": True,
       """image_mean""": [0.4_8_1_4_5_4_6_6, 0.4_5_7_8_2_7_5, 0.4_0_8_2_1_0_7_3],
       """image_std""": [0.2_6_8_6_2_9_5_4, 0.2_6_1_3_0_2_5_8, 0.2_7_5_7_7_7_1_1],
   }
   lowercase__	: Optional[Any]     		=   os.path.join(self.tmpdirname       ,	__lowerCamelCase)
   with open(self.image_processor_file       ,	"""w"""       ,	encoding="""utf-8""") as fp:
     json.dump(__lowerCamelCase       ,	__lowerCamelCase)


 def   lowercase__  (       self       ,	**SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   return CLIPTokenizer.from_pretrained(self.tmpdirname       ,	pad_token="""!"""       ,	**__lowerCamelCase)


 def   lowercase__  (       self       ,	**SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   return CLIPTokenizerFast.from_pretrained(self.tmpdirname       ,	pad_token="""!"""       ,	**__lowerCamelCase)


 def   lowercase__  (       self       ,	**SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   return OwlViTImageProcessor.from_pretrained(self.tmpdirname       ,	**__lowerCamelCase)


 def   lowercase__  (       self):


   '''simple docstring'''




   shutil.rmtree(self.tmpdirname)


 def   lowercase__  (       self):


   '''simple docstring'''




   lowercase__	: Union[str, Any]     		=   [np.random.randint(2_55       ,	size=(3, 30, 4_00)       ,	dtype=np.uinta)]

   lowercase__	: Optional[Any]     		=   [Image.fromarray(np.moveaxis(__lowerCamelCase       ,	0       ,	-1)) for x in image_inputs]

   return image_inputs


 def   lowercase__  (       self):


   '''simple docstring'''




   lowercase__	: Tuple     		=   self.get_tokenizer()
   lowercase__	: Optional[Any]     		=   self.get_rust_tokenizer()
   lowercase__	: Any     		=   self.get_image_processor()

   lowercase__	: Optional[int]     		=   OwlViTProcessor(tokenizer=__lowerCamelCase       ,	image_processor=__lowerCamelCase)
   processor_slow.save_pretrained(self.tmpdirname)
   lowercase__	: List[str]     		=   OwlViTProcessor.from_pretrained(self.tmpdirname       ,	use_fast=__lowerCamelCase)

   lowercase__	: List[str]     		=   OwlViTProcessor(tokenizer=__lowerCamelCase       ,	image_processor=__lowerCamelCase)
   processor_fast.save_pretrained(self.tmpdirname)
   lowercase__	: Dict     		=   OwlViTProcessor.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   lowercase__  (       self):


   '''simple docstring'''




   lowercase__	: List[Any]     		=   OwlViTProcessor(tokenizer=self.get_tokenizer()       ,	image_processor=self.get_image_processor())
   processor.save_pretrained(self.tmpdirname)

   lowercase__	: Union[str, Any]     		=   self.get_tokenizer(bos_token="""(BOS)"""       ,	eos_token="""(EOS)""")
   lowercase__	: str     		=   self.get_image_processor(do_normalize=__lowerCamelCase)

   lowercase__	: Any     		=   OwlViTProcessor.from_pretrained(
       self.tmpdirname       ,	bos_token="""(BOS)"""       ,	eos_token="""(EOS)"""       ,	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   lowercase__  (       self):


   '''simple docstring'''




   lowercase__	: int     		=   self.get_image_processor()
   lowercase__	: Tuple     		=   self.get_tokenizer()

   lowercase__	: Tuple     		=   OwlViTProcessor(tokenizer=__lowerCamelCase       ,	image_processor=__lowerCamelCase)

   lowercase__	: str     		=   self.prepare_image_inputs()

   lowercase__	: int     		=   image_processor(__lowerCamelCase       ,	return_tensors="""np""")
   lowercase__	: List[str]     		=   processor(images=__lowerCamelCase       ,	return_tensors="""np""")

   for key in input_image_proc.keys():
     self.assertAlmostEqual(input_image_proc[key].sum()       ,	input_processor[key].sum()       ,	delta=1E-2)


 def   lowercase__  (       self):


   '''simple docstring'''




   lowercase__	: Tuple     		=   self.get_image_processor()
   lowercase__	: Any     		=   self.get_tokenizer()

   lowercase__	: Any     		=   OwlViTProcessor(tokenizer=__lowerCamelCase       ,	image_processor=__lowerCamelCase)

   lowercase__	: int     		=   """lower newer"""

   lowercase__	: Tuple     		=   processor(text=__lowerCamelCase       ,	return_tensors="""np""")

   lowercase__	: Any     		=   tokenizer(__lowerCamelCase       ,	return_tensors="""np""")

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


 def   lowercase__  (       self):


   '''simple docstring'''




   lowercase__	: Tuple     		=   self.get_image_processor()
   lowercase__	: Optional[Any]     		=   self.get_tokenizer()

   lowercase__	: Dict     		=   OwlViTProcessor(tokenizer=__lowerCamelCase       ,	image_processor=__lowerCamelCase)

   lowercase__	: str     		=   """lower newer"""
   lowercase__	: Any     		=   self.prepare_image_inputs()

   lowercase__	: Dict     		=   processor(text=__lowerCamelCase       ,	images=__lowerCamelCase)

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

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


 def   lowercase__  (       self):


   '''simple docstring'''




   lowercase__	: Optional[Any]     		=   """google/owlvit-base-patch32"""
   lowercase__	: Tuple     		=   OwlViTProcessor.from_pretrained(__lowerCamelCase)

   lowercase__	: List[Any]     		=   ["""cat""", """nasa badge"""]
   lowercase__	: Tuple     		=   processor(text=__lowerCamelCase)

   lowercase__	: List[Any]     		=   16
   self.assertListEqual(list(inputs.keys())       ,	["""input_ids""", """attention_mask"""])
   self.assertEqual(inputs["""input_ids"""].shape       ,	(2, seq_length))

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


 def   lowercase__  (       self):


   '''simple docstring'''




   lowercase__	: Union[str, Any]     		=   """google/owlvit-base-patch32"""
   lowercase__	: int     		=   OwlViTProcessor.from_pretrained(__lowerCamelCase)

   lowercase__	: int     		=   [["""cat""", """nasa badge"""], ["""person"""]]
   lowercase__	: int     		=   processor(text=__lowerCamelCase)

   lowercase__	: Optional[int]     		=   16
   lowercase__	: Optional[Any]     		=   len(__lowerCamelCase)
   lowercase__	: Dict     		=   max([len(__lowerCamelCase) for texts in input_texts])

   self.assertListEqual(list(inputs.keys())       ,	["""input_ids""", """attention_mask"""])
   self.assertEqual(inputs["""input_ids"""].shape       ,	(batch_size * num_max_text_queries, seq_length))

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


 def   lowercase__  (       self):


   '''simple docstring'''




   lowercase__	: int     		=   """google/owlvit-base-patch32"""
   lowercase__	: Optional[Any]     		=   OwlViTProcessor.from_pretrained(__lowerCamelCase)

   lowercase__	: Tuple     		=   ["""cat""", """nasa badge"""]
   lowercase__	: Any     		=   processor(text=__lowerCamelCase)

   lowercase__	: Tuple     		=   16
   lowercase__	: int     		=   inputs["""input_ids"""]
   lowercase__	: int     		=   [
       [4_94_06, 23_68, 4_94_07, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [4_94_06, 68_41, 1_13_01, 4_94_07, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
   ]

   self.assertListEqual(list(inputs.keys())       ,	["""input_ids""", """attention_mask"""])
   self.assertEqual(inputs["""input_ids"""].shape       ,	(2, seq_length))
   self.assertListEqual(list(input_ids[0])       ,	predicted_ids[0])
   self.assertListEqual(list(input_ids[1])       ,	predicted_ids[1])


 def   lowercase__  (       self):


   '''simple docstring'''




   lowercase__	: Dict     		=   self.get_image_processor()
   lowercase__	: Optional[int]     		=   self.get_tokenizer()

   lowercase__	: Any     		=   OwlViTProcessor(tokenizer=__lowerCamelCase       ,	image_processor=__lowerCamelCase)

   lowercase__	: Optional[Any]     		=   self.prepare_image_inputs()
   lowercase__	: Any     		=   self.prepare_image_inputs()

   lowercase__	: Optional[int]     		=   processor(images=__lowerCamelCase       ,	query_images=__lowerCamelCase)

   self.assertListEqual(list(inputs.keys())       ,	["""query_pixel_values""", """pixel_values"""])

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



 def   lowercase__  (       self):


   '''simple docstring'''




   lowercase__	: Optional[Any]     		=   self.get_image_processor()
   lowercase__	: Dict     		=   self.get_tokenizer()

   lowercase__	: Optional[Any]     		=   OwlViTProcessor(tokenizer=__lowerCamelCase       ,	image_processor=__lowerCamelCase)

   lowercase__	: Union[str, Any]     		=   [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

   lowercase__	: str     		=   processor.batch_decode(__lowerCamelCase)
   lowercase__	: List[Any]     		=   tokenizer.batch_decode(__lowerCamelCase)

   self.assertListEqual(__lowerCamelCase       ,	__lowerCamelCase)











"""simple docstring"""






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

import yaml



class     UpperCAmelCase    (	yaml.SafeLoader  ):



   def    __UpperCAmelCase    (       self    :	Tuple							,       __lowerCamelCase    :	List[str]				):



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





   def    __UpperCAmelCase    (       self    :	str							,       __lowerCamelCase    :	Any							,       __lowerCamelCase    :	Optional[Any]=False				):



     """simple docstring"""
     _snake_case   	=      super().construct_mapping(__lowerCamelCase							,       deep=__lowerCamelCase				)
     self._check_no_duplicates_on_constructed_node(__lowerCamelCase				)
     return mapping




def 			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     UpperCAmelCase    (	__SCREAMING_SNAKE_CASE  ):
   # class attributes
   A__		:	int           =  {'''train_eval_index'''}  # train-eval-index in the YAML metadata



   @classmethod
   def    __UpperCAmelCase    (       cls    :	str							,       __lowerCamelCase    :	Path				):



     """simple docstring"""
     with open(__lowerCamelCase							,       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(__lowerCamelCase				)
     else:
       return cls()



   def    __UpperCAmelCase    (       self    :	List[Any]							,       __lowerCamelCase    :	Path				):



     """simple docstring"""
     if path.exists():
       with open(__lowerCamelCase							,       encoding='''utf-8'''				) as readme_file:
         _snake_case   	=      readme_file.read()
     else:
       _snake_case   	=      None
     _snake_case   	=      self._to_readme(__lowerCamelCase				)
     with open(__lowerCamelCase							,       '''w'''							,       encoding='''utf-8'''				) as readme_file:
       readme_file.write(__lowerCamelCase				)



   def    __UpperCAmelCase    (       self    :	Union[str, Any]							,       __lowerCamelCase    :	Optional[str] = None				):



     """simple docstring"""
     if readme_content is not None:
       _snake_case	,     _snake_case   	=      _split_yaml_from_readme(__lowerCamelCase				)
       _snake_case   	=      '''---\n''' + self.to_yaml_string() + '''---\n''' + content
     else:
       _snake_case   	=      '''---\n''' + self.to_yaml_string() + '''---\n'''
     return full_content



   @classmethod
   def    __UpperCAmelCase    (       cls    :	Tuple							,       __lowerCamelCase    :	str				):



     """simple docstring"""
     _snake_case   	=      yaml.load(__lowerCamelCase							,       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(**__lowerCamelCase				)





   def    __UpperCAmelCase    (       self    :	Dict				):



     """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=__lowerCamelCase							,       allow_unicode=__lowerCamelCase							,       encoding='''utf-8'''							,       ).decode('''utf-8'''				)

snake_case   					=				{
    '''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

     snake_case   					=				ArgumentParser(usage='''Validate the yaml metadata block of a README.md file.''')
     ap.add_argument('''readme_filepath''')
     snake_case   					=				ap.parse_args()

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





