



from math import ceil
from typing import List, Optional, Union

import numpy as np

from ...audio_utils import mel_filter_bank, spectrogram, window_function
from ...feature_extraction_sequence_utils import BatchFeature, SequenceFeatureExtractor
from ...utils import TensorType, logging


lowerCAmelCase__		:					Any							=logging.get_logger(__name__)

class        UpperCAmelCase_  (       UpperCamelCase_	):

						'''simple docstring'''




						UpperCamelCase__      :			Tuple      =  ['''audio_values''', '''audio_mask''']






						def __init__(   self				,							_A=2_048				,							_A=1				,							_A=[16, 16]				,							_A=128				,							_A=44_100				,							_A=86				,							_A=2_048				,							_A=0.0				,							**_A				,							):







													'''simple docstring'''

													super().__init__(
													    feature_size=_A				,							sampling_rate=_A				,							padding_value=_A				,							**_A				,							)

													__SCREAMING_SNAKE_CASE									= spectrogram_length
													__SCREAMING_SNAKE_CASE									= num_channels
													__SCREAMING_SNAKE_CASE									= patch_size
													__SCREAMING_SNAKE_CASE									= feature_size // self.patch_size[1]
													__SCREAMING_SNAKE_CASE									= n_fft
													__SCREAMING_SNAKE_CASE									= sampling_rate // hop_length_to_sampling_rate
													__SCREAMING_SNAKE_CASE									= sampling_rate
													__SCREAMING_SNAKE_CASE									= padding_value
													__SCREAMING_SNAKE_CASE									= mel_filter_bank(
													    num_frequency_bins=1 + n_fft // 2				,							num_mel_filters=_A				,							min_frequency=0.0				,							max_frequency=2_2_0_5_0.0				,							sampling_rate=_A				,							norm='slaney'				,							mel_scale='slaney'				,							).T






						def 	_A     (   self				,							_A		):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE									= spectrogram(
													    _A				,							window_function(self.n_fft				,							'hann'		)				,							frame_length=self.n_fft				,							hop_length=self.hop_length				,							power=2.0				,							mel_filters=self.mel_filters.T				,							log_mel='dB'				,							db_range=8_0.0				,							)
													__SCREAMING_SNAKE_CASE									= log_spec[:, :-1]
													__SCREAMING_SNAKE_CASE									= log_spec - 2_0.0
													__SCREAMING_SNAKE_CASE									= np.clip(log_spec / 4_0.0				,							-2.0				,							0.0		) + 1.0
													return log_spec






						def __call__(   self				,							_A				,							_A = None				,							_A = True				,							_A = None				,							_A = False				,							_A = False				,							**_A				,							):







													'''simple docstring'''

													if sampling_rate is not None:
																				if sampling_rate != self.sampling_rate:
																											raise ValueError(
																											    'This feature extractor is set to support sampling rate'
																											    f""" of {self.sampling_rate}. Please make sure that the provided `raw_speech` input was sampled"""
																											    f""" with {self.sampling_rate} and not {sampling_rate}."""		)
													else:
																				logger.warning(
																				    'It is strongly recommended to pass the `sampling_rate` argument to this function. '
																				    'Failing to do so can result in silent errors that might be hard to debug.'		)

													__SCREAMING_SNAKE_CASE									= isinstance(_A				,							np.ndarray		) and len(raw_speech.shape		) > 1
													if is_batched_numpy and len(raw_speech.shape		) > 2:
																				raise ValueError(f"""Only mono-channel audio is supported for input to {self}"""		)
													__SCREAMING_SNAKE_CASE									= is_batched_numpy or (
													    isinstance(_A				,							(list, tuple)		) and (isinstance(raw_speech[0]				,							(np.ndarray, tuple, list)		))
													)
													if is_batched:
																				__SCREAMING_SNAKE_CASE									= [np.asarray([speech]				,							dtype=np.floataa		).T for speech in raw_speech]
													elif not is_batched and not isinstance(_A				,							np.ndarray		):
																				__SCREAMING_SNAKE_CASE									= np.asarray(_A				,							dtype=np.floataa		)
													elif isinstance(_A				,							np.ndarray		) and raw_speech.dtype is np.dtype(np.floataa		):
																				__SCREAMING_SNAKE_CASE									= raw_speech.astype(np.floataa		)
													# always return batch
													if not is_batched:
																				__SCREAMING_SNAKE_CASE									= [np.asarray([raw_speech]		).T]

													# Convert audio signals to log mel spectrograms, truncate by time axis
													__SCREAMING_SNAKE_CASE									= [
													    self._np_extract_fbank_features(waveform.squeeze()		).T[: self.spectrogram_length] for waveform in raw_speech
													]
													if isinstance(audio_features[0]				,							_A		):
																				__SCREAMING_SNAKE_CASE									= [np.asarray(_A				,							dtype=np.floataa		) for feature in audio_features]

													# Create audio attention mask
													__SCREAMING_SNAKE_CASE									= max(
													    [ceil(feature.shape[0] / self.patch_size[0]		) * self.freq_len for feature in audio_features]		)  # The maximum number of audio patches in a batch
													if return_attention_mask:
																				__SCREAMING_SNAKE_CASE									= [
																				    (ceil(feature.shape[0] / self.patch_size[0]		) * self.freq_len) * [1]
																				    + (max_patch_len - ceil(feature.shape[0] / self.patch_size[0]		) * self.freq_len) * [0]
																				    for feature in audio_features
																				]
																				__SCREAMING_SNAKE_CASE									= np.array(_A		).astype(np.floataa		)

													# convert into correct format for padding
													__SCREAMING_SNAKE_CASE									= max_patch_len // self.freq_len * self.patch_size[0]  # The maximum audio size in a batch
													__SCREAMING_SNAKE_CASE									= np.ones([len(_A		), 1, max_time_len, self.feature_size]		).astype(np.floataa		)
													__SCREAMING_SNAKE_CASE									= padded_audio_features * self.padding_value
													for i in range(len(_A		)		):
																				__SCREAMING_SNAKE_CASE									= audio_features[i]
																				__SCREAMING_SNAKE_CASE									= feature

													# return as BatchFeature
													if return_attention_mask:
																				__SCREAMING_SNAKE_CASE									= {'audio_values': padded_audio_features, 'audio_mask': audio_mask}
													else:
																				__SCREAMING_SNAKE_CASE									= {'audio_values': padded_audio_features}

													__SCREAMING_SNAKE_CASE									= BatchFeature(data=_A				,							tensor_type=_A		)
													return encoded_inputs











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        UpperCAmelCase_  (       UpperCamelCase_	):

						'''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		)
													__SCREAMING_SNAKE_CASE									= Sql(
													    cache_dir=_A				,							features=_A				,							sql=_A				,							con=_A				,							**_A				,							)






						def 	_A     (   self		):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE									= None
													__SCREAMING_SNAKE_CASE									= None
													__SCREAMING_SNAKE_CASE									= None
													__SCREAMING_SNAKE_CASE									= None

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

													# Build dataset for splits
													__SCREAMING_SNAKE_CASE									= self.builder.as_dataset(
													    split='train'				,							verification_mode=_A				,							in_memory=self.keep_in_memory		)
													return dataset







class        UpperCAmelCase_  :

						'''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."""		)

													__SCREAMING_SNAKE_CASE									= dataset
													__SCREAMING_SNAKE_CASE									= name
													__SCREAMING_SNAKE_CASE									= con
													__SCREAMING_SNAKE_CASE									= batch_size if batch_size else config.DEFAULT_MAX_BATCH_SIZE
													__SCREAMING_SNAKE_CASE									= num_proc
													__SCREAMING_SNAKE_CASE									= to_sql_kwargs






						def 	_A     (   self		):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE									= self.to_sql_kwargs.pop('sql'				,							_A		)
													__SCREAMING_SNAKE_CASE									= self.to_sql_kwargs.pop('con'				,							_A		)
													__SCREAMING_SNAKE_CASE									= self.to_sql_kwargs.pop('index'				,							_A		)

													__SCREAMING_SNAKE_CASE									= self._write(index=_A				,							**self.to_sql_kwargs		)
													return written






						def 	_A     (   self				,							_A		):







													'''simple docstring'''

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






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







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE									= 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:
																				__SCREAMING_SNAKE_CASE							,	__SCREAMING_SNAKE_CASE									= 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






