
"""simple docstring"""

import os
from datetime import datetime as dt

from github import Github


SCREAMING_SNAKE_CASE_    					=						[
    '''good first issue''',
    '''feature request''',
    '''wip''',
]




def     lowercase__						(					) ->      List[str]:






    """simple docstring"""




    UpperCAmelCase									=					Github(os.environ['GITHUB_TOKEN'] )
    UpperCAmelCase									=					g.get_repo('huggingface/accelerate' )
    UpperCAmelCase									=					repo.get_issues(state='open' )

    for issue in open_issues:
        UpperCAmelCase									=					sorted([comment for comment in issue.get_comments()] ,				key=lambda lowerCAmelCase : i.created_at ,				reverse=lowerCAmelCase )
        UpperCAmelCase									=					comments[0] if len(lowerCAmelCase ) > 0 else None
        UpperCAmelCase									=					dt.utcnow()
        UpperCAmelCase									=					(current_time - issue.updated_at).days
        UpperCAmelCase									=					(current_time - issue.created_at).days
        if (
            last_comment is not None
            and last_comment.user.login == "github-actions[bot]"
            and days_since_updated > 7
            and days_since_creation >= 30
            and not any(label.name.lower() in LABELS_TO_EXEMPT for label in issue.get_labels() )
        ):
            # Close issue since it has been 7 days of inactivity since bot mention.
            issue.edit(state='closed' )
        elif (
            days_since_updated > 23
            and days_since_creation >= 30
            and not any(label.name.lower() in LABELS_TO_EXEMPT for label in issue.get_labels() )
        ):
            # Add stale comment
            issue.create_comment(
                'This issue has been automatically marked as stale because it has not had '
                'recent activity. If you think this still needs to be addressed '
                'please comment on this thread.\n\nPlease note that issues that do not follow the '
                '[contributing guidelines](https://github.com/huggingface/accelerate/blob/main/CONTRIBUTING.md) '
                'are likely to be ignored.' )


if __name__ == "__main__":
   main()





"""simple docstring"""

import itertools
import os
import random
import tempfile
import unittest

import numpy as np

from transformers import TvltFeatureExtractor, is_datasets_available
from transformers.testing_utils import check_json_file_has_correct_format, require_torch, require_torchaudio
from transformers.utils.import_utils import is_torch_available

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


if is_torch_available():
   import torch

if is_datasets_available():
   from datasets import load_dataset

SCREAMING_SNAKE_CASE_    					=						random.Random()




def     lowercase__						(					lowerCAmelCase				:					Any ,				lowerCAmelCase				:					Tuple=1.0 ,				lowerCAmelCase				:					Tuple=None ,				lowerCAmelCase				:					List[str]=None ) ->      Dict:






    """simple docstring"""




    if rng is None:
        UpperCAmelCase									=					global_rng

    UpperCAmelCase									=					[]
    for batch_idx in range(shape[0] ):
        values.append([] )
        for _ in range(shape[1] ):
            values[-1].append(rng.random() * scale )

    return values
class      _UpperCAmelCase							(		unittest.TestCase							):




      def __init__(							self   ,       lowercase_   ,       lowercase_=7   ,       lowercase_=4_0_0   ,       lowercase_=2_0_0_0   ,       lowercase_=2_0_4_8   ,       lowercase_=1_2_8   ,       lowercase_=1   ,       lowercase_=5_1_2   ,       lowercase_=3_0   ,       lowercase_=4_4_1_0_0   ,       )						->					str:
          UpperCAmelCase									=					parent
          UpperCAmelCase									=					batch_size
          UpperCAmelCase									=					min_seq_length
          UpperCAmelCase									=					max_seq_length
          UpperCAmelCase									=					(self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
          UpperCAmelCase									=					spectrogram_length
          UpperCAmelCase									=					feature_size
          UpperCAmelCase									=					num_audio_channels
          UpperCAmelCase									=					hop_length
          UpperCAmelCase									=					chunk_length
          UpperCAmelCase									=					sampling_rate




      def     a_      (							self							)						->					str:
          return {
              "spectrogram_length": self.spectrogram_length,
              "feature_size": self.feature_size,
              "num_audio_channels": self.num_audio_channels,
              "hop_length": self.hop_length,
              "chunk_length": self.chunk_length,
              "sampling_rate": self.sampling_rate,
          }




      def     a_      (							self   ,       lowercase_=False   ,       lowercase_=False							)						->					Optional[Any]:
          def _flatten(lowercase_							):
              return list(itertools.chain(*lowercase_							)							)

          if equal_length:
              UpperCAmelCase									=					[floats_list((self.max_seq_length, self.feature_size)							) for _ in range(self.batch_size							)]
          else:
              # make sure that inputs increase in size
              UpperCAmelCase									=					[
                  floats_list((x, self.feature_size)							)
                  for x in range(self.min_seq_length   ,       self.max_seq_length   ,       self.seq_length_diff							)
              ]
          if numpify:
              UpperCAmelCase									=					[np.asarray(lowercase_							) for x in speech_inputs]
          return speech_inputs

@require_torch
@require_torchaudio
class      _UpperCAmelCase							(		SCREAMING_SNAKE_CASE_     ,			unittest.TestCase							):
      __SCREAMING_SNAKE_CASE   :							List[str]							      =	TvltFeatureExtractor




      def     a_      (							self							)						->					Optional[int]:
          UpperCAmelCase									=					TvltFeatureExtractionTester(self							)




      def     a_      (							self							)						->					Optional[Any]:
          UpperCAmelCase									=					self.feature_extraction_class(**self.feat_extract_dict							)
          self.assertTrue(hasattr(lowercase_   ,       'spectrogram_length'							)							)
          self.assertTrue(hasattr(lowercase_   ,       'feature_size'							)							)
          self.assertTrue(hasattr(lowercase_   ,       'num_audio_channels'							)							)
          self.assertTrue(hasattr(lowercase_   ,       'hop_length'							)							)
          self.assertTrue(hasattr(lowercase_   ,       'chunk_length'							)							)
          self.assertTrue(hasattr(lowercase_   ,       'sampling_rate'							)							)




      def     a_      (							self							)						->					List[Any]:
          UpperCAmelCase									=					self.feature_extraction_class(**self.feat_extract_dict							)

          with tempfile.TemporaryDirectory() as tmpdirname:
              UpperCAmelCase									=					feat_extract_first.save_pretrained(lowercase_							)[0]
              check_json_file_has_correct_format(lowercase_							)
              UpperCAmelCase									=					self.feature_extraction_class.from_pretrained(lowercase_							)

          UpperCAmelCase									=					feat_extract_first.to_dict()
          UpperCAmelCase									=					feat_extract_second.to_dict()
          UpperCAmelCase									=					dict_first.pop('mel_filters'							)
          UpperCAmelCase									=					dict_second.pop('mel_filters'							)
          self.assertTrue(np.allclose(lowercase_   ,       lowercase_							)							)
          self.assertEqual(lowercase_   ,       lowercase_							)




      def     a_      (							self							)						->					str:
          UpperCAmelCase									=					self.feature_extraction_class(**self.feat_extract_dict							)

          with tempfile.TemporaryDirectory() as tmpdirname:
              UpperCAmelCase									=					os.path.join(lowercase_   ,       'feat_extract.json'							)
              feat_extract_first.to_json_file(lowercase_							)
              UpperCAmelCase									=					self.feature_extraction_class.from_json_file(lowercase_							)

          UpperCAmelCase									=					feat_extract_first.to_dict()
          UpperCAmelCase									=					feat_extract_second.to_dict()
          UpperCAmelCase									=					dict_first.pop('mel_filters'							)
          UpperCAmelCase									=					dict_second.pop('mel_filters'							)
          self.assertTrue(np.allclose(lowercase_   ,       lowercase_							)							)
          self.assertEqual(lowercase_   ,       lowercase_							)




      def     a_      (							self							)						->					int:
          # Initialize feature_extractor
          UpperCAmelCase									=					self.feature_extraction_class(**self.feat_extract_dict							)

          # create three inputs of length 800, 1000, and 1200
          UpperCAmelCase									=					[floats_list((1, x)							)[0] for x in range(8_0_0   ,       1_4_0_0   ,       2_0_0							)]
          UpperCAmelCase									=					[np.asarray(lowercase_							) for speech_input in speech_inputs]

          # Test not batched input
          UpperCAmelCase									=					feature_extractor(np_speech_inputs[0]   ,       return_tensors='np'   ,       sampling_rate=4_4_1_0_0							).audio_values

          self.assertTrue(encoded_audios.ndim == 4							)
          self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size							)
          self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length							)
          self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels							)

          # Test batched
          UpperCAmelCase									=					feature_extractor(lowercase_   ,       return_tensors='np'   ,       sampling_rate=4_4_1_0_0							).audio_values

          self.assertTrue(encoded_audios.ndim == 4							)
          self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size							)
          self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length							)
          self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels							)

          # Test audio masking
          UpperCAmelCase									=					feature_extractor(
              lowercase_   ,       return_tensors='np'   ,       sampling_rate=4_4_1_0_0   ,       mask_audio=lowercase_							).audio_values

          self.assertTrue(encoded_audios.ndim == 4							)
          self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size							)
          self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length							)
          self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels							)

          # Test 2-D numpy arrays are batched.
          UpperCAmelCase									=					[floats_list((1, x)							)[0] for x in (8_0_0, 8_0_0, 8_0_0)]
          UpperCAmelCase									=					np.asarray(lowercase_							)
          UpperCAmelCase									=					feature_extractor(lowercase_   ,       return_tensors='np'   ,       sampling_rate=4_4_1_0_0							).audio_values
          self.assertTrue(encoded_audios.ndim == 4							)
          self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size							)
          self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length							)
          self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels							)




      def     a_      (							self   ,       lowercase_							)						->					Optional[Any]:
          UpperCAmelCase									=					load_dataset('hf-internal-testing/librispeech_asr_dummy'   ,       'clean'   ,       split='validation'							)
          # automatic decoding with librispeech
          UpperCAmelCase									=					ds.sort('id'							).select(range(lowercase_							)							)[:num_samples]['audio']

          return [x["array"] for x in speech_samples]




      def     a_      (							self							)						->					Tuple:
          UpperCAmelCase									=					self._load_datasamples(1							)
          UpperCAmelCase									=					TvltFeatureExtractor()
          UpperCAmelCase									=					feature_extractor(lowercase_   ,       return_tensors='pt'							).audio_values

          self.assertEquals(audio_values.shape   ,       (1, 1, 1_9_2, 1_2_8)							)

          UpperCAmelCase									=					torch.tensor([[-0.3_0_3_2, -0.2_7_0_8], [-0.4_4_3_4, -0.4_0_0_7]]							)
          self.assertTrue(torch.allclose(audio_values[0, 0, :2, :2]   ,       lowercase_   ,       atol=1E-4							)							)



