





'''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 							__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			,					)			->		Union[str, Any]:
		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		)			->		Any:
		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		)			->		Dict:
		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_		)			->		List[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		)			->		int:
		snake_case__  					=      self.prepare_config_and_inputs()
		snake_case__					,     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		)			->		Optional[Any]:
		snake_case__  					=      MgpstrModelTester(self		)
		snake_case__  					=      ConfigTester(self			,					config_class=UpperCamelCase_			,					has_text_modality=UpperCamelCase_		)





	def      _snake_case      (		self		)			->		Dict:
		self.config_tester.run_common_tests()





	def      _snake_case      (		self		)			->		Optional[Any]:
		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		)			->		int:
		pass





	def      _snake_case      (		self		)			->		int:
		snake_case__					,     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		)			->		int:
		snake_case__					,     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		)			->		Union[str, Any]:
		pass





	def      _snake_case      (		self		)			->		Any:
		snake_case__					,     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		)			->		Tuple:
		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__					,     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		)			->		Any:
		snake_case__					,     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		)			->		Tuple:
		pass





def 	__lowerCamelCase (  )      ->str:
	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		)			->		Optional[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		)		)










'''simple docstring'''




import shutil
import tempfile
import unittest

import pytest

from transformers import WhisperTokenizer, is_speech_available
from transformers.testing_utils import require_sentencepiece, require_torch, require_torchaudio

from .test_feature_extraction_whisper import floats_list


if is_speech_available():
			from transformers import WhisperFeatureExtractor, WhisperProcessor


a__				: Tuple									=    5_0358
a__				: str									=    5_0362






@require_torch
@require_torchaudio
@require_sentencepiece
class 							__snake_case    (				unittest.TestCase       ):





	def      _snake_case      (		self		)			->		Dict:
		snake_case__  					=      'openai/whisper-small.en'
		snake_case__  					=      tempfile.mkdtemp()





	def      _snake_case      (		self			,					**UpperCamelCase_		)			->		Optional[int]:
		return WhisperTokenizer.from_pretrained(self.checkpoint			,					**UpperCamelCase_		)





	def      _snake_case      (		self			,					**UpperCamelCase_		)			->		Any:
		return WhisperFeatureExtractor.from_pretrained(self.checkpoint			,					**UpperCamelCase_		)





	def      _snake_case      (		self		)			->		Tuple:
		shutil.rmtree(self.tmpdirname		)





	def      _snake_case      (		self		)			->		Any:
		snake_case__  					=      self.get_tokenizer()
		snake_case__  					=      self.get_feature_extractor()

		snake_case__  					=      WhisperProcessor(tokenizer=UpperCamelCase_			,					feature_extractor=UpperCamelCase_		)

		processor.save_pretrained(self.tmpdirname		)
		snake_case__  					=      WhisperProcessor.from_pretrained(self.tmpdirname		)

		self.assertEqual(processor.tokenizer.get_vocab()			,					tokenizer.get_vocab()		)
		self.assertIsInstance(processor.tokenizer			,					UpperCamelCase_		)

		self.assertEqual(processor.feature_extractor.to_json_string()			,					feature_extractor.to_json_string()		)
		self.assertIsInstance(processor.feature_extractor			,					UpperCamelCase_		)





	def      _snake_case      (		self		)			->		Dict:
		snake_case__  					=      WhisperProcessor(tokenizer=self.get_tokenizer()			,					feature_extractor=self.get_feature_extractor()		)
		processor.save_pretrained(self.tmpdirname		)

		snake_case__  					=      self.get_tokenizer(bos_token='(BOS)'			,					eos_token='(EOS)'		)
		snake_case__  					=      self.get_feature_extractor(do_normalize=UpperCamelCase_			,					padding_value=1.0		)

		snake_case__  					=      WhisperProcessor.from_pretrained(
		    self.tmpdirname			,					bos_token='(BOS)'			,					eos_token='(EOS)'			,					do_normalize=UpperCamelCase_			,					padding_value=1.0		)

		self.assertEqual(processor.tokenizer.get_vocab()			,					tokenizer_add_kwargs.get_vocab()		)
		self.assertIsInstance(processor.tokenizer			,					UpperCamelCase_		)

		self.assertEqual(processor.feature_extractor.to_json_string()			,					feature_extractor_add_kwargs.to_json_string()		)
		self.assertIsInstance(processor.feature_extractor			,					UpperCamelCase_		)





	def      _snake_case      (		self		)			->		List[str]:
		snake_case__  					=      self.get_feature_extractor()
		snake_case__  					=      self.get_tokenizer()

		snake_case__  					=      WhisperProcessor(tokenizer=UpperCamelCase_			,					feature_extractor=UpperCamelCase_		)

		snake_case__  					=      floats_list((3, 1000)		)

		snake_case__  					=      feature_extractor(UpperCamelCase_			,					return_tensors='np'		)
		snake_case__  					=      processor(UpperCamelCase_			,					return_tensors='np'		)

		for key in input_feat_extract.keys():
			self.assertAlmostEqual(input_feat_extract[key].sum()			,					input_processor[key].sum()			,					delta=1E-2		)





	def      _snake_case      (		self		)			->		Union[str, Any]:
		snake_case__  					=      self.get_feature_extractor()
		snake_case__  					=      self.get_tokenizer()

		snake_case__  					=      WhisperProcessor(tokenizer=UpperCamelCase_			,					feature_extractor=UpperCamelCase_		)

		snake_case__  					=      'This is a test string'

		snake_case__  					=      processor(text=UpperCamelCase_		)

		snake_case__  					=      tokenizer(UpperCamelCase_		)

		for key in encoded_tok.keys():
			self.assertListEqual(encoded_tok[key]			,					encoded_processor[key]		)





	def      _snake_case      (		self		)			->		Tuple:
		snake_case__  					=      self.get_feature_extractor()
		snake_case__  					=      self.get_tokenizer()

		snake_case__  					=      WhisperProcessor(tokenizer=UpperCamelCase_			,					feature_extractor=UpperCamelCase_		)

		snake_case__  					=      [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

		snake_case__  					=      processor.batch_decode(UpperCamelCase_		)
		snake_case__  					=      tokenizer.batch_decode(UpperCamelCase_		)

		self.assertListEqual(UpperCamelCase_			,					UpperCamelCase_		)





	def      _snake_case      (		self		)			->		List[str]:
		snake_case__  					=      self.get_feature_extractor()
		snake_case__  					=      self.get_tokenizer()

		snake_case__  					=      WhisperProcessor(tokenizer=UpperCamelCase_			,					feature_extractor=UpperCamelCase_		)

		self.assertListEqual(
		    processor.model_input_names			,					feature_extractor.model_input_names			,					msg='`processor` and `feature_extractor` model input names do not match'			,					)





	def      _snake_case      (		self		)			->		Dict:
		snake_case__  					=      self.get_feature_extractor()
		snake_case__  					=      self.get_tokenizer()

		snake_case__  					=      WhisperProcessor(tokenizer=UpperCamelCase_			,					feature_extractor=UpperCamelCase_		)
		snake_case__  					=      processor.get_decoder_prompt_ids(task='transcribe'			,					no_timestamps=UpperCamelCase_		)

		self.assertIsInstance(UpperCamelCase_			,					UpperCamelCase_		)
		for ids in forced_decoder_ids:
			self.assertIsInstance(UpperCamelCase_			,					(list, tuple)		)

		snake_case__  					=      [TRANSCRIBE, NOTIMESTAMPS]
		self.assertListEqual([ids[-1] for ids in forced_decoder_ids]			,					UpperCamelCase_		)





	def      _snake_case      (		self		)			->		List[str]:
		snake_case__  					=      WhisperProcessor(tokenizer=self.get_tokenizer()			,					feature_extractor=self.get_feature_extractor()		)
		snake_case__  					=      processor.get_prompt_ids('Mr. Quilter'		)
		snake_case__  					=      processor.tokenizer.decode(UpperCamelCase_		)

		self.assertListEqual(prompt_ids.tolist()			,					[5_0360, 1770, 13, 2264, 346, 353]		)
		self.assertEqual(UpperCamelCase_			,					'<|startofprev|> Mr. Quilter'		)





	def      _snake_case      (		self		)			->		Dict:
		snake_case__  					=      WhisperProcessor(tokenizer=self.get_tokenizer()			,					feature_extractor=self.get_feature_extractor()		)
		snake_case__  					=      processor.get_prompt_ids(''		)
		snake_case__  					=      processor.tokenizer.decode(UpperCamelCase_		)

		self.assertListEqual(prompt_ids.tolist()			,					[5_0360, 220]		)
		self.assertEqual(UpperCamelCase_			,					'<|startofprev|> '		)





	def      _snake_case      (		self		)			->		int:
		snake_case__  					=      WhisperProcessor(tokenizer=self.get_tokenizer()			,					feature_extractor=self.get_feature_extractor()		)

		def _test_prompt_error_raised_helper(UpperCamelCase_			,					UpperCamelCase_		):
			with pytest.raises(UpperCamelCase_		) as excinfo:
				processor.get_prompt_ids(UpperCamelCase_		)
			snake_case__  					=      F'''Encountered text in the prompt corresponding to disallowed special token: {special_token}.'''
			self.assertEqual(UpperCamelCase_			,					str(excinfo.value		)		)

		_test_prompt_error_raised_helper('<|startofprev|> test'			,					'<|startofprev|>'		)
		_test_prompt_error_raised_helper('test <|notimestamps|>'			,					'<|notimestamps|>'		)
		_test_prompt_error_raised_helper('test <|zh|> test <|transcribe|>'			,					'<|zh|>'		)



