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							,			):
			__A							:   Union[str, Any]	  =				parent
			__A							:   List[Any]	  =				is_training
			__A							:   Dict	  =				batch_size
			__A							:   Optional[Any]	  =				image_size
			__A							:   List[Any]	  =				patch_size
			__A							:   List[Any]	  =				num_channels
			__A							:   List[Any]	  =				max_token_length
			__A							:   List[str]	  =				num_character_labels
			__A							:   Optional[int]	  =				num_bpe_labels
			__A							:   Any	  =				num_wordpiece_labels
			__A							:   str	  =				hidden_size
			__A							:   Any	  =				num_hidden_layers
			__A							:   Optional[int]	  =				num_attention_heads
			__A							:   int	  =				mlp_ratio
			__A							:   Dict	  =				patch_embeds_hidden_size
			__A							:   Union[str, Any]	  =				output_hidden_states

	def 					__UpperCAmelCase(       self   ):
			__A							:   List[str]	  =				floats_tensor([self.batch_size, self.num_channels, self.image_size[0], self.image_size[1]]   )
			__A							:   str	  =				self.get_config()
			return config, pixel_values

	def 					__UpperCAmelCase(       self   ):
			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 					__UpperCAmelCase(       self							,			__UpperCAmelCase							,			__UpperCAmelCase   ):
			__A							:   Any	  =				MgpstrForSceneTextRecognition(__UpperCAmelCase   )
			model.to(__UpperCAmelCase   )
			model.eval()
			with torch.no_grad():
					__A							:   Dict	  =				model(__UpperCAmelCase   )
			self.parent.assertEqual(
			    generated_ids[0][0].shape							,			(self.batch_size, self.max_token_length, self.num_character_labels)   )







	def 					__UpperCAmelCase(       self   ):
			__A							:   Union[str, Any]	  =				self.prepare_config_and_inputs()
			__A  ,       __A							:   Optional[int]	  =				config_and_inputs
			__A							:   str	  =				{"pixel_values": pixel_values}
			return config, inputs_dict


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

	'''simple docstring'''



	lowerCamelCase_   :      Any       =	(MgpstrForSceneTextRecognition,) if is_torch_available() else ()
	lowerCamelCase_   :      List[Any]       =	{"""feature-extraction""": MgpstrForSceneTextRecognition} if is_torch_available() else {}
	lowerCamelCase_   :      List[str]       =	False

	lowerCamelCase_   :      int       =	False
	lowerCamelCase_   :      Tuple       =	False
	lowerCamelCase_   :      int       =	False
	lowerCamelCase_   :      str       =	False

	def 					__UpperCAmelCase(       self   ):
			__A							:   Union[str, Any]	  =				MgpstrModelTester(self   )
			__A							:   Optional[int]	  =				ConfigTester(self							,			config_class=__UpperCAmelCase							,			has_text_modality=__UpperCAmelCase   )

	def 					__UpperCAmelCase(       self   ):
			self.config_tester.run_common_tests()

	def 					__UpperCAmelCase(       self   ):
			__A							:   int	  =				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 					__UpperCAmelCase(       self   ):
			pass

	def 					__UpperCAmelCase(       self   ):
			__A  ,       __A							:   Optional[int]	  =				self.model_tester.prepare_config_and_inputs_for_common()

			for model_class in self.all_model_classes:
					__A							:   Dict	  =				model_class(__UpperCAmelCase   )
					self.assertIsInstance(model.get_input_embeddings()							,			(nn.Module)   )
					__A							:   int	  =				model.get_output_embeddings()
					self.assertTrue(x is None or isinstance(__UpperCAmelCase							,			nn.Linear   )   )

	def 					__UpperCAmelCase(       self   ):
			__A  ,       __A							:   Optional[Any]	  =				self.model_tester.prepare_config_and_inputs_for_common()

			for model_class in self.all_model_classes:
					__A							:   List[str]	  =				model_class(__UpperCAmelCase   )
					__A							:   Any	  =				inspect.signature(model.forward   )
					# signature.parameters is an OrderedDict => so arg_names order is deterministic
					__A							:   Union[str, Any]	  =				[*signature.parameters.keys()]

					__A							:   Dict	  =				["pixel_values"]
					self.assertListEqual(arg_names[:1]							,			__UpperCAmelCase   )

	@unittest.skip(reason="MgpstrModel does not support feedforward chunking"   )
	def 					__UpperCAmelCase(       self   ):
			pass

	def 					__UpperCAmelCase(       self   ):
			__A  ,       __A							:   int	  =				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

					__A							:   Optional[int]	  =				True
					__A							:   Any	  =				model_class(__UpperCAmelCase   )
					self.assertTrue(model.is_gradient_checkpointing   )

	def 					__UpperCAmelCase(       self   ):
			def check_hidden_states_output(__UpperCAmelCase							,			__UpperCAmelCase							,			__UpperCAmelCase   ):
					__A							:   Tuple	  =				model_class(__UpperCAmelCase   )
					model.to(__UpperCAmelCase   )
					model.eval()

					with torch.no_grad():
							__A							:   Optional[int]	  =				model(**self._prepare_for_class(__UpperCAmelCase							,			__UpperCAmelCase   )   )

					__A							:   Tuple	  =				outputs.hidden_states

					__A							:   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]							,			)

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

			for model_class in self.all_model_classes:
					__A							:   Optional[Any]	  =				True
					check_hidden_states_output(__UpperCAmelCase							,			__UpperCAmelCase							,			__UpperCAmelCase   )

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

					check_hidden_states_output(__UpperCAmelCase							,			__UpperCAmelCase							,			__UpperCAmelCase   )

	def 					__UpperCAmelCase(       self   ):
			__A  ,       __A							:   str	  =				self.model_tester.prepare_config_and_inputs_for_common()

			__A							:   Any	  =				_config_zero_init(__UpperCAmelCase   )
			for model_class in self.all_model_classes:
					__A							:   List[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 					__UpperCAmelCase(       self   ):
			pass



def    lowerCamelCase_   (  )							->	Optional[int]:
		__A							:   Tuple	  =				"https://i.postimg.cc/ZKwLg2Gw/367-14.png"
		__A							:   Optional[Any]	  =				Image.open(requests.get(_lowercase ,     stream=_lowercase    ).raw    ).convert("RGB"    )
		return im





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

	'''simple docstring'''



	@slow
	def 					__UpperCAmelCase(       self   ):
			__A							:   List[str]	  =				"alibaba-damo/mgp-str-base"
			__A							:   Tuple	  =				MgpstrForSceneTextRecognition.from_pretrained(__UpperCAmelCase   ).to(__UpperCAmelCase   )
			__A							:   Tuple	  =				MgpstrProcessor.from_pretrained(__UpperCAmelCase   )

			__A							:   Optional[int]	  =				prepare_img()
			__A							:   str	  =				processor(images=__UpperCAmelCase							,			return_tensors="pt"   ).pixel_values.to(__UpperCAmelCase   )

			# forward pass
			with torch.no_grad():
					__A							:   Union[str, Any]	  =				model(__UpperCAmelCase   )

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

			__A							:   List[str]	  =				processor.batch_decode(outputs.logits   )
			__A							:   Optional[Any]	  =				"ticket"

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

			__A							:   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   )   )






import re
import tempfile
import unittest
from pathlib import Path

from datasets.utils.metadata import DatasetMetadata



def    lowerCamelCase_   (  _lowercase    )							->	str:
		__A							:   Optional[int]	  =				min(re.search("^ +" ,     _lowercase    ).end() if t.startswith(" "    ) else 0 for t in string.splitlines()    )
		return "\n".join([line[indent_level:] for line in string.splitlines() if indent_level < len(_lowercase    )]    )


UpperCamelCase      			=     '\\n---\nlanguage:\n- zh\n- en\ntask_ids:\n- sentiment-classification\n---\n# Begin of markdown\n\nSome cool dataset card\n'

UpperCamelCase      			=     '\\n---\n---\n# Begin of markdown\n\nSome cool dataset card\n'


UpperCamelCase      			=     '\\n# Begin of markdown\n\nSome cool dataset card\n'





class _a       (	unittest.TestCase ):

	'''simple docstring'''



	def 					__UpperCAmelCase(       self   ):
			with tempfile.TemporaryDirectory() as tmp_dir:
					__A							:   List[Any]	  =				Path(__UpperCAmelCase   ) / "README.md"
					with open(__UpperCAmelCase							,			"w+"   ) as readme_file:
							readme_file.write(__UpperCAmelCase   )
					__A							:   Tuple	  =				DatasetMetadata.from_readme(__UpperCAmelCase   )
					self.assertDictEqual(__UpperCAmelCase							,			{"language": ["zh", "en"], "task_ids": ["sentiment-classification"]}   )

					with open(__UpperCAmelCase							,			"w+"   ) as readme_file:
							readme_file.write(__UpperCAmelCase   )
					__A							:   Any	  =				DatasetMetadata.from_readme(__UpperCAmelCase   )
					self.assertDictEqual(__UpperCAmelCase							,			{}   )

					with open(__UpperCAmelCase							,			"w+"   ) as readme_file:
							readme_file.write(__UpperCAmelCase   )
					__A							:   Union[str, Any]	  =				DatasetMetadata.from_readme(__UpperCAmelCase   )
					self.assertEqual(__UpperCAmelCase							,			{}   )







	def 					__UpperCAmelCase(       self   ):
			__A							:   List[str]	  =				_dedent(
			    "\\n            annotations_creators:\n            - found\n            language_creators:\n            - found\n            language:\n            - en\n            license:\n            - unknown\n            multilinguality:\n            - monolingual\n            pretty_name: Test Dataset\n            size_categories:\n            - 10K<n<100K\n            source_datasets:\n            - extended|other-yahoo-webscope-l6\n            task_categories:\n            - question-answering\n            task_ids:\n            - open-domain-qa\n            "   )
			assert DatasetMetadata.from_yaml_string(__UpperCAmelCase   )

			__A							:   List[Any]	  =				_dedent(
			    "\\n            annotations_creators:\n            - found\n            language:\n            - en\n            license:\n            - unknown\n            multilinguality:\n            - monolingual\n            pretty_name: Test Dataset\n            size_categories:\n            - 10K<n<100K\n            source_datasets:\n            - extended|other-yahoo-webscope-l6\n            task_categories:\n            - question-answering\n            task_ids:\n            - open-domain-qa\n            task_ids:\n            - open-domain-qa\n            "   )
			with self.assertRaises(__UpperCAmelCase   ):
					DatasetMetadata.from_yaml_string(__UpperCAmelCase   )

			__A							:   Optional[int]	  =				_dedent(
			    "\\n            annotations_creators:\n            - found\n            language_creators:\n            - found\n            language:\n            - en\n            license:\n            - unknown\n            multilinguality:\n            - monolingual\n            pretty_name: Test Dataset\n            size_categories:\n            - 10K<n<100K\n            source_datasets:\n            - extended|other-yahoo-webscope-l6\n            task_categories:\n            - text-classification\n            task_ids:\n            - multi-class-classification\n            paperswithcode_id:\n            - squad\n            configs:\n            - en\n            train-eval-index:\n            - config: en\n              task: text-classification\n              task_id: multi_class_classification\n              splits:\n                train_split: train\n                eval_split: test\n              col_mapping:\n                text: text\n                label: target\n              metrics:\n                - type: accuracy\n                  name: Accuracy\n            extra_gated_prompt: |\n              By clicking on “Access repository” below, you also agree to ImageNet Terms of Access:\n              [RESEARCHER_FULLNAME] (the \"Researcher\") has requested permission to use the ImageNet database (the \"Database\") at Princeton University and Stanford University. In exchange for such permission, Researcher hereby agrees to the following terms and conditions:\n              1. Researcher shall use the Database only for non-commercial research and educational purposes.\n            extra_gated_fields:\n              Company: text\n              Country: text\n              I agree to use this model for non-commerical use ONLY: checkbox\n            "   )
			assert DatasetMetadata.from_yaml_string(__UpperCAmelCase   )





