


'''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      ):
      __UpperCamelCase       :  Tuple	       =							MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING
      __UpperCamelCase       :  Any	       =							TF_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING






      def __UpperCAmelCase       ( self			:Dict     ,						SCREAMING_SNAKE_CASE			:int     ,						SCREAMING_SNAKE_CASE			:int     ,						SCREAMING_SNAKE_CASE			:Dict	)       ->					Any:




             '''simple docstring'''
             _a    : Optional[Any]     					=AudioClassificationPipeline(model=SCREAMING_SNAKE_CASE     ,						feature_extractor=SCREAMING_SNAKE_CASE	)

             # test with a raw waveform
             _a    : List[Any]     					=np.zeros((3_4_0_0_0,)	)
             _a    : Any     					=np.zeros((1_4_0_0_0,)	)
             return audio_classifier, [audioa, audio]






      def __UpperCAmelCase       ( self			:Optional[Any]     ,						SCREAMING_SNAKE_CASE			:Union[str, Any]     ,						SCREAMING_SNAKE_CASE			:Union[str, Any]	)       ->					Optional[Any]:




             '''simple docstring'''
             _a					,    _a    : List[Any]     					=examples
             _a    : Dict     					=audio_classifier(SCREAMING_SNAKE_CASE	)
             # by default a model is initialized with num_labels=2
             self.assertEqual(
                 SCREAMING_SNAKE_CASE     ,						[
                     {"""score""": ANY(SCREAMING_SNAKE_CASE	), """label""": ANY(SCREAMING_SNAKE_CASE	)},
                     {"""score""": ANY(SCREAMING_SNAKE_CASE	), """label""": ANY(SCREAMING_SNAKE_CASE	)},
                 ]     ,						)
             _a    : List[str]     					=audio_classifier(SCREAMING_SNAKE_CASE     ,						top_k=1	)
             self.assertEqual(
                 SCREAMING_SNAKE_CASE     ,						[
                     {"""score""": ANY(SCREAMING_SNAKE_CASE	), """label""": ANY(SCREAMING_SNAKE_CASE	)},
                 ]     ,						)

             self.run_torchaudio(SCREAMING_SNAKE_CASE	)






      @require_torchaudio
      def __UpperCAmelCase       ( self			:Union[str, Any]     ,						SCREAMING_SNAKE_CASE			:Optional[int]	)       ->					Optional[Any]:




             '''simple docstring'''
             import datasets

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






      @require_torch
      def __UpperCAmelCase       ( self			:Tuple	)       ->					List[Any]:




             '''simple docstring'''
             _a    : Tuple     					="""anton-l/wav2vec2-random-tiny-classifier"""

             _a    : str     					=pipeline("""audio-classification"""     ,						model=SCREAMING_SNAKE_CASE	)

             _a    : Union[str, Any]     					=np.ones((8_0_0_0,)	)
             _a    : List[str]     					=audio_classifier(SCREAMING_SNAKE_CASE     ,						top_k=4	)

             _a    : Optional[Any]     					=[
                 {"""score""": 0.0_842, """label""": """no"""},
                 {"""score""": 0.0_838, """label""": """up"""},
                 {"""score""": 0.0_837, """label""": """go"""},
                 {"""score""": 0.0_834, """label""": """right"""},
             ]
             _a    : str     					=[
                 {"""score""": 0.0_845, """label""": """stop"""},
                 {"""score""": 0.0_844, """label""": """on"""},
                 {"""score""": 0.0_841, """label""": """right"""},
                 {"""score""": 0.0_834, """label""": """left"""},
             ]
             self.assertIn(nested_simplify(SCREAMING_SNAKE_CASE     ,						decimals=4	)     ,						[EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2]	)

             _a    : str     					={"""array""": np.ones((8_0_0_0,)	), """sampling_rate""": audio_classifier.feature_extractor.sampling_rate}
             _a    : int     					=audio_classifier(SCREAMING_SNAKE_CASE     ,						top_k=4	)
             self.assertIn(nested_simplify(SCREAMING_SNAKE_CASE     ,						decimals=4	)     ,						[EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2]	)






      @require_torch
      @slow
      def __UpperCAmelCase       ( self			:Any	)       ->					List[str]:




             '''simple docstring'''
             import datasets

             _a    : Union[str, Any]     					="""superb/wav2vec2-base-superb-ks"""

             _a    : int     					=pipeline("""audio-classification"""     ,						model=SCREAMING_SNAKE_CASE	)
             _a    : Optional[Any]     					=datasets.load_dataset("""anton-l/superb_dummy"""     ,						"""ks"""     ,						split="""test"""	)

             _a    : List[str]     					=np.array(dataset[3]["""speech"""]     ,						dtype=np.floataa	)
             _a    : Union[str, Any]     					=audio_classifier(SCREAMING_SNAKE_CASE     ,						top_k=4	)
             self.assertEqual(
                 nested_simplify(SCREAMING_SNAKE_CASE     ,						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			:Any	)       ->					Dict:




             '''simple docstring'''
             pass






'''simple docstring'''







from datetime import datetime

import requests
from bsa import BeautifulSoup

if __name__ == "__main__":
   A__:    Union[str, Any]					    =       input('''Enter image url: ''').strip()
   print(F"Downloading image from {url} ...")
   A__:    Tuple					    =       BeautifulSoup(requests.get(url).content, '''html.parser''')
   # The image URL is in the content field of the first meta tag with property og:image
   A__:    Union[str, Any]					    =       soup.find('''meta''', {'''property''': '''og:image'''})['''content''']
   A__:    List[Any]					    =       requests.get(image_url).content
   A__:    List[str]					    =       F"{datetime.now():%Y-%m-%d_%H:%M:%S}.jpg"
   with open(file_name, '''wb''') as fp:
      fp.write(image_data)
   print(F"Done. Image saved to disk as {file_name}.")


