


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 		_SCREAMING_SNAKE_CASE     (						unittest.TestCase     ):
     __SCREAMING_SNAKE_CASE     					=    MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING
     __SCREAMING_SNAKE_CASE     					=    TF_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING


     def        __snake_case(  self							,					A_							,					A_							,					A_    ):
       _UpperCAmelCase		:							Tuple							=     AudioClassificationPipeline(model=A_							,					feature_extractor=A_    )

       # test with a raw waveform
       _UpperCAmelCase		:							Union[str, Any]							=     np.zeros((3_40_00,)    )
       _UpperCAmelCase		:							Union[str, Any]							=     np.zeros((1_40_00,)    )
       return audio_classifier, [audioa, audio]


     def        __snake_case(  self							,					A_							,					A_    ):
       _UpperCAmelCase,_UpperCAmelCase		:							int							=     examples
       _UpperCAmelCase		:							Dict							=     audio_classifier(A_    )
       # by default a model is initialized with num_labels=2
       self.assertEqual(
           A_							,					[
               {"""score""": ANY(A_    ), """label""": ANY(A_    )},
               {"""score""": ANY(A_    ), """label""": ANY(A_    )},
           ]							,					)
       _UpperCAmelCase		:							int							=     audio_classifier(A_							,					top_k=1    )
       self.assertEqual(
           A_							,					[
               {"""score""": ANY(A_    ), """label""": ANY(A_    )},
           ]							,					)

       self.run_torchaudio(A_    )


     @require_torchaudio
     def        __snake_case(  self							,					A_    ):
       import datasets

       # test with a local file
       _UpperCAmelCase		:							Any							=     datasets.load_dataset("""hf-internal-testing/librispeech_asr_dummy"""							,					"""clean"""							,					split="""validation"""    )
       _UpperCAmelCase		:							Optional[int]							=     dataset[0]["""audio"""]["""array"""]
       _UpperCAmelCase		:							Dict							=     audio_classifier(A_    )
       self.assertEqual(
           A_							,					[
               {"""score""": ANY(A_    ), """label""": ANY(A_    )},
               {"""score""": ANY(A_    ), """label""": ANY(A_    )},
           ]							,					)


     @require_torch
     def        __snake_case(  self    ):
       _UpperCAmelCase		:							Optional[Any]							=     """anton-l/wav2vec2-random-tiny-classifier"""

       _UpperCAmelCase		:							Tuple							=     pipeline("""audio-classification"""							,					model=A_    )

       _UpperCAmelCase		:							Dict							=     np.ones((80_00,)    )
       _UpperCAmelCase		:							Union[str, Any]							=     audio_classifier(A_							,					top_k=4    )

       _UpperCAmelCase		:							str							=     [
           {"""score""": 0.0_8_4_2, """label""": """no"""},
           {"""score""": 0.0_8_3_8, """label""": """up"""},
           {"""score""": 0.0_8_3_7, """label""": """go"""},
           {"""score""": 0.0_8_3_4, """label""": """right"""},
       ]
       _UpperCAmelCase		:							int							=     [
           {"""score""": 0.0_8_4_5, """label""": """stop"""},
           {"""score""": 0.0_8_4_4, """label""": """on"""},
           {"""score""": 0.0_8_4_1, """label""": """right"""},
           {"""score""": 0.0_8_3_4, """label""": """left"""},
       ]
       self.assertIn(nested_simplify(A_							,					decimals=4    )							,					[EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2]    )

       _UpperCAmelCase		:							Optional[Any]							=     {"""array""": np.ones((80_00,)    ), """sampling_rate""": audio_classifier.feature_extractor.sampling_rate}
       _UpperCAmelCase		:							Tuple							=     audio_classifier(A_							,					top_k=4    )
       self.assertIn(nested_simplify(A_							,					decimals=4    )							,					[EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2]    )


     @require_torch
     @slow
     def        __snake_case(  self    ):
       import datasets

       _UpperCAmelCase		:							int							=     """superb/wav2vec2-base-superb-ks"""

       _UpperCAmelCase		:							int							=     pipeline("""audio-classification"""							,					model=A_    )
       _UpperCAmelCase		:							Tuple							=     datasets.load_dataset("""anton-l/superb_dummy"""							,					"""ks"""							,					split="""test"""    )

       _UpperCAmelCase		:							Dict							=     np.array(dataset[3]["""speech"""]							,					dtype=np.floataa    )
       _UpperCAmelCase		:							List[Any]							=     audio_classifier(A_							,					top_k=4    )
       self.assertEqual(
           nested_simplify(A_							,					decimals=3    )							,					[
               {"""score""": 0.9_8_1, """label""": """go"""},
               {"""score""": 0.0_0_7, """label""": """up"""},
               {"""score""": 0.0_0_6, """label""": """_unknown_"""},
               {"""score""": 0.0_0_1, """label""": """down"""},
           ]							,					)




     @require_tf
     @unittest.skip("""Audio classification is not implemented for TF"""    )
     def        __snake_case(  self    ):
       pass









from PIL import Image




def    a__    (		snake_case__					:							Image						,   snake_case__					:							int     ):
  _UpperCAmelCase		:							Optional[Any]							=     (259 * (level + 255)) / (255 * (259 - level))

  def contrast(snake_case__					:							int     ) -> int:
    return int(128 + factor * (c - 128)     )

  return img.point(snake_case__     )


if __name__ == "__main__":
 # Load image
 with Image.open('image_data/lena.jpg') as img:
  # Change contrast to 170
  SCREAMING_SNAKE_CASE__					:     int       =     change_contrast(img, 170)
  cont_img.save('image_data/lena_high_contrast.png', format='png')





