
"""simple docstring"""


import inspect
import unittest

import numpy as np

from transformers import BeitConfig
from transformers.testing_utils import require_flax, require_vision, slow
from transformers.utils import cached_property, is_flax_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor, ids_tensor


if is_flax_available():
							import jax

							from transformers import FlaxBeitForImageClassification, FlaxBeitForMaskedImageModeling, FlaxBeitModel

if is_vision_available():
							from PIL import Image

							from transformers import BeitImageProcessor

class     A_	(     unittest.TestCase   ):



							"""simple docstring"""






							def __init__(  self  :Optional[int]     ,							lowerCamelCase_  :Any     ,							lowerCamelCase_  :Optional[Any]=100     ,							lowerCamelCase_  :List[str]=13     ,							lowerCamelCase_  :List[Any]=30     ,							lowerCamelCase_  :int=2     ,							lowerCamelCase_  :int=3     ,							lowerCamelCase_  :Union[str, Any]=True     ,							lowerCamelCase_  :List[str]=True     ,							lowerCamelCase_  :Any=32     ,							lowerCamelCase_  :List[str]=5     ,							lowerCamelCase_  :Tuple=4     ,							lowerCamelCase_  :Any=37     ,							lowerCamelCase_  :List[Any]="gelu"     ,							lowerCamelCase_  :int=0.1     ,							lowerCamelCase_  :Dict=0.1     ,							lowerCamelCase_  :int=10     ,							lowerCamelCase_  :Dict=0.02     ,							lowerCamelCase_  :Dict=3     ,							):





										"""simple docstring"""

										lowerCamelCase__	:     Union[str, Any]              =parent
										lowerCamelCase__	:     str              =vocab_size
										lowerCamelCase__	:     int              =batch_size
										lowerCamelCase__	:     Any              =image_size
										lowerCamelCase__	:     Any              =patch_size
										lowerCamelCase__	:     Optional[int]              =num_channels
										lowerCamelCase__	:     Any              =is_training
										lowerCamelCase__	:     List[str]              =use_labels
										lowerCamelCase__	:     Tuple              =hidden_size
										lowerCamelCase__	:     Dict              =num_hidden_layers
										lowerCamelCase__	:     str              =num_attention_heads
										lowerCamelCase__	:     List[Any]              =intermediate_size
										lowerCamelCase__	:     Any              =hidden_act
										lowerCamelCase__	:     Optional[int]              =hidden_dropout_prob
										lowerCamelCase__	:     Optional[Any]              =attention_probs_dropout_prob
										lowerCamelCase__	:     List[str]              =type_sequence_label_size
										lowerCamelCase__	:     List[Any]              =initializer_range

										# in BeiT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
										lowerCamelCase__	:     List[str]              =(image_size // patch_size) ** 2
										lowerCamelCase__	:     Union[str, Any]              =num_patches + 1





							def       UpperCAmelCase__							(  self  :Union[str, Any]       ):





										"""simple docstring"""

										lowerCamelCase__	:     int              =floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]       )

										lowerCamelCase__	:     List[Any]              =None
										if self.use_labels:
													lowerCamelCase__	:     List[Any]              =ids_tensor([self.batch_size]     ,							self.type_sequence_label_size       )

										lowerCamelCase__	:     Union[str, Any]              =BeitConfig(
										    vocab_size=self.vocab_size     ,							image_size=self.image_size     ,							patch_size=self.patch_size     ,							num_channels=self.num_channels     ,							hidden_size=self.hidden_size     ,							num_hidden_layers=self.num_hidden_layers     ,							num_attention_heads=self.num_attention_heads     ,							intermediate_size=self.intermediate_size     ,							hidden_act=self.hidden_act     ,							hidden_dropout_prob=self.hidden_dropout_prob     ,							attention_probs_dropout_prob=self.attention_probs_dropout_prob     ,							is_decoder=SCREAMING_SNAKE_CASE__     ,							initializer_range=self.initializer_range     ,							)

										return config, pixel_values, labels





							def       UpperCAmelCase__							(  self  :List[Any]     ,							lowerCamelCase_  :List[str]     ,							lowerCamelCase_  :Optional[Any]     ,							lowerCamelCase_  :Union[str, Any]       ):





										"""simple docstring"""

										lowerCamelCase__	:     List[Any]              =FlaxBeitModel(config=SCREAMING_SNAKE_CASE__       )
										lowerCamelCase__	:     int              =model(SCREAMING_SNAKE_CASE__       )
										self.parent.assertEqual(result.last_hidden_state.shape     ,							(self.batch_size, self.seq_length, self.hidden_size)       )





							def       UpperCAmelCase__							(  self  :Any     ,							lowerCamelCase_  :Union[str, Any]     ,							lowerCamelCase_  :str     ,							lowerCamelCase_  :List[str]       ):





										"""simple docstring"""

										lowerCamelCase__	:     List[Any]              =FlaxBeitForMaskedImageModeling(config=SCREAMING_SNAKE_CASE__       )
										lowerCamelCase__	:     Optional[Any]              =model(SCREAMING_SNAKE_CASE__       )
										self.parent.assertEqual(result.logits.shape     ,							(self.batch_size, self.seq_length - 1, self.vocab_size)       )





							def       UpperCAmelCase__							(  self  :int     ,							lowerCamelCase_  :Optional[int]     ,							lowerCamelCase_  :int     ,							lowerCamelCase_  :Optional[int]       ):





										"""simple docstring"""

										lowerCamelCase__	:     List[Any]              =self.type_sequence_label_size
										lowerCamelCase__	:     List[str]              =FlaxBeitForImageClassification(config=SCREAMING_SNAKE_CASE__       )
										lowerCamelCase__	:     Any              =model(SCREAMING_SNAKE_CASE__       )
										self.parent.assertEqual(result.logits.shape     ,							(self.batch_size, self.type_sequence_label_size)       )

										# test greyscale images
										lowerCamelCase__	:     Optional[int]              =1
										lowerCamelCase__	:     str              =FlaxBeitForImageClassification(SCREAMING_SNAKE_CASE__       )

										lowerCamelCase__	:     Optional[int]              =floats_tensor([self.batch_size, 1, self.image_size, self.image_size]       )
										lowerCamelCase__	:     List[Any]              =model(SCREAMING_SNAKE_CASE__       )





							def       UpperCAmelCase__							(  self  :Tuple       ):





										"""simple docstring"""

										lowerCamelCase__	:     int              =self.prepare_config_and_inputs()
										(
										    (
										    lowerCamelCase__    
										)       ,				(
										    lowerCamelCase__    
										)       ,				(
										    lowerCamelCase__    
										)       ,				    
										)	:     List[Any]              =config_and_inputs
										lowerCamelCase__	:     Optional[Any]              ={'pixel_values': pixel_values}
										return config, inputs_dict






@require_flax
class     A_	(     __lowercase      ,					unittest.TestCase   ):



							"""simple docstring"""






							SCREAMING_SNAKE_CASE_	      =   (
							    (FlaxBeitModel, FlaxBeitForImageClassification, FlaxBeitForMaskedImageModeling) if is_flax_available() else ()
							)





							def       UpperCAmelCase__							(  self  :int       ):





										"""simple docstring"""

										lowerCamelCase__	:     Optional[Any]              =FlaxBeitModelTester(self       )
										lowerCamelCase__	:     int              =ConfigTester(self     ,							config_class=SCREAMING_SNAKE_CASE__     ,							has_text_modality=SCREAMING_SNAKE_CASE__     ,							hidden_size=37       )





							def       UpperCAmelCase__							(  self  :Optional[int]       ):





										"""simple docstring"""

										self.config_tester.run_common_tests()





							def       UpperCAmelCase__							(  self  :List[Any]       ):





										"""simple docstring"""

										lowerCamelCase__       ,				lowerCamelCase__	:     Union[str, Any]              =self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes:
													lowerCamelCase__	:     Optional[int]              =model_class(SCREAMING_SNAKE_CASE__       )
													lowerCamelCase__	:     Tuple              =inspect.signature(model.__call__       )
													# signature.parameters is an OrderedDict => so arg_names order is deterministic
													lowerCamelCase__	:     Optional[Any]              =[*signature.parameters.keys()]

													lowerCamelCase__	:     Dict              =['pixel_values']
													self.assertListEqual(arg_names[:1]     ,							SCREAMING_SNAKE_CASE__       )





							def       UpperCAmelCase__							(  self  :Union[str, Any]       ):





										"""simple docstring"""

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

										for model_class in self.all_model_classes:
													with self.subTest(model_class.__name__       ):
																lowerCamelCase__	:     List[Any]              =self._prepare_for_class(SCREAMING_SNAKE_CASE__     ,							SCREAMING_SNAKE_CASE__       )
																lowerCamelCase__	:     int              =model_class(SCREAMING_SNAKE_CASE__       )

																@jax.jit
																def model_jitted(lowerCamelCase_  :str     ,							**lowerCamelCase_  :Dict       ):
																			return model(pixel_values=SCREAMING_SNAKE_CASE__     ,							**SCREAMING_SNAKE_CASE__       )

																with self.subTest('JIT Enabled'       ):
																			lowerCamelCase__	:     Optional[Any]              =model_jitted(**SCREAMING_SNAKE_CASE__       ).to_tuple()

																with self.subTest('JIT Disabled'       ):
																			with jax.disable_jit():
																						lowerCamelCase__	:     Any              =model_jitted(**SCREAMING_SNAKE_CASE__       ).to_tuple()

																self.assertEqual(len(SCREAMING_SNAKE_CASE__       )     ,							len(SCREAMING_SNAKE_CASE__       )       )
																for jitted_output, output in zip(SCREAMING_SNAKE_CASE__     ,							SCREAMING_SNAKE_CASE__       ):
																			self.assertEqual(jitted_output.shape     ,							output.shape       )





							def       UpperCAmelCase__							(  self  :Any       ):





										"""simple docstring"""

										lowerCamelCase__	:     List[str]              =self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE__       )





							def       UpperCAmelCase__							(  self  :Any       ):





										"""simple docstring"""

										lowerCamelCase__	:     Optional[int]              =self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_masked_lm(*SCREAMING_SNAKE_CASE__       )





							def       UpperCAmelCase__							(  self  :Optional[int]       ):





										"""simple docstring"""

										lowerCamelCase__	:     Optional[int]              =self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_image_classification(*SCREAMING_SNAKE_CASE__       )





							@slow
							def       UpperCAmelCase__							(  self  :List[Any]       ):





										"""simple docstring"""

										for model_class_name in self.all_model_classes:
													lowerCamelCase__	:     int              =model_class_name.from_pretrained('microsoft/beit-base-patch16-224'       )
													lowerCamelCase__	:     int              =model(np.ones((1, 3, 224, 224)       )       )
													self.assertIsNotNone(SCREAMING_SNAKE_CASE__       )






def    lowerCAmelCase_   (		)   ->List[str]:
			lowerCamelCase__	:     Dict              =Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'			)
			return image

@require_vision
@require_flax
class     A_	(     unittest.TestCase   ):



							"""simple docstring"""






							@cached_property
							def       UpperCAmelCase__							(  self  :List[str]       ):





										"""simple docstring"""

										return BeitImageProcessor.from_pretrained('microsoft/beit-base-patch16-224'       ) if is_vision_available() else None





							@slow
							def       UpperCAmelCase__							(  self  :int       ):





										"""simple docstring"""

										lowerCamelCase__	:     List[str]              =FlaxBeitForMaskedImageModeling.from_pretrained('microsoft/beit-base-patch16-224-pt22k'       )

										lowerCamelCase__	:     List[str]              =self.default_image_processor
										lowerCamelCase__	:     List[str]              =prepare_img()
										lowerCamelCase__	:     Tuple              =image_processor(images=SCREAMING_SNAKE_CASE__     ,							return_tensors='np'       ).pixel_values

										# prepare bool_masked_pos
										lowerCamelCase__	:     Optional[int]              =np.ones((1, 196)     ,							dtype=SCREAMING_SNAKE_CASE__       )

										# forward pass
										lowerCamelCase__	:     int              =model(pixel_values=SCREAMING_SNAKE_CASE__     ,							bool_masked_pos=SCREAMING_SNAKE_CASE__       )
										lowerCamelCase__	:     Union[str, Any]              =outputs.logits

										# verify the logits
										lowerCamelCase__	:     int              =(1, 196, 8_192)
										self.assertEqual(logits.shape     ,							SCREAMING_SNAKE_CASE__       )

										lowerCamelCase__	:     str              =np.array(
										    [[-3.24_37, 0.50_72, -13.91_74], [-3.24_56, 0.49_48, -13.94_01], [-3.20_33, 0.51_21, -13.85_50]]       )

										self.assertTrue(np.allclose(logits[bool_masked_pos][:3, :3]     ,							SCREAMING_SNAKE_CASE__     ,							atol=1e-2       )       )





							@slow
							def       UpperCAmelCase__							(  self  :int       ):





										"""simple docstring"""

										lowerCamelCase__	:     Dict              =FlaxBeitForImageClassification.from_pretrained('microsoft/beit-base-patch16-224'       )

										lowerCamelCase__	:     int              =self.default_image_processor
										lowerCamelCase__	:     Any              =prepare_img()
										lowerCamelCase__	:     Union[str, Any]              =image_processor(images=SCREAMING_SNAKE_CASE__     ,							return_tensors='np'       )

										# forward pass
										lowerCamelCase__	:     Dict              =model(**SCREAMING_SNAKE_CASE__       )
										lowerCamelCase__	:     Tuple              =outputs.logits

										# verify the logits
										lowerCamelCase__	:     Dict              =(1, 1_000)
										self.assertEqual(logits.shape     ,							SCREAMING_SNAKE_CASE__       )

										lowerCamelCase__	:     int              =np.array([-1.23_85, -1.09_87, -1.01_08]       )

										self.assertTrue(np.allclose(logits[0, :3]     ,							SCREAMING_SNAKE_CASE__     ,							atol=1e-4       )       )

										lowerCamelCase__	:     Any              =281
										self.assertEqual(logits.argmax(-1       ).item()     ,							SCREAMING_SNAKE_CASE__       )





							@slow
							def       UpperCAmelCase__							(  self  :Dict       ):





										"""simple docstring"""

										lowerCamelCase__	:     Union[str, Any]              =FlaxBeitForImageClassification.from_pretrained('microsoft/beit-large-patch16-224-pt22k-ft22k'       )

										lowerCamelCase__	:     List[str]              =self.default_image_processor
										lowerCamelCase__	:     int              =prepare_img()
										lowerCamelCase__	:     Tuple              =image_processor(images=SCREAMING_SNAKE_CASE__     ,							return_tensors='np'       )

										# forward pass
										lowerCamelCase__	:     List[str]              =model(**SCREAMING_SNAKE_CASE__       )
										lowerCamelCase__	:     List[Any]              =outputs.logits

										# verify the logits
										lowerCamelCase__	:     str              =(1, 21_841)
										self.assertEqual(logits.shape     ,							SCREAMING_SNAKE_CASE__       )

										lowerCamelCase__	:     Dict              =np.array([1.68_81, -0.27_87, 0.59_01]       )

										self.assertTrue(np.allclose(logits[0, :3]     ,							SCREAMING_SNAKE_CASE__     ,							atol=1e-4       )       )

										lowerCamelCase__	:     Optional[int]              =2_396
										self.assertEqual(logits.argmax(-1       ).item()     ,							SCREAMING_SNAKE_CASE__       )

import pprint

import requests

SCREAMING_SNAKE_CASE__   :			str												=    "https://zenquotes.io/api"




def  __magic_name__ (   )			->   list:
				return requests.get(API_ENDPOINT_URL + '''/today'''							).json()




def  __magic_name__ (   )			->   list:
				return requests.get(API_ENDPOINT_URL + '''/random'''							).json()


if __name__ == "__main__":
					SCREAMING_SNAKE_CASE__   :			int												=    random_quotes()
					pprint.pprint(response)



