



from typing import Dict, List, Optional, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import (
    get_resize_output_image_size,
    normalize,
    pad,
    rescale,
    resize,
    to_channel_dimension_format,
)
from ...image_utils import (
    IMAGENET_STANDARD_MEAN,
    IMAGENET_STANDARD_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    get_image_size,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, logging
from ...utils.import_utils import is_vision_available


__lowerCamelCase								=      logging.get_logger(__name__)


if is_vision_available():
   import PIL


class 	snake_case_			(lowercase__	):




       """simple docstring"""






       _lowerCamelCase           =					["""pixel_values"""]



       def __init__(  self  ,lowercase = True  ,lowercase = None  ,lowercase = PILImageResampling.BILINEAR  ,lowercase = True  ,lowercase = False  ,lowercase = True  ,lowercase = True  ,lowercase = 1 / 255  ,lowercase = True  ,lowercase = None  ,lowercase = None  ,**lowercase  ,):

             """simple docstring"""





             super().__init__(**lowercase)

             UpperCAmelCase_	:						List[str]					   =		size if size is not None else {"height": 2560, "width": 1920}
             if isinstance(lowercase  ,(tuple, list)):
                   # The previous feature extractor size parameter was in (width, height) format
                   UpperCAmelCase_	:						Any					   =		size[::-1]
             UpperCAmelCase_	:						str					   =		get_size_dict(lowercase)

             UpperCAmelCase_	:						List[str]					   =		do_resize
             UpperCAmelCase_	:						Tuple					   =		size
             UpperCAmelCase_	:						Dict					   =		resample
             UpperCAmelCase_	:						Optional[int]					   =		do_thumbnail
             UpperCAmelCase_	:						List[Any]					   =		do_align_long_axis
             UpperCAmelCase_	:						List[Any]					   =		do_pad
             UpperCAmelCase_	:						Any					   =		do_rescale
             UpperCAmelCase_	:						List[str]					   =		rescale_factor
             UpperCAmelCase_	:						List[str]					   =		do_normalize
             UpperCAmelCase_	:						List[Any]					   =		image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
             UpperCAmelCase_	:						Optional[Any]					   =		image_std if image_std is not None else IMAGENET_STANDARD_STD



       def 			A_ (  self  ,lowercase  ,lowercase  ,lowercase = None):

             """simple docstring"""





             UpperCAmelCase_   ,   UpperCAmelCase_	:						Optional[int]					   =		get_image_size(lowercase)
             UpperCAmelCase_   ,   UpperCAmelCase_	:						Any					   =		size["height"], size["width"]

             if (output_width < output_height and input_width > input_height) or (
                 output_width > output_height and input_width < input_height
             ):
                   UpperCAmelCase_	:						Any					   =		np.rotaa(lowercase  ,3)

             if data_format is not None:
                   UpperCAmelCase_	:						Union[str, Any]					   =		to_channel_dimension_format(lowercase  ,lowercase)

             return image



       def 			A_ (  self  ,lowercase  ,lowercase  ,lowercase = False  ,lowercase = None  ,):

             """simple docstring"""





             UpperCAmelCase_   ,   UpperCAmelCase_	:						Optional[int]					   =		size["height"], size["width"]
             UpperCAmelCase_   ,   UpperCAmelCase_	:						List[Any]					   =		get_image_size(lowercase)

             UpperCAmelCase_	:						List[str]					   =		output_width - input_width
             UpperCAmelCase_	:						Dict					   =		output_height - input_height

             if random_padding:
                   UpperCAmelCase_	:						Union[str, Any]					   =		np.random.randint(low=0  ,high=delta_height + 1)
                   UpperCAmelCase_	:						Optional[Any]					   =		np.random.randint(low=0  ,high=delta_width + 1)
             else:
                   UpperCAmelCase_	:						Any					   =		delta_height // 2
                   UpperCAmelCase_	:						List[str]					   =		delta_width // 2

             UpperCAmelCase_	:						str					   =		delta_height - pad_top
             UpperCAmelCase_	:						List[Any]					   =		delta_width - pad_left

             UpperCAmelCase_	:						int					   =		((pad_top, pad_bottom), (pad_left, pad_right))
             return pad(lowercase  ,lowercase  ,data_format=lowercase)



       def 			A_ (  self  ,*lowercase  ,**lowercase):

             """simple docstring"""





             logger.info("pad is deprecated and will be removed in version 4.27. Please use pad_image instead.")
             return self.pad_image(*lowercase  ,**lowercase)



       def 			A_ (  self  ,lowercase  ,lowercase  ,lowercase = PILImageResampling.BICUBIC  ,lowercase = None  ,**lowercase  ,):

             """simple docstring"""





             UpperCAmelCase_   ,   UpperCAmelCase_	:						int					   =		get_image_size(lowercase)
             UpperCAmelCase_   ,   UpperCAmelCase_	:						List[Any]					   =		size["height"], size["width"]

             # We always resize to the smallest of either the input or output size.
             UpperCAmelCase_	:						Optional[int]					   =		min(lowercase  ,lowercase)
             UpperCAmelCase_	:						Dict					   =		min(lowercase  ,lowercase)

             if height == input_height and width == input_width:
                   return image

             if input_height > input_width:
                   UpperCAmelCase_	:						List[Any]					   =		int(input_width * height / input_height)
             elif input_width > input_height:
                   UpperCAmelCase_	:						str					   =		int(input_height * width / input_width)

             return resize(
                 lowercase  ,size=(height, width)  ,resample=lowercase  ,reducing_gap=2.0  ,data_format=lowercase  ,**lowercase)



       def 			A_ (  self  ,lowercase  ,lowercase  ,lowercase = PILImageResampling.BICUBIC  ,lowercase = None  ,**lowercase  ,):

             """simple docstring"""





             UpperCAmelCase_	:						Dict					   =		get_size_dict(lowercase)
             UpperCAmelCase_	:						Optional[Any]					   =		min(size["height"]  ,size["width"])
             UpperCAmelCase_	:						Union[str, Any]					   =		get_resize_output_image_size(lowercase  ,size=lowercase  ,default_to_square=lowercase)
             UpperCAmelCase_	:						str					   =		resize(lowercase  ,size=lowercase  ,resample=lowercase  ,data_format=lowercase  ,**lowercase)
             return resized_image



       def 			A_ (  self  ,lowercase  ,lowercase  ,lowercase = None  ,**lowercase  ,):

             """simple docstring"""





             return rescale(lowercase  ,scale=lowercase  ,data_format=lowercase  ,**lowercase)



       def 			A_ (  self  ,lowercase  ,lowercase  ,lowercase  ,lowercase = None  ,**lowercase  ,):

             """simple docstring"""





             return normalize(lowercase  ,mean=lowercase  ,std=lowercase  ,data_format=lowercase  ,**lowercase)



       def 			A_ (  self  ,lowercase  ,lowercase = None  ,lowercase = None  ,lowercase = None  ,lowercase = None  ,lowercase = None  ,lowercase = None  ,lowercase = False  ,lowercase = None  ,lowercase = None  ,lowercase = None  ,lowercase = None  ,lowercase = None  ,lowercase = None  ,lowercase = ChannelDimension.FIRST  ,**lowercase  ,):

             """simple docstring"""





             UpperCAmelCase_	:						Tuple					   =		do_resize if do_resize is not None else self.do_resize
             UpperCAmelCase_	:						Optional[int]					   =		size if size is not None else self.size
             if isinstance(lowercase  ,(tuple, list)):
                   # Previous feature extractor had size in (width, height) format
                   UpperCAmelCase_	:						Tuple					   =		size[::-1]
             UpperCAmelCase_	:						Dict					   =		get_size_dict(lowercase)
             UpperCAmelCase_	:						List[Any]					   =		resample if resample is not None else self.resample
             UpperCAmelCase_	:						Dict					   =		do_thumbnail if do_thumbnail is not None else self.do_thumbnail
             UpperCAmelCase_	:						Any					   =		do_align_long_axis if do_align_long_axis is not None else self.do_align_long_axis
             UpperCAmelCase_	:						Union[str, Any]					   =		do_pad if do_pad is not None else self.do_pad
             UpperCAmelCase_	:						Union[str, Any]					   =		do_rescale if do_rescale is not None else self.do_rescale
             UpperCAmelCase_	:						Tuple					   =		rescale_factor if rescale_factor is not None else self.rescale_factor
             UpperCAmelCase_	:						Optional[int]					   =		do_normalize if do_normalize is not None else self.do_normalize
             UpperCAmelCase_	:						Dict					   =		image_mean if image_mean is not None else self.image_mean
             UpperCAmelCase_	:						List[Any]					   =		image_std if image_std is not None else self.image_std

             UpperCAmelCase_	:						Union[str, Any]					   =		make_list_of_images(lowercase)

             if not valid_images(lowercase):
                   raise ValueError(
                       "Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, "
                       "torch.Tensor, tf.Tensor or jax.ndarray.")

             if do_resize and size is None:
                   raise ValueError("Size must be specified if do_resize is True.")

             if do_rescale and rescale_factor is None:
                   raise ValueError("Rescale factor must be specified if do_rescale is True.")

             if do_pad and size is None:
                   raise ValueError("Size must be specified if do_pad is True.")

             if do_normalize and (image_mean is None or image_std is None):
                   raise ValueError("Image mean and std must be specified if do_normalize is True.")

             # All transformations expect numpy arrays.
             UpperCAmelCase_	:						Tuple					   =		[to_numpy_array(lowercase) for image in images]

             if do_align_long_axis:
                   UpperCAmelCase_	:						List[str]					   =		[self.align_long_axis(lowercase  ,size=lowercase) for image in images]

             if do_resize:
                   UpperCAmelCase_	:						Dict					   =		[self.resize(image=lowercase  ,size=lowercase  ,resample=lowercase) for image in images]

             if do_thumbnail:
                   UpperCAmelCase_	:						int					   =		[self.thumbnail(image=lowercase  ,size=lowercase) for image in images]

             if do_pad:
                   UpperCAmelCase_	:						int					   =		[self.pad_image(image=lowercase  ,size=lowercase  ,random_padding=lowercase) for image in images]

             if do_rescale:
                   UpperCAmelCase_	:						List[str]					   =		[self.rescale(image=lowercase  ,scale=lowercase) for image in images]

             if do_normalize:
                   UpperCAmelCase_	:						List[str]					   =		[self.normalize(image=lowercase  ,mean=lowercase  ,std=lowercase) for image in images]

             UpperCAmelCase_	:						List[Any]					   =		[to_channel_dimension_format(lowercase  ,lowercase) for image in images]

             UpperCAmelCase_	:						str					   =		{"pixel_values": images}
             return BatchFeature(data=lowercase  ,tensor_type=lowercase)





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

import transformers
from transformers import (
    AutoConfig,
    AutoModelForQuestionAnswering,
    AutoTokenizer,
    DataCollatorWithPadding,
    HfArgumentParser,
    SquadDataset,
    Trainer,
    TrainingArguments,
)
from transformers import SquadDataTrainingArguments as DataTrainingArguments
from transformers.trainer_utils import is_main_process


__lowerCamelCase								=      logging.getLogger(__name__)


@dataclass
class 	snake_case_			:




       """simple docstring"""






       _lowerCamelCase           =					field(
           metadata={"""help""": """Path to pretrained model or model identifier from huggingface.co/models"""}	)
       _lowerCamelCase           =					field(
           default=lowercase__			,			metadata={"""help""": """Pretrained config name or path if not the same as model_name"""}	)
       _lowerCamelCase           =					field(
           default=lowercase__			,			metadata={"""help""": """Pretrained tokenizer name or path if not the same as model_name"""}	)
       _lowerCamelCase           =					field(default=lowercase__			,			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.
       _lowerCamelCase           =					field(
           default=lowercase__			,			metadata={"""help""": """Where do you want to store the pretrained models downloaded from huggingface.co"""}			,			)







def  _snake_case			(							)  ->  Union[str, Any]:






      '''simple docstring'''







      UpperCAmelCase_	:						Union[str, Any]					   =		HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments)     )

      if len(sys.argv     ) == 2 and sys.argv[1].endswith(".json"     ):
            # If we pass only one argument to the script and it's the path to a json file,
            # let's parse it to get our arguments.
            UpperCAmelCase_   ,   UpperCAmelCase_   ,   UpperCAmelCase_	:						Any					   =		parser.parse_json_file(json_file=os.path.abspath(sys.argv[1]     )     )
      else:
            UpperCAmelCase_   ,   UpperCAmelCase_   ,   UpperCAmelCase_	:						Union[str, Any]					   =		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 if training_args.local_rank in [-1, 0] else logging.WARN       ,	)
      logger.warning(
          "Process rank: %s, device: %s, n_gpu: %s, distributed training: %s, 16-bits training: %s"       ,	training_args.local_rank       ,	training_args.device       ,	training_args.n_gpu       ,	bool(training_args.local_rank != -1     )       ,	training_args.fpaa       ,	)
      # Set the verbosity to info of the Transformers logger (on main process only):
      if is_main_process(training_args.local_rank     ):
            transformers.utils.logging.set_verbosity_info()
            transformers.utils.logging.enable_default_handler()
            transformers.utils.logging.enable_explicit_format()
      logger.info("Training/evaluation parameters %s"       ,	__snake_case     )

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

      UpperCAmelCase_	:						Any					   =		AutoConfig.from_pretrained(
          model_args.config_name if model_args.config_name else model_args.model_name_or_path       ,	cache_dir=model_args.cache_dir       ,	)
      UpperCAmelCase_	:						List[str]					   =		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       ,	use_fast=__snake_case       ,	)
      UpperCAmelCase_	:						Optional[int]					   =		AutoModelForQuestionAnswering.from_pretrained(
          model_args.model_name_or_path       ,	from_tf=bool(".ckpt" in model_args.model_name_or_path     )       ,	config=__snake_case       ,	cache_dir=model_args.cache_dir       ,	)

      # Get datasets
      UpperCAmelCase_	:						str					   =		hasattr(model.config       ,	"lang2id"     )
      UpperCAmelCase_	:						Optional[Any]					   =		(
          SquadDataset(
              __snake_case       ,	tokenizer=__snake_case       ,	is_language_sensitive=__snake_case       ,	cache_dir=model_args.cache_dir     )
          if training_args.do_train
          else None
      )
      UpperCAmelCase_	:						Optional[Any]					   =		(
          SquadDataset(
              __snake_case       ,	tokenizer=__snake_case       ,	mode="dev"       ,	is_language_sensitive=__snake_case       ,	cache_dir=model_args.cache_dir       ,	)
          if training_args.do_eval
          else None
      )

      # Data collator
      UpperCAmelCase_	:						Optional[Any]					   =		DataCollatorWithPadding(__snake_case       ,	pad_to_multiple_of=8     ) if training_args.fpaa else None

      # Initialize our Trainer
      UpperCAmelCase_	:						Any					   =		Trainer(
          model=__snake_case       ,	args=__snake_case       ,	train_dataset=__snake_case       ,	eval_dataset=__snake_case       ,	data_collator=__snake_case       ,	)

      # Training
      if training_args.do_train:
            trainer.train(
                model_path=model_args.model_name_or_path if os.path.isdir(model_args.model_name_or_path     ) else None     )
            trainer.save_model()
            # For convenience, we also re-save the tokenizer to the same directory,
            # so that you can share your model easily on huggingface.co/models =)
            if trainer.is_world_master():
                  tokenizer.save_pretrained(training_args.output_dir     )







def  _snake_case			(							__snake_case     )  ->  List[str]:






      '''simple docstring'''







      main()


if __name__ == "__main__":
   main()
