


"""simple docstring"""




import logging
import os
from dataclasses import dataclass, field
from typing import Dict, Optional

import datasets
import numpy as np
import tensorflow as tf

from transformers import (
    AutoConfig,
    AutoTokenizer,
    EvalPrediction,
    HfArgumentParser,
    PreTrainedTokenizer,
    TFAutoModelForSequenceClassification,
    TFTrainer,
    TFTrainingArguments,
)
from transformers.utils import logging as hf_logging


hf_logging.set_verbosity_info()
hf_logging.enable_default_handler()
hf_logging.enable_explicit_format()
def 				UpperCamelCase__					(  SCREAMING_SNAKE_CASE__      ,	SCREAMING_SNAKE_CASE__      ,	SCREAMING_SNAKE_CASE__      ,	SCREAMING_SNAKE_CASE__      ,	SCREAMING_SNAKE_CASE__      ,	SCREAMING_SNAKE_CASE__ = None      ,	):
 __lowerCamelCase		:							List[str]		 =						{}

 if train_file is not None:
  __lowerCamelCase		:							Tuple		 =						[train_file]
 if eval_file is not None:
  __lowerCamelCase		:							List[str]		 =						[eval_file]
 if test_file is not None:
  __lowerCamelCase		:							List[Any]		 =						[test_file]

 __lowerCamelCase		:							List[Any]		 =						datasets.load_dataset('csv'      ,	data_files=__lowerCAmelCase						)
 __lowerCamelCase		:							Optional[int]		 =						list(ds[list(files.keys()						)[0]].features.keys()						)
 __lowerCamelCase		:							List[str]		 =						features_name.pop(__lowerCAmelCase						)
 __lowerCamelCase		:							Optional[Any]		 =						list(set(ds[list(files.keys()						)[0]][label_name]						)						)
 __lowerCamelCase		:							Optional[Any]		 =						{label: i for i, label in enumerate(__lowerCAmelCase						)}
 __lowerCamelCase		:							int		 =						tokenizer.model_input_names
 __lowerCamelCase		:							List[Any]		 =						{}

 if len(__lowerCAmelCase						) == 1:
  for k in files.keys():
   __lowerCamelCase		:							Optional[int]		 =						ds[k].map(
       lambda SCREAMING_SNAKE_CASE__						: tokenizer.batch_encode_plus(
           example[features_name[0]]      ,	truncation=__lowerCAmelCase      ,	max_length=__lowerCAmelCase      ,	padding='max_length'						)      ,	batched=__lowerCAmelCase      ,	)
 elif len(__lowerCAmelCase						) == 2:
  for k in files.keys():
   __lowerCamelCase		:							Optional[Any]		 =						ds[k].map(
       lambda SCREAMING_SNAKE_CASE__						: tokenizer.batch_encode_plus(
           (example[features_name[0]], example[features_name[1]])      ,	truncation=__lowerCAmelCase      ,	max_length=__lowerCAmelCase      ,	padding='max_length'      ,	)      ,	batched=__lowerCAmelCase      ,	)

 def gen_train():
  for ex in transformed_ds[datasets.Split.TRAIN]:
   __lowerCamelCase		:							int		 =						{k: v for k, v in ex.items() if k in input_names}
   __lowerCamelCase		:							Union[str, Any]		 =						labelaid[ex[label_name]]
   yield (d, label)

 def gen_val():
  for ex in transformed_ds[datasets.Split.VALIDATION]:
   __lowerCamelCase		:							Optional[Any]		 =						{k: v for k, v in ex.items() if k in input_names}
   __lowerCamelCase		:							List[str]		 =						labelaid[ex[label_name]]
   yield (d, label)

 def gen_test():
  for ex in transformed_ds[datasets.Split.TEST]:
   __lowerCamelCase		:							List[Any]		 =						{k: v for k, v in ex.items() if k in input_names}
   __lowerCamelCase		:							Any		 =						labelaid[ex[label_name]]
   yield (d, label)

 __lowerCamelCase		:							List[Any]		 =						(
     tf.data.Dataset.from_generator(
         __lowerCAmelCase      ,	({k: tf.intaa for k in input_names}, tf.intaa)      ,	({k: tf.TensorShape([None]						) for k in input_names}, tf.TensorShape([]						))      ,	)
     if datasets.Split.TRAIN in transformed_ds
     else None
 )

 if train_ds is not None:
  __lowerCamelCase		:							str		 =						train_ds.apply(tf.data.experimental.assert_cardinality(len(ds[datasets.Split.TRAIN]						)						)						)

 __lowerCamelCase		:							Tuple		 =						(
     tf.data.Dataset.from_generator(
         __lowerCAmelCase      ,	({k: tf.intaa for k in input_names}, tf.intaa)      ,	({k: tf.TensorShape([None]						) for k in input_names}, tf.TensorShape([]						))      ,	)
     if datasets.Split.VALIDATION in transformed_ds
     else None
 )

 if val_ds is not None:
  __lowerCamelCase		:							List[Any]		 =						val_ds.apply(tf.data.experimental.assert_cardinality(len(ds[datasets.Split.VALIDATION]						)						)						)

 __lowerCamelCase		:							List[Any]		 =						(
     tf.data.Dataset.from_generator(
         __lowerCAmelCase      ,	({k: tf.intaa for k in input_names}, tf.intaa)      ,	({k: tf.TensorShape([None]						) for k in input_names}, tf.TensorShape([]						))      ,	)
     if datasets.Split.TEST in transformed_ds
     else None
 )

 if test_ds is not None:
  __lowerCamelCase		:							Optional[int]		 =						test_ds.apply(tf.data.experimental.assert_cardinality(len(ds[datasets.Split.TEST]						)						)						)

 return train_ds, val_ds, test_ds, labelaid


lowercase_	   =     logging.getLogger(__name__)


@dataclass
class 	A_ :




      '''simple docstring'''


      __snake_case 	=       field(metadata={"""help""": """Which column contains the label"""}   )
      __snake_case 	=       field(default=UpperCamelCase_ ,				metadata={"""help""": """The path of the training file"""}   )
      __snake_case 	=       field(default=UpperCamelCase_ ,				metadata={"""help""": """The path of the development file"""}   )
      __snake_case 	=       field(default=UpperCamelCase_ ,				metadata={"""help""": """The path of the test file"""}   )
      __snake_case 	=       field(
          default=128 ,				metadata={
              """help""": (
                  """The maximum total input sequence length after tokenization. Sequences longer """
                  """than this will be truncated, sequences shorter will be padded."""
              )
          } ,				)
      __snake_case 	=       field(
          default=UpperCamelCase_ ,				metadata={"""help""": """Overwrite the cached training and evaluation sets"""}   )



@dataclass
class 	A_ :




      '''simple docstring'''


      __snake_case 	=       field(
          metadata={"""help""": """Path to pretrained model or model identifier from huggingface.co/models"""}   )
      __snake_case 	=       field(
          default=UpperCamelCase_ ,				metadata={"""help""": """Pretrained config name or path if not the same as model_name"""}   )
      __snake_case 	=       field(
          default=UpperCamelCase_ ,				metadata={"""help""": """Pretrained tokenizer name or path if not the same as model_name"""}   )
      __snake_case 	=       field(default=UpperCamelCase_ ,				metadata={"""help""": """Set this flag to use fast tokenization."""}   )
      # If you want to tweak more attributes on your tokenizer, you should do it in a distinct script,
      # or just modify its tokenizer_config.json.
      __snake_case 	=       field(
          default=UpperCamelCase_ ,				metadata={"""help""": """Where do you want to store the pretrained models downloaded from huggingface.co"""} ,				)



def 				UpperCamelCase__					(  ):
 # See all possible arguments in src/transformers/training_args.py
 # or by passing the --help flag to this script.
 # We now keep distinct sets of args, for a cleaner separation of concerns.
 __lowerCamelCase		:							Optional[Any]		 =						HfArgumentParser((ModelArguments, DataTrainingArguments, TFTrainingArguments)						)
 __lowerCamelCase		:							Optional[int]		 =						parser.parse_args_into_dataclasses()

 if (
     os.path.exists(training_args.output_dir						)
     and os.listdir(training_args.output_dir						)
     and training_args.do_train
     and not training_args.overwrite_output_dir
 ):
  raise ValueError(
      f'Output directory ({training_args.output_dir}) already exists and is not empty. Use'
      ' --overwrite_output_dir to overcome.'						)

 # Setup logging
 logging.basicConfig(
     format='%(asctime)s - %(levelname)s - %(name)s - %(message)s'      ,	datefmt='%m/%d/%Y %H:%M:%S'      ,	level=logging.INFO      ,	)
 logger.info(
     f'n_replicas: {training_args.n_replicas}, distributed training: {bool(training_args.n_replicas > 1						)}, '
     f'16-bits training: {training_args.fpaa}'						)
 logger.info(f'Training/evaluation parameters {training_args}'						)

 # Load pretrained model and tokenizer
 #
 # Distributed training:
 # The .from_pretrained methods guarantee that only one local process can concurrently
 # download model & vocab.

 __lowerCamelCase		:							List[Any]		 =						AutoTokenizer.from_pretrained(
     model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path      ,	cache_dir=model_args.cache_dir      ,	)

 __lowerCamelCase		:							Union[str, Any]		 =						get_tfds(
     train_file=data_args.train_file      ,	eval_file=data_args.dev_file      ,	test_file=data_args.test_file      ,	tokenizer=__lowerCAmelCase      ,	label_column_id=data_args.label_column_id      ,	max_seq_length=data_args.max_seq_length      ,	)

 __lowerCamelCase		:							List[str]		 =						AutoConfig.from_pretrained(
     model_args.config_name if model_args.config_name else model_args.model_name_or_path      ,	num_labels=len(__lowerCAmelCase						)      ,	labelaid=__lowerCAmelCase      ,	idalabel={id: label for label, id in labelaid.items()}      ,	finetuning_task='text-classification'      ,	cache_dir=model_args.cache_dir      ,	)

 with training_args.strategy.scope():
  __lowerCamelCase		:							Any		 =						TFAutoModelForSequenceClassification.from_pretrained(
      model_args.model_name_or_path      ,	from_pt=bool('.bin' in model_args.model_name_or_path						)      ,	config=__lowerCAmelCase      ,	cache_dir=model_args.cache_dir      ,	)

 def compute_metrics(SCREAMING_SNAKE_CASE__						) -> Dict:
  __lowerCamelCase		:							Optional[int]		 =						np.argmax(p.predictions      ,	axis=1						)

  return {"acc": (preds == p.label_ids).mean()}

 # Initialize our Trainer
 __lowerCamelCase		:							str		 =						TFTrainer(
     model=__lowerCAmelCase      ,	args=__lowerCAmelCase      ,	train_dataset=__lowerCAmelCase      ,	eval_dataset=__lowerCAmelCase      ,	compute_metrics=__lowerCAmelCase      ,	)

 # Training
 if training_args.do_train:
  trainer.train()
  trainer.save_model()
  tokenizer.save_pretrained(training_args.output_dir						)

 # Evaluation
 __lowerCamelCase		:							Union[str, Any]		 =						{}
 if training_args.do_eval:
  logger.info('*** Evaluate ***'						)
  __lowerCamelCase		:							Union[str, Any]		 =						trainer.evaluate()
  __lowerCamelCase		:							Tuple		 =						os.path.join(training_args.output_dir      ,	'eval_results.txt'						)

  with open(__lowerCAmelCase      ,	'w'						) as writer:
   logger.info('***** Eval results *****'						)

   for key, value in result.items():
    logger.info(f'  {key} = {value}'						)
    writer.write(f'{key} = {value}\n'						)

   results.update(__lowerCAmelCase						)

 return results


if __name__ == "__main__":
       main()





import copy
import os
from collections import OrderedDict
from typing import TYPE_CHECKING, Any, Dict, Mapping, Optional, Union


if TYPE_CHECKING:
       from ...processing_utils import ProcessorMixin
       from ...utils import TensorType

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


lowercase_	   =     logging.get_logger(__name__)

lowercase_	   =     {
    'google/owlvit-base-patch32': 'https://huggingface.co/google/owlvit-base-patch32/resolve/main/config.json',
    'google/owlvit-base-patch16': 'https://huggingface.co/google/owlvit-base-patch16/resolve/main/config.json',
    'google/owlvit-large-patch14': 'https://huggingface.co/google/owlvit-large-patch14/resolve/main/config.json',
}


class 	A_ (	__UpperCamelCase   ):




      '''simple docstring'''


      __snake_case 	=       """owlvit_text_model"""




      def __init__(    self:	Optional[int]						,				a:	Dict=4_9408						,				a:	Optional[Any]=512						,				a:	Dict=2048						,				a:	Optional[Any]=12						,				a:	Tuple=8						,				a:	Union[str, Any]=16						,				a:	str="quick_gelu"						,				a:	List[Any]=1e-5						,				a:	Dict=0.0						,				a:	Optional[int]=0.0_2						,				a:	Dict=1.0						,				a:	Any=0						,				a:	Union[str, Any]=4_9406						,				a:	Any=4_9407						,				**a:	Dict						,				):
       super().__init__(pad_token_id=a						,				bos_token_id=a						,				eos_token_id=a						,				**a    )

       __lowerCamelCase		:							List[Any]		 =						vocab_size
       __lowerCamelCase		:							int		 =						hidden_size
       __lowerCamelCase		:							Optional[Any]		 =						intermediate_size
       __lowerCamelCase		:							List[Any]		 =						num_hidden_layers
       __lowerCamelCase		:							Any		 =						num_attention_heads
       __lowerCamelCase		:							Union[str, Any]		 =						max_position_embeddings
       __lowerCamelCase		:							List[Any]		 =						hidden_act
       __lowerCamelCase		:							List[str]		 =						layer_norm_eps
       __lowerCamelCase		:							Tuple		 =						attention_dropout
       __lowerCamelCase		:							Optional[int]		 =						initializer_range
       __lowerCamelCase		:							Tuple		 =						initializer_factor




      @classmethod
      def 					_snake_case     (    cls:	Dict						,				a:	Union[str, os.PathLike]						,				**a:	Optional[int]    ):
       cls._set_token_in_kwargs(a    )

       __lowerCamelCase ,					__lowerCamelCase		:							Dict		 =						cls.get_config_dict(a						,				**a    )

       # get the text config dict if we are loading from OwlViTConfig
       if config_dict.get('model_type'    ) == "owlvit":
        __lowerCamelCase		:							Optional[Any]		 =						config_dict['text_config']

       if "model_type" in config_dict and hasattr(cls						,				'model_type'    ) and config_dict["model_type"] != cls.model_type:
        logger.warning(
            F'You are using a model of type {config_dict["model_type"]} to instantiate a model of type '
            F'{cls.model_type}. This is not supported for all configurations of models and can yield errors.'    )

       return cls.from_dict(a						,				**a    )



class 	A_ (	__UpperCamelCase   ):




      '''simple docstring'''


      __snake_case 	=       """owlvit_vision_model"""




      def __init__(    self:	int						,				a:	Tuple=768						,				a:	int=3072						,				a:	List[str]=12						,				a:	Optional[Any]=12						,				a:	Optional[int]=3						,				a:	Optional[int]=768						,				a:	Optional[Any]=32						,				a:	Optional[int]="quick_gelu"						,				a:	Union[str, Any]=1e-5						,				a:	Union[str, Any]=0.0						,				a:	Union[str, Any]=0.0_2						,				a:	int=1.0						,				**a:	Union[str, Any]						,				):
       super().__init__(**a    )

       __lowerCamelCase		:							str		 =						hidden_size
       __lowerCamelCase		:							Tuple		 =						intermediate_size
       __lowerCamelCase		:							Dict		 =						num_hidden_layers
       __lowerCamelCase		:							Optional[Any]		 =						num_attention_heads
       __lowerCamelCase		:							int		 =						num_channels
       __lowerCamelCase		:							Optional[Any]		 =						image_size
       __lowerCamelCase		:							Tuple		 =						patch_size
       __lowerCamelCase		:							List[str]		 =						hidden_act
       __lowerCamelCase		:							Tuple		 =						layer_norm_eps
       __lowerCamelCase		:							List[Any]		 =						attention_dropout
       __lowerCamelCase		:							Tuple		 =						initializer_range
       __lowerCamelCase		:							List[Any]		 =						initializer_factor




      @classmethod
      def 					_snake_case     (    cls:	Optional[int]						,				a:	Union[str, os.PathLike]						,				**a:	int    ):
       cls._set_token_in_kwargs(a    )

       __lowerCamelCase ,					__lowerCamelCase		:							Dict		 =						cls.get_config_dict(a						,				**a    )

       # get the vision config dict if we are loading from OwlViTConfig
       if config_dict.get('model_type'    ) == "owlvit":
        __lowerCamelCase		:							Any		 =						config_dict['vision_config']

       if "model_type" in config_dict and hasattr(cls						,				'model_type'    ) and config_dict["model_type"] != cls.model_type:
        logger.warning(
            F'You are using a model of type {config_dict["model_type"]} to instantiate a model of type '
            F'{cls.model_type}. This is not supported for all configurations of models and can yield errors.'    )

       return cls.from_dict(a						,				**a    )



class 	A_ (	__UpperCamelCase   ):




      '''simple docstring'''


      __snake_case 	=       """owlvit"""
      __snake_case 	=       True




      def __init__(    self:	Dict						,				a:	int=None						,				a:	str=None						,				a:	Tuple=512						,				a:	Tuple=2.6_5_9_2						,				a:	int=True						,				**a:	int						,				):
       super().__init__(**a    )

       if text_config is None:
        __lowerCamelCase		:							List[str]		 =						{}
        logger.info('text_config is None. Initializing the OwlViTTextConfig with default values.'    )

       if vision_config is None:
        __lowerCamelCase		:							str		 =						{}
        logger.info('vision_config is None. initializing the OwlViTVisionConfig with default values.'    )

       __lowerCamelCase		:							List[Any]		 =						OwlViTTextConfig(**a    )
       __lowerCamelCase		:							str		 =						OwlViTVisionConfig(**a    )

       __lowerCamelCase		:							Union[str, Any]		 =						projection_dim
       __lowerCamelCase		:							Tuple		 =						logit_scale_init_value
       __lowerCamelCase		:							Dict		 =						return_dict
       __lowerCamelCase		:							Tuple		 =						1.0




      @classmethod
      def 					_snake_case     (    cls:	str						,				a:	Union[str, os.PathLike]						,				**a:	List[Any]    ):
       cls._set_token_in_kwargs(a    )

       __lowerCamelCase ,					__lowerCamelCase		:							List[Any]		 =						cls.get_config_dict(a						,				**a    )

       if "model_type" in config_dict and hasattr(cls						,				'model_type'    ) and config_dict["model_type"] != cls.model_type:
        logger.warning(
            F'You are using a model of type {config_dict["model_type"]} to instantiate a model of type '
            F'{cls.model_type}. This is not supported for all configurations of models and can yield errors.'    )

       return cls.from_dict(a						,				**a    )




      @classmethod
      def 					_snake_case     (    cls:	Tuple						,				a:	Dict						,				a:	Dict						,				**a:	str    ):
       __lowerCamelCase		:							List[str]		 =						{}
       __lowerCamelCase		:							List[str]		 =						text_config
       __lowerCamelCase		:							Optional[int]		 =						vision_config

       return cls.from_dict(a						,				**a    )




      def 					_snake_case     (    self:	Optional[int]    ):
       __lowerCamelCase		:							Optional[Any]		 =						copy.deepcopy(self.__dict__    )
       __lowerCamelCase		:							List[Any]		 =						self.text_config.to_dict()
       __lowerCamelCase		:							List[str]		 =						self.vision_config.to_dict()
       __lowerCamelCase		:							Optional[Any]		 =						self.__class__.model_type
       return output



class 	A_ (	__UpperCamelCase   ):




      '''simple docstring'''


      @property
      def 					_snake_case     (    self:	str    ):
       return OrderedDict(
           [
               ('input_ids', {0: 'batch', 1: 'sequence'}),
               ('pixel_values', {0: 'batch', 1: 'num_channels', 2: 'height', 3: 'width'}),
               ('attention_mask', {0: 'batch', 1: 'sequence'}),
           ]    )




      @property
      def 					_snake_case     (    self:	Dict    ):
       return OrderedDict(
           [
               ('logits_per_image', {0: 'batch'}),
               ('logits_per_text', {0: 'batch'}),
               ('text_embeds', {0: 'batch'}),
               ('image_embeds', {0: 'batch'}),
           ]    )




      @property
      def 					_snake_case     (    self:	int    ):
       return 1e-4




      def 					_snake_case     (    self:	Any						,				a:	"ProcessorMixin"						,				a:	int = -1						,				a:	int = -1						,				a:	Optional["TensorType"] = None						,				):
       __lowerCamelCase		:							List[str]		 =						super().generate_dummy_inputs(
           processor.tokenizer						,				batch_size=a						,				seq_length=a						,				framework=a    )
       __lowerCamelCase		:							int		 =						super().generate_dummy_inputs(
           processor.image_processor						,				batch_size=a						,				framework=a    )
       return {**text_input_dict, **image_input_dict}




      @property
      def 					_snake_case     (    self:	int    ):
       return 14

