




'''simple docstring'''







import inspect
import unittest

import numpy as np

from transformers import ViTConfig, is_flax_available
from transformers.testing_utils import require_flax, slow

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


if is_flax_available():
   import jax

   from transformers.models.vit.modeling_flax_vit import FlaxViTForImageClassification, FlaxViTModel


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



           '''simple docstring'''

           UpperCAmelCase     =	parent
           UpperCAmelCase     =	batch_size
           UpperCAmelCase     =	image_size
           UpperCAmelCase     =	patch_size
           UpperCAmelCase     =	num_channels
           UpperCAmelCase     =	is_training
           UpperCAmelCase     =	use_labels
           UpperCAmelCase     =	hidden_size
           UpperCAmelCase     =	num_hidden_layers
           UpperCAmelCase     =	num_attention_heads
           UpperCAmelCase     =	intermediate_size
           UpperCAmelCase     =	hidden_act
           UpperCAmelCase     =	hidden_dropout_prob
           UpperCAmelCase     =	attention_probs_dropout_prob
           UpperCAmelCase     =	type_sequence_label_size
           UpperCAmelCase     =	initializer_range

           # in ViT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
           UpperCAmelCase     =	(image_size // patch_size) ** 2
           UpperCAmelCase     =	num_patches + 1
       def 					A   (			self		:			str       ):



           '''simple docstring'''

           UpperCAmelCase     =	floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]       )

           UpperCAmelCase     =	ViTConfig(
               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=lowercase     ,       initializer_range=self.initializer_range     ,       )

           return config, pixel_values
       def 					A   (			self		:			Optional[Any]     ,       lowercase		:			Union[str, Any]     ,       lowercase		:			str       ):



           '''simple docstring'''

           UpperCAmelCase     =	FlaxViTModel(config=lowercase       )
           UpperCAmelCase     =	model(lowercase       )
           # expected sequence length = num_patches + 1 (we add 1 for the [CLS] token)
           UpperCAmelCase     =	(self.image_size, self.image_size)
           UpperCAmelCase     =	(self.patch_size, self.patch_size)
           UpperCAmelCase     =	(image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])
           self.parent.assertEqual(result.last_hidden_state.shape     ,       (self.batch_size, num_patches + 1, self.hidden_size)       )
       def 					A   (			self		:			List[Any]     ,       lowercase		:			List[Any]     ,       lowercase		:			Tuple       ):



           '''simple docstring'''

           UpperCAmelCase     =	self.type_sequence_label_size
           UpperCAmelCase     =	FlaxViTForImageClassification(config=lowercase       )
           UpperCAmelCase     =	model(lowercase       )
           self.parent.assertEqual(result.logits.shape     ,       (self.batch_size, self.type_sequence_label_size)       )

           # test greyscale images
           UpperCAmelCase     =	1
           UpperCAmelCase     =	FlaxViTForImageClassification(lowercase       )

           UpperCAmelCase     =	floats_tensor([self.batch_size, 1, self.image_size, self.image_size]       )
           UpperCAmelCase     =	model(lowercase       )







       def 					A   (			self		:			Any       ):



           '''simple docstring'''

           UpperCAmelCase     =	self.prepare_config_and_inputs()
           (
               (
               UpperCAmelCase
           ) ,  (
               UpperCAmelCase
           ) ,  
           )     =	config_and_inputs
           UpperCAmelCase     =	{'''pixel_values''': pixel_values}
           return config, inputs_dict






@require_flax
class     _a						(			__a							,							unittest.TestCase				):
       __a       :  List[Any]            =      (FlaxViTModel, FlaxViTForImageClassification) if is_flax_available() else ()
       def 					A   (			self		:			Any       ):



           '''simple docstring'''

           UpperCAmelCase     =	FlaxViTModelTester(self       )
           UpperCAmelCase     =	ConfigTester(self     ,       config_class=lowercase     ,       has_text_modality=lowercase     ,       hidden_size=37       )
       def 					A   (			self		:			List[Any]       ):



           '''simple docstring'''

           self.config_tester.run_common_tests()
       def 					A   (			self		:			Any       ):



           '''simple docstring'''

           UpperCAmelCase     =	self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*lowercase       )
       def 					A   (			self		:			Any       ):



           '''simple docstring'''

           UpperCAmelCase     =	self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_image_classification(*lowercase       )
       def 					A   (			self		:			List[str]       ):



           '''simple docstring'''

           UpperCAmelCase ,  UpperCAmelCase     =	self.model_tester.prepare_config_and_inputs_for_common()

           for model_class in self.all_model_classes:
               UpperCAmelCase     =	model_class(lowercase       )
               UpperCAmelCase     =	inspect.signature(model.__call__       )
               # signature.parameters is an OrderedDict => so arg_names order is deterministic
               UpperCAmelCase     =	[*signature.parameters.keys()]

               UpperCAmelCase     =	['''pixel_values''']
               self.assertListEqual(arg_names[:1]     ,       lowercase       )
       def 					A   (			self		:			List[Any]       ):



           '''simple docstring'''

           UpperCAmelCase ,  UpperCAmelCase     =	self.model_tester.prepare_config_and_inputs_for_common()

           for model_class in self.all_model_classes:
               with self.subTest(model_class.__name__       ):
                   UpperCAmelCase     =	self._prepare_for_class(lowercase     ,       lowercase       )
                   UpperCAmelCase     =	model_class(lowercase       )

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

                   with self.subTest('''JIT Enabled'''       ):
                       UpperCAmelCase     =	model_jitted(**lowercase       ).to_tuple()

                   with self.subTest('''JIT Disabled'''       ):
                       with jax.disable_jit():
                           UpperCAmelCase     =	model_jitted(**lowercase       ).to_tuple()

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







       @slow
       def 					A   (			self		:			Optional[Any]       ):



           '''simple docstring'''

           for model_class_name in self.all_model_classes:
               UpperCAmelCase     =	model_class_name.from_pretrained('''google/vit-base-patch16-224'''       )
               UpperCAmelCase     =	model(np.ones((1, 3, 224, 224)       )       )
               self.assertIsNotNone(lowercase       )










'''simple docstring'''







from __future__ import annotations

import requests





def 		snake_case_							(_a			:							str  ):
    UpperCAmelCase     =	F"https://hacker-news.firebaseio.com/v0/item/{story_id}.json?print=pretty"
    return requests.get(_a  ).json()





def 		snake_case_							(_a			:							int = 1_0  ):
    UpperCAmelCase     =	'''https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty'''
    UpperCAmelCase     =	requests.get(_a  ).json()[:max_stories]
    return [get_hackernews_story(_a  ) for story_id in story_ids]





def 		snake_case_							(_a			:							int = 1_0  ):
    UpperCAmelCase     =	hackernews_top_stories(_a  )
    return "\n".join('''* [{title}]({url})'''.format(**_a  ) for story in stories  )


if __name__ == "__main__":
   print(hackernews_top_stories_as_markdown())




