

'''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 					lowercase						(  unittest.TestCase ):
							_lowerCamelCase		:      str=   MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING
							_lowerCamelCase		:      List[Any]=   TF_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING


							def 						_snake_case							(			self    ,			_snake_case    ,			_snake_case    ,			_snake_case)							->				str:
								UpperCAmelCase_		:      List[str]       	=					AudioClassificationPipeline(model=_snake_case    ,			feature_extractor=_snake_case)

								# test with a raw waveform
								UpperCAmelCase_		:      Tuple       	=					np.zeros((3_4000,))
								UpperCAmelCase_		:      Optional[Any]       	=					np.zeros((1_4000,))
								return audio_classifier, [audioa, audio]


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

								self.run_torchaudio(_snake_case)


							@require_torchaudio
							def 						_snake_case							(			self    ,			_snake_case)							->				Optional[int]:
								import datasets

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


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

								UpperCAmelCase_		:      Tuple       	=					pipeline('audio-classification'    ,			model=_snake_case)

								UpperCAmelCase_		:      Union[str, Any]       	=					np.ones((8000,))
								UpperCAmelCase_		:      Tuple       	=					audio_classifier(_snake_case    ,			top_k=4)

								UpperCAmelCase_		:      Dict       	=					[
								    {'score': 0.0_842, 'label': 'no'},
								    {'score': 0.0_838, 'label': 'up'},
								    {'score': 0.0_837, 'label': 'go'},
								    {'score': 0.0_834, 'label': 'right'},
								]
								UpperCAmelCase_		:      List[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(_snake_case    ,			decimals=4)    ,			[EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2])

								UpperCAmelCase_		:      str       	=					{'array': np.ones((8000,)), 'sampling_rate': audio_classifier.feature_extractor.sampling_rate}
								UpperCAmelCase_		:      List[Any]       	=					audio_classifier(_snake_case    ,			top_k=4)
								self.assertIn(nested_simplify(_snake_case    ,			decimals=4)    ,			[EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2])


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

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

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

								UpperCAmelCase_		:      List[str]       	=					np.array(dataset[3]['speech']    ,			dtype=np.floataa)
								UpperCAmelCase_		:      List[str]       	=					audio_classifier(_snake_case    ,			top_k=4)
								self.assertEqual(
								    nested_simplify(_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 						_snake_case							(			self)							->				int:
								pass









'''simple docstring'''


class 					lowercase						:


							def __init__(			self)							->				Dict:
								UpperCAmelCase_		:      List[str]       	=					0
								UpperCAmelCase_		:      str       	=					0
								UpperCAmelCase_		:      Union[str, Any]       	=					{}


							def 						_snake_case							(			self    ,			_snake_case)							->				Tuple:
								if vertex not in self.adjacency:
									UpperCAmelCase_		:      Tuple       	=					{}
									self.num_vertices += 1


							def 						_snake_case							(			self    ,			_snake_case    ,			_snake_case    ,			_snake_case)							->				Any:
								self.add_vertex(_snake_case)
								self.add_vertex(_snake_case)

								if head == tail:
									return

								UpperCAmelCase_		:      str       	=					weight
								UpperCAmelCase_		:      Optional[int]       	=					weight


							def 						_snake_case							(			self)							->				Union[str, Any]:
								UpperCAmelCase_		:      Dict       	=					self.get_edges()
								for edge in edges:
									UpperCAmelCase_	,    UpperCAmelCase_	,    UpperCAmelCase_		:      str       	=					edge
									edges.remove((tail, head, weight))
								for i in range(len(_snake_case)):
									UpperCAmelCase_		:      Union[str, Any]       	=					list(edges[i])

								edges.sort(key=lambda _snake_case: e[2])
								for i in range(len(_snake_case) - 1):
									if edges[i][2] >= edges[i + 1][2]:
										UpperCAmelCase_		:      Tuple       	=					edges[i][2] + 1
								for edge in edges:
									UpperCAmelCase_	,    UpperCAmelCase_	,    UpperCAmelCase_		:      List[Any]       	=					edge
									UpperCAmelCase_		:      Any       	=					weight
									UpperCAmelCase_		:      Union[str, Any]       	=					weight


							def __str__(			self)							->				str:
								UpperCAmelCase_		:      int       	=					''
								for tail in self.adjacency:
									for head in self.adjacency[tail]:
										UpperCAmelCase_		:      Dict       	=					self.adjacency[head][tail]
										string += F"""{head} -> {tail} == {weight}\n"""
								return string.rstrip('\n')


							def 						_snake_case							(			self)							->				str:
								UpperCAmelCase_		:      Optional[int]       	=					[]
								for tail in self.adjacency:
									for head in self.adjacency[tail]:
										output.append((tail, head, self.adjacency[head][tail]))
								return output


							def 						_snake_case							(			self)							->				Any:
								return self.adjacency.keys()


							@staticmethod
							def 						_snake_case							(			_snake_case=None    ,			_snake_case=None)							->				Dict:
								UpperCAmelCase_		:      List[str]       	=					Graph()
								if vertices is None:
									UpperCAmelCase_		:      Optional[Any]       	=					[]
								if edges is None:
									UpperCAmelCase_		:      int       	=					[]
								for vertex in vertices:
									g.add_vertex(_snake_case)
								for edge in edges:
									g.add_edge(*_snake_case)
								return g



							class 					lowercase						:


														def __init__(			self)							->				Dict:
															UpperCAmelCase_		:      Tuple       	=					{}
															UpperCAmelCase_		:      str       	=					{}


														def __len__(			self)							->				List[Any]:
															return len(self.parent)


														def 						_snake_case							(			self    ,			_snake_case)							->				Union[str, Any]:
															if item in self.parent:
																return self.find(_snake_case)

															UpperCAmelCase_		:      Any       	=					item
															UpperCAmelCase_		:      List[str]       	=					0
															return item


														def 						_snake_case							(			self    ,			_snake_case)							->				str:
															if item not in self.parent:
																return self.make_set(_snake_case)
															if item != self.parent[item]:
																UpperCAmelCase_		:      Dict       	=					self.find(self.parent[item])
															return self.parent[item]





														def 						_snake_case							(			self    ,			_snake_case    ,			_snake_case)							->				int:
															UpperCAmelCase_		:      List[str]       	=					self.find(_snake_case)
															UpperCAmelCase_		:      List[str]       	=					self.find(_snake_case)

															if roota == roota:
																return roota

															if self.rank[roota] > self.rank[roota]:
																UpperCAmelCase_		:      int       	=					roota
																return roota

															if self.rank[roota] < self.rank[roota]:
																UpperCAmelCase_		:      Dict       	=					roota
																return roota

															if self.rank[roota] == self.rank[roota]:
																self.rank[roota] += 1
																UpperCAmelCase_		:      Any       	=					roota
																return roota
															return None


							@staticmethod
							def 						_snake_case							(			_snake_case)							->				int:
								UpperCAmelCase_		:      Optional[Any]       	=					graph.num_vertices

								UpperCAmelCase_		:      Optional[int]       	=					Graph.UnionFind()
								UpperCAmelCase_		:      int       	=					[]
								while num_components > 1:
									UpperCAmelCase_		:      Any       	=					{}
									for vertex in graph.get_vertices():
										UpperCAmelCase_		:      Optional[Any]       	=					-1

									UpperCAmelCase_		:      List[str]       	=					graph.get_edges()
									for edge in edges:
										UpperCAmelCase_	,    UpperCAmelCase_	,    UpperCAmelCase_		:      List[str]       	=					edge
										edges.remove((tail, head, weight))
									for edge in edges:
										UpperCAmelCase_	,    UpperCAmelCase_	,    UpperCAmelCase_		:      Union[str, Any]       	=					edge
										UpperCAmelCase_		:      Any       	=					union_find.find(_snake_case)
										UpperCAmelCase_		:      Dict       	=					union_find.find(_snake_case)
										if seta != seta:
											if cheap_edge[seta] == -1 or cheap_edge[seta][2] > weight:
												UpperCAmelCase_		:      List[str]       	=					[head, tail, weight]

											if cheap_edge[seta] == -1 or cheap_edge[seta][2] > weight:
												UpperCAmelCase_		:      str       	=					[head, tail, weight]
									for vertex in cheap_edge:
										if cheap_edge[vertex] != -1:
											UpperCAmelCase_	,    UpperCAmelCase_	,    UpperCAmelCase_		:      Any       	=					cheap_edge[vertex]
											if union_find.find(_snake_case) != union_find.find(_snake_case):
												union_find.union(_snake_case    ,			_snake_case)
												mst_edges.append(cheap_edge[vertex])
												UpperCAmelCase_		:      Tuple       	=					num_components - 1
								UpperCAmelCase_		:      int       	=					Graph.build(edges=_snake_case)
								return mst






