






import os
import re
import shutil
import sys
import tempfile
import unittest

import black


A__      :     Dict       =					os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
sys.path.append(os.path.join(git_repo_path, 'utils'))

import check_copies  # noqa: E402


# This is the reference code that will be used in the tests.
# If BertLMPredictionHead is changed in modeling_bert.py, this code needs to be manually updated.
A__      :     int       =					'    def __init__(self, config):\n        super().__init__()\n        self.transform = BertPredictionHeadTransform(config)\n\n        # The output weights are the same as the input embeddings, but there is\n        # an output-only bias for each token.\n        self.decoder = nn.Linear(config.hidden_size, config.vocab_size, bias=False)\n\n        self.bias = nn.Parameter(torch.zeros(config.vocab_size))\n\n        # Need a link between the two variables so that the bias is correctly resized with `resize_token_embeddings`\n        self.decoder.bias = self.bias\n\n    def forward(self, hidden_states):\n        hidden_states = self.transform(hidden_states)\n        hidden_states = self.decoder(hidden_states)\n        return hidden_states\n'
class  _UpperCAmelCase   (			unittest.TestCase      ):

				"""simple docstring"""




				def 							lowercase__  (	self		:       Tuple    ):

								'''simple docstring'''
								lowercase__  					=     tempfile.mkdtemp()
								os.makedirs(os.path.join(self.transformer_dir,    '''models/bert/'''    )    )
								lowercase__  					=     self.transformer_dir
								shutil.copy(
								    os.path.join(lowerCamelCase,    '''src/transformers/models/bert/modeling_bert.py'''    ),    os.path.join(self.transformer_dir,    '''models/bert/modeling_bert.py'''    ),    )




				def 							lowercase__  (	self		:       Union[str, Any]    ):

								'''simple docstring'''
								lowercase__  					=     '''src/transformers'''
								shutil.rmtree(self.transformer_dir    )




				def 							lowercase__  (	self		:       Optional[int],    lowerCamelCase		:       Optional[Any],    lowerCamelCase		:       Dict,    lowerCamelCase		:       Tuple,    lowerCamelCase		:       List[Any]=None    ):

								'''simple docstring'''
								lowercase__  					=     comment + F"""\nclass {class_name}(nn.Module):\n""" + class_code
								if overwrite_result is not None:
												lowercase__  					=     comment + F"""\nclass {class_name}(nn.Module):\n""" + overwrite_result
								lowercase__  					=     black.Mode(target_versions={black.TargetVersion.PYaa},    line_length=119    )
								lowercase__  					=     black.format_str(lowerCamelCase,    mode=lowerCamelCase    )
								lowercase__  					=     os.path.join(self.transformer_dir,    '''new_code.py'''    )
								with open(lowerCamelCase,    '''w''',    newline='''\n'''    ) as f:
												f.write(lowerCamelCase    )
								if overwrite_result is None:
												self.assertTrue(len(check_copies.is_copy_consistent(lowerCamelCase    )    ) == 0    )
								else:
												check_copies.is_copy_consistent(f.name,    overwrite=lowerCamelCase    )
												with open(lowerCamelCase,    '''r'''    ) as f:
																self.assertTrue(f.read(),    lowerCamelCase    )




				def 							lowercase__  (	self		:       str    ):

								'''simple docstring'''
								lowercase__  					=     check_copies.find_code_in_transformers('''models.bert.modeling_bert.BertLMPredictionHead'''    )
								self.assertEqual(lowerCamelCase,    lowerCamelCase    )




				def 							lowercase__  (	self		:       Dict    ):

								'''simple docstring'''
								# Base copy consistency
								self.check_copy_consistency(
								    '''# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead''',    '''BertLMPredictionHead''',    REFERENCE_CODE + '''\n''',    )

								# With no empty line at the end
								self.check_copy_consistency(
								    '''# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead''',    '''BertLMPredictionHead''',    lowerCamelCase,    )

								# Copy consistency with rename
								self.check_copy_consistency(
								    '''# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead with Bert->TestModel''',    '''TestModelLMPredictionHead''',    re.sub('''Bert''',    '''TestModel''',    lowerCamelCase    ),    )

								# Copy consistency with a really long name
								lowercase__  					=     '''TestModelWithAReallyLongNameBecauseSomePeopleLikeThatForSomeReason'''
								self.check_copy_consistency(
								    F"""# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead with Bert->{long_class_name}""",    F"""{long_class_name}LMPredictionHead""",    re.sub('''Bert''',    lowerCamelCase,    lowerCamelCase    ),    )

								# Copy consistency with overwrite
								self.check_copy_consistency(
								    '''# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead with Bert->TestModel''',    '''TestModelLMPredictionHead''',    lowerCamelCase,    overwrite_result=re.sub('''Bert''',    '''TestModel''',    lowerCamelCase    ),    )




				def 							lowercase__  (	self		:       Any    ):

								'''simple docstring'''
								lowercase__  					=     check_copies.LOCALIZED_READMES['''README_zh-hans.md''']

								lowercase__  					=     (
								    '''1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (from Google Research and the'''
								    ''' Toyota Technological Institute at Chicago) released with the paper [ALBERT: A Lite BERT for'''
								    ''' Self-supervised Learning of Language Representations](https://arxiv.org/abs/1909.11942), by Zhenzhong'''
								    ''' Lan, Mingda Chen, Sebastian Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut.\n1.'''
								    ''' **[DistilBERT](https://huggingface.co/transformers/model_doc/distilbert.html)** (from HuggingFace),'''
								    ''' released together with the paper [DistilBERT, a distilled version of BERT: smaller, faster, cheaper and'''
								    ''' lighter](https://arxiv.org/abs/1910.01108) by Victor Sanh, Lysandre Debut and Thomas Wolf. The same'''
								    ''' method has been applied to compress GPT2 into'''
								    ''' [DistilGPT2](https://github.com/huggingface/transformers/tree/main/examples/distillation), RoBERTa into'''
								    ''' [DistilRoBERTa](https://github.com/huggingface/transformers/tree/main/examples/distillation),'''
								    ''' Multilingual BERT into'''
								    ''' [DistilmBERT](https://github.com/huggingface/transformers/tree/main/examples/distillation) and a German'''
								    ''' version of DistilBERT.\n1. **[ELECTRA](https://huggingface.co/transformers/model_doc/electra.html)**'''
								    ''' (from Google Research/Stanford University) released with the paper [ELECTRA: Pre-training text encoders'''
								    ''' as discriminators rather than generators](https://arxiv.org/abs/2003.10555) by Kevin Clark, Minh-Thang'''
								    ''' Luong, Quoc V. Le, Christopher D. Manning.'''
								)
								lowercase__  					=     (
								    '''1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (来自 Google Research and the'''
								    ''' Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of'''
								    ''' Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian'''
								    ''' Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n'''
								)
								lowercase__  					=     (
								    '''1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (来自 Google Research and the'''
								    ''' Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of'''
								    ''' Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian'''
								    ''' Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n1.'''
								    ''' **[DistilBERT](https://huggingface.co/transformers/model_doc/distilbert.html)** (来自 HuggingFace) 伴随论文'''
								    ''' [DistilBERT, a distilled version of BERT: smaller, faster, cheaper and'''
								    ''' lighter](https://arxiv.org/abs/1910.01108) 由 Victor Sanh, Lysandre Debut and Thomas Wolf 发布。 The same'''
								    ''' method has been applied to compress GPT2 into'''
								    ''' [DistilGPT2](https://github.com/huggingface/transformers/tree/main/examples/distillation), RoBERTa into'''
								    ''' [DistilRoBERTa](https://github.com/huggingface/transformers/tree/main/examples/distillation),'''
								    ''' Multilingual BERT into'''
								    ''' [DistilmBERT](https://github.com/huggingface/transformers/tree/main/examples/distillation) and a German'''
								    ''' version of DistilBERT.\n1. **[ELECTRA](https://huggingface.co/transformers/model_doc/electra.html)** (来自'''
								    ''' Google Research/Stanford University) 伴随论文 [ELECTRA: Pre-training text encoders as discriminators rather'''
								    ''' than generators](https://arxiv.org/abs/2003.10555) 由 Kevin Clark, Minh-Thang Luong, Quoc V. Le,'''
								    ''' Christopher D. Manning 发布。\n'''
								)

								lowercase__					,		lowercase__  					=     check_copies.convert_to_localized_md(
								    lowerCamelCase,    lowerCamelCase,    localized_readme['''format_model_list''']    )

								self.assertFalse(lowerCamelCase    )
								self.assertEqual(lowerCamelCase,    lowerCamelCase    )

								lowercase__					,		lowercase__  					=     check_copies.convert_to_localized_md(
								    lowerCamelCase,    lowerCamelCase,    localized_readme['''format_model_list''']    )

								# Check whether the number of models is equal to README.md after conversion.
								self.assertTrue(lowerCamelCase    )

								lowercase__  					=     (
								    '''1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (from Google Research and the'''
								    ''' Toyota Technological Institute at Chicago) released with the paper [ALBERT: A Lite BERT for'''
								    ''' Self-supervised Learning of Language Representations](https://arxiv.org/abs/1909.11942), by Zhenzhong'''
								    ''' Lan, Mingda Chen, Sebastian Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut.'''
								)
								lowercase__  					=     (
								    '''1. **[ALBERT](https://huggingface.co/transformers/main/model_doc/albert.html)** (来自 Google Research and'''
								    ''' the Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of'''
								    ''' Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian'''
								    ''' Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n'''
								)
								lowercase__  					=     (
								    '''1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (来自 Google Research and the'''
								    ''' Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of'''
								    ''' Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian'''
								    ''' Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n'''
								)

								lowercase__					,		lowercase__  					=     check_copies.convert_to_localized_md(
								    lowerCamelCase,    lowerCamelCase,    localized_readme['''format_model_list''']    )

								# Check if the model link is synchronized.
								self.assertEqual(lowerCamelCase,    lowerCamelCase    )












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"""




				lowercase__											=			MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING
				lowercase__											=			TF_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING




				def 							lowercase__  (	self		:       Any,    lowerCamelCase		:       Optional[Any],    lowerCamelCase		:       Any,    lowerCamelCase		:       Tuple    ):

								'''simple docstring'''
								lowercase__  					=     AudioClassificationPipeline(model=lowerCamelCase,    feature_extractor=lowerCamelCase    )

								# test with a raw waveform
								lowercase__  					=     np.zeros((34_000,)    )
								lowercase__  					=     np.zeros((14_000,)    )
								return audio_classifier, [audioa, audio]




				def 							lowercase__  (	self		:       str,    lowerCamelCase		:       Dict,    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 							lowercase__  (	self		:       Optional[int],    lowerCamelCase		:       List[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 							lowercase__  (	self		:       int    ):

								'''simple docstring'''
								lowercase__  					=     '''anton-l/wav2vec2-random-tiny-classifier'''

								lowercase__  					=     pipeline('''audio-classification''',    model=lowerCamelCase    )

								lowercase__  					=     np.ones((8_000,)    )
								lowercase__  					=     audio_classifier(lowerCamelCase,    top_k=4    )

								lowercase__  					=     [
								    {'''score''': 0.0842, '''label''': '''no'''},
								    {'''score''': 0.0838, '''label''': '''up'''},
								    {'''score''': 0.0837, '''label''': '''go'''},
								    {'''score''': 0.0834, '''label''': '''right'''},
								]
								lowercase__  					=     [
								    {'''score''': 0.0845, '''label''': '''stop'''},
								    {'''score''': 0.0844, '''label''': '''on'''},
								    {'''score''': 0.0841, '''label''': '''right'''},
								    {'''score''': 0.0834, '''label''': '''left'''},
								]
								self.assertIn(nested_simplify(lowerCamelCase,    decimals=4    ),    [EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2]    )

								lowercase__  					=     {'''array''': np.ones((8_000,)    ), '''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 							lowercase__  (	self		:       List[str]    ):

								'''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.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 							lowercase__  (	self		:       Optional[int]    ):

								'''simple docstring'''
								pass




