





import os
import pickle
import tempfile
import unittest
from typing import Callable, Optional

import numpy as np

from transformers import (
    BatchEncoding,
    BertTokenizer,
    BertTokenizerFast,
    PreTrainedTokenizer,
    PreTrainedTokenizerFast,
    TensorType,
    TokenSpan,
    is_tokenizers_available,
)
from transformers.models.gpta.tokenization_gpta import GPTaTokenizer
from transformers.testing_utils import CaptureStderr, require_flax, require_tf, require_tokenizers, require_torch, slow


if is_tokenizers_available():
 from tokenizers import Tokenizer
 from tokenizers.models import WordPiece







class     lowerCamelCase(	unittest.TestCase  ):
     '''simple docstring'''


     def 					lowerCAmelCase__		(						self							,    snake_case_       ):
         _A    =       list(tokenizer_class.max_model_input_sizes.keys()       )
         for model_name in sa_models[:1]:
             _A    =       tokenizer_class.from_pretrained(snake_case_       )
             self.assertIsNotNone(snake_case_       )
             self.assertIsInstance(snake_case_							,    snake_case_       )
             self.assertIsInstance(snake_case_							,    snake_case_       )

             for special_tok in tokenizer.all_special_tokens:
                 self.assertIsInstance(snake_case_							,    snake_case_       )
                 _A    =       tokenizer.convert_tokens_to_ids(snake_case_       )
                 self.assertIsInstance(snake_case_							,    snake_case_       )




     def 					lowerCAmelCase__		(						self							,    snake_case_							,    snake_case_ = None       ):
         _A    =       pickle.dumps(snake_case_       )
         self.assertIsNotNone(snake_case_       )

         _A    =       pickle.loads(snake_case_       )

         # Ensure is_fast is correctly restored
         self.assertEqual(be_restored.is_fast							,    be_original.is_fast       )

         # Ensure encodings are potentially correctly restored
         if be_original.is_fast:
             self.assertIsNotNone(be_restored.encodings       )
         else:
             self.assertIsNone(be_restored.encodings       )

         # Ensure the keys are the same
         for original_v, restored_v in zip(be_original.values()							,    be_restored.values()       ):
             if equal_op:
                 self.assertTrue(equal_op(snake_case_							,    snake_case_       )       )
             else:
                 self.assertEqual(snake_case_							,    snake_case_       )




     @slow
     def 					lowerCAmelCase__		(						self       ):
         self.check_tokenizer_from_pretrained(snake_case_       )




     def 					lowerCAmelCase__		(						self       ):
         self.assertEqual(TensorType('tf'       )							,    TensorType.TENSORFLOW       )
         self.assertEqual(TensorType('pt'       )							,    TensorType.PYTORCH       )
         self.assertEqual(TensorType('np'       )							,    TensorType.NUMPY       )




     @require_tokenizers
     def 					lowerCAmelCase__		(						self       ):
         import numpy as np

         _A    =       BertTokenizer.from_pretrained('bert-base-cased'       )
         _A    =       BertTokenizerFast.from_pretrained('bert-base-cased'       )

         # Python no tensor
         with self.subTest('BatchEncoding (Python, return_tensors=None)'       ):
             self.assert_dump_and_restore(tokenizer_p('Small example to encode'       )       )

         with self.subTest('BatchEncoding (Python, return_tensors=NUMPY)'       ):
             self.assert_dump_and_restore(
                 tokenizer_p('Small example to encode'							,    return_tensors=TensorType.NUMPY       )							,    np.array_equal       )

         with self.subTest('BatchEncoding (Rust, return_tensors=None)'       ):
             self.assert_dump_and_restore(tokenizer_r('Small example to encode'       )       )

         with self.subTest('BatchEncoding (Rust, return_tensors=NUMPY)'       ):
             self.assert_dump_and_restore(
                 tokenizer_r('Small example to encode'							,    return_tensors=TensorType.NUMPY       )							,    np.array_equal       )




     @require_tf
     @require_tokenizers
     def 					lowerCAmelCase__		(						self       ):
         import tensorflow as tf

         def tf_array_equals(snake_case_							,    snake_case_       ):
             return tf.reduce_all(tf.equal(snake_case_							,    snake_case_       )       )

         _A    =       BertTokenizer.from_pretrained('bert-base-cased'       )
         _A    =       BertTokenizerFast.from_pretrained('bert-base-cased'       )

         with self.subTest('BatchEncoding (Python, return_tensors=TENSORFLOW)'       ):
             self.assert_dump_and_restore(
                 tokenizer_p('Small example to encode'							,    return_tensors=TensorType.TENSORFLOW       )							,    snake_case_       )

         with self.subTest('BatchEncoding (Rust, return_tensors=TENSORFLOW)'       ):
             self.assert_dump_and_restore(
                 tokenizer_r('Small example to encode'							,    return_tensors=TensorType.TENSORFLOW       )							,    snake_case_       )




     @require_torch
     @require_tokenizers
     def 					lowerCAmelCase__		(						self       ):
         import torch

         _A    =       BertTokenizer.from_pretrained('bert-base-cased'       )
         _A    =       BertTokenizerFast.from_pretrained('bert-base-cased'       )

         with self.subTest('BatchEncoding (Python, return_tensors=PYTORCH)'       ):
             self.assert_dump_and_restore(
                 tokenizer_p('Small example to encode'							,    return_tensors=TensorType.PYTORCH       )							,    torch.equal       )

         with self.subTest('BatchEncoding (Rust, return_tensors=PYTORCH)'       ):
             self.assert_dump_and_restore(
                 tokenizer_r('Small example to encode'							,    return_tensors=TensorType.PYTORCH       )							,    torch.equal       )




     @require_tokenizers
     def 					lowerCAmelCase__		(						self       ):
         _A    =       BertTokenizer.from_pretrained('bert-base-cased'       )
         _A    =       BertTokenizerFast.from_pretrained('bert-base-cased'       )

         with self.subTest('Python Tokenizer'       ):
             self.assertFalse(tokenizer_p('Small example to_encode'       ).is_fast       )

         with self.subTest('Rust Tokenizer'       ):
             self.assertTrue(tokenizer_r('Small example to_encode'       ).is_fast       )




     @require_tokenizers
     def 					lowerCAmelCase__		(						self       ):
         _A    =       BertTokenizerFast.from_pretrained('bert-base-cased'       )
         _A    =       tokenizer_r(['Test', '\xad', 'test']							,    is_split_into_words=snake_case_       )

         self.assertEqual(encoded.word_to_tokens(0       )							,    TokenSpan(start=1							,    end=2       )       )
         self.assertEqual(encoded.word_to_tokens(1       )							,    snake_case_       )
         self.assertEqual(encoded.word_to_tokens(2       )							,    TokenSpan(start=2							,    end=3       )       )




     def 					lowerCAmelCase__		(						self       ):
         _A    =       BatchEncoding({'inputs': [[1, 2, 3], [4, 5, 6]], 'labels': [0, 1]}       )
         _A    =       batch.convert_to_tensors(tensor_type='np'       )
         self.assertEqual(tensor_batch['inputs'].shape							,    (2, 3)       )
         self.assertEqual(tensor_batch['labels'].shape							,    (2,)       )
         # test converting the converted
         with CaptureStderr() as cs:
             _A    =       batch.convert_to_tensors(tensor_type='np'       )
         self.assertFalse(len(cs.err       )							,    msg=F"should have no warning, but got {cs.err}"       )

         _A    =       BatchEncoding({'inputs': [1, 2, 3], 'labels': 0}       )
         _A    =       batch.convert_to_tensors(tensor_type='np'							,    prepend_batch_axis=snake_case_       )
         self.assertEqual(tensor_batch['inputs'].shape							,    (1, 3)       )
         self.assertEqual(tensor_batch['labels'].shape							,    (1,)       )




     @require_torch
     def 					lowerCAmelCase__		(						self       ):
         _A    =       BatchEncoding({'inputs': [[1, 2, 3], [4, 5, 6]], 'labels': [0, 1]}       )
         _A    =       batch.convert_to_tensors(tensor_type='pt'       )
         self.assertEqual(tensor_batch['inputs'].shape							,    (2, 3)       )
         self.assertEqual(tensor_batch['labels'].shape							,    (2,)       )
         # test converting the converted
         with CaptureStderr() as cs:
             _A    =       batch.convert_to_tensors(tensor_type='pt'       )
         self.assertFalse(len(cs.err       )							,    msg=F"should have no warning, but got {cs.err}"       )

         _A    =       BatchEncoding({'inputs': [1, 2, 3], 'labels': 0}       )
         _A    =       batch.convert_to_tensors(tensor_type='pt'							,    prepend_batch_axis=snake_case_       )
         self.assertEqual(tensor_batch['inputs'].shape							,    (1, 3)       )
         self.assertEqual(tensor_batch['labels'].shape							,    (1,)       )




     @require_tf
     def 					lowerCAmelCase__		(						self       ):
         _A    =       BatchEncoding({'inputs': [[1, 2, 3], [4, 5, 6]], 'labels': [0, 1]}       )
         _A    =       batch.convert_to_tensors(tensor_type='tf'       )
         self.assertEqual(tensor_batch['inputs'].shape							,    (2, 3)       )
         self.assertEqual(tensor_batch['labels'].shape							,    (2,)       )
         # test converting the converted
         with CaptureStderr() as cs:
             _A    =       batch.convert_to_tensors(tensor_type='tf'       )
         self.assertFalse(len(cs.err       )							,    msg=F"should have no warning, but got {cs.err}"       )

         _A    =       BatchEncoding({'inputs': [1, 2, 3], 'labels': 0}       )
         _A    =       batch.convert_to_tensors(tensor_type='tf'							,    prepend_batch_axis=snake_case_       )
         self.assertEqual(tensor_batch['inputs'].shape							,    (1, 3)       )
         self.assertEqual(tensor_batch['labels'].shape							,    (1,)       )




     @require_flax
     def 					lowerCAmelCase__		(						self       ):
         _A    =       BatchEncoding({'inputs': [[1, 2, 3], [4, 5, 6]], 'labels': [0, 1]}       )
         _A    =       batch.convert_to_tensors(tensor_type='jax'       )
         self.assertEqual(tensor_batch['inputs'].shape							,    (2, 3)       )
         self.assertEqual(tensor_batch['labels'].shape							,    (2,)       )
         # test converting the converted
         with CaptureStderr() as cs:
             _A    =       batch.convert_to_tensors(tensor_type='jax'       )
         self.assertFalse(len(cs.err       )							,    msg=F"should have no warning, but got {cs.err}"       )

         _A    =       BatchEncoding({'inputs': [1, 2, 3], 'labels': 0}       )
         _A    =       batch.convert_to_tensors(tensor_type='jax'							,    prepend_batch_axis=snake_case_       )
         self.assertEqual(tensor_batch['inputs'].shape							,    (1, 3)       )
         self.assertEqual(tensor_batch['labels'].shape							,    (1,)       )




     def 					lowerCAmelCase__		(						self       ):
         _A    =       [{'input_ids': np.array([0, 1, 2]       )}, {'input_ids': np.array([0, 1, 2, 3]       )}]
         _A    =       BertTokenizer.from_pretrained('bert-base-cased'       )

         _A    =       tokenizer.pad(snake_case_							,    padding=snake_case_       )
         self.assertTrue(isinstance(batch['input_ids']							,    np.ndarray       )       )
         self.assertEqual(batch['input_ids'].tolist()							,    [[0, 1, 2, tokenizer.pad_token_id], [0, 1, 2, 3]]       )
         _A    =       tokenizer.pad(snake_case_							,    padding=snake_case_							,    return_tensors='np'       )
         self.assertTrue(isinstance(batch['input_ids']							,    np.ndarray       )       )
         self.assertEqual(batch['input_ids'].tolist()							,    [[0, 1, 2, tokenizer.pad_token_id], [0, 1, 2, 3]]       )




     @require_torch
     def 					lowerCAmelCase__		(						self       ):
         import torch

         _A    =       [{'input_ids': torch.tensor([0, 1, 2]       )}, {'input_ids': torch.tensor([0, 1, 2, 3]       )}]
         _A    =       BertTokenizer.from_pretrained('bert-base-cased'       )

         _A    =       tokenizer.pad(snake_case_							,    padding=snake_case_       )
         self.assertTrue(isinstance(batch['input_ids']							,    torch.Tensor       )       )
         self.assertEqual(batch['input_ids'].tolist()							,    [[0, 1, 2, tokenizer.pad_token_id], [0, 1, 2, 3]]       )
         _A    =       tokenizer.pad(snake_case_							,    padding=snake_case_							,    return_tensors='pt'       )
         self.assertTrue(isinstance(batch['input_ids']							,    torch.Tensor       )       )
         self.assertEqual(batch['input_ids'].tolist()							,    [[0, 1, 2, tokenizer.pad_token_id], [0, 1, 2, 3]]       )




     @require_tf
     def 					lowerCAmelCase__		(						self       ):
         import tensorflow as tf

         _A    =       [{'input_ids': tf.constant([0, 1, 2]       )}, {'input_ids': tf.constant([0, 1, 2, 3]       )}]
         _A    =       BertTokenizer.from_pretrained('bert-base-cased'       )

         _A    =       tokenizer.pad(snake_case_							,    padding=snake_case_       )
         self.assertTrue(isinstance(batch['input_ids']							,    tf.Tensor       )       )
         self.assertEqual(batch['input_ids'].numpy().tolist()							,    [[0, 1, 2, tokenizer.pad_token_id], [0, 1, 2, 3]]       )
         _A    =       tokenizer.pad(snake_case_							,    padding=snake_case_							,    return_tensors='tf'       )
         self.assertTrue(isinstance(batch['input_ids']							,    tf.Tensor       )       )
         self.assertEqual(batch['input_ids'].numpy().tolist()							,    [[0, 1, 2, tokenizer.pad_token_id], [0, 1, 2, 3]]       )




     @require_tokenizers
     def 					lowerCAmelCase__		(						self       ):
         _A    =       Tokenizer(WordPiece(unk_token='[UNK]'       )       )
         PreTrainedTokenizerFast(tokenizer_object=snake_case_       )




     @require_tokenizers
     def 					lowerCAmelCase__		(						self       ):
         _A    =       Tokenizer(WordPiece(unk_token='[UNK]'       )       )
         with tempfile.TemporaryDirectory() as tmpdirname:
             bert_tokenizer.save(os.path.join(snake_case_							,    'tokenizer.json'       )       )
             PreTrainedTokenizerFast(tokenizer_file=os.path.join(snake_case_							,    'tokenizer.json'       )       )












from sklearn.metrics import accuracy_score

import datasets


__A       : Union[str, Any]						    = "\nAccuracy is the proportion of correct predictions among the total number of cases processed. It can be computed with:\nAccuracy = (TP + TN) / (TP + TN + FP + FN)\n Where:\nTP: True positive\nTN: True negative\nFP: False positive\nFN: False negative\n"


__A       : List[Any]						    = "\nArgs:\n    predictions (`list` of `int`): Predicted labels.\n    references (`list` of `int`): Ground truth labels.\n    normalize (`boolean`): If set to False, returns the number of correctly classified samples. Otherwise, returns the fraction of correctly classified samples. Defaults to True.\n    sample_weight (`list` of `float`): Sample weights Defaults to None.\n\nReturns:\n    accuracy (`float` or `int`): Accuracy score. Minimum possible value is 0. Maximum possible value is 1.0, or the number of examples input, if `normalize` is set to `True`.. A higher score means higher accuracy.\n\nExamples:\n\n    Example 1-A simple example\n        >>> accuracy_metric = datasets.load_metric(\"accuracy\")\n        >>> results = accuracy_metric.compute(references=[0, 1, 2, 0, 1, 2], predictions=[0, 1, 1, 2, 1, 0])\n        >>> print(results)\n        {'accuracy': 0.5}\n\n    Example 2-The same as Example 1, except with `normalize` set to `False`.\n        >>> accuracy_metric = datasets.load_metric(\"accuracy\")\n        >>> results = accuracy_metric.compute(references=[0, 1, 2, 0, 1, 2], predictions=[0, 1, 1, 2, 1, 0], normalize=False)\n        >>> print(results)\n        {'accuracy': 3.0}\n\n    Example 3-The same as Example 1, except with `sample_weight` set.\n        >>> accuracy_metric = datasets.load_metric(\"accuracy\")\n        >>> results = accuracy_metric.compute(references=[0, 1, 2, 0, 1, 2], predictions=[0, 1, 1, 2, 1, 0], sample_weight=[0.5, 2, 0.7, 0.5, 9, 0.4])\n        >>> print(results)\n        {'accuracy': 0.8778625954198473}\n"


__A       : List[str]						    = "\n@article{scikit-learn,\n  title={Scikit-learn: Machine Learning in {P}ython},\n  author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V.\n         and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P.\n         and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and\n         Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.},\n  journal={Journal of Machine Learning Research},\n  volume={12},\n  pages={2825--2830},\n  year={2011}\n}\n"







@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION ,   _KWARGS_DESCRIPTION  )
class     lowerCamelCase(	datasets.Metric  ):
     '''simple docstring'''


     def 					lowerCAmelCase__		(						self       ):
         return datasets.MetricInfo(
             description=_DESCRIPTION							,    citation=_CITATION							,    inputs_description=_KWARGS_DESCRIPTION							,    features=datasets.Features(
                 {
                     'predictions': datasets.Sequence(datasets.Value('int32'       )       ),
                     'references': datasets.Sequence(datasets.Value('int32'       )       ),
                 }
                 if self.config_name == 'multilabel'
                 else {
                     'predictions': datasets.Value('int32'       ),
                     'references': datasets.Value('int32'       ),
                 }       )							,    reference_urls=['https://scikit-learn.org/stable/modules/generated/sklearn.metrics.accuracy_score.html']							,    )




     def 					lowerCAmelCase__		(						self							,    snake_case_							,    snake_case_							,    snake_case_=True							,    snake_case_=None       ):
         return {
             "accuracy": float(
                 accuracy_score(snake_case_							,    snake_case_							,    normalize=snake_case_							,    sample_weight=snake_case_       )       )
         }





