
import datasets

from .evaluate import evaluate


SCREAMING_SNAKE_CASE :		int       			=							"""\
@inproceedings{Rajpurkar2016SQuAD10,
  title={SQuAD: 100, 000+ Questions for Machine Comprehension of Text},
  author={Pranav Rajpurkar and Jian Zhang and Konstantin Lopyrev and Percy Liang},
  booktitle={EMNLP},
  year={2016}
}
"""

SCREAMING_SNAKE_CASE :		Optional[Any]       			=							"""
This metric wrap the official scoring script for version 1 of the Stanford Question Answering Dataset (SQuAD).

Stanford Question Answering Dataset (SQuAD) is a reading comprehension dataset, consisting of questions posed by
crowdworkers on a set of Wikipedia articles, where the answer to every question is a segment of text, or span,
from the corresponding reading passage, or the question might be unanswerable.
"""

SCREAMING_SNAKE_CASE :		Optional[Any]       			=							"""
Computes SQuAD scores (F1 and EM).
Args:
    predictions: List of question-answers dictionaries with the following key-values:
        - 'id': id of the question-answer pair as given in the references (see below)
        - 'prediction_text': the text of the answer
    references: List of question-answers dictionaries with the following key-values:
        - 'id': id of the question-answer pair (see above),
        - 'answers': a Dict in the SQuAD dataset format
            {
                'text': list of possible texts for the answer, as a list of strings
                'answer_start': list of start positions for the answer, as a list of ints
            }
            Note that answer_start values are not taken into account to compute the metric.
Returns:
    'exact_match': Exact match (the normalized answer exactly match the gold answer)
    'f1': The F-score of predicted tokens versus the gold answer
Examples:

    >>> predictions = [{'prediction_text': '1976', 'id': '56e10a3be3433e1400422b22'}]
    >>> references = [{'answers': {'answer_start': [97], 'text': ['1976']}, 'id': '56e10a3be3433e1400422b22'}]
    >>> squad_metric = datasets.load_metric(\"squad\")
    >>> results = squad_metric.compute(predictions=predictions, references=references)
    >>> print(results)
    {'exact_match': 100.0, 'f1': 100.0}
"""






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


					def 							lowerCAmelCase_     (						self    )		->				Optional[int]:





									'''simple docstring'''
									return datasets.MetricInfo(
									    description=_DESCRIPTION							,							citation=_CITATION							,							inputs_description=_KWARGS_DESCRIPTION							,							features=datasets.Features(
									        {
									            '''predictions''': {'''id''': datasets.Value('''string'''    ), '''prediction_text''': datasets.Value('''string'''    )},
									            '''references''': {
									                '''id''': datasets.Value('''string'''    ),
									                '''answers''': datasets.features.Sequence(
									                    {
									                        '''text''': datasets.Value('''string'''    ),
									                        '''answer_start''': datasets.Value('''int32'''    ),
									                    }    ),
									            },
									        }    )							,							codebase_urls=['''https://rajpurkar.github.io/SQuAD-explorer/''']							,							reference_urls=['''https://rajpurkar.github.io/SQuAD-explorer/''']							,							)


					def 							lowerCAmelCase_     (						self							,							a__							,							a__    )		->				Optional[Any]:





									'''simple docstring'''
									A_													=	{prediction['''id''']: prediction['''prediction_text'''] for prediction in predictions}
									A_													=	[
									    {
									        '''paragraphs''': [
									            {
									                '''qas''': [
									                    {
									                        '''answers''': [{'''text''': answer_text} for answer_text in ref['''answers''']['''text''']],
									                        '''id''': ref['''id'''],
									                    }
									                    for ref in references
									                ]
									            }
									        ]
									    }
									]
									A_													=	evaluate(dataset=a__							,							predictions=a__    )
									return score







'''simple docstring'''



from typing import List, Optional, Union

import numpy as np
import PIL.Image

from ...image_processing_utils import BaseImageProcessor, BatchFeature
from ...image_transforms import rescale, resize, to_channel_dimension_format
from ...image_utils import (
    ChannelDimension,
    PILImageResampling,
    get_image_size,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, logging


__UpperCamelCase   :							Dict         =   logging.get_logger(__name__)
class 				__SCREAMING_SNAKE_CASE							(				_lowerCAmelCase  ):
      __a	       =["pixel_values"]




      def __init__(	self			, lowerCamelCase = True			, lowerCamelCase = 32			, lowerCamelCase=PILImageResampling.BILINEAR			, lowerCamelCase = True			, **lowerCamelCase			, )				->None:



         '''simple docstring'''
         __a					  =     do_resize
         __a					  =     do_rescale
         __a					  =     size_divisor
         __a					  =     resample
         super().__init__(**lowerCamelCase   )




      def 			__UpperCamelCase				(	self			, lowerCamelCase			, lowerCamelCase			, lowerCamelCase			, lowerCamelCase = None			, **lowerCamelCase   )				->np.ndarray:



         '''simple docstring'''
         __a  ,      __a					  =     get_image_size(lowerCamelCase   )
         # Rounds the height and width down to the closest multiple of size_divisor
         __a					  =     height // size_divisor * size_divisor
         __a					  =     width // size_divisor * size_divisor
         __a					  =     resize(lowerCamelCase			, (new_h, new_w)			, resample=lowerCamelCase			, data_format=lowerCamelCase			, **lowerCamelCase   )
         return image




      def 			__UpperCamelCase				(	self			, lowerCamelCase			, lowerCamelCase			, lowerCamelCase = None			, **lowerCamelCase   )				->np.ndarray:



         '''simple docstring'''
         return rescale(image=lowerCamelCase			, scale=lowerCamelCase			, data_format=lowerCamelCase			, **lowerCamelCase   )






      def 			__UpperCamelCase				(	self			, lowerCamelCase			, lowerCamelCase = None			, lowerCamelCase = None			, lowerCamelCase=None			, lowerCamelCase = None			, lowerCamelCase = None			, lowerCamelCase = ChannelDimension.FIRST			, **lowerCamelCase			, )				->BatchFeature:



         '''simple docstring'''
         __a					  =     do_resize if do_resize is not None else self.do_resize
         __a					  =     do_rescale if do_rescale is not None else self.do_rescale
         __a					  =     size_divisor if size_divisor is not None else self.size_divisor
         __a					  =     resample if resample is not None else self.resample

         if do_resize and size_divisor is None:
            raise ValueError('size_divisor is required for resizing'   )

         __a					  =     make_list_of_images(lowerCamelCase   )

         if not valid_images(lowerCamelCase   ):
            raise ValueError('Invalid image(s)'   )

         # All transformations expect numpy arrays.
         __a					  =     [to_numpy_array(lowerCamelCase   ) for img in images]

         if do_resize:
            __a					  =     [self.resize(lowerCamelCase			, size_divisor=lowerCamelCase			, resample=lowerCamelCase   ) for image in images]

         if do_rescale:
            __a					  =     [self.rescale(lowerCamelCase			, scale=1 / 255   ) for image in images]

         __a					  =     [to_channel_dimension_format(lowerCamelCase			, lowerCamelCase   ) for image in images]

         __a					  =     {'pixel_values': images}
         return BatchFeature(data=lowerCamelCase			, tensor_type=lowerCamelCase   )