

'''simple docstring'''

import unittest

import numpy as np

from transformers import MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING, TF_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING
from transformers.pipelines import AudioClassificationPipeline, pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    require_torch,
    require_torchaudio,
    slow,
)

from .test_pipelines_common import ANY




@is_pipeline_test
class 	A_			(    unittest.TestCase							):






     '''simple docstring'''



     UpperCAmelCase_						:     Any  		=  MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING
     UpperCAmelCase_						:     Union[str, Any]  		=  TF_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING


     def        UpperCAmelCase_	(		self			:					Dict					,      lowercase_			:					Any					,      lowercase_			:					Union[str, Any]					,      lowercase_			:					Optional[int]	)						->     Optional[Any]:
      UpperCAmelCase	:     Optional[Any]						 =			AudioClassificationPipeline(model=lowercase_					,      feature_extractor=lowercase_	)

      # test with a raw waveform
      UpperCAmelCase	:     str						 =			np.zeros((34_000,)	)
      UpperCAmelCase	:     List[Any]						 =			np.zeros((14_000,)	)
      return audio_classifier, [audioa, audio]


     def        UpperCAmelCase_	(		self			:					Any					,      lowercase_			:					List[Any]					,      lowercase_			:					List[str]	)						->     Tuple:
      UpperCAmelCase ,			UpperCAmelCase	:     Union[str, Any]						 =			examples
      UpperCAmelCase	:     Union[str, Any]						 =			audio_classifier(lowercase_	)
      # by default a model is initialized with num_labels=2
      self.assertEqual(
          lowercase_					,      [
              {'score': ANY(lowercase_	), 'label': ANY(lowercase_	)},
              {'score': ANY(lowercase_	), 'label': ANY(lowercase_	)},
          ]					,      )
      UpperCAmelCase	:     Union[str, Any]						 =			audio_classifier(lowercase_					,      top_k=1	)
      self.assertEqual(
          lowercase_					,      [
              {'score': ANY(lowercase_	), 'label': ANY(lowercase_	)},
          ]					,      )

      self.run_torchaudio(lowercase_	)


     @require_torchaudio
     def        UpperCAmelCase_	(		self			:					int					,      lowercase_			:					Any	)						->     Dict:
      import datasets

      # test with a local file
      UpperCAmelCase	:     str						 =			datasets.load_dataset('hf-internal-testing/librispeech_asr_dummy'					,      'clean'					,      split='validation'	)
      UpperCAmelCase	:     Union[str, Any]						 =			dataset[0]['audio']['array']
      UpperCAmelCase	:     List[str]						 =			audio_classifier(lowercase_	)
      self.assertEqual(
          lowercase_					,      [
              {'score': ANY(lowercase_	), 'label': ANY(lowercase_	)},
              {'score': ANY(lowercase_	), 'label': ANY(lowercase_	)},
          ]					,      )


     @require_torch
     def        UpperCAmelCase_	(		self			:					List[Any]	)						->     List[str]:
      UpperCAmelCase	:     Any						 =			'anton-l/wav2vec2-random-tiny-classifier'

      UpperCAmelCase	:     Any						 =			pipeline('audio-classification'					,      model=lowercase_	)

      UpperCAmelCase	:     Dict						 =			np.ones((8_000,)	)
      UpperCAmelCase	:     List[Any]						 =			audio_classifier(lowercase_					,      top_k=4	)

      UpperCAmelCase	:     Union[str, Any]						 =			[
          {'score': 0.0842, 'label': 'no'},
          {'score': 0.0838, 'label': 'up'},
          {'score': 0.0837, 'label': 'go'},
          {'score': 0.0834, 'label': 'right'},
      ]
      UpperCAmelCase	:     Any						 =			[
          {'score': 0.0845, 'label': 'stop'},
          {'score': 0.0844, 'label': 'on'},
          {'score': 0.0841, 'label': 'right'},
          {'score': 0.0834, 'label': 'left'},
      ]
      self.assertIn(nested_simplify(lowercase_					,      decimals=4	)					,      [EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2]	)

      UpperCAmelCase	:     int						 =			{'array': np.ones((8_000,)	), 'sampling_rate': audio_classifier.feature_extractor.sampling_rate}
      UpperCAmelCase	:     List[str]						 =			audio_classifier(lowercase_					,      top_k=4	)
      self.assertIn(nested_simplify(lowercase_					,      decimals=4	)					,      [EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2]	)


     @require_torch
     @slow
     def        UpperCAmelCase_	(		self			:					str	)						->     List[str]:
      import datasets

      UpperCAmelCase	:     Union[str, Any]						 =			'superb/wav2vec2-base-superb-ks'

      UpperCAmelCase	:     Optional[int]						 =			pipeline('audio-classification'					,      model=lowercase_	)
      UpperCAmelCase	:     Optional[int]						 =			datasets.load_dataset('anton-l/superb_dummy'					,      'ks'					,      split='test'	)

      UpperCAmelCase	:     Union[str, Any]						 =			np.array(dataset[3]['speech']					,      dtype=np.floataa	)
      UpperCAmelCase	:     Optional[int]						 =			audio_classifier(lowercase_					,      top_k=4	)
      self.assertEqual(
          nested_simplify(lowercase_					,      decimals=3	)					,      [
              {'score': 0.981, 'label': 'go'},
              {'score': 0.007, 'label': 'up'},
              {'score': 0.006, 'label': '_unknown_'},
              {'score': 0.001, 'label': 'down'},
          ]					,      )




     @require_tf
     @unittest.skip('Audio classification is not implemented for TF'	)
     def        UpperCAmelCase_	(		self			:					List[str]	)						->     Any:
      pass







'''simple docstring'''

import os
import random
import sys

from . import cryptomath_module as cryptomath
from . import rabin_miller

lowercase__         =      3




def 				UpperCamelCase(					UpperCAmelCase_     ):
 print('Generating primitive root of p'     )
 while True:
  UpperCAmelCase	:     Union[str, Any]						 =			random.randrange(3  ,	UpperCAmelCase_     )
  if pow(UpperCAmelCase_  ,	2  ,	UpperCAmelCase_     ) == 1:
   continue
  if pow(UpperCAmelCase_  ,	UpperCAmelCase_  ,	UpperCAmelCase_     ) == 1:
   continue
  return g




def 				UpperCamelCase(					UpperCAmelCase_     ):
 print('Generating prime p...'     )
 UpperCAmelCase	:     str						 =			rabin_miller.generate_large_prime(UpperCAmelCase_     )  # select large prime number.
 UpperCAmelCase	:     List[str]						 =			primitive_root(UpperCAmelCase_     )  # one primitive root on modulo p.
 UpperCAmelCase	:     List[Any]						 =			random.randrange(3  ,	UpperCAmelCase_     )  # private_key -> have to be greater than 2 for safety.
 UpperCAmelCase	:     List[Any]						 =			cryptomath.find_mod_inverse(pow(UpperCAmelCase_  ,	UpperCAmelCase_  ,	UpperCAmelCase_     )  ,	UpperCAmelCase_     )

 UpperCAmelCase	:     Tuple						 =			(key_size, e_a, e_a, p)
 UpperCAmelCase	:     Optional[int]						 =			(key_size, d)

 return public_key, private_key




def 				UpperCamelCase(					UpperCAmelCase_  ,	UpperCAmelCase_     ):
 if os.path.exists(F"""{name}_pubkey.txt"""     ) or os.path.exists(F"""{name}_privkey.txt"""     ):
  print('\nWARNING:'     )
  print(
      F"""\"{name}_pubkey.txt\" or \"{name}_privkey.txt\" already exists. \n"""
      'Use a different name or delete these files and re-run this program.'     )
  sys.exit()

 UpperCAmelCase ,			UpperCAmelCase	:     Dict						 =			generate_key(UpperCAmelCase_     )
 print(F"""\nWriting public key to file {name}_pubkey.txt..."""     )
 with open(F"""{name}_pubkey.txt"""  ,	'w'     ) as fo:
  fo.write(F"""{public_key[0]},{public_key[1]},{public_key[2]},{public_key[3]}"""     )

 print(F"""Writing private key to file {name}_privkey.txt..."""     )
 with open(F"""{name}_privkey.txt"""  ,	'w'     ) as fo:
  fo.write(F"""{private_key[0]},{private_key[1]}"""     )




def 				UpperCamelCase(					):
 print('Making key files...'     )
 make_key_files('elgamal'  ,	20_48     )
 print('Key files generation successful'     )


if __name__ == "__main__":
 main()




