





'''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						):







	'''simple docstring'''

	a    :				str											=       MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING
	a    :				List[str]											=       TF_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING


	def 						_UpperCAmelCase     (self     ,_lowerCamelCase     ,_lowerCamelCase     ,_lowerCamelCase )							->							Any:







				'''simple docstring'''





				__lowercase										=							AudioClassificationPipeline(model=_lowerCamelCase     ,feature_extractor=_lowerCamelCase )

				# test with a raw waveform
				__lowercase										=							np.zeros((34000,) )
				__lowercase										=							np.zeros((14000,) )
				return audio_classifier, [audioa, audio]


	def 						_UpperCAmelCase     (self     ,_lowerCamelCase     ,_lowerCamelCase )							->							Union[str, Any]:







				'''simple docstring'''





				__lowercase						,		__lowercase										=							examples
				__lowercase										=							audio_classifier(_lowerCamelCase )
				# by default a model is initialized with num_labels=2
				self.assertEqual(
				    _lowerCamelCase     ,[
				        {'''score''': ANY(_lowerCamelCase ), '''label''': ANY(_lowerCamelCase )},
				        {'''score''': ANY(_lowerCamelCase ), '''label''': ANY(_lowerCamelCase )},
				    ]     ,)
				__lowercase										=							audio_classifier(_lowerCamelCase     ,top_k=1 )
				self.assertEqual(
				    _lowerCamelCase     ,[
				        {'''score''': ANY(_lowerCamelCase ), '''label''': ANY(_lowerCamelCase )},
				    ]     ,)

				self.run_torchaudio(_lowerCamelCase )


	@require_torchaudio
	def 						_UpperCAmelCase     (self     ,_lowerCamelCase )							->							Optional[Any]:







				'''simple docstring'''





				import datasets

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


	@require_torch
	def 						_UpperCAmelCase     (self )							->							int:







				'''simple docstring'''





				__lowercase										=							'''anton-l/wav2vec2-random-tiny-classifier'''

				__lowercase										=							pipeline('''audio-classification'''     ,model=_lowerCamelCase )

				__lowercase										=							np.ones((8000,) )
				__lowercase										=							audio_classifier(_lowerCamelCase     ,top_k=4 )

				__lowercase										=							[
				    {'''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'''},
				]
				__lowercase										=							[
				    {'''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(_lowerCamelCase     ,decimals=4 )     ,[EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2] )

				__lowercase										=							{'''array''': np.ones((8000,) ), '''sampling_rate''': audio_classifier.feature_extractor.sampling_rate}
				__lowercase										=							audio_classifier(_lowerCamelCase     ,top_k=4 )
				self.assertIn(nested_simplify(_lowerCamelCase     ,decimals=4 )     ,[EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2] )


	@require_torch
	@slow
	def 						_UpperCAmelCase     (self )							->							Any:







				'''simple docstring'''





				import datasets

				__lowercase										=							'''superb/wav2vec2-base-superb-ks'''

				__lowercase										=							pipeline('''audio-classification'''     ,model=_lowerCamelCase )
				__lowercase										=							datasets.load_dataset('''anton-l/superb_dummy'''     ,'''ks'''     ,split='''test''' )

				__lowercase										=							np.array(dataset[3]['''speech''']     ,dtype=np.floataa )
				__lowercase										=							audio_classifier(_lowerCamelCase     ,top_k=4 )
				self.assertEqual(
				    nested_simplify(_lowerCamelCase     ,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 						_UpperCAmelCase     (self )							->							Any:







				'''simple docstring'''





				pass






'''simple docstring'''



import json
from typing import Iterator, List, Union

from tokenizers import AddedToken, Regex, Tokenizer, decoders, normalizers, pre_tokenizers, trainers
from tokenizers.implementations.base_tokenizer import BaseTokenizer
from tokenizers.models import Unigram
from tokenizers.processors import TemplateProcessing






class 							__lowercase       (      lowerCAmelCase__						):







	'''simple docstring'''

	def __init__(self     ,_lowerCamelCase = "▁"     ,_lowerCamelCase = True     ,_lowerCamelCase = "<unk>"     ,_lowerCamelCase = "</s>"     ,_lowerCamelCase = "<pad>"     ,)							->							List[Any]:







				'''simple docstring'''





				__lowercase										=							{
				    '''pad''': {'''id''': 0, '''token''': pad_token},
				    '''eos''': {'''id''': 1, '''token''': eos_token},
				    '''unk''': {'''id''': 2, '''token''': unk_token},
				}

				__lowercase										=							[None] * len(self.special_tokens )
				for token_dict in self.special_tokens.values():
							__lowercase										=							token_dict['''token''']

				__lowercase										=							Tokenizer(Unigram() )

				__lowercase										=							normalizers.Sequence(
				    [
				        normalizers.Nmt(),
				        normalizers.NFKC(),
				        normalizers.Replace(Regex(''' {2,}''' )     ,''' ''' ),
				        normalizers.Lowercase(),
				    ] )
				__lowercase										=							pre_tokenizers.Sequence(
				    [
				        pre_tokenizers.Metaspace(replacement=_lowerCamelCase     ,add_prefix_space=_lowerCamelCase ),
				        pre_tokenizers.Digits(individual_digits=_lowerCamelCase ),
				        pre_tokenizers.Punctuation(),
				    ] )
				__lowercase										=							decoders.Metaspace(replacement=_lowerCamelCase     ,add_prefix_space=_lowerCamelCase )

				__lowercase										=							TemplateProcessing(
				    single=f"$A {self.special_tokens['eos']['token']}"     ,special_tokens=[(self.special_tokens['''eos''']['''token'''], self.special_tokens['''eos''']['''id'''])]     ,)

				__lowercase										=							{
				    '''model''': '''SentencePieceUnigram''',
				    '''replacement''': replacement,
				    '''add_prefix_space''': add_prefix_space,
				}

				super().__init__(_lowerCamelCase     ,_lowerCamelCase )


	def 						_UpperCAmelCase     (self     ,_lowerCamelCase     ,_lowerCamelCase = 8000     ,_lowerCamelCase = True     ,)							->							Union[str, Any]:







				'''simple docstring'''





				__lowercase										=							trainers.UnigramTrainer(
				    vocab_size=_lowerCamelCase     ,special_tokens=self.special_tokens_list     ,show_progress=_lowerCamelCase     ,)

				if isinstance(_lowerCamelCase     ,_lowerCamelCase ):
							__lowercase										=							[files]
				self._tokenizer.train(_lowerCamelCase     ,trainer=_lowerCamelCase )

				self.add_unk_id()


	def 						_UpperCAmelCase     (self     ,_lowerCamelCase     ,_lowerCamelCase = 8000     ,_lowerCamelCase = True     ,)							->							List[str]:







				'''simple docstring'''





				__lowercase										=							trainers.UnigramTrainer(
				    vocab_size=_lowerCamelCase     ,special_tokens=self.special_tokens_list     ,show_progress=_lowerCamelCase     ,)

				self._tokenizer.train_from_iterator(_lowerCamelCase     ,trainer=_lowerCamelCase )

				self.add_unk_id()


	def 						_UpperCAmelCase     (self )							->							Tuple:







				'''simple docstring'''





				__lowercase										=							json.loads(self._tokenizer.to_str() )

				__lowercase										=							self.special_tokens['''unk''']['''id''']

				__lowercase										=							Tokenizer.from_str(json.dumps(_lowerCamelCase ) )





