






"""simple docstring"""





import inspect
import unittest

import requests

from transformers import MgpstrConfig
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
  import torch
  from torch import nn

  from transformers import MgpstrForSceneTextRecognition


if is_vision_available():
  from PIL import Image

  from transformers import MgpstrProcessor







class 						_a  :





   '''simple docstring'''






   def __init__(       self					, UpperCAmelCase_					, UpperCAmelCase_=False					, UpperCAmelCase_=13					, UpperCAmelCase_=(32, 128)					, UpperCAmelCase_=4					, UpperCAmelCase_=3					, UpperCAmelCase_=27					, UpperCAmelCase_=38					, UpperCAmelCase_=99					, UpperCAmelCase_=99					, UpperCAmelCase_=32					, UpperCAmelCase_=5					, UpperCAmelCase_=4					, UpperCAmelCase_=4.0					, UpperCAmelCase_=257					, UpperCAmelCase_=None					, )			->   Union[str, Any]:




          '''simple docstring'''




          lowercase__:							List[Any]         =     parent
          lowercase__:							Dict         =     is_training
          lowercase__:							List[Any]         =     batch_size
          lowercase__:							Tuple         =     image_size
          lowercase__:							List[str]         =     patch_size
          lowercase__:							Tuple         =     num_channels
          lowercase__:							int         =     max_token_length
          lowercase__:							int         =     num_character_labels
          lowercase__:							List[str]         =     num_bpe_labels
          lowercase__:							Dict         =     num_wordpiece_labels
          lowercase__:							Any         =     hidden_size
          lowercase__:							List[str]         =     num_hidden_layers
          lowercase__:							str         =     num_attention_heads
          lowercase__:							Any         =     mlp_ratio
          lowercase__:							Dict         =     patch_embeds_hidden_size
          lowercase__:							Any         =     output_hidden_states





   def __lowercase							(       self)			->   str:




          '''simple docstring'''




          lowercase__:							Union[str, Any]         =     floats_tensor([self.batch_size, self.num_channels, self.image_size[0], self.image_size[1]])
          lowercase__:							str         =     self.get_config()
          return config, pixel_values





   def __lowercase							(       self)			->   Any:




          '''simple docstring'''




          return MgpstrConfig(
              image_size=self.image_size					, patch_size=self.patch_size					, num_channels=self.num_channels					, max_token_length=self.max_token_length					, num_character_labels=self.num_character_labels					, num_bpe_labels=self.num_bpe_labels					, num_wordpiece_labels=self.num_wordpiece_labels					, hidden_size=self.hidden_size					, num_hidden_layers=self.num_hidden_layers					, num_attention_heads=self.num_attention_heads					, mlp_ratio=self.mlp_ratio					, output_hidden_states=self.output_hidden_states					, )





   def __lowercase							(       self					, UpperCAmelCase_					, UpperCAmelCase_)			->   Optional[Any]:




          '''simple docstring'''




          lowercase__:							Dict         =     MgpstrForSceneTextRecognition(UpperCAmelCase_)
          model.to(UpperCAmelCase_)
          model.eval()
          with torch.no_grad():
                 lowercase__:							Optional[Any]         =     model(UpperCAmelCase_)
          self.parent.assertEqual(
              generated_ids[0][0].shape					, (self.batch_size, self.max_token_length, self.num_character_labels))





   def __lowercase							(       self)			->   int:




          '''simple docstring'''




          lowercase__:							Optional[int]         =     self.prepare_config_and_inputs()
          lowercase__    ,      lowercase__:							Dict         =     config_and_inputs
          lowercase__:							str         =     {"pixel_values": pixel_values}
          return config, inputs_dict







@require_torch
class 						_a  (		lowercase_							,	lowercase_							,	unittest.TestCase       ):





   '''simple docstring'''






   UpperCamelCase__     	=							(MgpstrForSceneTextRecognition,) if is_torch_available() else ()
   UpperCamelCase__     	=							{"""feature-extraction""": MgpstrForSceneTextRecognition} if is_torch_available() else {}
   UpperCamelCase__     	=							False

   UpperCamelCase__     	=							False
   UpperCamelCase__     	=							False
   UpperCamelCase__     	=							False
   UpperCamelCase__     	=							False





   def __lowercase							(       self)			->   str:




          '''simple docstring'''




          lowercase__:							str         =     MgpstrModelTester(self)
          lowercase__:							Tuple         =     ConfigTester(self					, config_class=UpperCAmelCase_					, has_text_modality=UpperCAmelCase_)





   def __lowercase							(       self)			->   List[Any]:




          '''simple docstring'''




          self.config_tester.run_common_tests()





   def __lowercase							(       self)			->   Tuple:




          '''simple docstring'''




          lowercase__:							str         =     self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*UpperCAmelCase_)





   @unittest.skip(reason="MgpstrModel does not use inputs_embeds")
   def __lowercase							(       self)			->   Union[str, Any]:




          '''simple docstring'''




          pass





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




          '''simple docstring'''




          lowercase__    ,      lowercase__:							int         =     self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
                 lowercase__:							List[Any]         =     model_class(UpperCAmelCase_)
                 self.assertIsInstance(model.get_input_embeddings()					, (nn.Module))
                 lowercase__:							Dict         =     model.get_output_embeddings()
                 self.assertTrue(x is None or isinstance(UpperCAmelCase_					, nn.Linear))





   def __lowercase							(       self)			->   str:




          '''simple docstring'''




          lowercase__    ,      lowercase__:							str         =     self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
                 lowercase__:							List[str]         =     model_class(UpperCAmelCase_)
                 lowercase__:							int         =     inspect.signature(model.forward)
                 # signature.parameters is an OrderedDict => so arg_names order is deterministic
                 lowercase__:							List[str]         =     [*signature.parameters.keys()]

                 lowercase__:							Any         =     ["pixel_values"]
                 self.assertListEqual(arg_names[:1]					, UpperCAmelCase_)





   @unittest.skip(reason="MgpstrModel does not support feedforward chunking")
   def __lowercase							(       self)			->   Optional[Any]:




          '''simple docstring'''




          pass





   def __lowercase							(       self)			->   Tuple:




          '''simple docstring'''




          lowercase__    ,      lowercase__:							str         =     self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
                 if not model_class.supports_gradient_checkpointing:
                        continue

                 lowercase__:							List[str]         =     True
                 lowercase__:							Tuple         =     model_class(UpperCAmelCase_)
                 self.assertTrue(model.is_gradient_checkpointing)





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




          '''simple docstring'''




          def check_hidden_states_output(UpperCAmelCase_					, UpperCAmelCase_					, UpperCAmelCase_):
                 lowercase__:							Optional[Any]         =     model_class(UpperCAmelCase_)
                 model.to(UpperCAmelCase_)
                 model.eval()

                 with torch.no_grad():
                        lowercase__:							str         =     model(**self._prepare_for_class(UpperCAmelCase_					, UpperCAmelCase_))

                 lowercase__:							Union[str, Any]         =     outputs.hidden_states

                 lowercase__:							Any         =     getattr(
                     self.model_tester					, "expected_num_hidden_layers"					, self.model_tester.num_hidden_layers + 1)
                 self.assertEqual(len(UpperCAmelCase_)					, UpperCAmelCase_)

                 self.assertListEqual(
                     list(hidden_states[0].shape[-2:])					, [self.model_tester.patch_embeds_hidden_size, self.model_tester.hidden_size]					, )

          lowercase__    ,      lowercase__:							Optional[int]         =     self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
                 lowercase__:							List[str]         =     True
                 check_hidden_states_output(UpperCAmelCase_					, UpperCAmelCase_					, UpperCAmelCase_)

                 # check that output_hidden_states also work using config
                 del inputs_dict["output_hidden_states"]
                 lowercase__:							Tuple         =     True

                 check_hidden_states_output(UpperCAmelCase_					, UpperCAmelCase_					, UpperCAmelCase_)





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




          '''simple docstring'''




          lowercase__    ,      lowercase__:							Any         =     self.model_tester.prepare_config_and_inputs_for_common()

          lowercase__:							Dict         =     _config_zero_init(UpperCAmelCase_)
          for model_class in self.all_model_classes:
                 lowercase__:							str         =     model_class(config=UpperCAmelCase_)
                 for name, param in model.named_parameters():
                        if isinstance(UpperCAmelCase_					, (nn.Linear, nn.Convad, nn.LayerNorm)):
                               if param.requires_grad:
                                      self.assertIn(
                                          ((param.data.mean() * 1E9).round() / 1E9).item()					, [0.0, 1.0]					, msg=F"""Parameter {name} of model {model_class} seems not properly initialized"""					, )





   @unittest.skip(reason="Retain_grad is tested in individual model tests")
   def __lowercase							(       self)			->   Tuple:




          '''simple docstring'''




          pass







def 							A(    ):
       """simple docstring"""





       lowercase__:							int         =     "https://i.postimg.cc/ZKwLg2Gw/367-14.png"
       lowercase__:							Dict         =     Image.open(requests.get(snake_case_		,  stream=snake_case_						).raw						).convert("RGB"						)
       return im







@require_vision
@require_torch
class 						_a  (		unittest.TestCase       ):





   '''simple docstring'''






   @slow
   def __lowercase							(       self)			->   List[str]:




          '''simple docstring'''




          lowercase__:							Dict         =     "alibaba-damo/mgp-str-base"
          lowercase__:							int         =     MgpstrForSceneTextRecognition.from_pretrained(UpperCAmelCase_).to(UpperCAmelCase_)
          lowercase__:							str         =     MgpstrProcessor.from_pretrained(UpperCAmelCase_)

          lowercase__:							Union[str, Any]         =     prepare_img()
          lowercase__:							List[Any]         =     processor(images=UpperCAmelCase_					, return_tensors="pt").pixel_values.to(UpperCAmelCase_)

          # forward pass
          with torch.no_grad():
                 lowercase__:							Dict         =     model(UpperCAmelCase_)

          # verify the logits
          self.assertEqual(outputs.logits[0].shape					, torch.Size((1, 27, 38)))

          lowercase__:							Any         =     processor.batch_decode(outputs.logits)
          lowercase__:							Optional[Any]         =     "ticket"

          self.assertEqual(out_strs["generated_text"][0]					, UpperCAmelCase_)

          lowercase__:							Optional[int]         =     torch.tensor(
              [[[-39.53_97, -44.40_24, -36.18_44], [-61.47_09, -63.86_39, -58.34_54], [-74.02_25, -68.54_94, -71.21_64]]]					, device=UpperCAmelCase_					, )

          self.assertTrue(torch.allclose(outputs.logits[0][:, 1:4, 1:4]					, UpperCAmelCase_					, atol=1E-4))














"""simple docstring"""





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






def 							A(    snake_case_						):
       """simple docstring"""





       def remove_articles(snake_case_						):
              return re.sub(r"\b(a|an|the)\b"		,  " "		,  snake_case_						)

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

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

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

       return white_space_fix(remove_articles(remove_punc(lower(snake_case_						)						)						)						)






def 							A(    snake_case_		,  snake_case_						):
       """simple docstring"""





       lowercase__:							Dict         =     normalize_answer(snake_case_						).split()
       lowercase__:							List[str]         =     normalize_answer(snake_case_						).split()
       lowercase__:							Optional[int]         =     Counter(snake_case_						) & Counter(snake_case_						)
       lowercase__:							Any         =     sum(common.values()						)
       if num_same == 0:
              return 0
       lowercase__:							Any         =     1.0 * num_same / len(snake_case_						)
       lowercase__:							List[Any]         =     1.0 * num_same / len(snake_case_						)
       lowercase__:							int         =     (2 * precision * recall) / (precision + recall)
       return fa






def 							A(    snake_case_		,  snake_case_						):
       """simple docstring"""





       return normalize_answer(snake_case_						) == normalize_answer(snake_case_						)






def 							A(    snake_case_		,  snake_case_		,  snake_case_						):
       """simple docstring"""





       lowercase__:							int         =     []
       for ground_truth in ground_truths:
              lowercase__:							str         =     metric_fn(snake_case_		,  snake_case_						)
              scores_for_ground_truths.append(snake_case_						)
       return max(snake_case_						)






def 							A(    snake_case_		,  snake_case_						):
       """simple docstring"""





       lowercase__:							Optional[int]         =     0
       for article in dataset:
              for paragraph in article["paragraphs"]:
                     for qa in paragraph["qas"]:
                            total += 1
                            if qa["id"] not in predictions:
                                   lowercase__:							Union[str, Any]         =     "Unanswered question " + qa["id"] + " will receive score 0."
                                   print(snake_case_		,  file=sys.stderr						)
                                   continue
                            lowercase__:							List[Any]         =     [x["text"] for x in qa["answers"]]
                            lowercase__:							Optional[Any]         =     predictions[qa["id"]]
                            exact_match += metric_max_over_ground_truths(snake_case_		,  snake_case_		,  snake_case_						)
                            fa += metric_max_over_ground_truths(snake_case_		,  snake_case_		,  snake_case_						)

       lowercase__:							Union[str, Any]         =     1_00.0 * exact_match / total
       lowercase__:							str         =     1_00.0 * fa / total

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


if __name__ == "__main__":
  UpperCamelCase					=     """1.1"""
  UpperCamelCase					=     argparse.ArgumentParser(description="""Evaluation for SQuAD """ + expected_version)
  parser.add_argument("""dataset_file""", help="""Dataset file""")
  parser.add_argument("""prediction_file""", help="""Prediction File""")
  UpperCamelCase					=     parser.parse_args()
  with open(args.dataset_file) as dataset_file:
    UpperCamelCase					=     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,
      )
    UpperCamelCase					=     dataset_json["""data"""]
  with open(args.prediction_file) as prediction_file:
    UpperCamelCase					=     json.load(prediction_file)
  print(json.dumps(evaluate(dataset, predictions)))






