
"""simple docstring"""




import unittest

import numpy as np

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

from ...test_image_processing_common import ImageProcessingSavingTestMixin


if is_torch_available():
	import torch

if is_vision_available():
	from PIL import Image

	from transformers import BlipImageProcessor




class 					UpperCamelCase     (	unittest.TestCase				):

				"""simple docstring"""



				def __init__(    self		,     lowercase__		,     lowercase__=7		,     lowercase__=3		,     lowercase__=18		,     lowercase__=30		,     lowercase__=400		,     lowercase__=True		,     lowercase__=None		,     lowercase__=True		,     lowercase__=False		,     lowercase__=[0.48_145_466, 0.4_578_275, 0.40_821_073]		,     lowercase__=[0.26_862_954, 0.26_130_258, 0.27_577_711]		,     lowercase__=True		,     )   ->   Union[str, Any]:
									"""simple docstring"""







									SCREAMING_SNAKE_CASE           =     size if size is not None else {'height': 20, 'width': 20}
									SCREAMING_SNAKE_CASE           =     parent
									SCREAMING_SNAKE_CASE           =     batch_size
									SCREAMING_SNAKE_CASE           =     num_channels
									SCREAMING_SNAKE_CASE           =     image_size
									SCREAMING_SNAKE_CASE           =     min_resolution
									SCREAMING_SNAKE_CASE           =     max_resolution
									SCREAMING_SNAKE_CASE           =     do_resize
									SCREAMING_SNAKE_CASE           =     size
									SCREAMING_SNAKE_CASE           =     do_normalize
									SCREAMING_SNAKE_CASE           =     image_mean
									SCREAMING_SNAKE_CASE           =     image_std
									SCREAMING_SNAKE_CASE           =     do_pad
									SCREAMING_SNAKE_CASE           =     do_convert_rgb


				def 							A (    self      )   ->   List[str]:
									"""simple docstring"""







									return {
									    "do_resize": self.do_resize,
									    "size": self.size,
									    "do_normalize": self.do_normalize,
									    "image_mean": self.image_mean,
									    "image_std": self.image_std,
									    "do_convert_rgb": self.do_convert_rgb,
									    "do_pad": self.do_pad,
									}




				def 							A (    self		,     lowercase__=False		,     lowercase__=False		,     lowercase__=False      )   ->   int:
									"""simple docstring"""







									assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

									if equal_resolution:
														SCREAMING_SNAKE_CASE           =     []
														for i in range(self.batch_size      ):
																			image_inputs.append(
																			    np.random.randint(
																			        255		,     size=(self.num_channels, self.max_resolution, self.max_resolution)		,     dtype=np.uinta      )      )
									else:
														SCREAMING_SNAKE_CASE           =     []
														for i in range(self.batch_size      ):
																			SCREAMING_SNAKE_CASE     ,   SCREAMING_SNAKE_CASE           =     np.random.choice(np.arange(self.min_resolution		,     self.max_resolution      )		,     2      )
																			image_inputs.append(np.random.randint(255		,     size=(self.num_channels, width, height)		,     dtype=np.uinta      )      )

									if not numpify and not torchify:
														# PIL expects the channel dimension as last dimension
														SCREAMING_SNAKE_CASE           =     [Image.fromarray(np.moveaxis(lowercase__		,     0		,     -1      )      ) for x in image_inputs]

									if torchify:
														SCREAMING_SNAKE_CASE           =     [torch.from_numpy(lowercase__      ) for x in image_inputs]

									return image_inputs






@require_torch
@require_vision
class 					UpperCamelCase     (	__magic_name__  ,				unittest.TestCase				):

				"""simple docstring"""



				UpperCAmelCase_    :				List[Any]						  =    BlipImageProcessor if is_vision_available() else None


				def 							A (    self      )   ->   List[str]:
									"""simple docstring"""







									SCREAMING_SNAKE_CASE           =     BlipImageProcessingTester(self      )


				@property
				def 							A (    self      )   ->   List[str]:
									"""simple docstring"""







									return self.image_processor_tester.prepare_image_processor_dict()


				def 							A (    self      )   ->   List[Any]:
									"""simple docstring"""







									SCREAMING_SNAKE_CASE           =     self.image_processing_class(**self.image_processor_dict      )
									self.assertTrue(hasattr(lowercase__		,     'do_resize'      )      )
									self.assertTrue(hasattr(lowercase__		,     'size'      )      )
									self.assertTrue(hasattr(lowercase__		,     'do_normalize'      )      )
									self.assertTrue(hasattr(lowercase__		,     'image_mean'      )      )
									self.assertTrue(hasattr(lowercase__		,     'image_std'      )      )
									self.assertTrue(hasattr(lowercase__		,     'do_convert_rgb'      )      )


				def 							A (    self      )   ->   Dict:
									"""simple docstring"""







									pass


				def 							A (    self      )   ->   int:
									"""simple docstring"""







									SCREAMING_SNAKE_CASE           =     self.image_processing_class(**self.image_processor_dict      )
									# create random PIL images
									SCREAMING_SNAKE_CASE           =     self.image_processor_tester.prepare_inputs(equal_resolution=lowercase__      )
									for image in image_inputs:
														self.assertIsInstance(lowercase__		,     Image.Image      )

									# Test not batched input
									SCREAMING_SNAKE_CASE           =     image_processor(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           =     image_processor(lowercase__		,     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'],
									    )		,     )


				def 							A (    self      )   ->   List[Any]:
									"""simple docstring"""







									SCREAMING_SNAKE_CASE           =     self.image_processing_class(**self.image_processor_dict      )
									# create random numpy tensors
									SCREAMING_SNAKE_CASE           =     self.image_processor_tester.prepare_inputs(equal_resolution=lowercase__		,     numpify=lowercase__      )
									for image in image_inputs:
														self.assertIsInstance(lowercase__		,     np.ndarray      )

									# Test not batched input
									SCREAMING_SNAKE_CASE           =     image_processor(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           =     image_processor(lowercase__		,     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'],
									    )		,     )




				def 							A (    self      )   ->   int:
									"""simple docstring"""







									SCREAMING_SNAKE_CASE           =     self.image_processing_class(**self.image_processor_dict      )
									# create random PyTorch tensors
									SCREAMING_SNAKE_CASE           =     self.image_processor_tester.prepare_inputs(equal_resolution=lowercase__		,     torchify=lowercase__      )
									for image in image_inputs:
														self.assertIsInstance(lowercase__		,     torch.Tensor      )

									# Test not batched input
									SCREAMING_SNAKE_CASE           =     image_processor(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           =     image_processor(lowercase__		,     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'],
									    )		,     )






@require_torch
@require_vision
class 					UpperCamelCase     (	__magic_name__  ,				unittest.TestCase				):

				"""simple docstring"""



				UpperCAmelCase_    :				int						  =    BlipImageProcessor if is_vision_available() else None


				def 							A (    self      )   ->   Dict:
									"""simple docstring"""







									SCREAMING_SNAKE_CASE           =     BlipImageProcessingTester(self		,     num_channels=4      )
									SCREAMING_SNAKE_CASE           =     3


				@property
				def 							A (    self      )   ->   Dict:
									"""simple docstring"""







									return self.image_processor_tester.prepare_image_processor_dict()


				def 							A (    self      )   ->   Union[str, Any]:
									"""simple docstring"""







									SCREAMING_SNAKE_CASE           =     self.image_processing_class(**self.image_processor_dict      )
									self.assertTrue(hasattr(lowercase__		,     'do_resize'      )      )
									self.assertTrue(hasattr(lowercase__		,     'size'      )      )
									self.assertTrue(hasattr(lowercase__		,     'do_normalize'      )      )
									self.assertTrue(hasattr(lowercase__		,     'image_mean'      )      )
									self.assertTrue(hasattr(lowercase__		,     'image_std'      )      )
									self.assertTrue(hasattr(lowercase__		,     'do_convert_rgb'      )      )


				def 							A (    self      )   ->   Tuple:
									"""simple docstring"""







									pass




				def 							A (    self      )   ->   Optional[int]:
									"""simple docstring"""







									SCREAMING_SNAKE_CASE           =     self.image_processing_class(**self.image_processor_dict      )
									# create random PIL images
									SCREAMING_SNAKE_CASE           =     self.image_processor_tester.prepare_inputs(equal_resolution=lowercase__      )
									for image in image_inputs:
														self.assertIsInstance(lowercase__		,     Image.Image      )

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

									# Test batched
									SCREAMING_SNAKE_CASE           =     image_processor(lowercase__		,     return_tensors='pt'      ).pixel_values
									self.assertEqual(
									    encoded_images.shape		,     (
									        self.image_processor_tester.batch_size,
									        self.expected_encoded_image_num_channels,
									        self.image_processor_tester.size['height'],
									        self.image_processor_tester.size['width'],
									    )		,     )


"""simple docstring"""




import argparse
import json
import re
import string
import sys
from collections import Counter

def   UpperCamelCase_  (		SCREAMING_SNAKE_CASE_      ):

					def remove_articles(SCREAMING_SNAKE_CASE_      ):
										return re.sub(R'\b(a|an|the)\b',					' ',					SCREAMING_SNAKE_CASE_      )

					def white_space_fix(SCREAMING_SNAKE_CASE_      ):
										return " ".join(text.split()      )

					def remove_punc(SCREAMING_SNAKE_CASE_      ):
										SCREAMING_SNAKE_CASE           =     set(string.punctuation      )
										return "".join(ch for ch in text if ch not in exclude      )

					def lower(SCREAMING_SNAKE_CASE_      ):
										return text.lower()

					return white_space_fix(remove_articles(remove_punc(lower(SCREAMING_SNAKE_CASE_      )      )      )      )

def   UpperCamelCase_  (		SCREAMING_SNAKE_CASE_,					SCREAMING_SNAKE_CASE_      ):
					SCREAMING_SNAKE_CASE           =     normalize_answer(SCREAMING_SNAKE_CASE_      ).split()
					SCREAMING_SNAKE_CASE           =     normalize_answer(SCREAMING_SNAKE_CASE_      ).split()
					SCREAMING_SNAKE_CASE           =     Counter(SCREAMING_SNAKE_CASE_      ) & Counter(SCREAMING_SNAKE_CASE_      )
					SCREAMING_SNAKE_CASE           =     sum(common.values()      )
					if num_same == 0:
										return 0
					SCREAMING_SNAKE_CASE           =     1.0 * num_same / len(SCREAMING_SNAKE_CASE_      )
					SCREAMING_SNAKE_CASE           =     1.0 * num_same / len(SCREAMING_SNAKE_CASE_      )
					SCREAMING_SNAKE_CASE           =     (2 * precision * recall) / (precision + recall)
					return fa

def   UpperCamelCase_  (		SCREAMING_SNAKE_CASE_,					SCREAMING_SNAKE_CASE_      ):
					return normalize_answer(SCREAMING_SNAKE_CASE_      ) == normalize_answer(SCREAMING_SNAKE_CASE_      )

def   UpperCamelCase_  (		SCREAMING_SNAKE_CASE_,					SCREAMING_SNAKE_CASE_,					SCREAMING_SNAKE_CASE_      ):
					SCREAMING_SNAKE_CASE           =     []
					for ground_truth in ground_truths:
										SCREAMING_SNAKE_CASE           =     metric_fn(SCREAMING_SNAKE_CASE_,					SCREAMING_SNAKE_CASE_      )
										scores_for_ground_truths.append(SCREAMING_SNAKE_CASE_      )
					return max(SCREAMING_SNAKE_CASE_      )

def   UpperCamelCase_  (		SCREAMING_SNAKE_CASE_,					SCREAMING_SNAKE_CASE_      ):
					SCREAMING_SNAKE_CASE           =     SCREAMING_SNAKE_CASE           =     SCREAMING_SNAKE_CASE           =     0
					for article in dataset:
										for paragraph in article["paragraphs"]:
															for qa in paragraph["qas"]:
																				total += 1
																				if qa["id"] not in predictions:
																									SCREAMING_SNAKE_CASE           =     'Unanswered question ' + qa['id'] + ' will receive score 0.'
																									print(SCREAMING_SNAKE_CASE_,					file=sys.stderr      )
																									continue
																				SCREAMING_SNAKE_CASE           =     [x['text'] for x in qa['answers']]
																				SCREAMING_SNAKE_CASE           =     predictions[qa['id']]
																				exact_match += metric_max_over_ground_truths(SCREAMING_SNAKE_CASE_,					SCREAMING_SNAKE_CASE_,					SCREAMING_SNAKE_CASE_      )
																				fa += metric_max_over_ground_truths(SCREAMING_SNAKE_CASE_,					SCREAMING_SNAKE_CASE_,					SCREAMING_SNAKE_CASE_      )

					SCREAMING_SNAKE_CASE           =     100.0 * exact_match / total
					SCREAMING_SNAKE_CASE           =     100.0 * fa / total

					return {"exact_match": exact_match, "f1": fa}


if __name__ == "__main__":
	snake_case			       =		'1.1'
	snake_case			       =		argparse.ArgumentParser(description='Evaluation for SQuAD ' + expected_version)
	parser.add_argument('dataset_file', help='Dataset file')
	parser.add_argument('prediction_file', help='Prediction File')
	snake_case			       =		parser.parse_args()
	with open(args.dataset_file) as dataset_file:
		snake_case			       =		json.load(dataset_file)
		if dataset_json["version"] != expected_version:
			print(
			    'Evaluation expects v-' + expected_version + ', but got dataset with v-' + dataset_json['version'],
			    file=sys.stderr,
			)
		snake_case			       =		dataset_json['data']
	with open(args.prediction_file) as prediction_file:
		snake_case			       =		json.load(prediction_file)
	print(json.dumps(evaluate(dataset, predictions)))
