'''simple docstring'''

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       __UpperCamelCase	(       UpperCAmelCase		,					UpperCAmelCase					):
 lowercase__    :   List[str]						= '''https://storage.googleapis.com/sfr-vision-language-research/BLIP/demo.jpg'''
 lowercase__    :   Tuple						= Image.open(requests.get(UpperCAmelCase		,					stream=UpperCAmelCase					).raw					).convert('''RGB'''					)

 lowercase__    :   int						= transforms.Compose(
     [
         transforms.Resize((image_size, image_size)		,					interpolation=InterpolationMode.BICUBIC					),
         transforms.ToTensor(),
         transforms.Normalize((0.4_8_1_4_5_4_6_6, 0.4_5_7_8_2_7_5, 0.4_0_8_2_1_0_7_3)		,					(0.2_6_8_6_2_9_5_4, 0.2_6_1_3_0_2_5_8, 0.2_7_5_7_7_7_1_1)					),
     ]					)
 lowercase__    :   Optional[int]						= transform(UpperCAmelCase					).unsqueeze(0					).to(UpperCAmelCase					)
 return image
def       __UpperCamelCase	(       UpperCAmelCase					):
 if "visual_encoder" in key:
  lowercase__    :   Optional[int]						= re.sub('''visual_encoder*'''		,					'''vision_model.encoder'''		,					UpperCAmelCase					)
 if "blocks" in key:
  lowercase__    :   List[str]						= re.sub(r'''blocks'''		,					'''layers'''		,					UpperCAmelCase					)
 if "attn" in key:
  lowercase__    :   Optional[Any]						= re.sub(r'''attn'''		,					'''self_attn'''		,					UpperCAmelCase					)
 if "norm1" in key:
  lowercase__    :   List[Any]						= re.sub(r'''norm1'''		,					'''layer_norm1'''		,					UpperCAmelCase					)
 if "norm2" in key:
  lowercase__    :   Optional[int]						= re.sub(r'''norm2'''		,					'''layer_norm2'''		,					UpperCAmelCase					)
 if "encoder.norm" in key:
  lowercase__    :   Dict						= re.sub(r'''encoder.norm'''		,					'''post_layernorm'''		,					UpperCAmelCase					)
 if "encoder.patch_embed.proj" in key:
  lowercase__    :   str						= re.sub(r'''encoder.patch_embed.proj'''		,					'''embeddings.patch_embedding'''		,					UpperCAmelCase					)

 if "encoder.pos_embed" in key:
  lowercase__    :   str						= re.sub(r'''encoder.pos_embed'''		,					'''embeddings.position_embedding'''		,					UpperCAmelCase					)
 if "encoder.cls_token" in key:
  lowercase__    :   Union[str, Any]						= re.sub(r'''encoder.cls_token'''		,					'''embeddings.class_embedding'''		,					UpperCAmelCase					)

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

 return key
@torch.no_grad()
def       __UpperCamelCase	(       UpperCAmelCase		,					UpperCAmelCase=None					):
 if config_path is not None:
  lowercase__    :   int						= BlipConfig.from_pretrained(UpperCAmelCase					)
 else:
  lowercase__    :   str						= BlipConfig(projection_dim=512		,					text_config={}		,					vision_config={}					)

 lowercase__    :   Tuple						= BlipForConditionalGeneration(UpperCAmelCase					).eval()

 lowercase__    :   Tuple						= '''https://storage.googleapis.com/sfr-vision-language-research/BLIP/models/model_base_capfilt_large.pth'''

 lowercase__    :   Dict						= blip_decoder(pretrained=UpperCAmelCase		,					image_size=384		,					vit='''base'''					)
 lowercase__    :   Union[str, Any]						= pt_model.eval()

 lowercase__    :   List[str]						= pt_model.state_dict()
 for key in modified_state_dict.copy():
  lowercase__    :   int						= modified_state_dict.pop(UpperCAmelCase					)
  lowercase__    :   List[str]						= rename_key(UpperCAmelCase					)
  lowercase__    :   str						= value

 hf_model.load_state_dict(UpperCAmelCase					)

 lowercase__    :   int						= 384
 lowercase__    :   Tuple						= load_demo_image(image_size=UpperCAmelCase		,					device='''cpu'''					)
 lowercase__    :   str						= BertTokenizer.from_pretrained('''bert-base-uncased'''					)
 lowercase__    :   str						= tokenizer(['''a picture of''']					).input_ids

 lowercase__    :   Optional[int]						= hf_model.generate(UpperCAmelCase		,					UpperCAmelCase					)

 assert out[0].tolist() == [3_0522, 1037, 3861, 1997, 1037, 2450, 3564, 2006, 1996, 3509, 2007, 2014, 3899, 102]

 lowercase__    :   List[Any]						= hf_model.generate(UpperCAmelCase					)

 assert out[0].tolist() == [3_0522, 1037, 2450, 3564, 2006, 1996, 3509, 2007, 2014, 3899, 102]

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

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

 lowercase__    :   Dict						= blip_vqa(pretrained=UpperCAmelCase		,					image_size=UpperCAmelCase		,					vit='''base'''					)
 vqa_model.eval()

 lowercase__    :   str						= vqa_model.state_dict()
 for key in modified_state_dict.copy():
  lowercase__    :   List[str]						= modified_state_dict.pop(UpperCAmelCase					)
  lowercase__    :   Optional[int]						= rename_key(UpperCAmelCase					)
  lowercase__    :   List[Any]						= value

 lowercase__    :   Optional[Any]						= BlipForQuestionAnswering(UpperCAmelCase					)

 hf_vqa_model.load_state_dict(UpperCAmelCase					)

 lowercase__    :   List[Any]						= ['''How many dogs are in this image?''']
 lowercase__    :   Any						= tokenizer(UpperCAmelCase		,					return_tensors='''pt'''					).input_ids

 lowercase__    :   Any						= hf_vqa_model.generate(UpperCAmelCase		,					UpperCAmelCase					)
 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'''					)

 lowercase__    :   List[str]						= '''https://storage.googleapis.com/sfr-vision-language-research/BLIP/models/model_base_retrieval_coco.pth'''

 lowercase__    :   int						= blip_itm(pretrained=UpperCAmelCase		,					image_size=UpperCAmelCase		,					vit='''base'''					)
 itm_model.eval()

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

 lowercase__    :   Optional[int]						= BlipForImageTextRetrieval(UpperCAmelCase					)

 lowercase__    :   List[str]						= ['''A picture of a woman with a dog sitting in a beach''']
 lowercase__    :   Tuple						= tokenizer(
     UpperCAmelCase		,					return_tensors='''pt'''		,					padding='''max_length'''		,					truncation=UpperCAmelCase		,					max_length=35		,					).input_ids

 hf_itm_model.load_state_dict(UpperCAmelCase					)
 hf_itm_model.eval()

 lowercase__    :   List[Any]						= hf_itm_model(UpperCAmelCase		,					UpperCAmelCase		,					use_itm_head=UpperCAmelCase					)
 lowercase__    :   Any						= hf_itm_model(UpperCAmelCase		,					UpperCAmelCase		,					use_itm_head=UpperCAmelCase					)

 assert out[0].item() == 0.2_1_1_0_6_8_7_4_9_4_2_7_7_9_5_4
 assert torch.nn.functional.softmax(out_itm[0]		,					dim=1					)[:, 1].item() == 0.4_5_6_9_8_8_4_5_3_8_6_5_0_5_1_2_7

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


if __name__ == "__main__":
       __a:			Optional[int]	       =       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""")
       __a:			Optional[Any]	       =       parser.parse_args()

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


'''simple docstring'''

def       __UpperCamelCase	(       ):
 lowercase__    :   Any						= [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

 lowercase__    :   Any						= 6
 lowercase__    :   Optional[Any]						= 1
 lowercase__    :   int						= 1901

 lowercase__    :   List[str]						= 0

 while year < 2001:
  day += 7

  if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
   if day > days_per_month[month - 1] and month != 2:
    month += 1
    lowercase__    :   List[Any]						= day - days_per_month[month - 2]
   elif day > 29 and month == 2:
    month += 1
    lowercase__    :   Any						= day - 29
  else:
   if day > days_per_month[month - 1]:
    month += 1
    lowercase__    :   List[Any]						= day - days_per_month[month - 2]

  if month > 12:
   year += 1
   lowercase__    :   Dict						= 1

  if year < 2001 and day == 1:
   sundays += 1
 return sundays


if __name__ == "__main__":
  print(solution())

