



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


_lowercase:					Union[str, Any]      =						logging.getLogger(__name__)



@dataclass
class       _lowercase				:





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






def        a( )						->			Optional[int]:
					"""simple docstring"""





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

					a   	=  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					,	)
					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					,	use_fast=A					,	)
					a   	=  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
					a   	=  hasattr(model.config					,	"lang2id"			)
					a   	=  (
					    SquadDataset(
					        A					,	tokenizer=A					,	is_language_sensitive=A					,	cache_dir=model_args.cache_dir			)
					    if training_args.do_train
					    else None
					)
					a   	=  (
					    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
					a   	=  DataCollatorWithPadding(A					,	pad_to_multiple_of=8			) if training_args.fpaa else None

					# Initialize our Trainer
					a   	=  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        a( A			:       Tuple			)						->			Dict:
					"""simple docstring"""





					main()


if __name__ == "__main__":
				main()





import sys
from dataclasses import dataclass
from typing import TYPE_CHECKING, Dict, List, Optional, Tuple, Union

import pandas as pd
import pyarrow as pa

import datasets
import datasets.config
from datasets.features.features import require_storage_cast
from datasets.table import table_cast


if TYPE_CHECKING:
				import sqlitea

				import sqlalchemy


_lowercase:					Optional[Any]      =						datasets.utils.logging.get_logger(__name__)



@dataclass
class       _lowercase				(  datasets.BuilderConfig      ):





	"""simple docstring"""
	__A		      =				None
	__A		      =				None
	__A		      =				None
	__A		      =				True
	__A		      =				None
	__A		      =				None
	__A		      =				None
	__A		      =				10000
	__A		      =				None




	def 	UpperCamelCase_			(self						):
						"""simple docstring"""

						if self.sql is None:
											raise ValueError("sql must be specified"						)
						if self.con is None:
											raise ValueError("con must be specified"						)




	def 	UpperCamelCase_			(self						,			lowerCamelCase_						,			lowerCamelCase_ = None						,			):
						"""simple docstring"""

						a   	=  config_kwargs.copy()
						# We need to stringify the Selectable object to make its hash deterministic

						# The process of stringifying is explained here: http://docs.sqlalchemy.org/en/latest/faq/sqlexpressions.html
						a   	=  config_kwargs["sql"]
						if not isinstance(lowerCamelCase_						,			lowerCamelCase_						):
											if datasets.config.SQLALCHEMY_AVAILABLE and "sqlalchemy" in sys.modules:
																import sqlalchemy

																if isinstance(lowerCamelCase_						,			sqlalchemy.sql.Selectable						):
																					a   	=  sqlalchemy.create_engine(config_kwargs["con"].split("://"						)[0] + "://"						)
																					a   	=  str(sql.compile(dialect=engine.dialect						)						)
																					a   	=  sql_str
																else:
																					raise TypeError(
																					    F'''Supported types for \'sql\' are string and sqlalchemy.sql.Selectable but got {type(lowerCamelCase_						)}: {sql}'''						)
											else:
																raise TypeError(
																    F'''Supported types for \'sql\' are string and sqlalchemy.sql.Selectable but got {type(lowerCamelCase_						)}: {sql}'''						)
						a   	=  config_kwargs["con"]
						if not isinstance(lowerCamelCase_						,			lowerCamelCase_						):
											a   	=  id(lowerCamelCase_						)
											logger.info(
											    F'''SQL connection \'con\' of type {type(lowerCamelCase_						)} couldn\'t be hashed properly. To enable hashing, specify \'con\' as URI string instead.'''						)

						return super().create_config_id(lowerCamelCase_						,			custom_features=lowerCamelCase_						)




	@property
	def 	UpperCamelCase_			(self						):
						"""simple docstring"""

						a   	=  {
						    "index_col": self.index_col,
						    "columns": self.columns,
						    "params": self.params,
						    "coerce_float": self.coerce_float,
						    "parse_dates": self.parse_dates,
						}
						return pd_read_sql_kwargs




class       _lowercase				(  datasets.ArrowBasedBuilder      ):





	"""simple docstring"""
	__A		      =				SqlConfig




	def 	UpperCamelCase_			(self						):
						"""simple docstring"""

						return datasets.DatasetInfo(features=self.config.features						)




	def 	UpperCamelCase_			(self						,			lowerCamelCase_						):
						"""simple docstring"""

						return [datasets.SplitGenerator(name=datasets.Split.TRAIN						,			gen_kwargs={}						)]




	def 	UpperCamelCase_			(self						,			lowerCamelCase_						):
						"""simple docstring"""

						if self.config.features is not None:
											a   	=  self.config.features.arrow_schema
											if all(not require_storage_cast(lowerCamelCase_						) for feature in self.config.features.values()						):
																# cheaper cast
																a   	=  pa.Table.from_arrays([pa_table[field.name] for field in schema]						,			schema=lowerCamelCase_						)
											else:
																# more expensive cast; allows str <-> int/float or str to Audio for example
																a   	=  table_cast(lowerCamelCase_						,			lowerCamelCase_						)
						return pa_table




	def 	UpperCamelCase_			(self						):
						"""simple docstring"""

						a   	=  self.config.chunksize
						a   	=  pd.read_sql(
						    self.config.sql						,			self.config.con						,			chunksize=lowerCamelCase_						,			**self.config.pd_read_sql_kwargs						)
						a   	=  [sql_reader] if chunksize is None else sql_reader
						for chunk_idx, df in enumerate(lowerCamelCase_						):
											a   	=  pa.Table.from_pandas(lowerCamelCase_						)
											yield chunk_idx, self._cast_table(lowerCamelCase_						)
