code: 
  
  
  
  
  
  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 							__snake_case    :
  
  
  
  
  
  	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			,					)			->		Dict:
  		snake_case__  					=      parent
  		snake_case__  					=      is_training
  		snake_case__  					=      batch_size
  		snake_case__  					=      image_size
  		snake_case__  					=      patch_size
  		snake_case__  					=      num_channels
  		snake_case__  					=      max_token_length
  		snake_case__  					=      num_character_labels
  		snake_case__  					=      num_bpe_labels
  		snake_case__  					=      num_wordpiece_labels
  		snake_case__  					=      hidden_size
  		snake_case__  					=      num_hidden_layers
  		snake_case__  					=      num_attention_heads
  		snake_case__  					=      mlp_ratio
  		snake_case__  					=      patch_embeds_hidden_size
  		snake_case__  					=      output_hidden_states
  
  
  
  
  
  	def      _snake_case      (		self		)			->		Dict:
  		snake_case__  					=      floats_tensor([self.batch_size, self.num_channels, self.image_size[0], self.image_size[1]]		)
  		snake_case__  					=      self.get_config()
  		return config, pixel_values
  
  
  
  
  
  	def      _snake_case      (		self		)			->		List[Any]:
  		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      _snake_case      (		self			,					UpperCamelCase_			,					UpperCamelCase_		)			->		Union[str, Any]:
  		snake_case__  					=      MgpstrForSceneTextRecognition(UpperCamelCase_		)
  		model.to(UpperCamelCase_		)
  		model.eval()
  		with torch.no_grad():
  			snake_case__  					=      model(UpperCamelCase_		)
  		self.parent.assertEqual(
  		    generated_ids[0][0].shape			,					(self.batch_size, self.max_token_length, self.num_character_labels)		)
  
  
  
  
  
  	def      _snake_case      (		self		)			->		Dict:
  		snake_case__  					=      self.prepare_config_and_inputs()
  		snake_case__  					=      config_and_inputs
  		snake_case__  					=      {"pixel_values": pixel_values}
  		return config, inputs_dict
  
  
  
  
  
  
  @require_torch
  class 							__snake_case    (				__magic_name__					,					__magic_name__					,					unittest.TestCase       ):
  	__lowerCAmelCase						=     (MgpstrForSceneTextRecognition,) if is_torch_available() else ()
  	__lowerCAmelCase						=     {"feature-extraction": MgpstrForSceneTextRecognition} if is_torch_available() else {}
  	__lowerCAmelCase						=     False
  
  	__lowerCAmelCase						=     False
  	__lowerCAmelCase						=     False
  	__lowerCAmelCase						=     False
  	__lowerCAmelCase						=     False
  
  
  
  
  
  	def      _snake_case      (		self		)			->		List[str]:
  		snake_case__  					=      MgpstrModelTester(self		)
  		snake_case__  					=      ConfigTester(self			,					config_class=UpperCamelCase_			,					has_text_modality=UpperCamelCase_		)
  
  
  
  
  
  	def      _snake_case      (		self		)			->		Any:
  		self.config_tester.run_common_tests()
  
  
  
  
  
  	def      _snake_case      (		self		)			->		Tuple:
  		snake_case__  					=      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      _snake_case      (		self		)			->		Optional[Any]:
  		pass
  
  
  
  
  
  	def      _snake_case      (		self		)			->		List[str]:
  		snake_case__  					=      self.model_tester.prepare_config_and_inputs_for_common()
  
  		for model_class in self.all_model_classes:
  			snake_case__  					=      model_class(UpperCamelCase_		)
  			self.assertIsInstance(model.get_input_embeddings()			,					(nn.Module)		)
  			snake_case__  					=      model.get_output_embeddings()
  			self.assertTrue(x is None or isinstance(UpperCamelCase_			,					nn.Linear		)		)
  
  
  
  
  
  	def      _snake_case      (		self		)			->		List[Any]:
  		snake_case__  					=      self.model_tester.prepare_config_and_inputs_for_common()
  
  		for model_class in self.all_model_classes:
  			snake_case__  					=      model_class(UpperCamelCase_		)
  			snake_case__  					=      inspect.signature(model.forward		)
  			# signature.parameters is an OrderedDict => so arg_names order is deterministic
  			snake_case__  					=      [*signature.parameters.keys()]
  
  			snake_case__  					=      ["pixel_values"]
  			self.assertListEqual(arg_names[:1]			,					UpperCamelCase_		)
  
  
  
  
  
  	@unittest.skip(reason='MgpstrModel does not support feedforward chunking'		)
  	def      _snake_case      (		self		)			->		int:
  		pass
  
  
  
  
  
  	def      _snake_case      (		self		)			->		Union[str, Any]:
  		snake_case__  					=      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
  
  			snake_case__  					=      True
  			snake_case__  					=      model_class(UpperCamelCase_		)
  			self.assertTrue(model.is_gradient_checkpointing		)
  
  
  
  
  
  	def      _snake_case      (		self		)			->		str:
  
  
  
  
  
  
  		def check_hidden_states_output(UpperCamelCase_			,					UpperCamelCase_			,					UpperCamelCase_		):
  			snake_case__  					=      model_class(UpperCamelCase_		)
  			model.to(UpperCamelCase_		)
  			model.eval()
  
  			with torch.no_grad():
  				snake_case__  					=      model(**self._prepare_for_class(UpperCamelCase_			,					UpperCamelCase_		)		)
  
  			snake_case__  					=      outputs.hidden_states
  
  			snake_case__  					=      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]			,					)
  
  		snake_case__  					=      self.model_tester.prepare_config_and_inputs_for_common()
  
  		for model_class in self.all_model_classes:
  			snake_case__  					=      True
  			check_hidden_states_output(UpperCamelCase_			,					UpperCamelCase_			,					UpperCamelCase_		)
  
  			# check that output_hidden_states also work using config
  			del inputs_dict["output_hidden_states"]
  			snake_case__  					=      True
  
  			check_hidden_states_output(UpperCamelCase_			,					UpperCamelCase_			,					UpperCamelCase_		)
  
  
  
  
  
  	def      _snake_case      (		self		)			->		int:
  		snake_case__  					=      self.model_tester.prepare_config_and_inputs_for_common()
  
  		snake_case__  					=      _config_zero_init(UpperCamelCase_		)
  		for model_class in self.all_model_classes:
  			snake_case__  					=      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      _snake_case      (		self		)			->		List[Any]:
  		pass
  
  
  
  
  
  def 	__lowerCamelCase (  )      ->Any:
  	snake_case__  					=      "https://i.postimg.cc/ZKwLg2Gw/367-14.png"
  	snake_case__  					=      Image.open(requests.get(UpperCAmelCase_				,							stream=UpperCAmelCase_       ).raw       ).convert('RGB'       )
  	return im
  
  
  
  
  
  
  
  @require_vision
  @require_torch
  class 							__snake_case    (				unittest.TestCase       ):
  
  
  
  
  
  	@slow
  	def      _snake_case      (		self		)			->		Union[str, Any]:
  		snake_case__  					=      "alibaba-damo/mgp-str-base"
  		snake_case__  					=      MgpstrForSceneTextRecognition.from_pretrained(UpperCamelCase_		).to(UpperCamelCase_		)
  		snake_case__  					=      MgpstrProcessor.from_pretrained(UpperCamelCase_		)
  
  		snake_case__  					=      prepare_img()
  		snake_case__  					=      processor(images=UpperCamelCase_			,					return_tensors='pt'		).pixel_values.to(UpperCamelCase_		)
  
  		# forward pass
  		with torch.no_grad():
  			snake_case__  					=      model(UpperCamelCase_		)
  
  		# verify the logits
  		self.assertEqual(outputs.logits[0].shape			,					torch.Size((1, 27, 38)		)		)
  
  		snake_case__  					=      processor.batch_decode(outputs.logits		)
  		snake_case__  					=      "ticket"
  
  		self.assertEqual(out_strs['generated_text'][0]			,					UpperCamelCase_		)
  
  		snake_case__  					=      torch.tensor(
  		    [[[-3_9.5_3_9_7, -4_4.4_0_2_4, -3_6.1_8_4_4], [-6_1.4_7_0_9, -6_3.8_6_3_9, -5_8.3_4_5_4], [-7_4.0_2_2_5, -6_8.5_4_9_4, -7_1.2_1_6_4]]]			,					device=UpperCamelCase_			,					)
  
  		self.assertTrue(torch.allclose(outputs.logits[0][:, 1:4, 1:4]			,					UpperCamelCase_			,					atol=1E-4		)		)
  
  
  
  
style_context: 
  
  
  
  
  
  '''simple docstring'''
  
  
  
  
  a__				: Any									=    {
      0: '''underwear''',
      1: '''pants''',
      2: '''belt''',
      3: '''suit''',
      4: '''shoe''',
      5: '''socks''',
      6: '''shirt''',
      7: '''tie''',
      8: '''watch''',
  }
  
  a__				: Optional[Any]									=    [[1, 4], [2, 4], [3], [], [], [4], [2, 7], [3], []]
  
  a__				: List[str]									=    [0 for x in range(len(graph))]
  a__				: List[Any]									=    []
  
  
  
  
  def 	__lowerCamelCase (  UpperCAmelCase_				,							UpperCAmelCase_       )      ->Dict:
  	snake_case__  					=      1
  	while stack:
  		snake_case__  					=      stack.pop()
  		print(UpperCAmelCase_				,							clothes[current_clothing]       )
  		order += 1
  
  
  
  
  def 	__lowerCamelCase (  UpperCAmelCase_				,							UpperCAmelCase_				,							UpperCAmelCase_       )      ->Dict:
  	snake_case__  					=      1
  	for v in graph[u]:
  		if not visited[v]:
  			depth_first_search(UpperCAmelCase_				,							UpperCAmelCase_				,							UpperCAmelCase_       )
  
  	stack.append(UpperCAmelCase_       )
  
  
  
  
  def 	__lowerCamelCase (  UpperCAmelCase_				,							UpperCAmelCase_       )      ->Union[str, Any]:
  	for v in range(len(UpperCAmelCase_       )       ):
  		if not visited[v]:
  			depth_first_search(UpperCAmelCase_				,							UpperCAmelCase_				,							UpperCAmelCase_       )
  
  
  if __name__ == "__main__":
  			topological_sort(graph, visited)
  			print(stack)
  			print_stack(stack, clothes)
  
  
  
  