



'''simple docstring'''



import os

from bleurt import score  # From: git+https://github.com/google-research/bleurt.git

import datasets


lowerCAmelCase_  :							Tuple             =    datasets.logging.get_logger(__name__)


lowerCAmelCase_  :							Optional[int]             =    '\\n@inproceedings{bleurt,\n  title={BLEURT: Learning Robust Metrics for Text Generation},\n  author={Thibault Sellam and Dipanjan Das and Ankur P. Parikh},\n  booktitle={ACL},\n  year={2020},\n  url={https://arxiv.org/abs/2004.04696}\n}\n'

lowerCAmelCase_  :							str             =    '\\nBLEURT a learnt evaluation metric for Natural Language Generation. It is built using multiple phases of transfer learning starting from a pretrained BERT model (Devlin et al. 2018)\nand then employing another pre-training phrase using synthetic data. Finally it is trained on WMT human annotations. You may run BLEURT out-of-the-box or fine-tune\nit for your specific application (the latter is expected to perform better).\n\nSee the project\'s README at https://github.com/google-research/bleurt#readme for more information.\n'

lowerCAmelCase_  :							int             =    '\nBLEURT score.\n\nArgs:\n    `predictions` (list of str): prediction/candidate sentences\n    `references` (list of str): reference sentences\n    `checkpoint` BLEURT checkpoint. Will default to BLEURT-tiny if None.\n\nReturns:\n    \'scores\': List of scores.\nExamples:\n\n    >>> predictions = ["hello there", "general kenobi"]\n    >>> references = ["hello there", "general kenobi"]\n    >>> bleurt = datasets.load_metric("bleurt")\n    >>> results = bleurt.compute(predictions=predictions, references=references)\n    >>> print([round(v, 2) for v in results["scores"]])\n    [1.03, 1.04]\n'

lowerCAmelCase_  :							str             =    {
    'bleurt-tiny-128': 'https://storage.googleapis.com/bleurt-oss/bleurt-tiny-128.zip',
    'bleurt-tiny-512': 'https://storage.googleapis.com/bleurt-oss/bleurt-tiny-512.zip',
    'bleurt-base-128': 'https://storage.googleapis.com/bleurt-oss/bleurt-base-128.zip',
    'bleurt-base-512': 'https://storage.googleapis.com/bleurt-oss/bleurt-base-512.zip',
    'bleurt-large-128': 'https://storage.googleapis.com/bleurt-oss/bleurt-large-128.zip',
    'bleurt-large-512': 'https://storage.googleapis.com/bleurt-oss/bleurt-large-512.zip',
    'BLEURT-20-D3': 'https://storage.googleapis.com/bleurt-oss-21/BLEURT-20-D3.zip',
    'BLEURT-20-D6': 'https://storage.googleapis.com/bleurt-oss-21/BLEURT-20-D6.zip',
    'BLEURT-20-D12': 'https://storage.googleapis.com/bleurt-oss-21/BLEURT-20-D12.zip',
    'BLEURT-20': 'https://storage.googleapis.com/bleurt-oss-21/BLEURT-20.zip',
}
@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION     ,					_KWARGS_DESCRIPTION      )
class 						lowerCamelCase_				(       datasets.Metric      ):







						def        __lowercase     (	self :   Dict					):

													"""simple docstring"""
													return datasets.MetricInfo(
													    description=_DESCRIPTION					,     citation=_CITATION					,     homepage='''https://github.com/google-research/bleurt'''					,     inputs_description=_KWARGS_DESCRIPTION					,     features=datasets.Features(
													        {
													            '''predictions''': datasets.Value('''string'''					,     id='''sequence'''					),
													            '''references''': datasets.Value('''string'''					,     id='''sequence'''					),
													        }					)					,     codebase_urls=['''https://github.com/google-research/bleurt''']					,     reference_urls=['''https://github.com/google-research/bleurt''', '''https://arxiv.org/abs/2004.04696''']					,     )







						def        __lowercase     (	self :   Tuple					,     lowerCAmelCase__ :   Optional[int]					):

													"""simple docstring"""
													# check that config name specifies a valid BLEURT model
													if self.config_name == "default":
																				logger.warning(
																				    '''Using default BLEURT-Base checkpoint for sequence maximum length 128. '''
																				    '''You can use a bigger model for better results with e.g.: datasets.load_metric(\'bleurt\', \'bleurt-large-512\').'''					)
																				SCREAMING_SNAKE_CASE       :   List[str]         =      '''bleurt-base-128'''

													if self.config_name.lower() in CHECKPOINT_URLS:
																				SCREAMING_SNAKE_CASE       :   Union[str, Any]         =      self.config_name.lower()

													elif self.config_name.upper() in CHECKPOINT_URLS:
																				SCREAMING_SNAKE_CASE       :   str         =      self.config_name.upper()

													else:
																				raise KeyError(
																				    F"""{self.config_name} model not found. You should supply the name of a model checkpoint for bleurt in {CHECKPOINT_URLS.keys()}"""					)

													# download the model checkpoint specified by self.config_name and set up the scorer
													SCREAMING_SNAKE_CASE       :   List[str]         =      dl_manager.download_and_extract(CHECKPOINT_URLS[checkpoint_name]					)
													SCREAMING_SNAKE_CASE       :   int         =      score.BleurtScorer(os.path.join(lowerCAmelCase__					,     lowerCAmelCase__					)					)







						def        __lowercase     (	self :   List[str]					,     lowerCAmelCase__ :   Dict					,     lowerCAmelCase__ :   Any					):

													"""simple docstring"""
													SCREAMING_SNAKE_CASE       :   List[str]         =      self.scorer.score(references=lowerCAmelCase__					,     candidates=lowerCAmelCase__					)
													return {"scores": scores}









'''simple docstring'''



import unittest

import numpy as np

from transformers.testing_utils import is_flaky, require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
							import torch

if is_vision_available():
							from PIL import Image

							from transformers import DonutImageProcessor
class 						lowerCamelCase_				(       unittest.TestCase      ):







						def __init__(	self :   Dict					,     lowerCAmelCase__ :   Union[str, Any]					,     lowerCAmelCase__ :   Dict=7					,     lowerCAmelCase__ :   List[str]=3					,     lowerCAmelCase__ :   Union[str, Any]=18					,     lowerCAmelCase__ :   Tuple=30					,     lowerCAmelCase__ :   Any=4_00					,     lowerCAmelCase__ :   Optional[int]=True					,     lowerCAmelCase__ :   List[Any]=None					,     lowerCAmelCase__ :   int=True					,     lowerCAmelCase__ :   Optional[Any]=False					,     lowerCAmelCase__ :   Any=True					,     lowerCAmelCase__ :   Optional[Any]=True					,     lowerCAmelCase__ :   Union[str, Any]=[0.5, 0.5, 0.5]					,     lowerCAmelCase__ :   List[str]=[0.5, 0.5, 0.5]					,     ):

													"""simple docstring"""
													SCREAMING_SNAKE_CASE       :   Union[str, Any]         =      parent
													SCREAMING_SNAKE_CASE       :   int         =      batch_size
													SCREAMING_SNAKE_CASE       :   Union[str, Any]         =      num_channels
													SCREAMING_SNAKE_CASE       :   Union[str, Any]         =      image_size
													SCREAMING_SNAKE_CASE       :   Any         =      min_resolution
													SCREAMING_SNAKE_CASE       :   int         =      max_resolution
													SCREAMING_SNAKE_CASE       :   Tuple         =      do_resize
													SCREAMING_SNAKE_CASE       :   Optional[int]         =      size if size is not None else {'''height''': 18, '''width''': 20}
													SCREAMING_SNAKE_CASE       :   List[Any]         =      do_thumbnail
													SCREAMING_SNAKE_CASE       :   Union[str, Any]         =      do_align_axis
													SCREAMING_SNAKE_CASE       :   Tuple         =      do_pad
													SCREAMING_SNAKE_CASE       :   Union[str, Any]         =      do_normalize
													SCREAMING_SNAKE_CASE       :   Union[str, Any]         =      image_mean
													SCREAMING_SNAKE_CASE       :   Optional[int]         =      image_std







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

													"""simple docstring"""
													return {
													    "do_resize": self.do_resize,
													    "size": self.size,
													    "do_thumbnail": self.do_thumbnail,
													    "do_align_long_axis": self.do_align_axis,
													    "do_pad": self.do_pad,
													    "do_normalize": self.do_normalize,
													    "image_mean": self.image_mean,
													    "image_std": self.image_std,
													}


@require_torch
@require_vision
class 						lowerCamelCase_				(       snake_case_     ,					unittest.TestCase      ):
						_lowerCAmelCase  : Any						  =					DonutImageProcessor if is_vision_available() else None







						def        __lowercase     (	self :   str					):

													"""simple docstring"""
													SCREAMING_SNAKE_CASE       :   Tuple         =      DonutImageProcessingTester(self					)







						@property
						def        __lowercase     (	self :   int					):

													"""simple docstring"""
													return self.image_processor_tester.prepare_image_processor_dict()







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

													"""simple docstring"""
													SCREAMING_SNAKE_CASE       :   str         =      self.image_processing_class(**self.image_processor_dict					)
													self.assertTrue(hasattr(lowerCAmelCase__					,     '''do_resize'''					)					)
													self.assertTrue(hasattr(lowerCAmelCase__					,     '''size'''					)					)
													self.assertTrue(hasattr(lowerCAmelCase__					,     '''do_thumbnail'''					)					)
													self.assertTrue(hasattr(lowerCAmelCase__					,     '''do_align_long_axis'''					)					)
													self.assertTrue(hasattr(lowerCAmelCase__					,     '''do_pad'''					)					)
													self.assertTrue(hasattr(lowerCAmelCase__					,     '''do_normalize'''					)					)
													self.assertTrue(hasattr(lowerCAmelCase__					,     '''image_mean'''					)					)
													self.assertTrue(hasattr(lowerCAmelCase__					,     '''image_std'''					)					)







						def        __lowercase     (	self :   Any					):

													"""simple docstring"""
													SCREAMING_SNAKE_CASE       :   Optional[int]         =      self.image_processing_class.from_dict(self.image_processor_dict					)
													self.assertEqual(image_processor.size					,     {'''height''': 18, '''width''': 20}					)

													SCREAMING_SNAKE_CASE       :   List[str]         =      self.image_processing_class.from_dict(self.image_processor_dict					,     size=42					)
													self.assertEqual(image_processor.size					,     {'''height''': 42, '''width''': 42}					)

													# Previous config had dimensions in (width, height) order
													SCREAMING_SNAKE_CASE       :   Dict         =      self.image_processing_class.from_dict(self.image_processor_dict					,     size=(42, 84)					)
													self.assertEqual(image_processor.size					,     {'''height''': 84, '''width''': 42}					)







						def        __lowercase     (	self :   int					):

													"""simple docstring"""
													pass







						@is_flaky()
						def        __lowercase     (	self :   int					):

													"""simple docstring"""
													# Initialize image_processing
													SCREAMING_SNAKE_CASE       :   Optional[int]         =      self.image_processing_class(**self.image_processor_dict					)
													# create random PIL images
													SCREAMING_SNAKE_CASE       :   List[str]         =      prepare_image_inputs(self.image_processor_tester					,     equal_resolution=lowerCAmelCase__					)
													for image in image_inputs:
																				self.assertIsInstance(lowerCAmelCase__					,     Image.Image					)

													# Test not batched input
													SCREAMING_SNAKE_CASE       :   Any         =      image_processing(image_inputs[0]					,     return_tensors='''pt'''					).pixel_values
													self.assertEqual(
													    encoded_images.shape					,     (
													        1,
													        self.image_processor_tester.num_channels,
													        self.image_processor_tester.size['''height'''],
													        self.image_processor_tester.size['''width'''],
													    )					,     )

													# Test batched
													SCREAMING_SNAKE_CASE       :   Optional[Any]         =      image_processing(lowerCAmelCase__					,     return_tensors='''pt'''					).pixel_values
													self.assertEqual(
													    encoded_images.shape					,     (
													        self.image_processor_tester.batch_size,
													        self.image_processor_tester.num_channels,
													        self.image_processor_tester.size['''height'''],
													        self.image_processor_tester.size['''width'''],
													    )					,     )







						@is_flaky()
						def        __lowercase     (	self :   Union[str, Any]					):

													"""simple docstring"""
													# Initialize image_processing
													SCREAMING_SNAKE_CASE       :   Optional[Any]         =      self.image_processing_class(**self.image_processor_dict					)
													# create random numpy tensors
													SCREAMING_SNAKE_CASE       :   Tuple         =      prepare_image_inputs(self.image_processor_tester					,     equal_resolution=lowerCAmelCase__					,     numpify=lowerCAmelCase__					)
													for image in image_inputs:
																				self.assertIsInstance(lowerCAmelCase__					,     np.ndarray					)

													# Test not batched input
													SCREAMING_SNAKE_CASE       :   int         =      image_processing(image_inputs[0]					,     return_tensors='''pt'''					).pixel_values
													self.assertEqual(
													    encoded_images.shape					,     (
													        1,
													        self.image_processor_tester.num_channels,
													        self.image_processor_tester.size['''height'''],
													        self.image_processor_tester.size['''width'''],
													    )					,     )

													# Test batched
													SCREAMING_SNAKE_CASE       :   int         =      image_processing(lowerCAmelCase__					,     return_tensors='''pt'''					).pixel_values
													self.assertEqual(
													    encoded_images.shape					,     (
													        self.image_processor_tester.batch_size,
													        self.image_processor_tester.num_channels,
													        self.image_processor_tester.size['''height'''],
													        self.image_processor_tester.size['''width'''],
													    )					,     )







						@is_flaky()
						def        __lowercase     (	self :   List[Any]					):

													"""simple docstring"""
													# Initialize image_processing
													SCREAMING_SNAKE_CASE       :   Optional[Any]         =      self.image_processing_class(**self.image_processor_dict					)
													# create random PyTorch tensors
													SCREAMING_SNAKE_CASE       :   Tuple         =      prepare_image_inputs(self.image_processor_tester					,     equal_resolution=lowerCAmelCase__					,     torchify=lowerCAmelCase__					)
													for image in image_inputs:
																				self.assertIsInstance(lowerCAmelCase__					,     torch.Tensor					)

													# Test not batched input
													SCREAMING_SNAKE_CASE       :   str         =      image_processing(image_inputs[0]					,     return_tensors='''pt'''					).pixel_values
													self.assertEqual(
													    encoded_images.shape					,     (
													        1,
													        self.image_processor_tester.num_channels,
													        self.image_processor_tester.size['''height'''],
													        self.image_processor_tester.size['''width'''],
													    )					,     )

													# Test batched
													SCREAMING_SNAKE_CASE       :   List[str]         =      image_processing(lowerCAmelCase__					,     return_tensors='''pt'''					).pixel_values
													self.assertEqual(
													    encoded_images.shape					,     (
													        self.image_processor_tester.batch_size,
													        self.image_processor_tester.num_channels,
													        self.image_processor_tester.size['''height'''],
													        self.image_processor_tester.size['''width'''],
													    )					,     )




