



'''simple docstring'''






import argparse
import os

import torch

from transformers import FlavaConfig, FlavaForPreTraining
from transformers.models.flava.convert_dalle_to_flava_codebook import convert_dalle_checkpoint


def 				snake_case_  (      _lowerCAmelCase						:		List[str]						)   ->     Optional[int]:
							# encoder.embeddings are double copied in original FLAVA
							return sum(param.float().sum() if '''encoder.embeddings''' not in key else 0 for key, param in state_dict.items()						)


def 				snake_case_  (      _lowerCAmelCase						:		List[Any]     ,   _lowerCAmelCase						:		Any						)   ->     List[str]:
							UpperCAmelCase				: Optional[Any]											=	{}

							for key, value in state_dict.items():
														if "text_encoder.embeddings" in key or "image_encoder.embeddings" in key:
																					continue

														UpperCAmelCase				: List[Any]											=	key.replace('''heads.cmd.mim_head.cls.predictions'''     ,   '''mmm_image_head'''						)
														UpperCAmelCase				: int											=	key.replace('''heads.cmd.mlm_head.cls.predictions'''     ,   '''mmm_text_head'''						)
														UpperCAmelCase				: str											=	key.replace('''heads.cmd.itm_head.cls'''     ,   '''itm_head'''						)
														UpperCAmelCase				: List[str]											=	key.replace('''heads.cmd.itm_head.pooler'''     ,   '''itm_head.pooler'''						)
														UpperCAmelCase				: Optional[Any]											=	key.replace('''heads.cmd.clip_head.logit_scale'''     ,   '''flava.logit_scale'''						)
														UpperCAmelCase				: List[Any]											=	key.replace('''heads.fairseq_mlm.cls.predictions'''     ,   '''mlm_head'''						)
														UpperCAmelCase				: Dict											=	key.replace('''heads.imagenet.mim_head.cls.predictions'''     ,   '''mim_head'''						)
														UpperCAmelCase				: Optional[Any]											=	key.replace('''mm_text_projection'''     ,   '''flava.text_to_mm_projection'''						)
														UpperCAmelCase				: str											=	key.replace('''mm_image_projection'''     ,   '''flava.image_to_mm_projection'''						)
														UpperCAmelCase				: Optional[int]											=	key.replace('''image_encoder.module'''     ,   '''flava.image_model'''						)
														UpperCAmelCase				: List[Any]											=	key.replace('''text_encoder.module'''     ,   '''flava.text_model'''						)
														UpperCAmelCase				: Optional[int]											=	key.replace('''mm_encoder.module.encoder.cls_token'''     ,   '''flava.multimodal_model.cls_token'''						)
														UpperCAmelCase				: Tuple											=	key.replace('''mm_encoder.module'''     ,   '''flava.multimodal_model'''						)
														UpperCAmelCase				: List[str]											=	key.replace('''text_projection'''     ,   '''flava.text_projection'''						)
														UpperCAmelCase				: Optional[int]											=	key.replace('''image_projection'''     ,   '''flava.image_projection'''						)

														UpperCAmelCase				: Dict											=	value.float()

							for key, value in codebook_state_dict.items():
														UpperCAmelCase				: str											=	value

							return upgrade


@torch.no_grad()
def 				snake_case_  (      _lowerCAmelCase						:		Union[str, Any]     ,   _lowerCAmelCase						:		List[Any]     ,   _lowerCAmelCase						:		Tuple     ,   _lowerCAmelCase						:		Dict=None						)   ->     Optional[int]:
							if config_path is not None:
														UpperCAmelCase				: Any											=	FlavaConfig.from_pretrained(_lowerCAmelCase						)
							else:
														UpperCAmelCase				: Tuple											=	FlavaConfig()

							UpperCAmelCase				: List[Any]											=	FlavaForPreTraining(_lowerCAmelCase						).eval()

							UpperCAmelCase				: Dict											=	convert_dalle_checkpoint(_lowerCAmelCase     ,   _lowerCAmelCase     ,   save_checkpoint=_lowerCAmelCase						)

							if os.path.exists(_lowerCAmelCase						):
														UpperCAmelCase				: List[Any]											=	torch.load(_lowerCAmelCase     ,   map_location='''cpu'''						)
							else:
														UpperCAmelCase				: List[Any]											=	torch.hub.load_state_dict_from_url(_lowerCAmelCase     ,   map_location='''cpu'''						)

							UpperCAmelCase				: Dict											=	upgrade_state_dict(_lowerCAmelCase     ,   _lowerCAmelCase						)
							hf_model.load_state_dict(_lowerCAmelCase						)
							UpperCAmelCase				: Dict											=	hf_model.state_dict()
							UpperCAmelCase				: Dict											=	count_parameters(_lowerCAmelCase						)
							UpperCAmelCase				: Any											=	count_parameters(_lowerCAmelCase						) + count_parameters(_lowerCAmelCase						)

							assert torch.allclose(_lowerCAmelCase     ,   _lowerCAmelCase     ,   atol=1e-3						)

							hf_model.save_pretrained(_lowerCAmelCase						)


if __name__ == "__main__":
						UpperCamelCase__:     str										=    argparse.ArgumentParser()
						parser.add_argument("--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model.")
						parser.add_argument("--checkpoint_path", default=None, type=str, help="Path to flava checkpoint")
						parser.add_argument("--codebook_path", default=None, type=str, help="Path to flava codebook checkpoint")
						parser.add_argument("--config_path", default=None, type=str, help="Path to hf config.json of model to convert")
						UpperCamelCase__:     Optional[int]										=    parser.parse_args()

						convert_flava_checkpoint(args.checkpoint_path, args.codebook_path, args.pytorch_dump_folder_path, args.config_path)










'''simple docstring'''






from __future__ import annotations

from collections.abc import Generator

import requests
from bsa import BeautifulSoup

UpperCamelCase__:     Union[str, Any]										=    "https://www.indeed.co.in/jobs?q=mobile+app+development&l="


def 				snake_case_  (      _lowerCAmelCase						:		str = "mumbai"						)   ->     Generator[tuple[str, str], None, None]:
							UpperCAmelCase				: Optional[int]											=	BeautifulSoup(requests.get(url + location						).content     ,   '''html.parser'''						)
							# This attribute finds out all the specifics listed in a job
							for job in soup.find_all('''div'''     ,   attrs={'''data-tn-component''': '''organicJob'''}						):
														UpperCAmelCase				: Optional[Any]											=	job.find('''a'''     ,   attrs={'''data-tn-element''': '''jobTitle'''}						).text.strip()
														UpperCAmelCase				: int											=	job.find('''span'''     ,   {'''class''': '''company'''}						).text.strip()
														yield job_title, company_name


if __name__ == "__main__":
						for i, job in enumerate(fetch_jobs("Bangalore"), 1):
												print(F"Job {i:>2} is {job[0]} at {job[1]}")





