



"""simple docstring"""






import itertools
from dataclasses import dataclass
from typing import Optional

import pyarrow as pa

import datasets
from datasets.table import table_cast


__UpperCAmelCase      :      int   					=					datasets.utils.logging.get_logger(__name__)

@dataclass
class 	__lowerCAmelCase							(datasets.BuilderConfig  ):


					'''simple docstring'''


					a__        =     None



class 	__lowerCAmelCase							(datasets.ArrowBasedBuilder  ):


					'''simple docstring'''


					a__        =     ArrowConfig







					def 		_a (      self   ):




										"""simple docstring"""





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







					def 		_a (      self    ,       a   ):




										"""simple docstring"""





										if not self.config.data_files:
															raise ValueError(F'''At least one data file must be specified, but got data_files={self.config.data_files}'''   )
										snake_case_						:int								=						dl_manager.download_and_extract(self.config.data_files   )
										if isinstance(a    ,       (str, list, tuple)   ):
															snake_case_						:List[str]								=						data_files
															if isinstance(a    ,       a   ):
																				snake_case_						:List[str]								=						[files]
															# Use `dl_manager.iter_files` to skip hidden files in an extracted archive
															snake_case_						:List[Any]								=						[dl_manager.iter_files(a   ) for file in files]
															return [datasets.SplitGenerator(name=datasets.Split.TRAIN    ,       gen_kwargs={"files": files}   )]
										snake_case_						:str								=						[]
										for split_name, files in data_files.items():
															if isinstance(a    ,       a   ):
																				snake_case_						:int								=						[files]
															# Use `dl_manager.iter_files` to skip hidden files in an extracted archive
															snake_case_						:List[Any]								=						[dl_manager.iter_files(a   ) for file in files]
															# Infer features is they are stoed in the arrow schema
															if self.info.features is None:
																				for file in itertools.chain.from_iterable(a   ):
																									with open(a    ,       "rb"   ) as f:
																														snake_case_						:str								=						datasets.Features.from_arrow_schema(pa.ipc.open_stream(a   ).schema   )
																									break
															splits.append(datasets.SplitGenerator(name=a    ,       gen_kwargs={"files": files}   )   )
										return splits







					def 		_a (      self    ,       a   ):




										"""simple docstring"""





										if self.info.features is not None:
															# more expensive cast to support nested features with keys in a different order
															# allows str <-> int/float or str to Audio for example
															snake_case_						:List[str]								=						table_cast(a    ,       self.info.features.arrow_schema   )
										return pa_table







					def 		_a (      self    ,       a   ):




										"""simple docstring"""





										for file_idx, file in enumerate(itertools.chain.from_iterable(a   )   ):
															with open(a    ,       "rb"   ) as f:
																				try:
																									for batch_idx, record_batch in enumerate(pa.ipc.open_stream(a   )   ):
																														snake_case_						:Tuple								=						pa.Table.from_batches([record_batch]   )
																														# Uncomment for debugging (will print the Arrow table size and elements)
																														# logger.warning(f"pa_table: {pa_table} num rows: {pa_table.num_rows}")
																														# logger.warning('\n'.join(str(pa_table.slice(i, 1).to_pydict()) for i in range(pa_table.num_rows)))
																														yield F'''{file_idx}_{batch_idx}''', self._cast_table(a   )
																				except ValueError as e:
																									logger.error(F'''Failed to read file \'{file}\' with error {type(a   )}: {e}'''   )
																									raise






"""simple docstring"""






from typing import TYPE_CHECKING

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


__UpperCAmelCase      :      Optional[int]   					=					{
    'configuration_segformer': ['SEGFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP', 'SegformerConfig', 'SegformerOnnxConfig']
}

try:
				if not is_vision_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				__UpperCAmelCase      :      int   					=					['SegformerFeatureExtractor']
				__UpperCAmelCase      :      Dict   					=					['SegformerImageProcessor']

try:
				if not is_torch_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				__UpperCAmelCase      :      Optional[Any]   					=					[
				    'SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST',
				    'SegformerDecodeHead',
				    'SegformerForImageClassification',
				    'SegformerForSemanticSegmentation',
				    'SegformerLayer',
				    'SegformerModel',
				    'SegformerPreTrainedModel',
				]

try:
				if not is_tf_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				__UpperCAmelCase      :      Dict   					=					[
				    'TF_SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST',
				    'TFSegformerDecodeHead',
				    'TFSegformerForImageClassification',
				    'TFSegformerForSemanticSegmentation',
				    'TFSegformerModel',
				    'TFSegformerPreTrainedModel',
				]


if TYPE_CHECKING:
				from .configuration_segformer import SEGFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, SegformerConfig, SegformerOnnxConfig

				try:
								if not is_vision_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .feature_extraction_segformer import SegformerFeatureExtractor
								from .image_processing_segformer import SegformerImageProcessor

				try:
								if not is_torch_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .modeling_segformer import (
								    SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
								    SegformerDecodeHead,
								    SegformerForImageClassification,
								    SegformerForSemanticSegmentation,
								    SegformerLayer,
								    SegformerModel,
								    SegformerPreTrainedModel,
								)
				try:
								if not is_tf_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .modeling_tf_segformer import (
								    TF_SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
								    TFSegformerDecodeHead,
								    TFSegformerForImageClassification,
								    TFSegformerForSemanticSegmentation,
								    TFSegformerModel,
								    TFSegformerPreTrainedModel,
								)

else:
				import sys

				__UpperCAmelCase      :      Optional[Any]   					=					_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)

