
import argparse
import re

import requests
import torch

# git clone https://github.com/salesforce/BLIP.git
from models.blip import blip_decoder
from models.blip_itm import blip_itm
from models.blip_vqa import blip_vqa
from PIL import Image
from torchvision import transforms
from torchvision.transforms.functional import InterpolationMode

from transformers import (
    BertTokenizer,
    BlipConfig,
    BlipForConditionalGeneration,
    BlipForImageTextRetrieval,
    BlipForQuestionAnswering,
)







def        A__					(						lowerCamelCase     ,    lowerCamelCase						)  ->	Tuple:
				UpperCamelCase_:							Optional[Any]	=						"""https://storage.googleapis.com/sfr-vision-language-research/BLIP/demo.jpg"""
				UpperCamelCase_:							Optional[Any]	=						Image.open(requests.get(__lowerCAmelCase     ,    stream=__lowerCAmelCase						).raw						).convert("""RGB"""						)

				UpperCamelCase_:							Optional[int]	=						transforms.Compose(
				    [
				        transforms.Resize((image_size, image_size)     ,    interpolation=InterpolationMode.BICUBIC						),
				        transforms.ToTensor(),
				        transforms.Normalize((0.48145466, 0.4578275, 0.40821073)     ,    (0.26862954, 0.26130258, 0.27577711)						),
				    ]						)
				UpperCamelCase_:							Optional[Any]	=						transform(__lowerCAmelCase						).unsqueeze(0						).to(__lowerCAmelCase						)
				return image







def        A__					(						lowerCamelCase						)  ->	Any:
				if "visual_encoder" in key:
								UpperCamelCase_:							Tuple	=						re.sub("""visual_encoder*"""     ,    """vision_model.encoder"""     ,    __lowerCAmelCase						)
				if "blocks" in key:
								UpperCamelCase_:							Dict	=						re.sub(r"""blocks"""     ,    """layers"""     ,    __lowerCAmelCase						)
				if "attn" in key:
								UpperCamelCase_:							List[Any]	=						re.sub(r"""attn"""     ,    """self_attn"""     ,    __lowerCAmelCase						)
				if "norm1" in key:
								UpperCamelCase_:							List[Any]	=						re.sub(r"""norm1"""     ,    """layer_norm1"""     ,    __lowerCAmelCase						)
				if "norm2" in key:
								UpperCamelCase_:							Dict	=						re.sub(r"""norm2"""     ,    """layer_norm2"""     ,    __lowerCAmelCase						)
				if "encoder.norm" in key:
								UpperCamelCase_:							int	=						re.sub(r"""encoder.norm"""     ,    """post_layernorm"""     ,    __lowerCAmelCase						)
				if "encoder.patch_embed.proj" in key:
								UpperCamelCase_:							List[str]	=						re.sub(r"""encoder.patch_embed.proj"""     ,    """embeddings.patch_embedding"""     ,    __lowerCAmelCase						)

				if "encoder.pos_embed" in key:
								UpperCamelCase_:							List[str]	=						re.sub(r"""encoder.pos_embed"""     ,    """embeddings.position_embedding"""     ,    __lowerCAmelCase						)
				if "encoder.cls_token" in key:
								UpperCamelCase_:							str	=						re.sub(r"""encoder.cls_token"""     ,    """embeddings.class_embedding"""     ,    __lowerCAmelCase						)

				if "self_attn" in key:
								UpperCamelCase_:							Union[str, Any]	=						re.sub(r"""self_attn.proj"""     ,    """self_attn.projection"""     ,    __lowerCAmelCase						)

				return key







@torch.no_grad()
def        A__					(						lowerCamelCase     ,    lowerCamelCase=None						)  ->	Tuple:
				if config_path is not None:
								UpperCamelCase_:							Optional[Any]	=						BlipConfig.from_pretrained(__lowerCAmelCase						)
				else:
								UpperCamelCase_:							Any	=						BlipConfig(projection_dim=5_12     ,    text_config={}     ,    vision_config={}						)

				UpperCamelCase_:							int	=						BlipForConditionalGeneration(__lowerCAmelCase						).eval()

				UpperCamelCase_:							str	=						"""https://storage.googleapis.com/sfr-vision-language-research/BLIP/models/model_base_capfilt_large.pth"""

				UpperCamelCase_:							str	=						blip_decoder(pretrained=__lowerCAmelCase     ,    image_size=3_84     ,    vit="""base"""						)
				UpperCamelCase_:							Dict	=						pt_model.eval()

				UpperCamelCase_:							Union[str, Any]	=						pt_model.state_dict()
				for key in modified_state_dict.copy():
								UpperCamelCase_:							Union[str, Any]	=						modified_state_dict.pop(__lowerCAmelCase						)
								UpperCamelCase_:							Tuple	=						rename_key(__lowerCAmelCase						)
								UpperCamelCase_:							Optional[Any]	=						value

				hf_model.load_state_dict(__lowerCAmelCase						)

				UpperCamelCase_:							Dict	=						3_84
				UpperCamelCase_:							Tuple	=						load_demo_image(image_size=__lowerCAmelCase     ,    device="""cpu"""						)
				UpperCamelCase_:							Tuple	=						BertTokenizer.from_pretrained("""bert-base-uncased"""						)
				UpperCamelCase_:							Dict	=						tokenizer(["""a picture of"""]						).input_ids

				UpperCamelCase_:							List[Any]	=						hf_model.generate(__lowerCAmelCase     ,    __lowerCAmelCase						)

				assert out[0].tolist() == [3_05_22, 10_37, 38_61, 19_97, 10_37, 24_50, 35_64, 20_06, 19_96, 35_09, 20_07, 20_14, 38_99, 1_02]

				UpperCamelCase_:							Any	=						hf_model.generate(__lowerCAmelCase						)

				assert out[0].tolist() == [3_05_22, 10_37, 24_50, 35_64, 20_06, 19_96, 35_09, 20_07, 20_14, 38_99, 1_02]

				if pytorch_dump_folder_path is not None:
								hf_model.save_pretrained(__lowerCAmelCase						)

				# model_url = 'https://storage.googleapis.com/sfr-vision-language-research/BLIP/models/model_vqa.pth'
				UpperCamelCase_:							str	=						(
				    """https://storage.googleapis.com/sfr-vision-language-research/BLIP/models/model_base_vqa_capfilt_large.pth"""
				)

				UpperCamelCase_:							List[Any]	=						blip_vqa(pretrained=__lowerCAmelCase     ,    image_size=__lowerCAmelCase     ,    vit="""base"""						)
				vqa_model.eval()

				UpperCamelCase_:							Union[str, Any]	=						vqa_model.state_dict()
				for key in modified_state_dict.copy():
								UpperCamelCase_:							List[Any]	=						modified_state_dict.pop(__lowerCAmelCase						)
								UpperCamelCase_:							int	=						rename_key(__lowerCAmelCase						)
								UpperCamelCase_:							int	=						value

				UpperCamelCase_:							Optional[Any]	=						BlipForQuestionAnswering(__lowerCAmelCase						)

				hf_vqa_model.load_state_dict(__lowerCAmelCase						)

				UpperCamelCase_:							str	=						["""How many dogs are in this image?"""]
				UpperCamelCase_:							Union[str, Any]	=						tokenizer(__lowerCAmelCase     ,    return_tensors="""pt"""						).input_ids

				UpperCamelCase_:							Dict	=						hf_vqa_model.generate(__lowerCAmelCase     ,    __lowerCAmelCase						)
				print(tokenizer.decode(answer[0]						)						)

				assert tokenizer.decode(answer[0]						) == "[UNK] 1 [SEP]"
				if pytorch_dump_folder_path is not None:
								hf_vqa_model.save_pretrained(pytorch_dump_folder_path + """_vqa"""						)

				UpperCamelCase_:							Any	=						"""https://storage.googleapis.com/sfr-vision-language-research/BLIP/models/model_base_retrieval_coco.pth"""

				UpperCamelCase_:							List[str]	=						blip_itm(pretrained=__lowerCAmelCase     ,    image_size=__lowerCAmelCase     ,    vit="""base"""						)
				itm_model.eval()

				UpperCamelCase_:							Optional[int]	=						itm_model.state_dict()
				for key in modified_state_dict.copy():
								UpperCamelCase_:							int	=						modified_state_dict.pop(__lowerCAmelCase						)
								UpperCamelCase_:							str	=						rename_key(__lowerCAmelCase						)
								UpperCamelCase_:							Dict	=						value

				UpperCamelCase_:							Union[str, Any]	=						BlipForImageTextRetrieval(__lowerCAmelCase						)

				UpperCamelCase_:							List[str]	=						["""A picture of a woman with a dog sitting in a beach"""]
				UpperCamelCase_:							List[Any]	=						tokenizer(
				    __lowerCAmelCase     ,    return_tensors="""pt"""     ,    padding="""max_length"""     ,    truncation=__lowerCAmelCase     ,    max_length=35     ,    ).input_ids

				hf_itm_model.load_state_dict(__lowerCAmelCase						)
				hf_itm_model.eval()

				UpperCamelCase_:							Union[str, Any]	=						hf_itm_model(__lowerCAmelCase     ,    __lowerCAmelCase     ,    use_itm_head=__lowerCAmelCase						)
				UpperCamelCase_:							Union[str, Any]	=						hf_itm_model(__lowerCAmelCase     ,    __lowerCAmelCase     ,    use_itm_head=__lowerCAmelCase						)

				assert out[0].item() == 0.2110687494277954
				assert torch.nn.functional.softmax(out_itm[0]     ,    dim=1						)[:, 1].item() == 0.45698845386505127

				if pytorch_dump_folder_path is not None:
								hf_itm_model.save_pretrained(pytorch_dump_folder_path + """_itm"""						)


if __name__ == "__main__":
							lowerCamelCase_						: List[Any]         =						argparse.ArgumentParser()
							parser.add_argument("""--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model.""")
							parser.add_argument("""--config_path""", default=None, type=str, help="""Path to hf config.json of model to convert""")
							lowerCamelCase_						: int         =						parser.parse_args()

							convert_blip_checkpoint(args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path)








def        A__					(						lowerCamelCase     ,    lowerCamelCase						)  ->	int:
				while second != 0:
								UpperCamelCase_:							Optional[Any]	=						first & second
								first ^= second
								UpperCamelCase_:							Any	=						c << 1
				return first


if __name__ == "__main__":
							import doctest

							doctest.testmod()

							lowerCamelCase_						: List[Any]         =  int(input("""Enter the first number: """).strip())
							lowerCamelCase_						: Tuple         =  int(input("""Enter the second number: """).strip())
							print(F"""{add(first, second) = }""")






