





"""simple docstring"""


import multiprocessing
from typing import TYPE_CHECKING, Optional, Union

from .. import Dataset, Features, config
from ..formatting import query_table
from ..packaged_modules.sql.sql import Sql
from ..utils import logging
from .abc import AbstractDatasetInputStream


if TYPE_CHECKING:
		import sqlitea

		import sqlalchemy






class 							lowercase__			(   __SCREAMING_SNAKE_CASE	):

			"""simple docstring"""







			def __init__(      self						,   _A						,   _A						,   _A = None						,   _A = None						,   _A = False						,   **_A						,   ):






									'''simple docstring'''







									super().__init__(features=_A						,   cache_dir=_A						,   keep_in_memory=_A						,   **_A    )
									UpperCamelCase				:		List[Any]				   =			Sql(
									    cache_dir=_A						,   features=_A						,   sql=_A						,   con=_A						,   **_A						,   )







			def   _a      (      self    ):






									'''simple docstring'''







									UpperCamelCase				:		Union[str, Any]				   =			None
									UpperCamelCase				:		Optional[int]				   =			None
									UpperCamelCase				:		Dict				   =			None
									UpperCamelCase				:		Union[str, Any]				   =			None

									self.builder.download_and_prepare(
									    download_config=_A						,   download_mode=_A						,   verification_mode=_A						,   base_path=_A						,   )

									# Build dataset for splits
									UpperCamelCase				:		Optional[int]				   =			self.builder.as_dataset(
									    split="""train"""						,   verification_mode=_A						,   in_memory=self.keep_in_memory    )
									return dataset






class 							lowercase__			:

			"""simple docstring"""







			def __init__(      self						,   _A						,   _A						,   _A						,   _A = None						,   _A = None						,   **_A						,   ):






									'''simple docstring'''







									if num_proc is not None and num_proc <= 0:
															raise ValueError(f"""num_proc {num_proc} must be an integer > 0."""    )

									UpperCamelCase				:		Tuple				   =			dataset
									UpperCamelCase				:		Dict				   =			name
									UpperCamelCase				:		Optional[int]				   =			con
									UpperCamelCase				:		str				   =			batch_size if batch_size else config.DEFAULT_MAX_BATCH_SIZE
									UpperCamelCase				:		Optional[int]				   =			num_proc
									UpperCamelCase				:		Tuple				   =			to_sql_kwargs







			def   _a      (      self    ):






									'''simple docstring'''







									UpperCamelCase				:		int				   =			self.to_sql_kwargs.pop("""sql"""						,   _A    )
									UpperCamelCase				:		Optional[Any]				   =			self.to_sql_kwargs.pop("""con"""						,   _A    )
									UpperCamelCase				:		List[Any]				   =			self.to_sql_kwargs.pop("""index"""						,   _A    )

									UpperCamelCase				:		int				   =			self._write(index=_A						,   **self.to_sql_kwargs    )
									return written







			def   _a      (      self						,   _A    ):






									'''simple docstring'''







									UpperCamelCase   ,						UpperCamelCase   ,						UpperCamelCase				:		List[str]				   =			args
									UpperCamelCase				:		Tuple				   =			{**to_sql_kwargs, """if_exists""": """append"""} if offset > 0 else to_sql_kwargs
									UpperCamelCase				:		Any				   =			query_table(
									    table=self.dataset.data						,   key=slice(_A						,   offset + self.batch_size    )						,   indices=self.dataset._indices						,   )
									UpperCamelCase				:		List[str]				   =			batch.to_pandas()
									UpperCamelCase				:		List[str]				   =			df.to_sql(self.name						,   self.con						,   index=_A						,   **_A    )
									return num_rows or len(_A    )







			def   _a      (      self						,   _A						,   **_A    ):






									'''simple docstring'''







									UpperCamelCase				:		List[Any]				   =			0

									if self.num_proc is None or self.num_proc == 1:
															for offset in logging.tqdm(
															    range(0						,   len(self.dataset    )						,   self.batch_size    )						,   unit="""ba"""						,   disable=not logging.is_progress_bar_enabled()						,   desc="""Creating SQL from Arrow format"""						,   ):
																					written += self._batch_sql((offset, index, to_sql_kwargs)    )
									else:
															UpperCamelCase   ,						UpperCamelCase				:		Optional[int]				   =			len(self.dataset    ), self.batch_size
															with multiprocessing.Pool(self.num_proc    ) as pool:
																					for num_rows in logging.tqdm(
																					    pool.imap(
																					        self._batch_sql						,   [(offset, index, to_sql_kwargs) for offset in range(0						,   _A						,   _A    )]						,   )						,   total=(num_rows // batch_size) + 1 if num_rows % batch_size else num_rows // batch_size						,   unit="""ba"""						,   disable=not logging.is_progress_bar_enabled()						,   desc="""Creating SQL from Arrow format"""						,   ):
																											written += num_rows

									return written










"""simple docstring"""


from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
    is_vision_available,
)


__magic_name__       :					Dict						=			{
    """configuration_layoutlmv3""": [
        """LAYOUTLMV3_PRETRAINED_CONFIG_ARCHIVE_MAP""",
        """LayoutLMv3Config""",
        """LayoutLMv3OnnxConfig""",
    ],
    """processing_layoutlmv3""": ["""LayoutLMv3Processor"""],
    """tokenization_layoutlmv3""": ["""LayoutLMv3Tokenizer"""],
}

try:
		if not is_tokenizers_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		__magic_name__       :					Optional[Any]						=			["""LayoutLMv3TokenizerFast"""]

try:
		if not is_torch_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		__magic_name__       :					int						=			[
		    """LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST""",
		    """LayoutLMv3ForQuestionAnswering""",
		    """LayoutLMv3ForSequenceClassification""",
		    """LayoutLMv3ForTokenClassification""",
		    """LayoutLMv3Model""",
		    """LayoutLMv3PreTrainedModel""",
		]

try:
		if not is_tf_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		__magic_name__       :					Tuple						=			[
		    """TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST""",
		    """TFLayoutLMv3ForQuestionAnswering""",
		    """TFLayoutLMv3ForSequenceClassification""",
		    """TFLayoutLMv3ForTokenClassification""",
		    """TFLayoutLMv3Model""",
		    """TFLayoutLMv3PreTrainedModel""",
		]

try:
		if not is_vision_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		__magic_name__       :					Union[str, Any]						=			["""LayoutLMv3FeatureExtractor"""]
		__magic_name__       :					Any						=			["""LayoutLMv3ImageProcessor"""]


if TYPE_CHECKING:
		from .configuration_layoutlmva import (
		    LAYOUTLMV3_PRETRAINED_CONFIG_ARCHIVE_MAP,
		    LayoutLMvaConfig,
		    LayoutLMvaOnnxConfig,
		)
		from .processing_layoutlmva import LayoutLMvaProcessor
		from .tokenization_layoutlmva import LayoutLMvaTokenizer

		try:
				if not is_tokenizers_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .tokenization_layoutlmva_fast import LayoutLMvaTokenizerFast

		try:
				if not is_torch_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_layoutlmva import (
				    LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST,
				    LayoutLMvaForQuestionAnswering,
				    LayoutLMvaForSequenceClassification,
				    LayoutLMvaForTokenClassification,
				    LayoutLMvaModel,
				    LayoutLMvaPreTrainedModel,
				)

		try:
				if not is_tf_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_tf_layoutlmva import (
				    TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST,
				    TFLayoutLMvaForQuestionAnswering,
				    TFLayoutLMvaForSequenceClassification,
				    TFLayoutLMvaForTokenClassification,
				    TFLayoutLMvaModel,
				    TFLayoutLMvaPreTrainedModel,
				)

		try:
				if not is_vision_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .feature_extraction_layoutlmva import LayoutLMvaFeatureExtractor
				from .image_processing_layoutlmva import LayoutLMvaImageProcessor

else:
		import sys

		__magic_name__       :					Union[str, Any]						=			_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)



