




"""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 						_snake_case     (					_snake_case							:					Any				,  _snake_case							:					str				,  _snake_case							:					int				,  _snake_case							:					Dict				,  _snake_case							:					List[str]				,  _snake_case							:					Dict = None				,  )    ->					List[Any]:






      '''simple docstring'''


      _A					   =   {}

      if train_file is not None:
            _A					   =   [train_file]
      if eval_file is not None:
            _A					   =   [eval_file]
      if test_file is not None:
            _A					   =   [test_file]

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

      if len(__lowerCAmelCase							) == 1:
            for k in files.keys():
                  _A					   =   ds[k].map(
                      lambda _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():
                  _A					   =   ds[k].map(
                      lambda _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]:
                  _A					   =   {k: v for k, v in ex.items() if k in input_names}
                  _A					   =   labelaid[ex[label_name]]
                  yield (d, label)

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

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

      _A					   =   (
          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:
            _A					   =   train_ds.apply(tf.data.experimental.assert_cardinality(len(ds[datasets.Split.TRAIN]							)							)							)

      _A					   =   (
          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:
            _A					   =   val_ds.apply(tf.data.experimental.assert_cardinality(len(ds[datasets.Split.VALIDATION]							)							)							)

      _A					   =   (
          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:
            _A					   =   test_ds.apply(tf.data.experimental.assert_cardinality(len(ds[datasets.Split.TEST]							)							)							)

      return train_ds, val_ds, test_ds, labelaid


a					=			logging.getLogger(__name__)





@dataclass
class 				lowercase_       :



 '''simple docstring'''
 UpperCAmelCase    :     int        =	field(metadata={'''help''': '''Which column contains the label'''}							)
 UpperCAmelCase    :     str        =	field(default=A__			,	metadata={'''help''': '''The path of the training file'''}							)
 UpperCAmelCase    :     Optional[str]        =	field(default=A__			,	metadata={'''help''': '''The path of the development file'''}							)
 UpperCAmelCase    :     Optional[str]        =	field(default=A__			,	metadata={'''help''': '''The path of the test file'''}							)
 UpperCAmelCase    :     int        =	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.'''
         )
     }			,	)
 UpperCAmelCase    :     bool        =	field(
     default=A__			,	metadata={'''help''': '''Overwrite the cached training and evaluation sets'''}							)





@dataclass
class 				lowercase_       :



 '''simple docstring'''
 UpperCAmelCase    :     str        =	field(
     metadata={'''help''': '''Path to pretrained model or model identifier from huggingface.co/models'''}							)
 UpperCAmelCase    :     Optional[str]        =	field(
     default=A__			,	metadata={'''help''': '''Pretrained config name or path if not the same as model_name'''}							)
 UpperCAmelCase    :     Optional[str]        =	field(
     default=A__			,	metadata={'''help''': '''Pretrained tokenizer name or path if not the same as model_name'''}							)
 UpperCAmelCase    :     bool        =	field(default=A__			,	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.
 UpperCAmelCase    :     Optional[str]        =	field(
     default=A__			,	metadata={'''help''': '''Where do you want to store the pretrained models downloaded from huggingface.co'''}			,	)






def 						_snake_case     (					)    ->					Tuple:






      '''simple docstring'''


      _A					   =   HfArgumentParser((ModelArguments, DataTrainingArguments, TFTrainingArguments)							)
      _A					   =   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.

      _A					   =   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				,  )

      _A					   =   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				,  )

      _A					   =   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():
            _A					   =   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(_snake_case							:					str							) -> Dict:
            _A					   =   np.argmax(p.predictions				,  axis=1							)

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

      # Initialize our Trainer
      _A					   =   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
      _A					   =   {}
      if training_args.do_eval:
            logger.info('*** Evaluate ***'							)
            _A					   =   trainer.evaluate()
            _A					   =   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


lowerCamelCase	:							Optional[Any]				=logging.get_logger(__name__)

lowerCamelCase	:							Optional[int]				={
    '''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  (    A__       ):
 _lowerCAmelCase       :       Optional[int]        = '''owlvit_text_model'''







 def __init__(		self					:   Any							,   SCREAMING_SNAKE_CASE					:   Union[str, Any]=4_94_08							,   SCREAMING_SNAKE_CASE					:   List[str]=5_12							,   SCREAMING_SNAKE_CASE					:   List[Any]=20_48							,   SCREAMING_SNAKE_CASE					:   Any=12							,   SCREAMING_SNAKE_CASE					:   Any=8							,   SCREAMING_SNAKE_CASE					:   Dict=16							,   SCREAMING_SNAKE_CASE					:   Union[str, Any]="quick_gelu"							,   SCREAMING_SNAKE_CASE					:   List[str]=1e-5							,   SCREAMING_SNAKE_CASE					:   Union[str, Any]=0.0							,   SCREAMING_SNAKE_CASE					:   Any=0.0_2							,   SCREAMING_SNAKE_CASE					:   int=1.0							,   SCREAMING_SNAKE_CASE					:   Any=0							,   SCREAMING_SNAKE_CASE					:   int=4_94_06							,   SCREAMING_SNAKE_CASE					:   List[str]=4_94_07							,   **SCREAMING_SNAKE_CASE					:   Optional[Any]							,   ):


      '''simple docstring'''






      super().__init__(pad_token_id=SCREAMING_SNAKE_CASE							,   bos_token_id=SCREAMING_SNAKE_CASE							,   eos_token_id=SCREAMING_SNAKE_CASE							,   **SCREAMING_SNAKE_CASE						)

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







 @classmethod
 def 					__lowercase					(		cls					:   Optional[int]							,   SCREAMING_SNAKE_CASE					:   Union[str, os.PathLike]							,   **SCREAMING_SNAKE_CASE					:   Optional[Any]						):


      '''simple docstring'''






      cls._set_token_in_kwargs(SCREAMING_SNAKE_CASE						)

      UpperCamelCase__				,     UpperCamelCase__			:   Any       				=  cls.get_config_dict(SCREAMING_SNAKE_CASE							,   **SCREAMING_SNAKE_CASE						)

      # get the text config dict if we are loading from OwlViTConfig
      if config_dict.get("model_type"						) == "owlvit":
           UpperCamelCase__			:   Dict       				=  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(SCREAMING_SNAKE_CASE							,   **SCREAMING_SNAKE_CASE						)







class 			__a  (    A__       ):
 _lowerCAmelCase       :       str        = '''owlvit_vision_model'''







 def __init__(		self					:   Optional[int]							,   SCREAMING_SNAKE_CASE					:   str=7_68							,   SCREAMING_SNAKE_CASE					:   Dict=30_72							,   SCREAMING_SNAKE_CASE					:   int=12							,   SCREAMING_SNAKE_CASE					:   Union[str, Any]=12							,   SCREAMING_SNAKE_CASE					:   Dict=3							,   SCREAMING_SNAKE_CASE					:   Union[str, Any]=7_68							,   SCREAMING_SNAKE_CASE					:   Optional[int]=32							,   SCREAMING_SNAKE_CASE					:   Dict="quick_gelu"							,   SCREAMING_SNAKE_CASE					:   Optional[Any]=1e-5							,   SCREAMING_SNAKE_CASE					:   List[str]=0.0							,   SCREAMING_SNAKE_CASE					:   Dict=0.0_2							,   SCREAMING_SNAKE_CASE					:   Optional[int]=1.0							,   **SCREAMING_SNAKE_CASE					:   Tuple							,   ):


      '''simple docstring'''






      super().__init__(**SCREAMING_SNAKE_CASE						)

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







 @classmethod
 def 					__lowercase					(		cls					:   Dict							,   SCREAMING_SNAKE_CASE					:   Union[str, os.PathLike]							,   **SCREAMING_SNAKE_CASE					:   List[Any]						):


      '''simple docstring'''






      cls._set_token_in_kwargs(SCREAMING_SNAKE_CASE						)

      UpperCamelCase__				,     UpperCamelCase__			:   List[Any]       				=  cls.get_config_dict(SCREAMING_SNAKE_CASE							,   **SCREAMING_SNAKE_CASE						)

      # get the vision config dict if we are loading from OwlViTConfig
      if config_dict.get("model_type"						) == "owlvit":
           UpperCamelCase__			:   List[str]       				=  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(SCREAMING_SNAKE_CASE							,   **SCREAMING_SNAKE_CASE						)







class 			__a  (    A__       ):
 _lowerCAmelCase       :       str        = '''owlvit'''
 _lowerCAmelCase       :       Tuple        = True







 def __init__(		self					:   Any							,   SCREAMING_SNAKE_CASE					:   Optional[Any]=None							,   SCREAMING_SNAKE_CASE					:   int=None							,   SCREAMING_SNAKE_CASE					:   str=5_12							,   SCREAMING_SNAKE_CASE					:   Any=2.6_5_9_2							,   SCREAMING_SNAKE_CASE					:   Union[str, Any]=True							,   **SCREAMING_SNAKE_CASE					:   List[Any]							,   ):


      '''simple docstring'''






      super().__init__(**SCREAMING_SNAKE_CASE						)

      if text_config is None:
           UpperCamelCase__			:   str       				=  {}
           logger.info("text_config is None. Initializing the OwlViTTextConfig with default values."						)

      if vision_config is None:
           UpperCamelCase__			:   List[str]       				=  {}
           logger.info("vision_config is None. initializing the OwlViTVisionConfig with default values."						)

      UpperCamelCase__			:   Dict       				=  OwlViTTextConfig(**SCREAMING_SNAKE_CASE						)
      UpperCamelCase__			:   Dict       				=  OwlViTVisionConfig(**SCREAMING_SNAKE_CASE						)

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







 @classmethod
 def 					__lowercase					(		cls					:   Any							,   SCREAMING_SNAKE_CASE					:   Union[str, os.PathLike]							,   **SCREAMING_SNAKE_CASE					:   Dict						):


      '''simple docstring'''






      cls._set_token_in_kwargs(SCREAMING_SNAKE_CASE						)

      UpperCamelCase__				,     UpperCamelCase__			:   Dict       				=  cls.get_config_dict(SCREAMING_SNAKE_CASE							,   **SCREAMING_SNAKE_CASE						)

      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(SCREAMING_SNAKE_CASE							,   **SCREAMING_SNAKE_CASE						)







 @classmethod
 def 					__lowercase					(		cls					:   int							,   SCREAMING_SNAKE_CASE					:   Dict							,   SCREAMING_SNAKE_CASE					:   Dict							,   **SCREAMING_SNAKE_CASE					:   Optional[int]						):


      '''simple docstring'''






      UpperCamelCase__			:   List[Any]       				=  {}
      UpperCamelCase__			:   Union[str, Any]       				=  text_config
      UpperCamelCase__			:   Optional[int]       				=  vision_config

      return cls.from_dict(SCREAMING_SNAKE_CASE							,   **SCREAMING_SNAKE_CASE						)







 def 					__lowercase					(		self					:   Tuple						):


      '''simple docstring'''






      UpperCamelCase__			:   Optional[int]       				=  copy.deepcopy(self.__dict__						)
      UpperCamelCase__			:   Union[str, Any]       				=  self.text_config.to_dict()
      UpperCamelCase__			:   List[str]       				=  self.vision_config.to_dict()
      UpperCamelCase__			:   Optional[int]       				=  self.__class__.model_type
      return output







class 			__a  (    A__       ):







 @property
 def 					__lowercase					(		self					:   Any						):


      '''simple docstring'''






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


      '''simple docstring'''






      return OrderedDict(
          [
              ("logits_per_image", {0: "batch"}),
              ("logits_per_text", {0: "batch"}),
              ("text_embeds", {0: "batch"}),
              ("image_embeds", {0: "batch"}),
          ]						)







 @property
 def 					__lowercase					(		self					:   Dict						):


      '''simple docstring'''






      return 1e-4







 def 					__lowercase					(		self					:   Optional[int]							,   SCREAMING_SNAKE_CASE					:   "ProcessorMixin"							,   SCREAMING_SNAKE_CASE					:   int = -1							,   SCREAMING_SNAKE_CASE					:   int = -1							,   SCREAMING_SNAKE_CASE					:   Optional["TensorType"] = None							,   ):


      '''simple docstring'''






      UpperCamelCase__			:   Optional[int]       				=  super().generate_dummy_inputs(
          processor.tokenizer							,   batch_size=SCREAMING_SNAKE_CASE							,   seq_length=SCREAMING_SNAKE_CASE							,   framework=SCREAMING_SNAKE_CASE						)
      UpperCamelCase__			:   List[str]       				=  super().generate_dummy_inputs(
          processor.image_processor							,   batch_size=SCREAMING_SNAKE_CASE							,   framework=SCREAMING_SNAKE_CASE						)
      return {**text_input_dict, **image_input_dict}







 @property
 def 					__lowercase					(		self					:   Tuple						):


      '''simple docstring'''






      return 14