
import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin


if is_torch_available():
   import torch

if is_vision_available():
   from PIL import Image

   from transformers import BlipImageProcessor







class 	lowerCamelCase						(    unittest.TestCase    ):





     def __init__(self :       Optional[Any]							,       _A :       List[str]							,       _A :       Any=7							,       _A :       str=3							,       _A :       Tuple=1_8							,       _A :       str=3_0							,       _A :       Any=4_0_0							,       _A :       Union[str, Any]=True							,       _A :       Tuple=None							,       _A :       Union[str, Any]=True							,       _A :       Optional[Any]=False							,       _A :       int=[0.48_14_54_66, 0.4_57_82_75, 0.40_82_10_73]							,       _A :       Dict=[0.26_86_29_54, 0.26_13_02_58, 0.27_57_77_11]							,       _A :       Union[str, Any]=True							,       )		->		Dict:
         snake_case						=   size if size is not None else {"height": 2_0, "width": 2_0}
         snake_case						=   parent
         snake_case						=   batch_size
         snake_case						=   num_channels
         snake_case						=   image_size
         snake_case						=   min_resolution
         snake_case						=   max_resolution
         snake_case						=   do_resize
         snake_case						=   size
         snake_case						=   do_normalize
         snake_case						=   image_mean
         snake_case						=   image_std
         snake_case						=   do_pad
         snake_case						=   do_convert_rgb





     def 						UpperCAmelCase(self :       int  )		->		Tuple:
         return {
             "do_resize": self.do_resize,
             "size": self.size,
             "do_normalize": self.do_normalize,
             "image_mean": self.image_mean,
             "image_std": self.image_std,
             "do_convert_rgb": self.do_convert_rgb,
             "do_pad": self.do_pad,
         }





     def 						UpperCAmelCase(self :       Tuple							,       _A :       Optional[int]=False							,       _A :       Optional[int]=False							,       _A :       Dict=False  )		->		int:
         assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

         if equal_resolution:
             snake_case						=   []
             for i in range(self.batch_size  ):
                 image_inputs.append(
                     np.random.randint(
                         2_5_5							,       size=(self.num_channels, self.max_resolution, self.max_resolution)							,       dtype=np.uinta  )  )
         else:
             snake_case						=   []
             for i in range(self.batch_size  ):
                 snake_case	,				snake_case						=   np.random.choice(np.arange(self.min_resolution							,       self.max_resolution  )							,       2  )
                 image_inputs.append(np.random.randint(2_5_5							,       size=(self.num_channels, width, height)							,       dtype=np.uinta  )  )

         if not numpify and not torchify:
             # PIL expects the channel dimension as last dimension
             snake_case						=   [Image.fromarray(np.moveaxis(_A							,       0							,       -1  )  ) for x in image_inputs]

         if torchify:
             snake_case						=   [torch.from_numpy(_A  ) for x in image_inputs]

         return image_inputs







@require_torch
@require_vision
class 	lowerCamelCase						(    A_				,					unittest.TestCase    ):
     UpperCAmelCase__							:				int 	=						BlipImageProcessor if is_vision_available() else None





     def 						UpperCAmelCase(self :       Optional[Any]  )		->		Optional[int]:
         snake_case						=   BlipImageProcessingTester(self  )





     @property
     def 						UpperCAmelCase(self :       Union[str, Any]  )		->		Optional[int]:
         return self.image_processor_tester.prepare_image_processor_dict()





     def 						UpperCAmelCase(self :       Optional[Any]  )		->		int:
         snake_case						=   self.image_processing_class(**self.image_processor_dict  )
         self.assertTrue(hasattr(_A							,       "do_resize"  )  )
         self.assertTrue(hasattr(_A							,       "size"  )  )
         self.assertTrue(hasattr(_A							,       "do_normalize"  )  )
         self.assertTrue(hasattr(_A							,       "image_mean"  )  )
         self.assertTrue(hasattr(_A							,       "image_std"  )  )
         self.assertTrue(hasattr(_A							,       "do_convert_rgb"  )  )





     def 						UpperCAmelCase(self :       Union[str, Any]  )		->		Union[str, Any]:
         pass





     def 						UpperCAmelCase(self :       Tuple  )		->		Union[str, Any]:
         # Initialize image_processor
         snake_case						=   self.image_processing_class(**self.image_processor_dict  )
         # create random PIL images
         snake_case						=   self.image_processor_tester.prepare_inputs(equal_resolution=_A  )
         for image in image_inputs:
             self.assertIsInstance(_A							,       Image.Image  )

         # Test not batched input
         snake_case						=   image_processor(image_inputs[0]							,       return_tensors="pt"  ).pixel_values
         self.assertEqual(
             encoded_images.shape							,       (
                 1,
                 self.image_processor_tester.num_channels,
                 self.image_processor_tester.size["height"],
                 self.image_processor_tester.size["width"],
             )							,       )

         # Test batched
         snake_case						=   image_processor(_A							,       return_tensors="pt"  ).pixel_values
         self.assertEqual(
             encoded_images.shape							,       (
                 self.image_processor_tester.batch_size,
                 self.image_processor_tester.num_channels,
                 self.image_processor_tester.size["height"],
                 self.image_processor_tester.size["width"],
             )							,       )





     def 						UpperCAmelCase(self :       Dict  )		->		Dict:
         # Initialize image_processor
         snake_case						=   self.image_processing_class(**self.image_processor_dict  )
         # create random numpy tensors
         snake_case						=   self.image_processor_tester.prepare_inputs(equal_resolution=_A							,       numpify=_A  )
         for image in image_inputs:
             self.assertIsInstance(_A							,       np.ndarray  )

         # Test not batched input
         snake_case						=   image_processor(image_inputs[0]							,       return_tensors="pt"  ).pixel_values
         self.assertEqual(
             encoded_images.shape							,       (
                 1,
                 self.image_processor_tester.num_channels,
                 self.image_processor_tester.size["height"],
                 self.image_processor_tester.size["width"],
             )							,       )

         # Test batched
         snake_case						=   image_processor(_A							,       return_tensors="pt"  ).pixel_values
         self.assertEqual(
             encoded_images.shape							,       (
                 self.image_processor_tester.batch_size,
                 self.image_processor_tester.num_channels,
                 self.image_processor_tester.size["height"],
                 self.image_processor_tester.size["width"],
             )							,       )





     def 						UpperCAmelCase(self :       List[str]  )		->		Optional[Any]:
         # Initialize image_processor
         snake_case						=   self.image_processing_class(**self.image_processor_dict  )
         # create random PyTorch tensors
         snake_case						=   self.image_processor_tester.prepare_inputs(equal_resolution=_A							,       torchify=_A  )
         for image in image_inputs:
             self.assertIsInstance(_A							,       torch.Tensor  )

         # Test not batched input
         snake_case						=   image_processor(image_inputs[0]							,       return_tensors="pt"  ).pixel_values
         self.assertEqual(
             encoded_images.shape							,       (
                 1,
                 self.image_processor_tester.num_channels,
                 self.image_processor_tester.size["height"],
                 self.image_processor_tester.size["width"],
             )							,       )

         # Test batched
         snake_case						=   image_processor(_A							,       return_tensors="pt"  ).pixel_values
         self.assertEqual(
             encoded_images.shape							,       (
                 self.image_processor_tester.batch_size,
                 self.image_processor_tester.num_channels,
                 self.image_processor_tester.size["height"],
                 self.image_processor_tester.size["width"],
             )							,       )







@require_torch
@require_vision
class 	lowerCamelCase						(    A_				,					unittest.TestCase    ):
     UpperCAmelCase__							:				str 	=						BlipImageProcessor if is_vision_available() else None





     def 						UpperCAmelCase(self :       Optional[int]  )		->		Dict:
         snake_case						=   BlipImageProcessingTester(self							,       num_channels=4  )
         snake_case						=   3





     @property
     def 						UpperCAmelCase(self :       Optional[Any]  )		->		Any:
         return self.image_processor_tester.prepare_image_processor_dict()





     def 						UpperCAmelCase(self :       List[str]  )		->		Any:
         snake_case						=   self.image_processing_class(**self.image_processor_dict  )
         self.assertTrue(hasattr(_A							,       "do_resize"  )  )
         self.assertTrue(hasattr(_A							,       "size"  )  )
         self.assertTrue(hasattr(_A							,       "do_normalize"  )  )
         self.assertTrue(hasattr(_A							,       "image_mean"  )  )
         self.assertTrue(hasattr(_A							,       "image_std"  )  )
         self.assertTrue(hasattr(_A							,       "do_convert_rgb"  )  )





     def 						UpperCAmelCase(self :       Any  )		->		int:
         pass





     def 						UpperCAmelCase(self :       Dict  )		->		Optional[int]:
         # Initialize image_processor
         snake_case						=   self.image_processing_class(**self.image_processor_dict  )
         # create random PIL images
         snake_case						=   self.image_processor_tester.prepare_inputs(equal_resolution=_A  )
         for image in image_inputs:
             self.assertIsInstance(_A							,       Image.Image  )

         # Test not batched input
         snake_case						=   image_processor(image_inputs[0]							,       return_tensors="pt"  ).pixel_values
         self.assertEqual(
             encoded_images.shape							,       (
                 1,
                 self.expected_encoded_image_num_channels,
                 self.image_processor_tester.size["height"],
                 self.image_processor_tester.size["width"],
             )							,       )

         # Test batched
         snake_case						=   image_processor(_A							,       return_tensors="pt"  ).pixel_values
         self.assertEqual(
             encoded_images.shape							,       (
                 self.image_processor_tester.batch_size,
                 self.expected_encoded_image_num_channels,
                 self.image_processor_tester.size["height"],
                 self.image_processor_tester.size["width"],
             )							,       )



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


_A       =					logging.getLogger(__name__)







@dataclass
class 	lowerCamelCase						:
     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 		lowercase_     (  )				->				int:
    """simple docstring"""

    snake_case						=   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.
        snake_case	,				snake_case	,				snake_case						=   parser.parse_json_file(json_file=os.path.abspath(sys.argv[1]						)						)
    else:
        snake_case	,				snake_case	,				snake_case						=   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"					,						A__						)

    # 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.

    snake_case						=   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					,						)
    snake_case						=   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=A__					,						)
    snake_case						=   AutoModelForQuestionAnswering.from_pretrained(
        model_args.model_name_or_path					,						from_tf=bool(".ckpt" in model_args.model_name_or_path						)					,						config=A__					,						cache_dir=model_args.cache_dir					,						)

    # Get datasets
    snake_case						=   hasattr(model.config					,						"lang2id"						)
    snake_case						=   (
        SquadDataset(
            A__					,						tokenizer=A__					,						is_language_sensitive=A__					,						cache_dir=model_args.cache_dir						)
        if training_args.do_train
        else None
    )
    snake_case						=   (
        SquadDataset(
            A__					,						tokenizer=A__					,						mode="dev"					,						is_language_sensitive=A__					,						cache_dir=model_args.cache_dir					,						)
        if training_args.do_eval
        else None
    )

    # Data collator
    snake_case						=   DataCollatorWithPadding(A__					,						pad_to_multiple_of=8						) if training_args.fpaa else None

    # Initialize our Trainer
    snake_case						=   Trainer(
        model=A__					,						args=A__					,						train_dataset=A__					,						eval_dataset=A__					,						data_collator=A__					,						)

    # 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 		lowercase_     (  A__						)				->				Optional[Any]:
    """simple docstring"""

    main()


if __name__ == "__main__":
   main()

