


'''simple docstring'''






from __future__ import annotations

import inspect
import unittest

from transformers import ViTConfig
from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import cached_property, is_tf_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
  import tensorflow as tf

  from transformers import TFViTForImageClassification, TFViTModel


if is_vision_available():
  from PIL import Image

  from transformers import ViTImageProcessor






class   __snake_case			:







    """simple docstring"""



    def __init__(  self       :    str       ,		lowerCamelCase       :    Optional[int]       ,		lowerCamelCase       :    Union[str, Any]=13       ,		lowerCamelCase       :    Dict=30       ,		lowerCamelCase       :    Dict=2       ,		lowerCamelCase       :    Optional[Any]=3       ,		lowerCamelCase       :    Any=True       ,		lowerCamelCase       :    List[str]=True       ,		lowerCamelCase       :    Union[str, Any]=32       ,		lowerCamelCase       :    Union[str, Any]=2       ,		lowerCamelCase       :    Tuple=4       ,		lowerCamelCase       :    Union[str, Any]=37       ,		lowerCamelCase       :    Any="gelu"       ,		lowerCamelCase       :    Tuple=0.1       ,		lowerCamelCase       :    Union[str, Any]=0.1       ,		lowerCamelCase       :    Dict=10       ,		lowerCamelCase       :    Dict=0.02       ,		lowerCamelCase       :    List[str]=3       ,		lowerCamelCase       :    Any=None       ,		)  ->    Tuple:
      lowerCAmelCase_     : List[Any]     =  parent
      lowerCAmelCase_     : Optional[Any]     =  batch_size
      lowerCAmelCase_     : List[str]     =  image_size
      lowerCAmelCase_     : Optional[int]     =  patch_size
      lowerCAmelCase_     : Union[str, Any]     =  num_channels
      lowerCAmelCase_     : str     =  is_training
      lowerCAmelCase_     : List[Any]     =  use_labels
      lowerCAmelCase_     : int     =  hidden_size
      lowerCAmelCase_     : int     =  num_hidden_layers
      lowerCAmelCase_     : str     =  num_attention_heads
      lowerCAmelCase_     : Any     =  intermediate_size
      lowerCAmelCase_     : Union[str, Any]     =  hidden_act
      lowerCAmelCase_     : List[str]     =  hidden_dropout_prob
      lowerCAmelCase_     : Any     =  attention_probs_dropout_prob
      lowerCAmelCase_     : Optional[int]     =  type_sequence_label_size
      lowerCAmelCase_     : List[Any]     =  initializer_range
      lowerCAmelCase_     : List[Any]     =  scope

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

    def __lowercase  (  self       :    Any						)  ->    Dict:
      lowerCAmelCase_     : Any     =  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

      lowerCAmelCase_     : Union[str, Any]     =  None
      if self.use_labels:
        lowerCAmelCase_     : Optional[Any]     =  ids_tensor([self.batch_size]       ,		self.type_sequence_label_size						)

      lowerCAmelCase_     : int     =  self.get_config()

      return config, pixel_values, labels

    def __lowercase  (  self       :    Optional[Any]						)  ->    Union[str, Any]:
      return 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=lowerCamelCase       ,		initializer_range=self.initializer_range       ,		)

    def __lowercase  (  self       :    Dict       ,		lowerCamelCase       :    Tuple       ,		lowerCamelCase       :    Tuple       ,		lowerCamelCase       :    List[Any]						)  ->    List[str]:
      lowerCAmelCase_     : Union[str, Any]     =  TFViTModel(config=lowerCamelCase						)
      lowerCAmelCase_     : int     =  model(lowerCamelCase       ,		training=lowerCamelCase						)
      self.parent.assertEqual(result.last_hidden_state.shape       ,		(self.batch_size, self.seq_length, self.hidden_size)						)

      # Test with an image with different size than the one specified in config.
      lowerCAmelCase_     : Optional[Any]     =  self.image_size // 2
      lowerCAmelCase_     : List[Any]     =  pixel_values[:, :, :image_size, :image_size]
      lowerCAmelCase_     : Tuple     =  model(lowerCamelCase       ,		interpolate_pos_encoding=lowerCamelCase       ,		training=lowerCamelCase						)
      lowerCAmelCase_     : Any     =  (image_size // self.patch_size) ** 2 + 1
      self.parent.assertEqual(result.last_hidden_state.shape       ,		(self.batch_size, seq_length, self.hidden_size)						)

    def __lowercase  (  self       :    int       ,		lowerCamelCase       :    List[str]       ,		lowerCamelCase       :    List[str]       ,		lowerCamelCase       :    List[Any]						)  ->    Union[str, Any]:
      lowerCAmelCase_     : int     =  self.type_sequence_label_size
      lowerCAmelCase_     : int     =  TFViTForImageClassification(lowerCamelCase						)
      lowerCAmelCase_     : Optional[Any]     =  model(lowerCamelCase       ,		labels=lowerCamelCase       ,		training=lowerCamelCase						)
      self.parent.assertEqual(result.logits.shape       ,		(self.batch_size, self.type_sequence_label_size)						)

      # Test with an image with different size than the one specified in config.
      lowerCAmelCase_     : Dict     =  self.image_size // 2
      lowerCAmelCase_     : Any     =  pixel_values[:, :, :image_size, :image_size]
      lowerCAmelCase_     : str     =  model(lowerCamelCase       ,		interpolate_pos_encoding=lowerCamelCase       ,		training=lowerCamelCase						)
      self.parent.assertEqual(result.logits.shape       ,		(self.batch_size, self.type_sequence_label_size)						)

      # test greyscale images
      lowerCAmelCase_     : List[str]     =  1
      lowerCAmelCase_     : Any     =  TFViTForImageClassification(lowerCamelCase						)
      lowerCAmelCase_     : List[str]     =  floats_tensor([self.batch_size, 1, self.image_size, self.image_size]						)
      lowerCAmelCase_     : Optional[int]     =  model(lowerCamelCase						)
      self.parent.assertEqual(result.logits.shape       ,		(self.batch_size, self.type_sequence_label_size)						)





    def __lowercase  (  self       :    List[str]						)  ->    List[Any]:
      lowerCAmelCase_     : Tuple     =  self.prepare_config_and_inputs()
      lowerCAmelCase_,				lowerCAmelCase_,				lowerCAmelCase_     : Dict     =  config_and_inputs
      lowerCAmelCase_     : str     =  {"""pixel_values""": pixel_values}
      return config, inputs_dict







@require_tf
class   __snake_case			(  _SCREAMING_SNAKE_CASE      ,_SCREAMING_SNAKE_CASE      ,unittest.TestCase):







    """simple docstring"""



    lowercase  					=   (TFViTModel, TFViTForImageClassification) if is_tf_available() else ()
    lowercase  					=   (
        {'feature-extraction': TFViTModel, 'image-classification': TFViTForImageClassification}
        if is_tf_available()
        else {}
    )

    lowercase  					=   False
    lowercase  					=   False
    lowercase  					=   False

    def __lowercase  (  self       :    Optional[int]						)  ->    Dict:
      lowerCAmelCase_     : List[str]     =  TFViTModelTester(self						)
      lowerCAmelCase_     : Tuple     =  ConfigTester(self       ,		config_class=lowerCamelCase       ,		has_text_modality=lowerCamelCase       ,		hidden_size=37						)

    def __lowercase  (  self       :    int						)  ->    List[Any]:
      self.config_tester.run_common_tests()

    @unittest.skip(reason="""ViT does not use inputs_embeds"""						)
    def __lowercase  (  self       :    List[Any]						)  ->    Optional[Any]:
      pass

    @unittest.skip(reason="""ViT does not use inputs_embeds"""						)
    def __lowercase  (  self       :    Any						)  ->    List[str]:
      pass

    def __lowercase  (  self       :    Optional[int]						)  ->    Union[str, Any]:
      lowerCAmelCase_,				lowerCAmelCase_     : List[str]     =  self.model_tester.prepare_config_and_inputs_for_common()

      for model_class in self.all_model_classes:
        lowerCAmelCase_     : str     =  model_class(lowerCamelCase						)
        self.assertIsInstance(model.get_input_embeddings()       ,		(tf.keras.layers.Layer)						)
        lowerCAmelCase_     : Optional[int]     =  model.get_output_embeddings()
        self.assertTrue(x is None or isinstance(lowerCamelCase       ,		tf.keras.layers.Layer						)						)

    def __lowercase  (  self       :    List[Any]						)  ->    List[str]:
      lowerCAmelCase_,				lowerCAmelCase_     : Dict     =  self.model_tester.prepare_config_and_inputs_for_common()

      for model_class in self.all_model_classes:
        lowerCAmelCase_     : int     =  model_class(lowerCamelCase						)
        lowerCAmelCase_     : List[Any]     =  inspect.signature(model.call						)
        # signature.parameters is an OrderedDict => so arg_names order is deterministic
        lowerCAmelCase_     : Any     =  [*signature.parameters.keys()]

        lowerCAmelCase_     : List[str]     =  ["""pixel_values"""]
        self.assertListEqual(arg_names[:1]       ,		lowerCamelCase						)

    def __lowercase  (  self       :    List[Any]						)  ->    Union[str, Any]:
      lowerCAmelCase_     : Optional[Any]     =  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*lowerCamelCase						)

    def __lowercase  (  self       :    Optional[Any]						)  ->    List[Any]:
      lowerCAmelCase_     : int     =  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_image_classification(*lowerCamelCase						)





    @slow
    def __lowercase  (  self       :    Tuple						)  ->    Tuple:
      lowerCAmelCase_     : Union[str, Any]     =  TFViTModel.from_pretrained("""google/vit-base-patch16-224"""						)
      self.assertIsNotNone(lowerCamelCase						)







def 							UpperCamelCase_     (       ):
  '''simple docstring'''





  lowerCAmelCase_     : Any     =  Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""   )
  return image






@require_tf
@require_vision
class   __snake_case			(  unittest.TestCase):







    """simple docstring"""



    @cached_property
    def __lowercase  (  self       :    List[str]						)  ->    Optional[int]:
      return ViTImageProcessor.from_pretrained("""google/vit-base-patch16-224"""						) if is_vision_available() else None





    @slow
    def __lowercase  (  self       :    Optional[Any]						)  ->    Tuple:
      lowerCAmelCase_     : Any     =  TFViTForImageClassification.from_pretrained("""google/vit-base-patch16-224"""						)

      lowerCAmelCase_     : Any     =  self.default_image_processor
      lowerCAmelCase_     : int     =  prepare_img()
      lowerCAmelCase_     : Dict     =  image_processor(images=lowerCamelCase       ,		return_tensors="""tf"""						)

      # forward pass
      lowerCAmelCase_     : int     =  model(**lowerCamelCase						)

      # verify the logits
      lowerCAmelCase_     : Union[str, Any]     =  tf.TensorShape((1, 10_00)						)
      self.assertEqual(outputs.logits.shape       ,		lowerCamelCase						)

      lowerCAmelCase_     : List[str]     =  tf.constant([-0.2_744, 0.8_215, -0.0_836]						)

      tf.debugging.assert_near(outputs.logits[0, :3]       ,		lowerCamelCase       ,		atol=1E-4						)







'''simple docstring'''






import requests
from bsa import BeautifulSoup







def 							UpperCamelCase_     (       A__   :  str = "AAPL"   ):
  '''simple docstring'''





  lowerCAmelCase_     : Dict     =  f'https://in.finance.yahoo.com/quote/{symbol}?s={symbol}'
  lowerCAmelCase_     : Optional[Any]     =  BeautifulSoup(requests.get(A__   ).text				,       """html.parser"""   )
  lowerCAmelCase_     : Union[str, Any]     =  """My(6px) Pos(r) smartphone_Mt(6px)"""
  return soup.find("""div"""				,       class_=class_   ).find("""span"""   ).text


if __name__ == "__main__":
  for symbol in "AAPL AMZN IBM GOOG MSFT ORCL".split():
    print(F'''Current {symbol:<4} stock price is {stock_price(symbol):>8}''')



