





import tempfile
import unittest

from transformers import SPIECE_UNDERLINE, BatchEncoding, PLBartTokenizer, is_torch_available
from transformers.testing_utils import (
    get_tests_dir,
    nested_simplify,
    require_sentencepiece,
    require_tokenizers,
    require_torch,
)

from ...test_tokenization_common import TokenizerTesterMixin


UpperCAmelCase_           =	get_tests_dir("fixtures/test_sentencepiece.model")


if is_torch_available():
   from transformers.models.plbart.modeling_plbart import shift_tokens_right

UpperCAmelCase_           =	5_00_03
UpperCAmelCase_           =	5_00_02




@require_sentencepiece
@require_tokenizers
class 			__UpperCamelCase    (							_UpperCamelCase	, unittest.TestCase    ):
     __A	:  List[str]							  =   PLBartTokenizer
     __A	:  List[str]							  =   None
     __A	:  List[str]							  =   False





     def 		UpperCamelCase(     self					):
        super().setUp()

        # We have a SentencePiece fixture for testing
        _UpperCAmelCase           =   PLBartTokenizer(_UpperCAmelCase				,							language_codes='''base'''				,							keep_accents=_UpperCAmelCase					)
        tokenizer.save_pretrained(self.tmpdirname					)





     def 		UpperCamelCase(     self					):
        _UpperCAmelCase           =   PLBartTokenizer(_UpperCAmelCase				,							language_codes='''base'''				,							keep_accents=_UpperCAmelCase					)

        _UpperCAmelCase           =   tokenizer.tokenize('''This is a test'''					)
        self.assertListEqual(_UpperCAmelCase				,							['''▁This''', '''▁is''', '''▁a''', '''▁t''', '''est''']					)

        self.assertListEqual(
            tokenizer.convert_tokens_to_ids(_UpperCAmelCase					)				,							[value + tokenizer.fairseq_offset for value in [285, 46, 10, 170, 382]]				,							)

        _UpperCAmelCase           =   tokenizer.tokenize('''I was born in 92000, and this is falsé.'''					)
        self.assertListEqual(
            _UpperCAmelCase				,							[
                SPIECE_UNDERLINE + '''I''',
                SPIECE_UNDERLINE + '''was''',
                SPIECE_UNDERLINE + '''b''',
                '''or''',
                '''n''',
                SPIECE_UNDERLINE + '''in''',
                SPIECE_UNDERLINE + '''''',
                '''9''',
                '''2''',
                '''0''',
                '''0''',
                '''0''',
                ''',''',
                SPIECE_UNDERLINE + '''and''',
                SPIECE_UNDERLINE + '''this''',
                SPIECE_UNDERLINE + '''is''',
                SPIECE_UNDERLINE + '''f''',
                '''al''',
                '''s''',
                '''é''',
                '''.''',
            ]				,							)
        _UpperCAmelCase           =   tokenizer.convert_tokens_to_ids(_UpperCAmelCase					)
        self.assertListEqual(
            _UpperCAmelCase				,							[
                value + tokenizer.fairseq_offset
                for value in [8, 21, 84, 55, 24, 19, 7, 2, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 2, 4]
            ]				,							)

        _UpperCAmelCase           =   tokenizer.convert_ids_to_tokens(_UpperCAmelCase					)
        self.assertListEqual(
            _UpperCAmelCase				,							[
                SPIECE_UNDERLINE + '''I''',
                SPIECE_UNDERLINE + '''was''',
                SPIECE_UNDERLINE + '''b''',
                '''or''',
                '''n''',
                SPIECE_UNDERLINE + '''in''',
                SPIECE_UNDERLINE + '''''',
                '''<unk>''',
                '''2''',
                '''0''',
                '''0''',
                '''0''',
                ''',''',
                SPIECE_UNDERLINE + '''and''',
                SPIECE_UNDERLINE + '''this''',
                SPIECE_UNDERLINE + '''is''',
                SPIECE_UNDERLINE + '''f''',
                '''al''',
                '''s''',
                '''<unk>''',
                '''.''',
            ]				,							)

        _UpperCAmelCase           =   tokenizer.vocab_size
        _UpperCAmelCase           =   [tokenizer.convert_ids_to_tokens(_UpperCAmelCase					) for x in range(end - 4				,							_UpperCAmelCase					)]

        self.assertListEqual(_UpperCAmelCase				,							['''__java__''', '''__python__''', '''__en_XX__''', '''<mask>''']					)

        _UpperCAmelCase           =   'java.lang.Exception, python.lang.Exception, javascript, php, ruby, go'
        _UpperCAmelCase           =   tokenizer(_UpperCAmelCase					).input_ids
        self.assertEqual(
            tokenizer.decode(_UpperCAmelCase				,							skip_special_tokens=_UpperCAmelCase				,							clean_up_tokenization_spaces=_UpperCAmelCase					)				,							_UpperCAmelCase				,							)





     def 		UpperCamelCase(     self					):
        _UpperCAmelCase           =   PLBartTokenizer(_UpperCAmelCase				,							language_codes='''multi'''				,							keep_accents=_UpperCAmelCase					)

        _UpperCAmelCase           =   tokenizer.tokenize('''This is a test'''					)
        self.assertListEqual(_UpperCAmelCase				,							['''▁This''', '''▁is''', '''▁a''', '''▁t''', '''est''']					)

        self.assertListEqual(
            tokenizer.convert_tokens_to_ids(_UpperCAmelCase					)				,							[value + tokenizer.fairseq_offset for value in [285, 46, 10, 170, 382]]				,							)

        _UpperCAmelCase           =   tokenizer.tokenize('''I was born in 92000, and this is falsé.'''					)
        self.assertListEqual(
            _UpperCAmelCase				,							[
                SPIECE_UNDERLINE + '''I''',
                SPIECE_UNDERLINE + '''was''',
                SPIECE_UNDERLINE + '''b''',
                '''or''',
                '''n''',
                SPIECE_UNDERLINE + '''in''',
                SPIECE_UNDERLINE + '''''',
                '''9''',
                '''2''',
                '''0''',
                '''0''',
                '''0''',
                ''',''',
                SPIECE_UNDERLINE + '''and''',
                SPIECE_UNDERLINE + '''this''',
                SPIECE_UNDERLINE + '''is''',
                SPIECE_UNDERLINE + '''f''',
                '''al''',
                '''s''',
                '''é''',
                '''.''',
            ]				,							)
        _UpperCAmelCase           =   tokenizer.convert_tokens_to_ids(_UpperCAmelCase					)
        self.assertListEqual(
            _UpperCAmelCase				,							[
                value + tokenizer.fairseq_offset
                for value in [8, 21, 84, 55, 24, 19, 7, 2, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 2, 4]
            ]				,							)

        _UpperCAmelCase           =   tokenizer.convert_ids_to_tokens(_UpperCAmelCase					)
        self.assertListEqual(
            _UpperCAmelCase				,							[
                SPIECE_UNDERLINE + '''I''',
                SPIECE_UNDERLINE + '''was''',
                SPIECE_UNDERLINE + '''b''',
                '''or''',
                '''n''',
                SPIECE_UNDERLINE + '''in''',
                SPIECE_UNDERLINE + '''''',
                '''<unk>''',
                '''2''',
                '''0''',
                '''0''',
                '''0''',
                ''',''',
                SPIECE_UNDERLINE + '''and''',
                SPIECE_UNDERLINE + '''this''',
                SPIECE_UNDERLINE + '''is''',
                SPIECE_UNDERLINE + '''f''',
                '''al''',
                '''s''',
                '''<unk>''',
                '''.''',
            ]				,							)
        _UpperCAmelCase           =   tokenizer.vocab_size
        _UpperCAmelCase           =   [tokenizer.convert_ids_to_tokens(_UpperCAmelCase					) for x in range(end - 7				,							_UpperCAmelCase					)]

        self.assertListEqual(
            _UpperCAmelCase				,							['''__java__''', '''__python__''', '''__en_XX__''', '''__javascript__''', '''__php__''', '''__ruby__''', '''__go__''']					)
        _UpperCAmelCase           =   'java.lang.Exception, python.lang.Exception, javascript, php, ruby, go'
        _UpperCAmelCase           =   tokenizer(_UpperCAmelCase					).input_ids
        self.assertEqual(
            tokenizer.decode(_UpperCAmelCase				,							skip_special_tokens=_UpperCAmelCase				,							clean_up_tokenization_spaces=_UpperCAmelCase					)				,							_UpperCAmelCase				,							)




@require_torch
@require_sentencepiece
@require_tokenizers
class 			__UpperCamelCase    (							unittest.TestCase    ):
     __A	:  List[Any]							  =   'uclanlp/plbart-python-en_XX'
     __A	:  Any							  =   [
         'def maximum(a,b,c):NEW_LINE_INDENTreturn max([a,b,c])',
         'def sum(a,b,c):NEW_LINE_INDENTreturn sum([a,b,c])',
     ]
     __A	:  str							  =   [
         'Returns the maximum value of a b c.',
         'Sums the values of a b c.',
     ]
     __A	:  Optional[Any]							  =   [
         1_34,
         54_52,
         3_34_60,
         3_34_41,
         3_34_63,
         3_34_65,
         3_34_63,
         3_34_49,
         9_88,
         20,
         3_34_56,
         19,
         3_34_56,
         7_71,
         39,
         42_58,
         8_89,
         33_18,
         3_34_41,
         3_34_63,
         3_34_65,
         3_34_63,
         3_34_49,
         24_71,
         2,
         PYTHON_CODE,
     ]





     @classmethod
     def 		UpperCamelCase(     cls					):
        _UpperCAmelCase           =   PLBartTokenizer.from_pretrained(
            cls.checkpoint_name				,							language_codes='''base'''				,							src_lang='''python'''				,							tgt_lang='''en_XX'''					)
        _UpperCAmelCase           =   1
        return cls





     def 		UpperCamelCase(     self					):
        self.assertEqual(self.tokenizer.fairseq_tokens_to_ids['''__java__''']				,							50001					)
        self.assertEqual(self.tokenizer.fairseq_tokens_to_ids['''__python__''']				,							50002					)
        self.assertEqual(self.tokenizer.fairseq_tokens_to_ids['''__en_XX__''']				,							50003					)





     def 		UpperCamelCase(     self					):
        _UpperCAmelCase           =   self.tokenizer.batch_encode_plus(self.src_text					).input_ids[0]
        self.assertListEqual(self.expected_src_tokens				,							_UpperCAmelCase					)





     def 		UpperCamelCase(     self					):
        self.assertIn(_UpperCAmelCase				,							self.tokenizer.all_special_ids					)
        _UpperCAmelCase           =   [EN_CODE, 9037, 33442, 57, 752, 153, 14, 56, 18, 9, 2]
        _UpperCAmelCase           =   self.tokenizer.decode(_UpperCAmelCase				,							skip_special_tokens=_UpperCAmelCase					)
        _UpperCAmelCase           =   self.tokenizer.decode(generated_ids[1:]				,							skip_special_tokens=_UpperCAmelCase					)
        self.assertEqual(_UpperCAmelCase				,							_UpperCAmelCase					)
        self.assertNotIn(self.tokenizer.eos_token				,							_UpperCAmelCase					)





     def 		UpperCamelCase(     self					):
        _UpperCAmelCase           =   ['def sum(a,b,c):NEW_LINE_INDENTreturn sum([a,b,c])' * 20]
        self.assertIsInstance(src_text[0]				,							_UpperCAmelCase					)
        _UpperCAmelCase           =   10
        _UpperCAmelCase           =   self.tokenizer(_UpperCAmelCase				,							max_length=_UpperCAmelCase				,							truncation=_UpperCAmelCase					).input_ids[0]
        self.assertEqual(ids[-2]				,							2					)
        self.assertEqual(ids[-1]				,							_UpperCAmelCase					)
        self.assertEqual(len(_UpperCAmelCase					)				,							_UpperCAmelCase					)





     def 		UpperCamelCase(     self					):
        self.assertListEqual(self.tokenizer.convert_tokens_to_ids(['''<mask>''', '''__java__''']					)				,							[50004, 50001]					)





     def 		UpperCamelCase(     self					):
        _UpperCAmelCase           =   tempfile.mkdtemp()
        _UpperCAmelCase           =   self.tokenizer.fairseq_tokens_to_ids
        self.tokenizer.save_pretrained(_UpperCAmelCase					)
        _UpperCAmelCase           =   PLBartTokenizer.from_pretrained(_UpperCAmelCase					)
        self.assertDictEqual(new_tok.fairseq_tokens_to_ids				,							_UpperCAmelCase					)





     @require_torch
     def 		UpperCamelCase(     self					):
        _UpperCAmelCase           =   self.tokenizer(self.src_text				,							text_target=self.tgt_text				,							padding=_UpperCAmelCase				,							return_tensors='''pt'''					)
        _UpperCAmelCase           =   shift_tokens_right(batch['''labels''']				,							self.tokenizer.pad_token_id					)

        # fairseq batch: https://gist.github.com/sshleifer/cba08bc2109361a74ac3760a7e30e4f4
        self.assertEqual(batch.input_ids[1][-2:].tolist()				,							[2, PYTHON_CODE]					)
        self.assertEqual(batch.decoder_input_ids[1][0]				,							_UpperCAmelCase					)
        self.assertEqual(batch.decoder_input_ids[1][-1]				,							2					)
        self.assertEqual(batch.labels[1][-2:].tolist()				,							[2, EN_CODE]					)





     @require_torch
     def 		UpperCamelCase(     self					):
        _UpperCAmelCase           =   self.tokenizer(
            self.src_text				,							text_target=self.tgt_text				,							padding=_UpperCAmelCase				,							truncation=_UpperCAmelCase				,							max_length=len(self.expected_src_tokens					)				,							return_tensors='''pt'''				,							)
        _UpperCAmelCase           =   shift_tokens_right(batch['''labels''']				,							self.tokenizer.pad_token_id					)

        self.assertIsInstance(_UpperCAmelCase				,							_UpperCAmelCase					)

        self.assertEqual((2, 26)				,							batch.input_ids.shape					)
        self.assertEqual((2, 26)				,							batch.attention_mask.shape					)
        _UpperCAmelCase           =   batch.input_ids.tolist()[0]
        self.assertListEqual(self.expected_src_tokens				,							_UpperCAmelCase					)
        self.assertEqual(2				,							batch.decoder_input_ids[0, -1]					)  # EOS
        # Test that special tokens are reset
        self.assertEqual(self.tokenizer.prefix_tokens				,							[]					)
        self.assertEqual(self.tokenizer.suffix_tokens				,							[self.tokenizer.eos_token_id, PYTHON_CODE]					)





     def 		UpperCamelCase(     self					):
        _UpperCAmelCase           =   self.tokenizer(self.src_text				,							padding=_UpperCAmelCase				,							truncation=_UpperCAmelCase				,							max_length=3				,							return_tensors='''pt'''					)
        _UpperCAmelCase           =   self.tokenizer(
            text_target=self.tgt_text				,							padding=_UpperCAmelCase				,							truncation=_UpperCAmelCase				,							max_length=10				,							return_tensors='''pt'''					)
        _UpperCAmelCase           =   targets['input_ids']
        _UpperCAmelCase           =   shift_tokens_right(_UpperCAmelCase				,							self.tokenizer.pad_token_id					)

        self.assertEqual(batch.input_ids.shape[1]				,							3					)
        self.assertEqual(batch.decoder_input_ids.shape[1]				,							10					)





     @require_torch
     def 		UpperCamelCase(     self					):
        _UpperCAmelCase           =   self.tokenizer._build_translation_inputs(
            '''A test'''				,							return_tensors='''pt'''				,							src_lang='''en_XX'''				,							tgt_lang='''java'''					)

        self.assertEqual(
            nested_simplify(_UpperCAmelCase					)				,							{
                # A, test, EOS, en_XX
                '''input_ids''': [[150, 242, 2, 50003]],
                '''attention_mask''': [[1, 1, 1, 1]],
                # java
                '''forced_bos_token_id''': 50001,
            }				,							)




'''simple docstring'''

import faiss  # noqa: F401 # Here to have a nice missing dependency error message early on
import numpy  # noqa: F401 # Here to have a nice missing dependency error message early on
import requests  # noqa: F401 # Here to have a nice missing dependency error message early on
import sklearn  # noqa: F401 # Here to have a nice missing dependency error message early on
import tqdm  # noqa: F401 # Here to have a nice missing dependency error message early on
from mauve import compute_mauve  # From: mauve-text

import datasets


__lowerCAmelCase		     =  '''\
@inproceedings{pillutla-etal:mauve:neurips2021,
  title={MAUVE: Measuring the Gap Between Neural Text and Human Text using Divergence Frontiers},
  author={Pillutla, Krishna and Swayamdipta, Swabha and Zellers, Rowan and Thickstun, John and Welleck, Sean and Choi, Yejin and Harchaoui, Zaid},
  booktitle = {NeurIPS},
  year      = {2021}
}

'''

__lowerCAmelCase		     =  '''\
MAUVE is a library built on PyTorch and HuggingFace Transformers to measure the gap between neural text and human text with the eponymous MAUVE measure.

MAUVE summarizes both Type I and Type II errors measured softly using Kullback–Leibler (KL) divergences.

For details, see the MAUVE paper: https://arxiv.org/abs/2102.01454 (Neurips, 2021).

This metrics is a wrapper around the official implementation of MAUVE:
https://github.com/krishnap25/mauve
'''

__lowerCAmelCase		     =  '''
Calculates MAUVE scores between two lists of generated text and reference text.
Args:
    predictions: list of generated text to score. Each predictions
        should be a string with tokens separated by spaces.
    references: list of reference for each prediction. Each
        reference should be a string with tokens separated by spaces.
Optional Args:
    num_buckets: the size of the histogram to quantize P and Q. Options: \'auto\' (default) or an integer
    pca_max_data: the number data points to use for PCA dimensionality reduction prior to clustering. If -1, use all the data. Default -1
    kmeans_explained_var: amount of variance of the data to keep in dimensionality reduction by PCA. Default 0.9
    kmeans_num_redo: number of times to redo k-means clustering (the best objective is kept). Default 5
    kmeans_max_iter: maximum number of k-means iterations. Default 500
    featurize_model_name: name of the model from which features are obtained. Default \'gpt2-large\' Use one of [\'gpt2\', \'gpt2-medium\', \'gpt2-large\', \'gpt2-xl\'].
    device_id: Device for featurization. Supply a GPU id (e.g. 0 or 3) to use GPU. If no GPU with this id is found, use CPU
    max_text_length: maximum number of tokens to consider. Default 1024
    divergence_curve_discretization_size: Number of points to consider on the divergence curve. Default 25
    mauve_scaling_factor: "c" from the paper. Default 5.
    verbose: If True (default), print running time updates
    seed: random seed to initialize k-means cluster assignments.
Returns:
    mauve: MAUVE score, a number between 0 and 1. Larger values indicate that P and Q are closer,
    frontier_integral: Frontier Integral, a number between 0 and 1. Smaller values indicate that P and Q are closer,
    divergence_curve: a numpy.ndarray of shape (m, 2); plot it with matplotlib to view the divergence curve,
    p_hist: a discrete distribution, which is a quantized version of the text distribution p_text,
    q_hist: same as above, but with q_text.
Examples:

    >>> # faiss segfaults in doctest for some reason, so the .compute call is not tested with doctest
    >>> import datasets
    >>> mauve = datasets.load_metric(\'mauve\')
    >>> predictions = ["hello there", "general kenobi"]
    >>> references = ["hello there", "general kenobi"]
    >>> out = mauve.compute(predictions=predictions, references=references) # doctest: +SKIP
    >>> print(out.mauve) # doctest: +SKIP
    1.0
'''

@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION     ,		_KWARGS_DESCRIPTION  )
class       __magic_name__		(       datasets.Metric  ):







							def 				__lowercase     (				self   :			Optional[Any]						):
									return datasets.MetricInfo(
									    description=_DESCRIPTION      ,citation=_CITATION      ,homepage='https://github.com/krishnap25/mauve'      ,inputs_description=_KWARGS_DESCRIPTION      ,features=datasets.Features(
									        {
									            'predictions': datasets.Value('string'      ,id='sequence'						),
									            'references': datasets.Value('string'      ,id='sequence'						),
									        }						)      ,codebase_urls=['https://github.com/krishnap25/mauve']      ,reference_urls=[
									        'https://arxiv.org/abs/2102.01454',
									        'https://github.com/krishnap25/mauve',
									    ]      ,)







							def 				__lowercase     (				self   :			Tuple      ,_UpperCAmelCase   :			Optional[Any]      ,_UpperCAmelCase   :			Dict      ,_UpperCAmelCase   :			Any=None      ,_UpperCAmelCase   :			Tuple=None      ,_UpperCAmelCase   :			List[str]=None      ,_UpperCAmelCase   :			Optional[Any]=None      ,_UpperCAmelCase   :			Any="auto"      ,_UpperCAmelCase   :			Tuple=-1      ,_UpperCAmelCase   :			Union[str, Any]=0.9      ,_UpperCAmelCase   :			List[Any]=5      ,_UpperCAmelCase   :			Optional[int]=500      ,_UpperCAmelCase   :			int="gpt2-large"      ,_UpperCAmelCase   :			int=-1      ,_UpperCAmelCase   :			Tuple=1024      ,_UpperCAmelCase   :			Any=25      ,_UpperCAmelCase   :			int=5      ,_UpperCAmelCase   :			Optional[int]=True      ,_UpperCAmelCase   :			Optional[int]=25      ,):
									_a	: Any								=				compute_mauve(
									    p_text=_UpperCAmelCase      ,q_text=_UpperCAmelCase      ,p_features=_UpperCAmelCase      ,q_features=_UpperCAmelCase      ,p_tokens=_UpperCAmelCase      ,q_tokens=_UpperCAmelCase      ,num_buckets=_UpperCAmelCase      ,pca_max_data=_UpperCAmelCase      ,kmeans_explained_var=_UpperCAmelCase      ,kmeans_num_redo=_UpperCAmelCase      ,kmeans_max_iter=_UpperCAmelCase      ,featurize_model_name=_UpperCAmelCase      ,device_id=_UpperCAmelCase      ,max_text_length=_UpperCAmelCase      ,divergence_curve_discretization_size=_UpperCAmelCase      ,mauve_scaling_factor=_UpperCAmelCase      ,verbose=_UpperCAmelCase      ,seed=_UpperCAmelCase      ,)
									return out




