


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__ :  Any						      =	MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING
     UpperCAmelCase__ :  Optional[Any]						      =	TF_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING





     def  SCREAMING_SNAKE_CASE__       (    self					:				str    ,	SCREAMING_SNAKE_CASE_					:				Any    ,	SCREAMING_SNAKE_CASE_					:				Dict    ,	SCREAMING_SNAKE_CASE_					:				Dict							):
        __lowerCamelCase:				Optional[Any]     =	AudioClassificationPipeline(model=SCREAMING_SNAKE_CASE_    ,	feature_extractor=SCREAMING_SNAKE_CASE_							)

        # test with a raw waveform
        __lowerCamelCase:				List[str]     =	np.zeros((34000,)							)
        __lowerCamelCase:				Optional[int]     =	np.zeros((14000,)							)
        return audio_classifier, [audioa, audio]





     def  SCREAMING_SNAKE_CASE__       (    self					:				Any    ,	SCREAMING_SNAKE_CASE_					:				Tuple    ,	SCREAMING_SNAKE_CASE_					:				Union[str, Any]							):
        __lowerCamelCase   ,     __lowerCamelCase:				str     =	examples
        __lowerCamelCase:				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_							)},
            ]    ,	)
        __lowerCamelCase:				Union[str, Any]     =	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  SCREAMING_SNAKE_CASE__       (    self					:				List[Any]    ,	SCREAMING_SNAKE_CASE_					:				Any							):
        import datasets

        # test with a local file
        __lowerCamelCase:				Dict     =	datasets.load_dataset("""hf-internal-testing/librispeech_asr_dummy"""    ,	"""clean"""    ,	split="""validation"""							)
        __lowerCamelCase:				Tuple     =	dataset[0]["""audio"""]["""array"""]
        __lowerCamelCase:				Optional[int]     =	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  SCREAMING_SNAKE_CASE__       (    self					:				Any							):
        __lowerCamelCase:				List[Any]     =	"""anton-l/wav2vec2-random-tiny-classifier"""

        __lowerCamelCase:				Dict     =	pipeline("""audio-classification"""    ,	model=SCREAMING_SNAKE_CASE_							)

        __lowerCamelCase:				Union[str, Any]     =	np.ones((8000,)							)
        __lowerCamelCase:				Union[str, Any]     =	audio_classifier(SCREAMING_SNAKE_CASE_    ,	top_k=4							)

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

        __lowerCamelCase:				Union[str, Any]     =	{"""array""": np.ones((8000,)							), """sampling_rate""": audio_classifier.feature_extractor.sampling_rate}
        __lowerCamelCase:				Union[str, Any]     =	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  SCREAMING_SNAKE_CASE__       (    self					:				Dict							):
        import datasets

        __lowerCamelCase:				List[str]     =	"""superb/wav2vec2-base-superb-ks"""

        __lowerCamelCase:				Dict     =	pipeline("""audio-classification"""    ,	model=SCREAMING_SNAKE_CASE_							)
        __lowerCamelCase:				int     =	datasets.load_dataset("""anton-l/superb_dummy"""    ,	"""ks"""    ,	split="""test"""							)

        __lowerCamelCase:				Union[str, Any]     =	np.array(dataset[3]["""speech"""]    ,	dtype=np.floataa							)
        __lowerCamelCase:				List[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  SCREAMING_SNAKE_CASE__       (    self					:				int							):
        pass






from manim import *



class        a					(			_UpperCAmelCase   ):





     def  SCREAMING_SNAKE_CASE__       (    self					:				int							):
        __lowerCamelCase:				int     =	Rectangle(height=0.5    ,	width=0.5							)
        __lowerCamelCase:				List[str]     =	Rectangle(height=0.25    ,	width=0.25							)
        __lowerCamelCase:				Optional[int]     =	Rectangle(height=0.46    ,	width=0.46							).set_stroke(width=0							)

        __lowerCamelCase:				str     =	[mem.copy() for i in range(6							)]
        __lowerCamelCase:				Dict     =	[mem.copy() for i in range(6							)]
        __lowerCamelCase:				Union[str, Any]     =	VGroup(*SCREAMING_SNAKE_CASE_							).arrange(SCREAMING_SNAKE_CASE_    ,	buff=0							)
        __lowerCamelCase:				Tuple     =	VGroup(*SCREAMING_SNAKE_CASE_							).arrange(SCREAMING_SNAKE_CASE_    ,	buff=0							)
        __lowerCamelCase:				Optional[int]     =	VGroup(SCREAMING_SNAKE_CASE_    ,	SCREAMING_SNAKE_CASE_							).arrange(SCREAMING_SNAKE_CASE_    ,	buff=0							)
        __lowerCamelCase:				Dict     =	Text("""CPU"""    ,	font_size=24							)
        __lowerCamelCase:				Any     =	Group(SCREAMING_SNAKE_CASE_    ,	SCREAMING_SNAKE_CASE_							).arrange(SCREAMING_SNAKE_CASE_    ,	buff=0.5    ,	aligned_edge=SCREAMING_SNAKE_CASE_							)
        cpu.move_to([-2.5, -0.5, 0]							)
        self.add(SCREAMING_SNAKE_CASE_							)

        __lowerCamelCase:				Optional[int]     =	[mem.copy() for i in range(4							)]
        __lowerCamelCase:				Optional[int]     =	VGroup(*SCREAMING_SNAKE_CASE_							).arrange(SCREAMING_SNAKE_CASE_    ,	buff=0							)
        __lowerCamelCase:				Dict     =	Text("""GPU"""    ,	font_size=24							)
        __lowerCamelCase:				Dict     =	Group(SCREAMING_SNAKE_CASE_    ,	SCREAMING_SNAKE_CASE_							).arrange(SCREAMING_SNAKE_CASE_    ,	buff=0.5    ,	aligned_edge=SCREAMING_SNAKE_CASE_							)
        gpu.move_to([-1, -1, 0]							)
        self.add(SCREAMING_SNAKE_CASE_							)

        __lowerCamelCase:				Any     =	[mem.copy() for i in range(6							)]
        __lowerCamelCase:				Dict     =	VGroup(*SCREAMING_SNAKE_CASE_							).arrange(SCREAMING_SNAKE_CASE_    ,	buff=0							)

        __lowerCamelCase:				List[Any]     =	Text("""Model"""    ,	font_size=24							)
        __lowerCamelCase:				Any     =	Group(SCREAMING_SNAKE_CASE_    ,	SCREAMING_SNAKE_CASE_							).arrange(SCREAMING_SNAKE_CASE_    ,	buff=0.5    ,	aligned_edge=SCREAMING_SNAKE_CASE_							)
        model.move_to([3, -1.0, 0]							)
        self.add(SCREAMING_SNAKE_CASE_							)

        __lowerCamelCase:				Tuple     =	[]
        __lowerCamelCase:				Any     =	[]
        __lowerCamelCase:				int     =	[]

        for i, rect in enumerate(SCREAMING_SNAKE_CASE_							):
           rect.set_stroke(SCREAMING_SNAKE_CASE_							)

           __lowerCamelCase:				int     =	Rectangle(height=0.46 / 4    ,	width=0.46 / 3							).set_stroke(width=0.0							).set_fill(SCREAMING_SNAKE_CASE_    ,	opacity=0.7							)

           if i == 0:
              cpu_target.next_to(cpu_left_col_base[0].get_corner(DOWN + LEFT							)    ,	buff=0.02    ,	direction=SCREAMING_SNAKE_CASE_							)
              cpu_target.set_x(cpu_target.get_x() + 0.1							)
           elif i == 3:
              cpu_target.next_to(model_cpu_arr[0]    ,	direction=SCREAMING_SNAKE_CASE_    ,	buff=0.0							)
           else:
              cpu_target.next_to(model_cpu_arr[i - 1]    ,	direction=SCREAMING_SNAKE_CASE_    ,	buff=0.0							)
           self.add(SCREAMING_SNAKE_CASE_							)
           model_cpu_arr.append(SCREAMING_SNAKE_CASE_							)

        self.add(*SCREAMING_SNAKE_CASE_    ,	*SCREAMING_SNAKE_CASE_    ,	*SCREAMING_SNAKE_CASE_							)

        __lowerCamelCase:				List[Any]     =	[mem.copy() for i in range(6							)]
        __lowerCamelCase:				Any     =	VGroup(*SCREAMING_SNAKE_CASE_							).arrange(SCREAMING_SNAKE_CASE_    ,	buff=0							)

        __lowerCamelCase:				Tuple     =	Text("""Loaded Checkpoint"""    ,	font_size=24							)
        __lowerCamelCase:				Tuple     =	Group(SCREAMING_SNAKE_CASE_    ,	SCREAMING_SNAKE_CASE_							).arrange(SCREAMING_SNAKE_CASE_    ,	buff=0.5    ,	aligned_edge=SCREAMING_SNAKE_CASE_							)
        checkpoint.move_to([3, 0.5, 0]							)

        self.add(SCREAMING_SNAKE_CASE_							)

        __lowerCamelCase:				List[Any]     =	[]
        __lowerCamelCase:				Optional[int]     =	[]

        for i, rect in enumerate(SCREAMING_SNAKE_CASE_							):
           __lowerCamelCase:				Optional[int]     =	fill.copy().set_fill(SCREAMING_SNAKE_CASE_    ,	opacity=0.7							)
           target.move_to(SCREAMING_SNAKE_CASE_							)
           ckpt_arr.append(SCREAMING_SNAKE_CASE_							)

           __lowerCamelCase:				List[Any]     =	target.copy()
           if i < 5:
              cpu_target.move_to(cpu_left_col_base[i + 1]							)
           else:
              cpu_target.move_to(cpu_right_col_base[i - 5]							)
           ckpt_cpu_arr.append(SCREAMING_SNAKE_CASE_							)
        self.add(*SCREAMING_SNAKE_CASE_    ,	*SCREAMING_SNAKE_CASE_							)

        __lowerCamelCase:				Optional[Any]     =	Square(side_length=2.2							)
        key.move_to([-5, 2, 0]							)

        __lowerCamelCase:				Dict     =	MarkupText(
            F'<b>Key:</b>\n\n<span fgcolor=\'{YELLOW}\'>●</span> Empty Model'    ,	font_size=18    ,	)

        key_text.move_to([-5, 2.4, 0]							)

        self.add(SCREAMING_SNAKE_CASE_    ,	SCREAMING_SNAKE_CASE_							)

        __lowerCamelCase:				Optional[int]     =	MarkupText(
            F'<span fgcolor=\'{BLUE}\'>●</span> Checkpoint'    ,	font_size=18    ,	)

        blue_text.next_to(SCREAMING_SNAKE_CASE_    ,	DOWN * 2.4    ,	aligned_edge=key_text.get_left()							)
        self.add(SCREAMING_SNAKE_CASE_							)

        __lowerCamelCase:				Tuple     =	MarkupText(
            F'Based on the passed in configuration, weights are stored in\na variety of np.memmaps on disk or to a particular device.'    ,	font_size=24    ,	)
        step_a.move_to([2, 2, 0]							)

        __lowerCamelCase:				List[Any]     =	[meta_mem.copy() for i in range(6							)]
        __lowerCamelCase:				Optional[int]     =	[meta_mem.copy() for i in range(6							)]
        __lowerCamelCase:				str     =	VGroup(*SCREAMING_SNAKE_CASE_							).arrange(SCREAMING_SNAKE_CASE_    ,	buff=0							)
        __lowerCamelCase:				Optional[int]     =	VGroup(*SCREAMING_SNAKE_CASE_							).arrange(SCREAMING_SNAKE_CASE_    ,	buff=0							)
        __lowerCamelCase:				List[str]     =	VGroup(SCREAMING_SNAKE_CASE_    ,	SCREAMING_SNAKE_CASE_							).arrange(SCREAMING_SNAKE_CASE_    ,	buff=0							)
        __lowerCamelCase:				Dict     =	Text("""Disk"""    ,	font_size=24							)
        __lowerCamelCase:				Any     =	Group(SCREAMING_SNAKE_CASE_    ,	SCREAMING_SNAKE_CASE_							).arrange(SCREAMING_SNAKE_CASE_    ,	buff=0.5    ,	aligned_edge=SCREAMING_SNAKE_CASE_							)
        disk.move_to([-4.0, -1.25, 0]							)
        self.play(Write(SCREAMING_SNAKE_CASE_    ,	run_time=3							)    ,	Write(SCREAMING_SNAKE_CASE_    ,	run_time=1							)    ,	Create(SCREAMING_SNAKE_CASE_    ,	run_time=1							)							)

        __lowerCamelCase:				int     =	[]
        for i, rect in enumerate(SCREAMING_SNAKE_CASE_							):
           __lowerCamelCase:				Optional[int]     =	rect.copy()
           target.generate_target()
           target.target.move_to(disk_left_col_base[i]							).scale(0.5							)
           animations.append(MoveToTarget(SCREAMING_SNAKE_CASE_    ,	run_time=1.5							)							)
        self.play(*SCREAMING_SNAKE_CASE_							)

        self.play(FadeOut(SCREAMING_SNAKE_CASE_							)							)

        __lowerCamelCase:				List[Any]     =	MarkupText(F'Then, the checkpoint is removed from memory\nthrough garbage collection.'    ,	font_size=24							)
        step_a.move_to([2, 2, 0]							)

        self.play(Write(SCREAMING_SNAKE_CASE_    ,	run_time=3							)							)

        self.play(
            FadeOut(SCREAMING_SNAKE_CASE_    ,	SCREAMING_SNAKE_CASE_    ,	*SCREAMING_SNAKE_CASE_    ,	*SCREAMING_SNAKE_CASE_							)    ,	)

        self.wait()


