






from transformers import BertTokenizer, EncoderDecoderModel, SeqaSeqTrainer, SeqaSeqTrainingArguments
from transformers.testing_utils import TestCasePlus, require_torch, slow
from transformers.utils import is_datasets_available


if is_datasets_available():
	import datasets




class 					UpperCamelCase_ (     snake_case_							):



	'''simple docstring'''




	@slow
	@require_torch
	def 				_UpperCamelCase     (			self   )			->       List[Any]:
								snake_case_           =				EncoderDecoderModel.from_encoder_decoder_pretrained('prajjwal1/bert-tiny'			,     'prajjwal1/bert-tiny'   )
								snake_case_           =				BertTokenizer.from_pretrained('bert-base-uncased'   )

								snake_case_           =				bertabert.config.encoder.vocab_size
								snake_case_           =				tokenizer.sep_token_id
								snake_case_           =				tokenizer.cls_token_id
								snake_case_           =				1_28

								snake_case_           =				datasets.load_dataset('cnn_dailymail'			,     '3.0.0'			,     split='train[:1%]'   )
								snake_case_           =				datasets.load_dataset('cnn_dailymail'			,     '3.0.0'			,     split='validation[:1%]'   )

								snake_case_           =				train_dataset.select(range(32   )   )
								snake_case_           =				val_dataset.select(range(16   )   )

								snake_case_           =				4

								def _map_to_encoder_decoder_inputs(a   ):
															# Tokenizer will automatically set [BOS] <text> [EOS]
															snake_case_           =				tokenizer(batch['article']			,     padding='max_length'			,     truncation=a			,     max_length=5_12   )
															snake_case_           =				tokenizer(batch['highlights']			,     padding='max_length'			,     truncation=a			,     max_length=1_28   )
															snake_case_           =				inputs.input_ids
															snake_case_           =				inputs.attention_mask

															snake_case_           =				outputs.input_ids
															snake_case_           =				outputs.input_ids.copy()
															snake_case_           =				[
															    [-1_00 if token == tokenizer.pad_token_id else token for token in labels] for labels in batch['labels']
															]
															snake_case_           =				outputs.attention_mask

															assert all(len(a   ) == 5_12 for x in inputs.input_ids   )
															assert all(len(a   ) == 1_28 for x in outputs.input_ids   )

															return batch

								def _compute_metrics(a   ):
															snake_case_           =				pred.label_ids
															snake_case_           =				pred.predictions

															# all unnecessary tokens are removed
															snake_case_           =				tokenizer.batch_decode(a			,     skip_special_tokens=a   )
															snake_case_           =				tokenizer.batch_decode(a			,     skip_special_tokens=a   )

															snake_case_           =				sum([int(pred_str[i] == label_str[i]   ) for i in range(len(a   )   )]   ) / len(a   )

															return {"accuracy": accuracy}

								# map train dataset
								snake_case_           =				train_dataset.map(
								    _map_to_encoder_decoder_inputs			,     batched=a			,     batch_size=a			,     remove_columns=['article', 'highlights']			,     )
								train_dataset.set_format(
								    type='torch'			,     columns=['input_ids', 'attention_mask', 'decoder_input_ids', 'decoder_attention_mask', 'labels']			,     )

								# same for validation dataset
								snake_case_           =				val_dataset.map(
								    _map_to_encoder_decoder_inputs			,     batched=a			,     batch_size=a			,     remove_columns=['article', 'highlights']			,     )
								val_dataset.set_format(
								    type='torch'			,     columns=['input_ids', 'attention_mask', 'decoder_input_ids', 'decoder_attention_mask', 'labels']			,     )

								snake_case_           =				self.get_auto_remove_tmp_dir()

								snake_case_           =				SeqaSeqTrainingArguments(
								    output_dir=a			,     per_device_train_batch_size=a			,     per_device_eval_batch_size=a			,     predict_with_generate=a			,     evaluation_strategy='steps'			,     do_train=a			,     do_eval=a			,     warmup_steps=0			,     eval_steps=2			,     logging_steps=2			,     )

								# instantiate trainer
								snake_case_           =				SeqaSeqTrainer(
								    model=a			,     args=a			,     compute_metrics=_compute_metrics			,     train_dataset=a			,     eval_dataset=a			,     tokenizer=a			,     )

								# start training
								trainer.train()













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



	'''simple docstring'''




	lowerCAmelCase						=					MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING
	lowerCAmelCase						=					TF_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING




	def 				_UpperCamelCase     (			self			,     a			,     a			,     a   )			->       Union[str, Any]:
								snake_case_           =				AudioClassificationPipeline(model=a			,     feature_extractor=a   )

								# test with a raw waveform
								snake_case_           =				np.zeros((3_40_00,)   )
								snake_case_           =				np.zeros((1_40_00,)   )
								return audio_classifier, [audioa, audio]




	def 				_UpperCamelCase     (			self			,     a			,     a   )			->       Tuple:
								snake_case_     ,						snake_case_           =				examples
								snake_case_           =				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   )},
								    ]			,     )
								snake_case_           =				audio_classifier(a			,     top_k=1   )
								self.assertEqual(
								    a			,     [
								        {'score': ANY(a   ), 'label': ANY(a   )},
								    ]			,     )

								self.run_torchaudio(a   )




	@require_torchaudio
	def 				_UpperCamelCase     (			self			,     a   )			->       List[str]:
								import datasets

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




	@require_torch
	def 				_UpperCamelCase     (			self   )			->       Dict:
								snake_case_           =				'anton-l/wav2vec2-random-tiny-classifier'

								snake_case_           =				pipeline('audio-classification'			,     model=a   )

								snake_case_           =				np.ones((80_00,)   )
								snake_case_           =				audio_classifier(a			,     top_k=4   )

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

								snake_case_           =				{'array': np.ones((80_00,)   ), 'sampling_rate': audio_classifier.feature_extractor.sampling_rate}
								snake_case_           =				audio_classifier(a			,     top_k=4   )
								self.assertIn(nested_simplify(a			,     decimals=4   )			,     [EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2]   )




	@require_torch
	@slow
	def 				_UpperCamelCase     (			self   )			->       str:
								import datasets

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

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

								snake_case_           =				np.array(dataset[3]['speech']			,     dtype=np.floataa   )
								snake_case_           =				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 				_UpperCamelCase     (			self   )			->       Optional[int]:
								pass





