



import itertools
from dataclasses import dataclass
from typing import Any, Callable, Dict, List, Optional, 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
from datasets.utils.py_utils import Literal


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

UpperCamelCase__ =						['names', 'prefix']
UpperCamelCase__ =						['warn_bad_lines', 'error_bad_lines', 'mangle_dupe_cols']
UpperCamelCase__ =						['encoding_errors', 'on_bad_lines']
UpperCamelCase__ =						['date_format']
@dataclass
class 			_lowerCAmelCase   ( datasets.BuilderConfig			):






			"""simple docstring"""





			lowercase__      :  str      =			","
			lowercase__      :  Optional[str]      =			None
			lowercase__      :  Optional[Union[int, List[int], str]]      =			"infer"
			lowercase__      :  Optional[List[str]]      =			None
			lowercase__      :  Optional[List[str]]      =			None
			lowercase__      :  Optional[Union[int, str, List[int], List[str]]]      =			None
			lowercase__      :  Optional[Union[List[int], List[str]]]      =			None
			lowercase__      :  Optional[str]      =			None
			lowercase__      :  bool      =			True
			lowercase__      :  Optional[Literal["c", "python", "pyarrow"]]      =			None
			lowercase__      :  Dict[Union[int, str], Callable[[Any], Any]]      =			None
			lowercase__      :  Optional[list]      =			None
			lowercase__      :  Optional[list]      =			None
			lowercase__      :  bool      =			False
			lowercase__      :  Optional[Union[int, List[int]]]      =			None
			lowercase__      :  Optional[int]      =			None
			lowercase__      :  Optional[Union[str, List[str]]]      =			None
			lowercase__      :  bool      =			True
			lowercase__      :  bool      =			True
			lowercase__      :  bool      =			False
			lowercase__      :  bool      =			True
			lowercase__      :  Optional[str]      =			None
			lowercase__      :  str      =			"."
			lowercase__      :  Optional[str]      =			None
			lowercase__      :  str      =			'"'
			lowercase__      :  int      =			0
			lowercase__      :  Optional[str]      =			None
			lowercase__      :  Optional[str]      =			None
			lowercase__      :  Optional[str]      =			None
			lowercase__      :  Optional[str]      =			None
			lowercase__      :  bool      =			True
			lowercase__      :  bool      =			True
			lowercase__      :  int      =			0
			lowercase__      :  bool      =			True
			lowercase__      :  bool      =			False
			lowercase__      :  Optional[str]      =			None
			lowercase__      :  int      =			10_000
			lowercase__      :  Optional[datasets.Features]      =			None
			lowercase__      :  Optional[str]      =			"strict"
			lowercase__      :  Literal["error", "warn", "skip"]      =			"error"
			lowercase__      :  Optional[str]      =			None





			def 		snake_case__			(				self	:       Optional[Any]   )						->					Optional[Any]:
									"""simple docstring"""







									if self.delimiter is not None:
															__lowercase					=				self.delimiter
									if self.column_names is not None:
															__lowercase					=				self.column_names







			@property
			def 		snake_case__			(				self	:       Dict   )						->					int:
									"""simple docstring"""







									__lowercase					=				{
									    """sep""": self.sep,
									    """header""": self.header,
									    """names""": self.names,
									    """index_col""": self.index_col,
									    """usecols""": self.usecols,
									    """prefix""": self.prefix,
									    """mangle_dupe_cols""": self.mangle_dupe_cols,
									    """engine""": self.engine,
									    """converters""": self.converters,
									    """true_values""": self.true_values,
									    """false_values""": self.false_values,
									    """skipinitialspace""": self.skipinitialspace,
									    """skiprows""": self.skiprows,
									    """nrows""": self.nrows,
									    """na_values""": self.na_values,
									    """keep_default_na""": self.keep_default_na,
									    """na_filter""": self.na_filter,
									    """verbose""": self.verbose,
									    """skip_blank_lines""": self.skip_blank_lines,
									    """thousands""": self.thousands,
									    """decimal""": self.decimal,
									    """lineterminator""": self.lineterminator,
									    """quotechar""": self.quotechar,
									    """quoting""": self.quoting,
									    """escapechar""": self.escapechar,
									    """comment""": self.comment,
									    """encoding""": self.encoding,
									    """dialect""": self.dialect,
									    """error_bad_lines""": self.error_bad_lines,
									    """warn_bad_lines""": self.warn_bad_lines,
									    """skipfooter""": self.skipfooter,
									    """doublequote""": self.doublequote,
									    """memory_map""": self.memory_map,
									    """float_precision""": self.float_precision,
									    """chunksize""": self.chunksize,
									    """encoding_errors""": self.encoding_errors,
									    """on_bad_lines""": self.on_bad_lines,
									    """date_format""": self.date_format,
									}

									# some kwargs must not be passed if they don't have a default value
									# some others are deprecated and we can also not pass them if they are the default value
									for pd_read_csv_parameter in _PANDAS_READ_CSV_NO_DEFAULT_PARAMETERS + _PANDAS_READ_CSV_DEPRECATED_PARAMETERS:
															if pd_read_csv_kwargs[pd_read_csv_parameter] == getattr(CsvConfig()			,     A__   ):
																					del pd_read_csv_kwargs[pd_read_csv_parameter]

        # Remove 2.0 new arguments
									if not (datasets.config.PANDAS_VERSION.major >= 2):
															for pd_read_csv_parameter in _PANDAS_READ_CSV_NEW_2_0_0_PARAMETERS:
																					del pd_read_csv_kwargs[pd_read_csv_parameter]

        # Remove 1.3 new arguments
									if not (datasets.config.PANDAS_VERSION.major >= 1 and datasets.config.PANDAS_VERSION.minor >= 3):
															for pd_read_csv_parameter in _PANDAS_READ_CSV_NEW_1_3_0_PARAMETERS:
																					del pd_read_csv_kwargs[pd_read_csv_parameter]

									return pd_read_csv_kwargs





class 			_lowerCAmelCase   ( datasets.ArrowBasedBuilder			):






			"""simple docstring"""





			lowercase__      :  Tuple      =			CsvConfig





			def 		snake_case__			(				self	:       List[Any]   )						->					str:
									"""simple docstring"""







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





			def 		snake_case__			(				self	:       int			,     lowercase	:       List[Any]   )						->					Optional[int]:
									"""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}"   )
									__lowercase					=				dl_manager.download_and_extract(self.config.data_files   )
									if isinstance(A__			,     (str, list, tuple)   ):
															__lowercase					=				data_files
															if isinstance(A__			,     A__   ):
																					__lowercase					=				[files]
															__lowercase					=				[dl_manager.iter_files(A__   ) for file in files]
															return [datasets.SplitGenerator(name=datasets.Split.TRAIN			,     gen_kwargs={"""files""": files}   )]
									__lowercase					=				[]
									for split_name, files in data_files.items():
															if isinstance(A__			,     A__   ):
																					__lowercase					=				[files]
															__lowercase					=				[dl_manager.iter_files(A__   ) for file in files]
															splits.append(datasets.SplitGenerator(name=A__			,     gen_kwargs={"""files""": files}   )   )
									return splits





			def 		snake_case__			(				self	:       Tuple			,     lowercase	:       List[str]   )						->					str:
									"""simple docstring"""







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







			def 		snake_case__			(				self	:       Any			,     lowercase	:       Optional[int]   )						->					List[Any]:
									"""simple docstring"""







									__lowercase					=				self.config.features.arrow_schema if self.config.features else None
									# dtype allows reading an int column as str
									__lowercase					=				(
									    {
									        name: dtype.to_pandas_dtype() if not require_storage_cast(A__   ) else object
									        for name, dtype, feature in zip(schema.names			,     schema.types			,     self.config.features.values()   )
									    }
									    if schema is not None
									    else None
									)
									for file_idx, file in enumerate(itertools.chain.from_iterable(A__   )   ):
															__lowercase					=				pd.read_csv(A__			,     iterator=A__			,     dtype=A__			,     **self.config.pd_read_csv_kwargs   )
															try:
																					for batch_idx, df in enumerate(A__   ):
																											__lowercase					=				pa.Table.from_pandas(A__   )
																											# 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 (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










import functools
import operator

from ...configuration_utils import PretrainedConfig
from ...utils import logging


UpperCamelCase__ =						logging.get_logger(__name__)

UpperCamelCase__ =						{
    "asapp/sew-tiny-100k": "https://huggingface.co/asapp/sew-tiny-100k/resolve/main/config.json",
    # See all SEW models at https://huggingface.co/models?filter=sew
}
class 			_lowerCAmelCase   ( _UpperCAmelCase			):






			"""simple docstring"""





			lowercase__      :  Union[str, Any]      =			"""sew"""





			def __init__(				self	:       List[Any]			,     lowercase	:       int=32			,     lowercase	:       List[str]=768			,     lowercase	:       Dict=12			,     lowercase	:       str=12			,     lowercase	:       str=3_072			,     lowercase	:       Optional[int]=2			,     lowercase	:       List[str]="gelu"			,     lowercase	:       List[str]=0.1			,     lowercase	:       Tuple=0.1			,     lowercase	:       Dict=0.1			,     lowercase	:       Any=0.0			,     lowercase	:       Dict=0.1			,     lowercase	:       Optional[int]=0.1			,     lowercase	:       List[str]=0.02			,     lowercase	:       Dict=1E-5			,     lowercase	:       Tuple="group"			,     lowercase	:       int="gelu"			,     lowercase	:       Any=(64, 128, 128, 128, 128, 256, 256, 256, 256, 512, 512, 512, 512)			,     lowercase	:       Optional[int]=(5, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1)			,     lowercase	:       Any=(10, 3, 1, 3, 1, 3, 1, 3, 1, 2, 1, 2, 1)			,     lowercase	:       List[str]=False			,     lowercase	:       Tuple=128			,     lowercase	:       int=16			,     lowercase	:       Union[str, Any]=True			,     lowercase	:       List[str]=0.05			,     lowercase	:       Optional[int]=10			,     lowercase	:       Any=2			,     lowercase	:       Optional[Any]=0.0			,     lowercase	:       Optional[Any]=10			,     lowercase	:       int=0			,     lowercase	:       Optional[int]="mean"			,     lowercase	:       List[Any]=False			,     lowercase	:       str=False			,     lowercase	:       int=256			,     lowercase	:       str=0			,     lowercase	:       List[Any]=1			,     lowercase	:       List[Any]=2			,     **lowercase	:       List[Any]			,     )						->					Optional[Any]:
									"""simple docstring"""







									super().__init__(**lowercase			,     pad_token_id=lowercase			,     bos_token_id=lowercase			,     eos_token_id=lowercase   )
									__lowercase					=				hidden_size
									__lowercase					=				feat_extract_norm
									__lowercase					=				feat_extract_activation
									__lowercase					=				list(lowercase   )
									__lowercase					=				list(lowercase   )
									__lowercase					=				list(lowercase   )
									__lowercase					=				conv_bias
									__lowercase					=				num_conv_pos_embeddings
									__lowercase					=				num_conv_pos_embedding_groups
									__lowercase					=				len(self.conv_dim   )
									__lowercase					=				num_hidden_layers
									__lowercase					=				intermediate_size
									__lowercase					=				squeeze_factor
									__lowercase					=				hidden_act
									__lowercase					=				num_attention_heads
									__lowercase					=				hidden_dropout
									__lowercase					=				attention_dropout
									__lowercase					=				activation_dropout
									__lowercase					=				feat_proj_dropout
									__lowercase					=				final_dropout
									__lowercase					=				layerdrop
									__lowercase					=				layer_norm_eps
									__lowercase					=				initializer_range
									__lowercase					=				vocab_size

									if (
									    (len(self.conv_stride   ) != self.num_feat_extract_layers)
									    or (len(self.conv_kernel   ) != self.num_feat_extract_layers)
									    or (len(self.conv_dim   ) != self.num_feat_extract_layers)
									):
															raise ValueError(
															    """Configuration for convolutional layers is incorrect."""
															    """It is required that `len(config.conv_dim)` == `len(config.conv_stride)` == `len(config.conv_kernel)`,"""
															    F"but is `len(config.conv_dim) = {len(self.conv_dim   )}`, `len(config.conv_stride)"
															    F"= {len(self.conv_stride   )}`, `len(config.conv_kernel) = {len(self.conv_kernel   )}`."   )

									# fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779
									__lowercase					=				apply_spec_augment
									__lowercase					=				mask_time_prob
									__lowercase					=				mask_time_length
									__lowercase					=				mask_time_min_masks
									__lowercase					=				mask_feature_prob
									__lowercase					=				mask_feature_length
									__lowercase					=				mask_feature_min_masks

									# ctc loss
									__lowercase					=				ctc_loss_reduction
									__lowercase					=				ctc_zero_infinity

									# sequence classification
									__lowercase					=				use_weighted_layer_sum
									__lowercase					=				classifier_proj_size







			@property
			def 		snake_case__			(				self	:       Dict   )						->					str:
									"""simple docstring"""







									return functools.reduce(operator.mul			,     self.conv_stride			,     1   )





