


import torch
import torch.nn as nn
from transformers import CLIPConfig, CLIPVisionModel, PreTrainedModel

from ...utils import logging


__UpperCamelCase     		=							logging.get_logger(__name__)
def 							UpperCamelCase_(					_A :str					,  _A :int	)->		Tuple:
			UpperCamelCase__       =							nn.functional.normalize(_A	)
			UpperCamelCase__       =							nn.functional.normalize(_A	)
			return torch.mm(_A					,  normalized_text_embeds.t()	)







class 							lowerCamelCase__						(				UpperCAmelCase   ):






			"""simple docstring"""






			_UpperCamelCase		:       Union[str, Any] 			=		CLIPConfig

			_UpperCamelCase		:       List[str] 			=		['CLIPEncoderLayer']




			def __init__(				self   ,    snake_case  ):

						'''simple docstring'''


						super().__init__(snake_case  )

						UpperCamelCase__       =							CLIPVisionModel(config.vision_config  )
						UpperCamelCase__       =							nn.Linear(config.vision_config.hidden_size   ,    config.projection_dim   ,    bias=snake_case  )

						UpperCamelCase__       =							nn.Parameter(torch.ones(17   ,    config.projection_dim  )   ,    requires_grad=snake_case  )
						UpperCamelCase__       =							nn.Parameter(torch.ones(3   ,    config.projection_dim  )   ,    requires_grad=snake_case  )

						UpperCamelCase__       =							nn.Parameter(torch.ones(17  )   ,    requires_grad=snake_case  )
						UpperCamelCase__       =							nn.Parameter(torch.ones(3  )   ,    requires_grad=snake_case  )




			@torch.no_grad()
			def 					snake_case__					(				self   ,    snake_case   ,    snake_case  ):

						'''simple docstring'''


						UpperCamelCase__       =							self.vision_model(snake_case  )[1]  # pooled_output
						UpperCamelCase__       =							self.visual_projection(snake_case  )

						# we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16
						UpperCamelCase__       =							cosine_distance(snake_case   ,    self.special_care_embeds  ).cpu().float().numpy()
						UpperCamelCase__       =							cosine_distance(snake_case   ,    self.concept_embeds  ).cpu().float().numpy()

						UpperCamelCase__       =							[]
						UpperCamelCase__       =							image_embeds.shape[0]
						for i in range(snake_case  ):
									UpperCamelCase__       =							{"special_scores": {}, "special_care": [], "concept_scores": {}, "bad_concepts": []}

									# increase this value to create a stronger `nfsw` filter
									# at the cost of increasing the possibility of filtering benign images
									UpperCamelCase__       =							0.0

									for concept_idx in range(len(special_cos_dist[0]  )  ):
												UpperCamelCase__       =							special_cos_dist[i][concept_idx]
												UpperCamelCase__       =							self.special_care_embeds_weights[concept_idx].item()
												UpperCamelCase__       =							round(concept_cos - concept_threshold + adjustment   ,    3  )
												if result_img["special_scores"][concept_idx] > 0:
															result_img["special_care"].append({concept_idx, result_img["special_scores"][concept_idx]}  )
															UpperCamelCase__       =							0.01

									for concept_idx in range(len(cos_dist[0]  )  ):
												UpperCamelCase__       =							cos_dist[i][concept_idx]
												UpperCamelCase__       =							self.concept_embeds_weights[concept_idx].item()
												UpperCamelCase__       =							round(concept_cos - concept_threshold + adjustment   ,    3  )
												if result_img["concept_scores"][concept_idx] > 0:
															result_img["bad_concepts"].append(snake_case  )

									result.append(snake_case  )

						UpperCamelCase__       =							[len(res["bad_concepts"]  ) > 0 for res in result]

						return images, has_nsfw_concepts







			@torch.no_grad()
			def 					snake_case__					(				self   ,    snake_case   ,    snake_case  ):

						'''simple docstring'''


						UpperCamelCase__       =							self.vision_model(snake_case  )[1]  # pooled_output
						UpperCamelCase__       =							self.visual_projection(snake_case  )

						UpperCamelCase__       =							cosine_distance(snake_case   ,    self.special_care_embeds  )
						UpperCamelCase__       =							cosine_distance(snake_case   ,    self.concept_embeds  )

						# increase this value to create a stronger `nsfw` filter
						# at the cost of increasing the possibility of filtering benign images
						UpperCamelCase__       =							0.0

						UpperCamelCase__       =							special_cos_dist - self.special_care_embeds_weights + adjustment
						# special_scores = special_scores.round(decimals=3)
						UpperCamelCase__       =							torch.any(special_scores > 0   ,    dim=1  )
						UpperCamelCase__       =							special_care * 0.01
						UpperCamelCase__       =							special_adjustment.unsqueeze(1  ).expand(-1   ,    cos_dist.shape[1]  )

						UpperCamelCase__       =							(cos_dist - self.concept_embeds_weights) + special_adjustment
						# concept_scores = concept_scores.round(decimals=3)
						UpperCamelCase__       =							torch.any(concept_scores > 0   ,    dim=1  )

						return images, has_nsfw_concepts






import requests
from bsa import BeautifulSoup
def 							UpperCamelCase_(					_A :str = "AAPL"	)->		str:
			UpperCamelCase__       =							F'''https://in.finance.yahoo.com/quote/{symbol}?s={symbol}'''
			UpperCamelCase__       =							BeautifulSoup(requests.get(_A	).text					,  "html.parser"	)
			UpperCamelCase__       =							"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}''')


