'''simple docstring'''




import hashlib
import unittest
from typing import Dict

import numpy as np

from transformers import (
    MODEL_FOR_MASK_GENERATION_MAPPING,
    TF_MODEL_FOR_MASK_GENERATION_MAPPING,
    is_vision_available,
    pipeline,
)
from transformers.pipelines import MaskGenerationPipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    require_torch,
    require_vision,
    slow,
)


if is_vision_available():
						from PIL import Image
else:





						class     __magic_name__						:







									@staticmethod
									def 	SCREAMING_SNAKE_CASE_     (	*lowercase_				:  Tuple				,			**lowercase_				:  Any    ):
																pass




def 							lowerCamelCase	(      UpperCAmelCase__       :					Image )			->      str:
							lowercase_					:			Optional[int]										=       hashlib.mda(image.tobytes() )
							return m.hexdigest()[:10]




def 							lowerCamelCase	(      UpperCAmelCase__       :					Image )			->      Dict:
							lowercase_					:			str										=       np.array(UpperCAmelCase__ )
							lowercase_					:			int										=       npimg.shape
							return {"hash": hashimage(UpperCAmelCase__ ), "shape": shape}





@is_pipeline_test
@require_vision
@require_torch
class     __magic_name__						(    unittest.TestCase):
			UpperCamelCase__				=       dict(
			    (list(MODEL_FOR_MASK_GENERATION_MAPPING.items()) if MODEL_FOR_MASK_GENERATION_MAPPING else []))
			UpperCamelCase__				=       dict(
			    (list(TF_MODEL_FOR_MASK_GENERATION_MAPPING.items()) if TF_MODEL_FOR_MASK_GENERATION_MAPPING else []))







			def 	SCREAMING_SNAKE_CASE_     (	self				:  str				,			lowercase_				:  Optional[int]				,			lowercase_				:  Optional[Any]				,			lowercase_				:  Dict    ):
										lowercase_					:			Dict										=       MaskGenerationPipeline(model=lowercase_				,			image_processor=lowercase_    )
										return image_segmenter, [
										    "./tests/fixtures/tests_samples/COCO/000000039769.png",
										    "./tests/fixtures/tests_samples/COCO/000000039769.png",
										]







			def 	SCREAMING_SNAKE_CASE_     (	self				:  List[Any]				,			lowercase_				:  List[str]				,			lowercase_				:  int    ):
										pass







			@require_tf
			@unittest.skip("""Image segmentation not implemented in TF"""    )
			def 	SCREAMING_SNAKE_CASE_     (	self				:  Optional[Any]    ):
										pass







			@slow
			@require_torch
			def 	SCREAMING_SNAKE_CASE_     (	self				:  Optional[int]    ):
										lowercase_					:			str										=       pipeline("""mask-generation"""				,			model="""facebook/sam-vit-huge"""    )

										lowercase_					:			Union[str, Any]										=       image_segmenter("""http://images.cocodataset.org/val2017/000000039769.jpg"""				,			points_per_batch=256    )

										# Shortening by hashing
										lowercase_					:			List[Any]										=       []
										for i, o in enumerate(outputs["""masks"""]    ):
																	new_outupt += [{"mask": mask_to_test_readable(lowercase_    ), "scores": outputs["scores"][i]}]

										# fmt: off
										self.assertEqual(
										    nested_simplify(lowercase_				,			decimals=4    )				,			[
										        {"""mask""": {"""hash""": """115ad19f5f""", """shape""": (480, 640)}, """scores""": 1.04_44},
										        {"""mask""": {"""hash""": """6affa964c6""", """shape""": (480, 640)}, """scores""": 1.0_21},
										        {"""mask""": {"""hash""": """dfe28a0388""", """shape""": (480, 640)}, """scores""": 1.01_67},
										        {"""mask""": {"""hash""": """c0a5f4a318""", """shape""": (480, 640)}, """scores""": 1.01_32},
										        {"""mask""": {"""hash""": """fe8065c197""", """shape""": (480, 640)}, """scores""": 1.00_53},
										        {"""mask""": {"""hash""": """e2d0b7a0b7""", """shape""": (480, 640)}, """scores""": 0.99_67},
										        {"""mask""": {"""hash""": """453c7844bd""", """shape""": (480, 640)}, """scores""": 0.9_93},
										        {"""mask""": {"""hash""": """3d44f2926d""", """shape""": (480, 640)}, """scores""": 0.99_09},
										        {"""mask""": {"""hash""": """64033ddc3f""", """shape""": (480, 640)}, """scores""": 0.98_79},
										        {"""mask""": {"""hash""": """801064ff79""", """shape""": (480, 640)}, """scores""": 0.98_34},
										        {"""mask""": {"""hash""": """6172f276ef""", """shape""": (480, 640)}, """scores""": 0.97_16},
										        {"""mask""": {"""hash""": """b49e60e084""", """shape""": (480, 640)}, """scores""": 0.96_12},
										        {"""mask""": {"""hash""": """a811e775fd""", """shape""": (480, 640)}, """scores""": 0.95_99},
										        {"""mask""": {"""hash""": """a6a8ebcf4b""", """shape""": (480, 640)}, """scores""": 0.95_52},
										        {"""mask""": {"""hash""": """9d8257e080""", """shape""": (480, 640)}, """scores""": 0.95_32},
										        {"""mask""": {"""hash""": """32de6454a8""", """shape""": (480, 640)}, """scores""": 0.95_16},
										        {"""mask""": {"""hash""": """af3d4af2c8""", """shape""": (480, 640)}, """scores""": 0.94_99},
										        {"""mask""": {"""hash""": """3c6db475fb""", """shape""": (480, 640)}, """scores""": 0.94_83},
										        {"""mask""": {"""hash""": """c290813fb9""", """shape""": (480, 640)}, """scores""": 0.94_64},
										        {"""mask""": {"""hash""": """b6f0b8f606""", """shape""": (480, 640)}, """scores""": 0.9_43},
										        {"""mask""": {"""hash""": """92ce16bfdf""", """shape""": (480, 640)}, """scores""": 0.9_43},
										        {"""mask""": {"""hash""": """c749b25868""", """shape""": (480, 640)}, """scores""": 0.94_08},
										        {"""mask""": {"""hash""": """efb6cab859""", """shape""": (480, 640)}, """scores""": 0.93_35},
										        {"""mask""": {"""hash""": """1ff2eafb30""", """shape""": (480, 640)}, """scores""": 0.93_26},
										        {"""mask""": {"""hash""": """788b798e24""", """shape""": (480, 640)}, """scores""": 0.92_62},
										        {"""mask""": {"""hash""": """abea804f0e""", """shape""": (480, 640)}, """scores""": 0.89_99},
										        {"""mask""": {"""hash""": """7b9e8ddb73""", """shape""": (480, 640)}, """scores""": 0.89_86},
										        {"""mask""": {"""hash""": """cd24047c8a""", """shape""": (480, 640)}, """scores""": 0.89_84},
										        {"""mask""": {"""hash""": """6943e6bcbd""", """shape""": (480, 640)}, """scores""": 0.88_73},
										        {"""mask""": {"""hash""": """b5f47c9191""", """shape""": (480, 640)}, """scores""": 0.88_71}
										    ]				,			)
										# fmt: on







			@require_torch
			@slow
			def 	SCREAMING_SNAKE_CASE_     (	self				:  int    ):
										lowercase_					:			Union[str, Any]										=       """facebook/sam-vit-huge"""
										lowercase_					:			Optional[int]										=       pipeline("""mask-generation"""				,			model=lowercase_    )

										lowercase_					:			str										=       image_segmenter(
										    """http://images.cocodataset.org/val2017/000000039769.jpg"""				,			pred_iou_thresh=1				,			points_per_batch=256    )

										# Shortening by hashing
										lowercase_					:			List[str]										=       []
										for i, o in enumerate(outputs["""masks"""]    ):
																	new_outupt += [{"mask": mask_to_test_readable(lowercase_    ), "scores": outputs["scores"][i]}]

										self.assertEqual(
										    nested_simplify(lowercase_				,			decimals=4    )				,			[
										        {"""mask""": {"""hash""": """115ad19f5f""", """shape""": (480, 640)}, """scores""": 1.04_44},
										        {"""mask""": {"""hash""": """6affa964c6""", """shape""": (480, 640)}, """scores""": 1.02_10},
										        {"""mask""": {"""hash""": """dfe28a0388""", """shape""": (480, 640)}, """scores""": 1.01_67},
										        {"""mask""": {"""hash""": """c0a5f4a318""", """shape""": (480, 640)}, """scores""": 1.01_32},
										        {"""mask""": {"""hash""": """fe8065c197""", """shape""": (480, 640)}, """scores""": 1.00_53},
										    ]				,			)






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


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

_lowercase						: List[Any]						=						"\\nMAUVE is a library built on PyTorch and HuggingFace Transformers to measure the gap between neural text and human text with the eponymous MAUVE measure.\n\nMAUVE summarizes both Type I and Type II errors measured softly using Kullback–Leibler (KL) divergences.\n\nFor details, see the MAUVE paper: https://arxiv.org/abs/2102.01454 (Neurips, 2021).\n\nThis metrics is a wrapper around the official implementation of MAUVE:\nhttps://github.com/krishnap25/mauve\n"

_lowercase						: List[Any]						=						"\nCalculates MAUVE scores between two lists of generated text and reference text.\nArgs:\n    predictions: list of generated text to score. Each predictions\n        should be a string with tokens separated by spaces.\n    references: list of reference for each prediction. Each\n        reference should be a string with tokens separated by spaces.\nOptional Args:\n    num_buckets: the size of the histogram to quantize P and Q. Options: 'auto' (default) or an integer\n    pca_max_data: the number data points to use for PCA dimensionality reduction prior to clustering. If -1, use all the data. Default -1\n    kmeans_explained_var: amount of variance of the data to keep in dimensionality reduction by PCA. Default 0.9\n    kmeans_num_redo: number of times to redo k-means clustering (the best objective is kept). Default 5\n    kmeans_max_iter: maximum number of k-means iterations. Default 500\n    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'].\n    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\n    max_text_length: maximum number of tokens to consider. Default 1024\n    divergence_curve_discretization_size: Number of points to consider on the divergence curve. Default 25\n    mauve_scaling_factor: \"c\" from the paper. Default 5.\n    verbose: If True (default), print running time updates\n    seed: random seed to initialize k-means cluster assignments.\nReturns:\n    mauve: MAUVE score, a number between 0 and 1. Larger values indicate that P and Q are closer,\n    frontier_integral: Frontier Integral, a number between 0 and 1. Smaller values indicate that P and Q are closer,\n    divergence_curve: a numpy.ndarray of shape (m, 2); plot it with matplotlib to view the divergence curve,\n    p_hist: a discrete distribution, which is a quantized version of the text distribution p_text,\n    q_hist: same as above, but with q_text.\nExamples:\n\n    >>> # faiss segfaults in doctest for some reason, so the .compute call is not tested with doctest\n    >>> import datasets\n    >>> mauve = datasets.load_metric('mauve')\n    >>> predictions = [\"hello there\", \"general kenobi\"]\n    >>> references = [\"hello there\", \"general kenobi\"]\n    >>> out = mauve.compute(predictions=predictions, references=references) # doctest: +SKIP\n    >>> print(out.mauve) # doctest: +SKIP\n    1.0\n"





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







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





