import inspect
import unittest

from huggingface_hub import hf_hub_download

from transformers import ConvNextConfig, UperNetConfig
from transformers.testing_utils import require_torch, require_torch_multi_gpu, 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, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
				import torch

				from transformers import UperNetForSemanticSegmentation
				from transformers.models.upernet.modeling_upernet import UPERNET_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
				from PIL import Image

				from transformers import AutoImageProcessor





class     lowerCAmelCase__     :


		'''simple docstring'''






		def __init__(       self     :     List[Any]     ,  snake_case__     :     Dict     ,  snake_case__     :     Optional[int]=1_3     ,  snake_case__     :     Optional[int]=3_2     ,  snake_case__     :     Optional[int]=3     ,  snake_case__     :     Any=4     ,  snake_case__     :     Dict=[1_0, 2_0, 3_0, 4_0]     ,  snake_case__     :     Tuple=[2, 2, 3, 2]     ,  snake_case__     :     str=True     ,  snake_case__     :     Optional[int]=True     ,  snake_case__     :     Tuple=3_7     ,  snake_case__     :     Dict="gelu"     ,  snake_case__     :     str=1_0     ,  snake_case__     :     List[Any]=0.02     ,  snake_case__     :     Optional[int]=["stage2", "stage3", "stage4"]     ,  snake_case__     :     Optional[Any]=3     ,  snake_case__     :     Dict=None     ,  )      ->	Dict:
				_lowerCamelCase				   =  parent
				_lowerCamelCase				   =  batch_size
				_lowerCamelCase				   =  image_size
				_lowerCamelCase				   =  num_channels
				_lowerCamelCase				   =  num_stages
				_lowerCamelCase				   =  hidden_sizes
				_lowerCamelCase				   =  depths
				_lowerCamelCase				   =  is_training
				_lowerCamelCase				   =  use_labels
				_lowerCamelCase				   =  intermediate_size
				_lowerCamelCase				   =  hidden_act
				_lowerCamelCase				   =  type_sequence_label_size
				_lowerCamelCase				   =  initializer_range
				_lowerCamelCase				   =  out_features
				_lowerCamelCase				   =  num_labels
				_lowerCamelCase				   =  scope
				_lowerCamelCase				   =  num_stages
		def   _snake_case       (       self     :     List[str]		)      ->	int:
				_lowerCamelCase				   =  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)

				_lowerCamelCase				   =  None
				if self.use_labels:
						_lowerCamelCase				   =  ids_tensor([self.batch_size]     ,  self.type_sequence_label_size		)

				_lowerCamelCase				   =  self.get_config()

				return config, pixel_values, labels
		def   _snake_case       (       self     :     Optional[int]		)      ->	int:
				return ConvNextConfig(
				    num_channels=self.num_channels     ,  num_stages=self.num_stages     ,  hidden_sizes=self.hidden_sizes     ,  depths=self.depths     ,  is_training=self.is_training     ,  intermediate_size=self.intermediate_size     ,  hidden_act=self.hidden_act     ,  out_features=self.out_features     ,  )
		def   _snake_case       (       self     :     List[Any]		)      ->	List[Any]:
				return UperNetConfig(
				    backbone_config=self.get_backbone_config()     ,  hidden_size=5_1_2     ,  pool_scales=[1, 2, 3, 6]     ,  use_auxiliary_head=snake_case__     ,  auxiliary_loss_weight=0.4     ,  auxiliary_in_channels=4_0     ,  auxiliary_channels=2_5_6     ,  auxiliary_num_convs=1     ,  auxiliary_concat_input=snake_case__     ,  loss_ignore_index=2_5_5     ,  num_labels=self.num_labels     ,  )
		def   _snake_case       (       self     :     List[Any]     ,  snake_case__     :     int     ,  snake_case__     :     List[str]     ,  snake_case__     :     int		)      ->	Optional[Any]:
				_lowerCamelCase				   =  UperNetForSemanticSegmentation(config=snake_case__		)
				model.to(snake_case__		)
				model.eval()
				_lowerCamelCase				   =  model(snake_case__		)
				self.parent.assertEqual(
				    result.logits.shape     ,  (self.batch_size, self.num_labels, self.image_size, self.image_size)		)



		def   _snake_case       (       self     :     Any		)      ->	Dict:
				_lowerCamelCase				   =  self.prepare_config_and_inputs()
				(
				    _lowerCamelCase      
				)				   =  config_and_inputs
				_lowerCamelCase				   =  {"""pixel_values""": pixel_values}
				return config, inputs_dict





@require_torch
class     lowerCAmelCase__     (				__snake_case     ,__snake_case     ,unittest.TestCase			):


		'''simple docstring'''






		lowerCAmelCase_     =   (UperNetForSemanticSegmentation,) if is_torch_available() else ()
		lowerCAmelCase_     =   {"""image-segmentation""": UperNetForSemanticSegmentation} if is_torch_available() else {}
		lowerCAmelCase_     =   False
		lowerCAmelCase_     =   False
		lowerCAmelCase_     =   False
		lowerCAmelCase_     =   False
		lowerCAmelCase_     =   False
		lowerCAmelCase_     =   False
		def   _snake_case       (       self     :     Dict		)      ->	Optional[int]:
				_lowerCamelCase				   =  UperNetModelTester(self		)
				_lowerCamelCase				   =  ConfigTester(self     ,  config_class=snake_case__     ,  has_text_modality=snake_case__     ,  hidden_size=3_7		)
		def   _snake_case       (       self     :     Dict		)      ->	Union[str, Any]:
				self.create_and_test_config_common_properties()
				self.config_tester.create_and_test_config_to_json_string()
				self.config_tester.create_and_test_config_to_json_file()
				self.config_tester.create_and_test_config_from_and_save_pretrained()
				self.config_tester.create_and_test_config_with_num_labels()
				self.config_tester.check_config_can_be_init_without_params()
				self.config_tester.check_config_arguments_init()
		def   _snake_case       (       self     :     Union[str, Any]		)      ->	Optional[int]:
				return
		def   _snake_case       (       self     :     Optional[int]		)      ->	Dict:
				_lowerCamelCase				   =  self.model_tester.prepare_config_and_inputs_for_common()

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

						_lowerCamelCase				   =  ["""pixel_values"""]
						self.assertListEqual(arg_names[:1]     ,  snake_case__		)
		def   _snake_case       (       self     :     List[Any]		)      ->	Union[str, Any]:
				_lowerCamelCase				   =  self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_semantic_segmentation(*snake_case__		)
		@unittest.skip(reason='UperNet does not use inputs_embeds'		)
		def   _snake_case       (       self     :     List[str]		)      ->	Any:
				pass
		@unittest.skip(reason='UperNet does not support input and output embeddings'		)
		def   _snake_case       (       self     :     Optional[Any]		)      ->	Any:
				pass
		@unittest.skip(reason='UperNet does not have a base model'		)
		def   _snake_case       (       self     :     Union[str, Any]		)      ->	Optional[int]:
				pass
		@unittest.skip(reason='UperNet does not have a base model'		)
		def   _snake_case       (       self     :     Any		)      ->	Tuple:
				pass
		@require_torch_multi_gpu
		@unittest.skip(reason='UperNet has some layers using `add_module` which doesn\'t work well with `nn.DataParallel`'		)
		def   _snake_case       (       self     :     List[str]		)      ->	Tuple:
				pass
		@unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.'		)
		def   _snake_case       (       self     :     List[str]		)      ->	Dict:
				pass
		def   _snake_case       (       self     :     Dict		)      ->	List[str]:
				def check_hidden_states_output(snake_case__     :     List[Any]     ,  snake_case__     :     str     ,  snake_case__     :     Any		):
						_lowerCamelCase				   =  model_class(snake_case__		)
						model.to(snake_case__		)
						model.eval()

						with torch.no_grad():
								_lowerCamelCase				   =  model(**self._prepare_for_class(snake_case__     ,  snake_case__		)		)

						_lowerCamelCase				   =  outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

						_lowerCamelCase				   =  self.model_tester.num_stages
						self.assertEqual(len(snake_case__		)     ,  expected_num_stages + 1		)

						# ConvNext's feature maps are of shape (batch_size, num_channels, height, width)
						self.assertListEqual(
						    list(hidden_states[0].shape[-2:]		)     ,  [self.model_tester.image_size // 4, self.model_tester.image_size // 4]     ,  )

				_lowerCamelCase				   =  self.model_tester.prepare_config_and_inputs_for_common()

				for model_class in self.all_model_classes:
						_lowerCamelCase				   =  True
						check_hidden_states_output(snake_case__     ,  snake_case__     ,  snake_case__		)

						# check that output_hidden_states also work using config
						del inputs_dict["output_hidden_states"]
						_lowerCamelCase				   =  True

						check_hidden_states_output(snake_case__     ,  snake_case__     ,  snake_case__		)
		def   _snake_case       (       self     :     str		)      ->	Optional[int]:
				_lowerCamelCase				   =  self.model_tester.prepare_config_and_inputs_for_common()

				_lowerCamelCase				   =  _config_zero_init(snake_case__		)
				_lowerCamelCase				   =  _config_zero_init(configs_no_init.backbone_config		)
				for model_class in self.all_model_classes:
						_lowerCamelCase				   =  model_class(config=snake_case__		)
						for name, param in model.named_parameters():
								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='UperNet does not have tied weights'		)
		def   _snake_case       (       self     :     List[str]		)      ->	List[Any]:
				pass



		@slow
		def   _snake_case       (       self     :     Union[str, Any]		)      ->	Tuple:
				for model_name in UPERNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
						_lowerCamelCase				   =  UperNetForSemanticSegmentation.from_pretrained(snake_case__		)
						self.assertIsNotNone(snake_case__		)



def 	lowerCamelCase					(	)   ->    Tuple:
		_lowerCamelCase				   =  hf_hub_download(
		    repo_id='hf-internal-testing/fixtures_ade20k'			,       repo_type='dataset'			,       filename='ADE_val_00000001.jpg'							)
		_lowerCamelCase				   =  Image.open(_A							).convert('RGB'							)
		return image





@require_torch
@require_vision
@slow
class     lowerCAmelCase__     (				unittest.TestCase			):


		'''simple docstring'''






		def   _snake_case       (       self     :     Optional[Any]		)      ->	str:
				_lowerCamelCase				   =  AutoImageProcessor.from_pretrained('openmmlab/upernet-swin-tiny'		)
				_lowerCamelCase				   =  UperNetForSemanticSegmentation.from_pretrained('openmmlab/upernet-swin-tiny'		).to(snake_case__		)

				_lowerCamelCase				   =  prepare_img()
				_lowerCamelCase				   =  processor(images=snake_case__     ,  return_tensors='pt'		).to(snake_case__		)

				with torch.no_grad():
						_lowerCamelCase				   =  model(**snake_case__		)

				_lowerCamelCase				   =  torch.Size((1, model.config.num_labels, 5_1_2, 5_1_2)		)
				self.assertEqual(outputs.logits.shape     ,  snake_case__		)

				_lowerCamelCase				   =  torch.tensor(
				    [[-7.5958, -7.5958, -7.4302], [-7.5958, -7.5958, -7.4302], [-7.4797, -7.4797, -7.3068]]		).to(snake_case__		)
				self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3]     ,  snake_case__     ,  atol=1e-4		)		)



		def   _snake_case       (       self     :     str		)      ->	Union[str, Any]:
				_lowerCamelCase				   =  AutoImageProcessor.from_pretrained('openmmlab/upernet-convnext-tiny'		)
				_lowerCamelCase				   =  UperNetForSemanticSegmentation.from_pretrained('openmmlab/upernet-convnext-tiny'		).to(snake_case__		)

				_lowerCamelCase				   =  prepare_img()
				_lowerCamelCase				   =  processor(images=snake_case__     ,  return_tensors='pt'		).to(snake_case__		)

				with torch.no_grad():
						_lowerCamelCase				   =  model(**snake_case__		)

				_lowerCamelCase				   =  torch.Size((1, model.config.num_labels, 5_1_2, 5_1_2)		)
				self.assertEqual(outputs.logits.shape     ,  snake_case__		)

				_lowerCamelCase				   =  torch.tensor(
				    [[-8.8110, -8.8110, -8.6521], [-8.8110, -8.8110, -8.6521], [-8.7746, -8.7746, -8.6130]]		).to(snake_case__		)
				self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3]     ,  snake_case__     ,  atol=1e-4		)		)







"""simple docstring"""







import requests
from bsa import BeautifulSoup


def      UpperCamelCase       ( _A = "AAPL" )     ->       str:
				lowercase    :      Optional[Any]       = F"""https://in.finance.yahoo.com/quote/{symbol}?s={symbol}"""
				lowercase    :      str       = BeautifulSoup(requests.get(_A ).text  ,				"""html.parser""" )
				lowercase    :      int       = """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}')
