






'''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 							_UpperCAmelCase    (		unittest.TestCase      ):
    __lowerCamelCase: Union[str, Any]									=     MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING
    __lowerCamelCase: Optional[Any]									=     TF_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING




    def 					lowerCAmelCase__					(      self	:							Union[str, Any]	,  a	:							Any	,  a	:							str	,  a	:							Optional[Any]      ):






       '''simple docstring'''






       lowercase_						:     int							     =  AudioClassificationPipeline(model=a	,  feature_extractor=a      )

       # test with a raw waveform
       lowercase_						:     Optional[int]							     =  np.zeros((3_4_0_0_0,)      )
       lowercase_						:     Dict							     =  np.zeros((1_4_0_0_0,)      )
       return audio_classifier, [audioa, audio]




    def 					lowerCAmelCase__					(      self	:							Optional[int]	,  a	:							List[Any]	,  a	:							Any      ):






       '''simple docstring'''






       lowercase_     ,		lowercase_						:     Optional[Any]							     =  examples
       lowercase_						:     Any							     =  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      )},
           ]	,  )
       lowercase_						:     str							     =  audio_classifier(a	,  top_k=1      )
       self.assertEqual(
           a	,  [
               {"score": ANY(a      ), "label": ANY(a      )},
           ]	,  )

       self.run_torchaudio(a      )




    @require_torchaudio
    def 					lowerCAmelCase__					(      self	:							List[Any]	,  a	:							Tuple      ):






       '''simple docstring'''






       import datasets

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




    @require_torch
    def 					lowerCAmelCase__					(      self	:							int      ):






       '''simple docstring'''






       lowercase_						:     str							     =  "anton-l/wav2vec2-random-tiny-classifier"

       lowercase_						:     Tuple							     =  pipeline("audio-classification"	,  model=a      )

       lowercase_						:     str							     =  np.ones((8_0_0_0,)      )
       lowercase_						:     List[str]							     =  audio_classifier(a	,  top_k=4      )

       lowercase_						:     Union[str, Any]							     =  [
           {"score": 0.0842, "label": "no"},
           {"score": 0.0838, "label": "up"},
           {"score": 0.0837, "label": "go"},
           {"score": 0.0834, "label": "right"},
       ]
       lowercase_						:     Optional[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(a	,  decimals=4      )	,  [EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2]      )

       lowercase_						:     Optional[Any]							     =  {"array": np.ones((8_0_0_0,)      ), "sampling_rate": audio_classifier.feature_extractor.sampling_rate}
       lowercase_						:     Optional[Any]							     =  audio_classifier(a	,  top_k=4      )
       self.assertIn(nested_simplify(a	,  decimals=4      )	,  [EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2]      )




    @require_torch
    @slow
    def 					lowerCAmelCase__					(      self	:							Optional[Any]      ):






       '''simple docstring'''






       import datasets

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

       lowercase_						:     Tuple							     =  pipeline("audio-classification"	,  model=a      )
       lowercase_						:     List[str]							     =  datasets.load_dataset("anton-l/superb_dummy"	,  "ks"	,  split="test"      )

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






       '''simple docstring'''






       pass












'''simple docstring'''
import unittest

import numpy as np

from diffusers import OnnxStableDiffusionInpaintPipelineLegacy
from diffusers.utils.testing_utils import (
    is_onnx_available,
    load_image,
    load_numpy,
    nightly,
    require_onnxruntime,
    require_torch_gpu,
)


if is_onnx_available():
 import onnxruntime as ort


@nightly
@require_onnxruntime
@require_torch_gpu
class 							_UpperCAmelCase    (		unittest.TestCase      ):




    @property
    def 					lowerCAmelCase__					(      self	:							Union[str, Any]      ):






       '''simple docstring'''






       return (
           "CUDAExecutionProvider",
           {
               "gpu_mem_limit": "15000000000",  # 15GB
               "arena_extend_strategy": "kSameAsRequested",
           },
       )




    @property
    def 					lowerCAmelCase__					(      self	:							List[Any]      ):






       '''simple docstring'''






       lowercase_						:     Union[str, Any]							     =  ort.SessionOptions()
       lowercase_						:     Optional[int]							     =  False
       return options





    def 					lowerCAmelCase__					(      self	:							str      ):






       '''simple docstring'''






       lowercase_						:     List[Any]							     =  load_image(
           "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
           "/in_paint/overture-creations-5sI6fQgYIuo.png"      )
       lowercase_						:     Tuple							     =  load_image(
           "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
           "/in_paint/overture-creations-5sI6fQgYIuo_mask.png"      )
       lowercase_						:     List[str]							     =  load_numpy(
           "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
           "/in_paint/red_cat_sitting_on_a_park_bench_onnx.npy"      )

       # using the PNDM scheduler by default
       lowercase_						:     List[Any]							     =  OnnxStableDiffusionInpaintPipelineLegacy.from_pretrained(
           "CompVis/stable-diffusion-v1-4"	,  revision="onnx"	,  safety_checker=a	,  feature_extractor=a	,  provider=self.gpu_provider	,  sess_options=self.gpu_options	,  )
       pipe.set_progress_bar_config(disable=a      )

       lowercase_						:     List[str]							     =  "A red cat sitting on a park bench"

       lowercase_						:     int							     =  np.random.RandomState(0      )
       lowercase_						:     int							     =  pipe(
           prompt=a	,  image=a	,  mask_image=a	,  strength=0.75	,  guidance_scale=7.5	,  num_inference_steps=1_5	,  generator=a	,  output_type="np"	,  )

       lowercase_						:     Tuple							     =  output.images[0]

       assert image.shape == (5_1_2, 5_1_2, 3)
       assert np.abs(expected_image - image      ).max() < 1e-2




