


import warnings
from typing import List, Optional, Union

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy
from ...utils import TensorType



class      snake_case__	(   _A	):
				__lowerCAmelCase		=   ['''image_processor''', '''tokenizer''']
				__lowerCAmelCase		=   '''LayoutLMv2ImageProcessor'''
				__lowerCAmelCase		=   ('''LayoutLMv2Tokenizer''', '''LayoutLMv2TokenizerFast''')
				def __init__(	self  ,   _UpperCAmelCase=None  ,   _UpperCAmelCase=None  ,   **_UpperCAmelCase   ):




										"""simple docstring"""




										__magic_name__					:				Any				=   None
										if "feature_extractor" in kwargs:
																warnings.warn(
																    'The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`'
																    ' instead.'  ,   _UpperCAmelCase  ,   )
																__magic_name__					:				Union[str, Any]				=   kwargs.pop('feature_extractor'   )

										__magic_name__					:				List[str]				=   image_processor if image_processor is not None else feature_extractor
										if image_processor is None:
																raise ValueError('You need to specify an `image_processor`.'   )
										if tokenizer is None:
																raise ValueError('You need to specify a `tokenizer`.'   )

										super().__init__(_UpperCAmelCase  ,   _UpperCAmelCase   )
				def __call__(	self  ,   _UpperCAmelCase  ,   _UpperCAmelCase = None  ,   _UpperCAmelCase = None  ,   _UpperCAmelCase = None  ,   _UpperCAmelCase = None  ,   _UpperCAmelCase = True  ,   _UpperCAmelCase = False  ,   _UpperCAmelCase = False  ,   _UpperCAmelCase = None  ,   _UpperCAmelCase = 0  ,   _UpperCAmelCase = None  ,   _UpperCAmelCase = None  ,   _UpperCAmelCase = None  ,   _UpperCAmelCase = False  ,   _UpperCAmelCase = False  ,   _UpperCAmelCase = False  ,   _UpperCAmelCase = False  ,   _UpperCAmelCase = True  ,   _UpperCAmelCase = None  ,   **_UpperCAmelCase  ,   ):




										"""simple docstring"""




										if self.image_processor.apply_ocr and (boxes is not None):
																raise ValueError(
																    'You cannot provide bounding boxes if you initialized the image processor with apply_ocr set to True.'   )

										if self.image_processor.apply_ocr and (word_labels is not None):
																raise ValueError(
																    'You cannot provide word labels if you initialized the image processor with apply_ocr set to True.'   )

										if return_overflowing_tokens is True and return_offsets_mapping is False:
																raise ValueError('You cannot return overflowing tokens without returning the offsets mapping.'   )

										# first, apply the image processor
										__magic_name__					:				str				=   self.image_processor(images=_UpperCAmelCase  ,   return_tensors=_UpperCAmelCase   )

										# second, apply the tokenizer
										if text is not None and self.image_processor.apply_ocr and text_pair is None:
																if isinstance(_UpperCAmelCase  ,   _UpperCAmelCase   ):
																						__magic_name__					:				Any				=   [text]  # add batch dimension (as the image processor always adds a batch dimension)
																__magic_name__					:				Dict				=   features['words']

										__magic_name__					:				Any				=   self.tokenizer(
										    text=text if text is not None else features['words']  ,   text_pair=text_pair if text_pair is not None else None  ,   boxes=boxes if boxes is not None else features['boxes']  ,   word_labels=_UpperCAmelCase  ,   add_special_tokens=_UpperCAmelCase  ,   padding=_UpperCAmelCase  ,   truncation=_UpperCAmelCase  ,   max_length=_UpperCAmelCase  ,   stride=_UpperCAmelCase  ,   pad_to_multiple_of=_UpperCAmelCase  ,   return_token_type_ids=_UpperCAmelCase  ,   return_attention_mask=_UpperCAmelCase  ,   return_overflowing_tokens=_UpperCAmelCase  ,   return_special_tokens_mask=_UpperCAmelCase  ,   return_offsets_mapping=_UpperCAmelCase  ,   return_length=_UpperCAmelCase  ,   verbose=_UpperCAmelCase  ,   return_tensors=_UpperCAmelCase  ,   **_UpperCAmelCase  ,   )

										# add pixel values
										__magic_name__					:				str				=   features.pop('pixel_values'   )
										if return_overflowing_tokens is True:
																__magic_name__					:				Dict				=   self.get_overflowing_images(_UpperCAmelCase  ,   encoded_inputs['overflow_to_sample_mapping']   )
										__magic_name__					:				List[str]				=   images

										return encoded_inputs
				def 			UpperCamelCase_    (	self  ,   _UpperCAmelCase  ,   _UpperCAmelCase   ):




										"""simple docstring"""




										__magic_name__					:				Union[str, Any]				=   []
										for sample_idx in overflow_to_sample_mapping:
																images_with_overflow.append(images[sample_idx]   )

										if len(_UpperCAmelCase   ) != len(_UpperCAmelCase   ):
																raise ValueError(
																    'Expected length of images to be the same as the length of `overflow_to_sample_mapping`, but got'
																    F" {len(_UpperCAmelCase   )} and {len(_UpperCAmelCase   )}"   )

										return images_with_overflow
				def 			UpperCamelCase_    (	self  ,   *_UpperCAmelCase  ,   **_UpperCAmelCase   ):




										"""simple docstring"""




										return self.tokenizer.batch_decode(*_UpperCAmelCase  ,   **_UpperCAmelCase   )
				def 			UpperCamelCase_    (	self  ,   *_UpperCAmelCase  ,   **_UpperCAmelCase   ):




										"""simple docstring"""




										return self.tokenizer.decode(*_UpperCAmelCase  ,   **_UpperCAmelCase   )
				@property
				def 			UpperCamelCase_    (	self   ):




										"""simple docstring"""




										return ["input_ids", "bbox", "token_type_ids", "attention_mask", "image"]
				@property
				def 			UpperCamelCase_    (	self   ):




										"""simple docstring"""




										warnings.warn(
										    '`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.'  ,   _UpperCAmelCase  ,   )
										return self.image_processor_class





				@property
				def 			UpperCamelCase_    (	self   ):




										"""simple docstring"""




										warnings.warn(
										    '`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.'  ,   _UpperCAmelCase  ,   )
										return self.image_processor






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


snake_case__  				=			datasets.utils.logging.get_logger(__name__)



@dataclass
class      snake_case__	(   datasets.BuilderConfig	):
				__lowerCAmelCase		=   None
				__lowerCAmelCase		=   None
				__lowerCAmelCase		=   None
				__lowerCAmelCase		=   True
				__lowerCAmelCase		=   None
				__lowerCAmelCase		=   None
				__lowerCAmelCase		=   None
				__lowerCAmelCase		=   10_000
				__lowerCAmelCase		=   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  ,   _UpperCAmelCase  ,   _UpperCAmelCase = None  ,   ):




										"""simple docstring"""




										__magic_name__					:				Optional[int]				=   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
										__magic_name__					:				Union[str, Any]				=   config_kwargs['sql']
										if not isinstance(_UpperCAmelCase  ,   _UpperCAmelCase   ):
																if datasets.config.SQLALCHEMY_AVAILABLE and "sqlalchemy" in sys.modules:
																						import sqlalchemy

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

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





				@property
				def 			UpperCamelCase_    (	self   ):




										"""simple docstring"""




										__magic_name__					:				str				=   {
										    '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      snake_case__	(   datasets.ArrowBasedBuilder	):
				__lowerCAmelCase		=   SqlConfig
				def 			UpperCamelCase_    (	self   ):




										"""simple docstring"""




										return datasets.DatasetInfo(features=self.config.features   )
				def 			UpperCamelCase_    (	self  ,   _UpperCAmelCase   ):




										"""simple docstring"""




										return [datasets.SplitGenerator(name=datasets.Split.TRAIN  ,   gen_kwargs={}   )]
				def 			UpperCamelCase_    (	self  ,   _UpperCAmelCase   ):




										"""simple docstring"""




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





				def 			UpperCamelCase_    (	self   ):




										"""simple docstring"""




										__magic_name__					:				int				=   self.config.chunksize
										__magic_name__					:				Tuple				=   pd.read_sql(
										    self.config.sql  ,   self.config.con  ,   chunksize=_UpperCAmelCase  ,   **self.config.pd_read_sql_kwargs   )
										__magic_name__					:				Dict				=   [sql_reader] if chunksize is None else sql_reader
										for chunk_idx, df in enumerate(_UpperCAmelCase   ):
																__magic_name__					:				Optional[Any]				=   pa.Table.from_pandas(_UpperCAmelCase   )
																yield chunk_idx, self._cast_table(_UpperCAmelCase   )


