

import unittest

from transformers import LiltConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
      import torch

      from transformers import (
          LiltForQuestionAnswering,
          LiltForSequenceClassification,
          LiltForTokenClassification,
          LiltModel,
      )
      from transformers.models.lilt.modeling_lilt import LILT_PRETRAINED_MODEL_ARCHIVE_LIST





class 				A_				:







       """simple docstring"""





       def __init__(    self	:	int						,__A	:	Union[str, Any]						,__A	:	Union[str, Any]=13						,__A	:	Optional[Any]=7						,__A	:	Optional[int]=True						,__A	:	str=True						,__A	:	Dict=True						,__A	:	Tuple=True						,__A	:	List[Any]=99						,__A	:	Any=24						,__A	:	List[str]=2						,__A	:	int=6						,__A	:	Tuple=37						,__A	:	Tuple="gelu"						,__A	:	str=0.1						,__A	:	List[Any]=0.1						,__A	:	List[Any]=512						,__A	:	int=16						,__A	:	Dict=2						,__A	:	List[str]=0.02						,__A	:	Optional[int]=3						,__A	:	Tuple=None						,__A	:	Dict=1000						,)						->					List[str]:
         _lowercase							= parent
         _lowercase							= batch_size
         _lowercase							= seq_length
         _lowercase							= is_training
         _lowercase							= use_input_mask
         _lowercase							= use_token_type_ids
         _lowercase							= use_labels
         _lowercase							= vocab_size
         _lowercase							= hidden_size
         _lowercase							= num_hidden_layers
         _lowercase							= num_attention_heads
         _lowercase							= intermediate_size
         _lowercase							= hidden_act
         _lowercase							= hidden_dropout_prob
         _lowercase							= attention_probs_dropout_prob
         _lowercase							= max_position_embeddings
         _lowercase							= type_vocab_size
         _lowercase							= type_sequence_label_size
         _lowercase							= initializer_range
         _lowercase							= num_labels
         _lowercase							= scope
         _lowercase							= range_bbox







       def    __UpperCAmelCase (    self	:	Optional[Any]  )						->					Optional[int]:
         _lowercase							= ids_tensor([self.batch_size, self.seq_length]						,self.vocab_size  )

         _lowercase							= ids_tensor([self.batch_size, self.seq_length, 4]						,self.range_bbox  )
         # Ensure that bbox is legal
         for i in range(bbox.shape[0]  ):
           for j in range(bbox.shape[1]  ):
             if bbox[i, j, 3] < bbox[i, j, 1]:
               _lowercase							= bbox[i, j, 3]
               _lowercase							= bbox[i, j, 1]
               _lowercase							= t
             if bbox[i, j, 2] < bbox[i, j, 0]:
               _lowercase							= bbox[i, j, 2]
               _lowercase							= bbox[i, j, 0]
               _lowercase							= t

         _lowercase							= None
         if self.use_input_mask:
           _lowercase							= ids_tensor([self.batch_size, self.seq_length]						,vocab_size=2  )

         _lowercase							= None
         if self.use_token_type_ids:
           _lowercase							= ids_tensor([self.batch_size, self.seq_length]						,self.type_vocab_size  )

         _lowercase							= None
         _lowercase							= None
         if self.use_labels:
           _lowercase							= ids_tensor([self.batch_size]						,self.type_sequence_label_size  )
           _lowercase							= ids_tensor([self.batch_size, self.seq_length]						,self.num_labels  )

         _lowercase							= self.get_config()

         return config, input_ids, bbox, token_type_ids, input_mask, sequence_labels, token_labels







       def    __UpperCAmelCase (    self	:	List[str]  )						->					List[str]:
         return LiltConfig(
             vocab_size=self.vocab_size						,hidden_size=self.hidden_size						,num_hidden_layers=self.num_hidden_layers						,num_attention_heads=self.num_attention_heads						,intermediate_size=self.intermediate_size						,hidden_act=self.hidden_act						,hidden_dropout_prob=self.hidden_dropout_prob						,attention_probs_dropout_prob=self.attention_probs_dropout_prob						,max_position_embeddings=self.max_position_embeddings						,type_vocab_size=self.type_vocab_size						,initializer_range=self.initializer_range						,)







       def    __UpperCAmelCase (    self	:	str						,__A	:	Any						,__A	:	List[str]						,__A	:	Tuple						,__A	:	Any						,__A	:	Tuple						,__A	:	int						,__A	:	int						,)						->					Union[str, Any]:
         _lowercase							= LiltModel(config=__A  )
         model.to(__A  )
         model.eval()
         _lowercase							= model(__A						,bbox=__A						,attention_mask=__A						,token_type_ids=__A  )
         _lowercase							= model(__A						,bbox=__A						,token_type_ids=__A  )
         _lowercase							= model(__A						,bbox=__A  )

         self.parent.assertEqual(result.last_hidden_state.shape						,(self.batch_size, self.seq_length, self.hidden_size)  )
         self.parent.assertEqual(result.pooler_output.shape						,(self.batch_size, self.hidden_size)  )







       def    __UpperCAmelCase (    self	:	List[str]						,__A	:	str						,__A	:	int						,__A	:	Dict						,__A	:	Tuple						,__A	:	Dict						,__A	:	List[Any]						,__A	:	Dict						,)						->					Dict:
         _lowercase							= self.num_labels
         _lowercase							= LiltForTokenClassification(config=__A  )
         model.to(__A  )
         model.eval()
         _lowercase							= model(
             __A						,bbox=__A						,attention_mask=__A						,token_type_ids=__A						,labels=__A  )
         self.parent.assertEqual(result.logits.shape						,(self.batch_size, self.seq_length, self.num_labels)  )







       def    __UpperCAmelCase (    self	:	str						,__A	:	List[Any]						,__A	:	int						,__A	:	Dict						,__A	:	Dict						,__A	:	Optional[Any]						,__A	:	str						,__A	:	Union[str, Any]						,)						->					str:
         _lowercase							= LiltForQuestionAnswering(config=__A  )
         model.to(__A  )
         model.eval()
         _lowercase							= model(
             __A						,bbox=__A						,attention_mask=__A						,token_type_ids=__A						,start_positions=__A						,end_positions=__A						,)
         self.parent.assertEqual(result.start_logits.shape						,(self.batch_size, self.seq_length)  )
         self.parent.assertEqual(result.end_logits.shape						,(self.batch_size, self.seq_length)  )







       def    __UpperCAmelCase (    self	:	Optional[int]  )						->					int:
         _lowercase							= self.prepare_config_and_inputs()
         (
             (
             _lowercase		
         )						,		(
             _lowercase		
         )						,		(
             _lowercase		
         )						,		(
             _lowercase		
         )						,		(
             _lowercase		
         )						,		(
             _lowercase		
         )						,		(
             _lowercase		
         )						,				
         )							= config_and_inputs
         _lowercase							= {
             'input_ids': input_ids,
             'bbox': bbox,
             'token_type_ids': token_type_ids,
             'attention_mask': input_mask,
         }
         return config, inputs_dict





@require_torch
class 				A_				(      UpperCAmelCase					,	UpperCAmelCase					,	UpperCAmelCase					,	unittest.TestCase	):







       """simple docstring"""





       SCREAMING_SNAKE_CASE_      : str      =  (
           (
               LiltModel,
               LiltForSequenceClassification,
               LiltForTokenClassification,
               LiltForQuestionAnswering,
           )
           if is_torch_available()
           else ()
       )
       SCREAMING_SNAKE_CASE_      : Tuple      =  (
           {
               '''feature-extraction''': LiltModel,
               '''question-answering''': LiltForQuestionAnswering,
               '''text-classification''': LiltForSequenceClassification,
               '''token-classification''': LiltForTokenClassification,
               '''zero-shot''': LiltForSequenceClassification,
           }
           if is_torch_available()
           else {}
       )
       SCREAMING_SNAKE_CASE_      : Optional[int]      =  False
       SCREAMING_SNAKE_CASE_      : Tuple      =  False







       def    __UpperCAmelCase (    self	:	Dict						,__A	:	Dict						,__A	:	Dict						,__A	:	Union[str, Any]						,__A	:	Any						,__A	:	int  )						->					int:
         return True







       def    __UpperCAmelCase (    self	:	int  )						->					int:
         _lowercase							= LiltModelTester(self  )
         _lowercase							= ConfigTester(self						,config_class=__A						,hidden_size=37  )







       def    __UpperCAmelCase (    self	:	Tuple  )						->					Optional[int]:
         self.config_tester.run_common_tests()







       def    __UpperCAmelCase (    self	:	Optional[int]  )						->					Tuple:
         _lowercase							= self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*__A  )







       def    __UpperCAmelCase (    self	:	Tuple  )						->					Any:
         _lowercase							= self.model_tester.prepare_config_and_inputs()
         for type in ["absolute", "relative_key", "relative_key_query"]:
           _lowercase							= type
           self.model_tester.create_and_check_model(*__A  )







       def    __UpperCAmelCase (    self	:	int  )						->					Optional[int]:
         _lowercase							= self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_token_classification(*__A  )







       def    __UpperCAmelCase (    self	:	Tuple  )						->					List[Any]:
         _lowercase							= self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_question_answering(*__A  )







       @slow
       def    __UpperCAmelCase (    self	:	Tuple  )						->					Optional[Any]:
         for model_name in LILT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           _lowercase							= LiltModel.from_pretrained(__A  )
           self.assertIsNotNone(__A  )





@require_torch
@slow
class 				A_				(      unittest.TestCase	):







       """simple docstring"""





       def    __UpperCAmelCase (    self	:	Tuple  )						->					Union[str, Any]:
         _lowercase							= LiltModel.from_pretrained('SCUT-DLVCLab/lilt-roberta-en-base'  ).to(__A  )

         _lowercase							= torch.tensor([[1, 2]]						,device=__A  )
         _lowercase							= torch.tensor([[[1, 2, 3, 4], [5, 6, 7, 8]]]						,device=__A  )

         # forward pass
         with torch.no_grad():
           _lowercase							= model(input_ids=__A						,bbox=__A  )

         _lowercase							= torch.Size([1, 2, 768]  )
         _lowercase							= torch.tensor(
             [[-0.0653, 0.0950, -0.0061], [-0.0545, 0.0926, -0.0324]]						,device=__A						,)

         self.assertTrue(outputs.last_hidden_state.shape						,__A  )
         self.assertTrue(torch.allclose(outputs.last_hidden_state[0, :, :3]						,__A						,atol=1e-3  )  )






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

import yaml





class 				A_				(      yaml.SafeLoader	):







       """simple docstring"""





       def    __UpperCAmelCase (    self	:	Dict						,__A	:	List[Any]  )						->					Any:
         _lowercase							= [self.constructed_objects[key_node] for key_node, _ in node.value]
         _lowercase							= [tuple(__A  ) if isinstance(__A						,__A  ) else key for key in keys]
         _lowercase							= Counter(__A  )
         _lowercase							= [key for key in counter if counter[key] > 1]
         if duplicate_keys:
           raise TypeError(F"""Got duplicate yaml keys: {duplicate_keys}"""  )







       def    __UpperCAmelCase (    self	:	Any						,__A	:	int						,__A	:	Dict=False  )						->					Union[str, Any]:
         _lowercase							= super().construct_mapping(__A						,deep=__A  )
         self._check_no_duplicates_on_constructed_node(__A  )
         return mapping

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

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





class 				A_				(      UpperCAmelCase	):







       """simple docstring"""





       SCREAMING_SNAKE_CASE_      : Any      =  {'''train_eval_index'''}  # train-eval-index in the YAML metadata







       @classmethod
       def    __UpperCAmelCase (    cls	:	List[Any]						,__A	:	Path  )						->					"DatasetMetadata":
         with open(__A						,encoding='utf-8'  ) as readme_file:
           _lowercase						,		_lowercase							= _split_yaml_from_readme(readme_file.read()  )
         if yaml_string is not None:
           return cls.from_yaml_string(__A  )
         else:
           return cls()







       def    __UpperCAmelCase (    self	:	Union[str, Any]						,__A	:	Path  )						->					Optional[Any]:
         if path.exists():
           with open(__A						,encoding='utf-8'  ) as readme_file:
             _lowercase							= readme_file.read()
         else:
           _lowercase							= None
         _lowercase							= self._to_readme(__A  )
         with open(__A						,'w'						,encoding='utf-8'  ) as readme_file:
           readme_file.write(__A  )







       def    __UpperCAmelCase (    self	:	Tuple						,__A	:	Optional[str] = None  )						->					str:
         if readme_content is not None:
           _lowercase						,		_lowercase							= _split_yaml_from_readme(__A  )
           _lowercase							= '---\n' + self.to_yaml_string() + '---\n' + content
         else:
           _lowercase							= '---\n' + self.to_yaml_string() + '---\n'
         return full_content







       @classmethod
       def    __UpperCAmelCase (    cls	:	List[Any]						,__A	:	str  )						->					"DatasetMetadata":
         _lowercase							= yaml.load(__A						,Loader=_NoDuplicateSafeLoader  ) or {}

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







       def    __UpperCAmelCase (    self	:	Optional[int]  )						->					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'  )
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)