




'''simple docstring'''





import re
import tempfile
import unittest
from pathlib import Path

from datasets.utils.metadata import DatasetMetadata





def UpperCAmelCase    (       UpperCAmelCase_						):
    _a				:							Any							       =      min(re.search("""^ +"""   ,	UpperCAmelCase_						).end() if t.startswith(""" """						) else 0 for t in string.splitlines()						)
    return "\n".join([line[indent_level:] for line in string.splitlines() if indent_level < len(UpperCAmelCase_						)]						)


a		:	int       =      '''\
---
language:
- zh
- en
task_ids:
- sentiment-classification
---
# Begin of markdown

Some cool dataset card
'''

a		:	List[str]       =      '''\
---
---
# Begin of markdown

Some cool dataset card
'''


a		:	Dict       =      '''\
# Begin of markdown

Some cool dataset card
'''





class      UpperCamelCase       (	unittest.TestCase       ):



  def     a_							(			self ):







      '''simple docstring'''


      with tempfile.TemporaryDirectory() as tmp_dir:
          _a				:							str							       =      Path(__lowerCAmelCase ) / """README.md"""
          with open(__lowerCAmelCase		,     """w+""" ) as readme_file:
              readme_file.write(__lowerCAmelCase )
          _a				:							Optional[int]							       =      DatasetMetadata.from_readme(__lowerCAmelCase )
          self.assertDictEqual(__lowerCAmelCase		,     {"""language""": ["""zh""", """en"""], """task_ids""": ["""sentiment-classification"""]} )

          with open(__lowerCAmelCase		,     """w+""" ) as readme_file:
              readme_file.write(__lowerCAmelCase )
          _a				:							Tuple							       =      DatasetMetadata.from_readme(__lowerCAmelCase )
          self.assertDictEqual(__lowerCAmelCase		,     {} )

          with open(__lowerCAmelCase		,     """w+""" ) as readme_file:
              readme_file.write(__lowerCAmelCase )
          _a				:							Any							       =      DatasetMetadata.from_readme(__lowerCAmelCase )
          self.assertEqual(__lowerCAmelCase		,     {} )



  def     a_							(			self ):







      '''simple docstring'''


      _a				:							Union[str, Any]							       =      _dedent(
          """\
            annotations_creators:
            - found
            language_creators:
            - found
            language:
            - en
            license:
            - unknown
            multilinguality:
            - monolingual
            pretty_name: Test Dataset
            size_categories:
            - 10K<n<100K
            source_datasets:
            - extended|other-yahoo-webscope-l6
            task_categories:
            - question-answering
            task_ids:
            - open-domain-qa
            """ )
      assert DatasetMetadata.from_yaml_string(__lowerCAmelCase )

      _a				:							Optional[int]							       =      _dedent(
          """\
            annotations_creators:
            - found
            language:
            - en
            license:
            - unknown
            multilinguality:
            - monolingual
            pretty_name: Test Dataset
            size_categories:
            - 10K<n<100K
            source_datasets:
            - extended|other-yahoo-webscope-l6
            task_categories:
            - question-answering
            task_ids:
            - open-domain-qa
            task_ids:
            - open-domain-qa
            """ )
      with self.assertRaises(__lowerCAmelCase ):
          DatasetMetadata.from_yaml_string(__lowerCAmelCase )

      _a				:							Optional[Any]							       =      _dedent(
          """\
            annotations_creators:
            - found
            language_creators:
            - found
            language:
            - en
            license:
            - unknown
            multilinguality:
            - monolingual
            pretty_name: Test Dataset
            size_categories:
            - 10K<n<100K
            source_datasets:
            - extended|other-yahoo-webscope-l6
            task_categories:
            - text-classification
            task_ids:
            - multi-class-classification
            paperswithcode_id:
            - squad
            configs:
            - en
            train-eval-index:
            - config: en
              task: text-classification
              task_id: multi_class_classification
              splits:
                train_split: train
                eval_split: test
              col_mapping:
                text: text
                label: target
              metrics:
                - type: accuracy
                  name: Accuracy
            extra_gated_prompt: |
              By clicking on “Access repository” below, you also agree to ImageNet Terms of Access:
              [RESEARCHER_FULLNAME] (the \"Researcher\") has requested permission to use the ImageNet database (the \"Database\") at Princeton University and Stanford University. In exchange for such permission, Researcher hereby agrees to the following terms and conditions:
              1. Researcher shall use the Database only for non-commercial research and educational purposes.
            extra_gated_fields:
              Company: text
              Country: text
              I agree to use this model for non-commerical use ONLY: checkbox
            """ )
      assert DatasetMetadata.from_yaml_string(__lowerCAmelCase )





from typing import List, Optional, Union

import numpy as np
import torch
import torchaudio.compliance.kaldi as ta_kaldi

from ...feature_extraction_sequence_utils import SequenceFeatureExtractor
from ...feature_extraction_utils import BatchFeature
from ...utils import TensorType, logging


lowercase_									=    logging.get_logger(__name__)


class 					A		(				_UpperCAmelCase					):







    """simple docstring"""





    lowerCamelCase						      =				['input_values', 'attention_mask']


    def __init__( self					:			Tuple,lowercase_					:			Optional[Any]=1,lowercase_					:			Union[str, Any]=1_6_0_0_0,lowercase_					:			str=1_2_8,lowercase_					:			Optional[Any]=1_0_2_4,lowercase_					:			Union[str, Any]=0.0,lowercase_					:			Any=True,lowercase_					:			List[Any]=-4.2_677_393,lowercase_					:			Union[str, Any]=4.5_689_974,lowercase_					:			Dict=False,**lowercase_					:			Dict,)->       int:





        '''simple docstring'''







        super().__init__(feature_size=lowercase_,sampling_rate=lowercase_,padding_value=lowercase_,**lowercase_						)
        A__        =  num_mel_bins
        A__        =  max_length
        A__        =  do_normalize
        A__        =  mean
        A__        =  std
        A__        =  return_attention_mask


    def 		snake_case__	( self					:			Any,lowercase_					:			np.ndarray,lowercase_					:			int,)->       np.ndarray:





        '''simple docstring'''







        A__        =  torch.from_numpy(lowercase_						).unsqueeze(0						)
        A__        =  ta_kaldi.fbank(
            lowercase_,htk_compat=lowercase_,sample_frequency=self.sampling_rate,use_energy=lowercase_,window_type='hanning',num_mel_bins=self.num_mel_bins,dither=0.0,frame_shift=1_0,)

        A__        =  fbank.shape[0]
        A__        =  max_length - n_frames

        # pad or truncate, depending on difference
        if difference > 0:
            A__        =  torch.nn.ZeroPadad((0, 0, 0, difference)						)
            A__        =  pad_module(lowercase_						)
        elif difference < 0:
            A__        =  fbank[0:max_length, :]

        A__        =  fbank.numpy()

        return fbank


    def 		snake_case__	( self					:			int,lowercase_					:			np.ndarray						)->       np.ndarray:





        '''simple docstring'''







        return (input_values - (self.mean)) / (self.std * 2)


    def __call__( self					:			str,lowercase_					:			Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]],lowercase_					:			Optional[int] = None,lowercase_					:			Optional[Union[str, TensorType]] = None,**lowercase_					:			str,)->       BatchFeature:





        '''simple docstring'''







        if sampling_rate is not None:
            if sampling_rate != self.sampling_rate:
                raise ValueError(
                    F'The model corresponding to this feature extractor: {self} was trained using a sampling rate of'
                    F' {self.sampling_rate}. Please make sure that the provided `raw_speech` input was sampled with'
                    F' {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.'						)

        A__        =  isinstance(lowercase_,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}'						)
        A__        =  is_batched_numpy or (
            isinstance(lowercase_,(list, tuple)						) and (isinstance(raw_speech[0],(np.ndarray, tuple, list)						))
        )

        if is_batched:
            A__        =  [np.asarray(lowercase_,dtype=np.floataa						) for speech in raw_speech]
        elif not is_batched and not isinstance(lowercase_,np.ndarray						):
            A__        =  np.asarray(lowercase_,dtype=np.floataa						)
        elif isinstance(lowercase_,np.ndarray						) and raw_speech.dtype is np.dtype(np.floataa						):
            A__        =  raw_speech.astype(np.floataa						)

        # always return batch
        if not is_batched:
            A__        =  [raw_speech]

        # extract fbank features and pad/truncate to max_length
        A__        =  [self._extract_fbank_features(lowercase_,max_length=self.max_length						) for waveform in raw_speech]

        # convert into BatchFeature
        A__        =  BatchFeature({'input_values': features}						)

        # make sure list is in array format
        A__        =  padded_inputs.get('input_values'						)
        if isinstance(input_values[0],lowercase_						):
            A__        =  [np.asarray(lowercase_,dtype=np.floataa						) for feature in input_values]

        # normalization
        if self.do_normalize:
            A__        =  [self.normalize(lowercase_						) for feature in input_values]

        if return_tensors is not None:
            A__        =  padded_inputs.convert_to_tensors(lowercase_						)

        return padded_inputs

