






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      _snake_case     (__lowercase     ,   __lowercase):
   UpperCamelCase_						=  'https://storage.googleapis.com/sfr-vision-language-research/BLIP/demo.jpg'
   UpperCamelCase_						=  Image.open(requests.get(__lowercase     ,   stream=__lowercase).raw).convert('RGB')

   UpperCamelCase_						=  transforms.Compose(
       [
           transforms.Resize((image_size, image_size)     ,   interpolation=InterpolationMode.BICUBIC),
           transforms.ToTensor(),
           transforms.Normalize((0.48_145_466, 0.4_578_275, 0.40_821_073)     ,   (0.26_862_954, 0.26_130_258, 0.27_577_711)),
       ])
   UpperCamelCase_						=  transform(__lowercase).unsqueeze(0).to(__lowercase)
   return image






def      _snake_case     (__lowercase):
   if "visual_encoder" in key:
      UpperCamelCase_						=  re.sub('visual_encoder*'     ,   'vision_model.encoder'     ,   __lowercase)
   if "blocks" in key:
      UpperCamelCase_						=  re.sub(r'blocks'     ,   'layers'     ,   __lowercase)
   if "attn" in key:
      UpperCamelCase_						=  re.sub(r'attn'     ,   'self_attn'     ,   __lowercase)
   if "norm1" in key:
      UpperCamelCase_						=  re.sub(r'norm1'     ,   'layer_norm1'     ,   __lowercase)
   if "norm2" in key:
      UpperCamelCase_						=  re.sub(r'norm2'     ,   'layer_norm2'     ,   __lowercase)
   if "encoder.norm" in key:
      UpperCamelCase_						=  re.sub(r'encoder.norm'     ,   'post_layernorm'     ,   __lowercase)
   if "encoder.patch_embed.proj" in key:
      UpperCamelCase_						=  re.sub(r'encoder.patch_embed.proj'     ,   'embeddings.patch_embedding'     ,   __lowercase)

   if "encoder.pos_embed" in key:
      UpperCamelCase_						=  re.sub(r'encoder.pos_embed'     ,   'embeddings.position_embedding'     ,   __lowercase)
   if "encoder.cls_token" in key:
      UpperCamelCase_						=  re.sub(r'encoder.cls_token'     ,   'embeddings.class_embedding'     ,   __lowercase)

   if "self_attn" in key:
      UpperCamelCase_						=  re.sub(r'self_attn.proj'     ,   'self_attn.projection'     ,   __lowercase)

   return key






@torch.no_grad()
def      _snake_case     (__lowercase     ,   __lowercase=None):
   if config_path is not None:
      UpperCamelCase_						=  BlipConfig.from_pretrained(__lowercase)
   else:
      UpperCamelCase_						=  BlipConfig(projection_dim=512     ,   text_config={}     ,   vision_config={})

   UpperCamelCase_						=  BlipForConditionalGeneration(__lowercase).eval()

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

   UpperCamelCase_						=  blip_decoder(pretrained=__lowercase     ,   image_size=384     ,   vit='base')
   UpperCamelCase_						=  pt_model.eval()

   UpperCamelCase_						=  pt_model.state_dict()
   for key in modified_state_dict.copy():
      UpperCamelCase_						=  modified_state_dict.pop(__lowercase)
      UpperCamelCase_						=  rename_key(__lowercase)
      UpperCamelCase_						=  value

   hf_model.load_state_dict(__lowercase)

   UpperCamelCase_						=  384
   UpperCamelCase_						=  load_demo_image(image_size=__lowercase     ,   device='cpu')
   UpperCamelCase_						=  BertTokenizer.from_pretrained('bert-base-uncased')
   UpperCamelCase_						=  tokenizer(['a picture of']).input_ids

   UpperCamelCase_						=  hf_model.generate(__lowercase     ,   __lowercase)

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

   UpperCamelCase_						=  hf_model.generate(__lowercase)

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

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

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

   UpperCamelCase_						=  blip_vqa(pretrained=__lowercase     ,   image_size=__lowercase     ,   vit='base')
   vqa_model.eval()

   UpperCamelCase_						=  vqa_model.state_dict()
   for key in modified_state_dict.copy():
      UpperCamelCase_						=  modified_state_dict.pop(__lowercase)
      UpperCamelCase_						=  rename_key(__lowercase)
      UpperCamelCase_						=  value

   UpperCamelCase_						=  BlipForQuestionAnswering(__lowercase)

   hf_vqa_model.load_state_dict(__lowercase)

   UpperCamelCase_						=  ['How many dogs are in this image?']
   UpperCamelCase_						=  tokenizer(__lowercase     ,   return_tensors='pt').input_ids

   UpperCamelCase_						=  hf_vqa_model.generate(__lowercase     ,   __lowercase)
   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_						=  'https://storage.googleapis.com/sfr-vision-language-research/BLIP/models/model_base_retrieval_coco.pth'

   UpperCamelCase_						=  blip_itm(pretrained=__lowercase     ,   image_size=__lowercase     ,   vit='base')
   itm_model.eval()

   UpperCamelCase_						=  itm_model.state_dict()
   for key in modified_state_dict.copy():
      UpperCamelCase_						=  modified_state_dict.pop(__lowercase)
      UpperCamelCase_						=  rename_key(__lowercase)
      UpperCamelCase_						=  value

   UpperCamelCase_						=  BlipForImageTextRetrieval(__lowercase)

   UpperCamelCase_						=  ['A picture of a woman with a dog sitting in a beach']
   UpperCamelCase_						=  tokenizer(
       __lowercase     ,   return_tensors='pt'     ,   padding='max_length'     ,   truncation=__lowercase     ,   max_length=35     ,   ).input_ids

   hf_itm_model.load_state_dict(__lowercase)
   hf_itm_model.eval()

   UpperCamelCase_						=  hf_itm_model(__lowercase     ,   __lowercase     ,   use_itm_head=__lowercase)
   UpperCamelCase_						=  hf_itm_model(__lowercase     ,   __lowercase     ,   use_itm_head=__lowercase)

   assert out[0].item() == 0.2_110_687_494_277_954
   assert torch.nn.functional.softmax(out_itm[0]     ,   dim=1)[:, 1].item() == 0.45_698_845_386_505_127

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


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

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













import os
import tempfile
from functools import partial
from unittest import TestCase
from unittest.mock import patch

import datasets
import datasets.config

from .utils import require_beam


class     _a							(	datasets.BeamBasedBuilder     ):






  """simple docstring"""





  def  _UpperCAmelCase  (  self			)      ->			List[str]:
     return datasets.DatasetInfo(
         features=datasets.Features({'content': datasets.Value('string'			)}			)		,		supervised_keys=_UpperCAmelCase		,		)




  def  _UpperCAmelCase  (  self		,		_UpperCAmelCase		,		_UpperCAmelCase			)      ->			Optional[Any]:
     return [datasets.SplitGenerator(name=datasets.Split.TRAIN		,		gen_kwargs={'examples': get_test_dummy_examples()}			)]




  def  _UpperCAmelCase  (  self		,		_UpperCAmelCase		,		_UpperCAmelCase			)      ->			Tuple:
     import apache_beam as beam

     return pipeline | "Load Examples" >> beam.Create(_UpperCAmelCase			)




class     _a							(	datasets.BeamBasedBuilder     ):






  """simple docstring"""





  def  _UpperCAmelCase  (  self			)      ->			Any:
     return datasets.DatasetInfo(
         features=datasets.Features({'a': datasets.Sequence({'b': datasets.Value('string'			)}			)}			)		,		supervised_keys=_UpperCAmelCase		,		)




  def  _UpperCAmelCase  (  self		,		_UpperCAmelCase		,		_UpperCAmelCase			)      ->			Tuple:
     return [
         datasets.SplitGenerator(name=datasets.Split.TRAIN		,		gen_kwargs={'examples': get_test_nested_examples()}			)
     ]




  def  _UpperCAmelCase  (  self		,		_UpperCAmelCase		,		_UpperCAmelCase			)      ->			Union[str, Any]:
     import apache_beam as beam

     return pipeline | "Load Examples" >> beam.Create(_UpperCAmelCase			)






def      _snake_case     ():
   return [(i, {"content": content}) for i, content in enumerate(['foo', 'bar', 'foobar'])]






def      _snake_case     ():
   return [(i, {"a": {"b": [content]}}) for i, content in enumerate(['foo', 'bar', 'foobar'])]


class     _a							(	UpperCAmelCase__     ):






  """simple docstring"""





  @require_beam
  def  _UpperCAmelCase  (  self			)      ->			Dict:
     UpperCamelCase_						=  len(get_test_dummy_examples()			)
     with tempfile.TemporaryDirectory() as tmp_cache_dir:
        UpperCamelCase_						=  DummyBeamDataset(cache_dir=_UpperCAmelCase		,		beam_runner='DirectRunner'			)
        builder.download_and_prepare()
        self.assertTrue(
            os.path.exists(
                os.path.join(_UpperCAmelCase		,		builder.name		,		'default'		,		'0.0.0'		,		f"""{builder.name}-train.arrow"""			)			)			)
        self.assertDictEqual(builder.info.features		,		datasets.Features({'content': datasets.Value('string'			)}			)			)
        UpperCamelCase_						=  builder.as_dataset()
        self.assertEqual(dset['train'].num_rows		,		_UpperCAmelCase			)
        self.assertEqual(dset['train'].info.splits['train'].num_examples		,		_UpperCAmelCase			)
        self.assertDictEqual(dset['train'][0]		,		get_test_dummy_examples()[0][1]			)
        self.assertDictEqual(
            dset['train'][expected_num_examples - 1]		,		get_test_dummy_examples()[expected_num_examples - 1][1]			)
        self.assertTrue(
            os.path.exists(os.path.join(_UpperCAmelCase		,		builder.name		,		'default'		,		'0.0.0'		,		'dataset_info.json'			)			)			)
        del dset




  @require_beam
  def  _UpperCAmelCase  (  self			)      ->			List[str]:
     import apache_beam as beam

     UpperCamelCase_						=  beam.io.parquetio.WriteToParquet

     UpperCamelCase_						=  len(get_test_dummy_examples()			)
     with tempfile.TemporaryDirectory() as tmp_cache_dir:
        UpperCamelCase_						=  DummyBeamDataset(cache_dir=_UpperCAmelCase		,		beam_runner='DirectRunner'			)
        with patch('apache_beam.io.parquetio.WriteToParquet'			) as write_parquet_mock:
           UpperCamelCase_						=  partial(_UpperCAmelCase		,		num_shards=2			)
           builder.download_and_prepare()
        self.assertTrue(
            os.path.exists(
                os.path.join(
                    _UpperCAmelCase		,		builder.name		,		'default'		,		'0.0.0'		,		f"""{builder.name}-train-00000-of-00002.arrow"""			)			)			)
        self.assertTrue(
            os.path.exists(
                os.path.join(
                    _UpperCAmelCase		,		builder.name		,		'default'		,		'0.0.0'		,		f"""{builder.name}-train-00000-of-00002.arrow"""			)			)			)
        self.assertDictEqual(builder.info.features		,		datasets.Features({'content': datasets.Value('string'			)}			)			)
        UpperCamelCase_						=  builder.as_dataset()
        self.assertEqual(dset['train'].num_rows		,		_UpperCAmelCase			)
        self.assertEqual(dset['train'].info.splits['train'].num_examples		,		_UpperCAmelCase			)
        # Order is not preserved when sharding, so we just check that all the elements are there
        self.assertListEqual(sorted(dset['train']['content']			)		,		sorted(['foo', 'bar', 'foobar']			)			)
        self.assertTrue(
            os.path.exists(os.path.join(_UpperCAmelCase		,		builder.name		,		'default'		,		'0.0.0'		,		'dataset_info.json'			)			)			)
        del dset




  @require_beam
  def  _UpperCAmelCase  (  self			)      ->			Any:
     with tempfile.TemporaryDirectory() as tmp_cache_dir:
        UpperCamelCase_						=  DummyBeamDataset(cache_dir=_UpperCAmelCase			)
        self.assertRaises(datasets.builder.MissingBeamOptions		,		builder.download_and_prepare			)




  @require_beam
  def  _UpperCAmelCase  (  self			)      ->			Union[str, Any]:
     UpperCamelCase_						=  len(get_test_nested_examples()			)
     with tempfile.TemporaryDirectory() as tmp_cache_dir:
        UpperCamelCase_						=  NestedBeamDataset(cache_dir=_UpperCAmelCase		,		beam_runner='DirectRunner'			)
        builder.download_and_prepare()
        self.assertTrue(
            os.path.exists(
                os.path.join(_UpperCAmelCase		,		builder.name		,		'default'		,		'0.0.0'		,		f"""{builder.name}-train.arrow"""			)			)			)
        self.assertDictEqual(
            builder.info.features		,		datasets.Features({'a': datasets.Sequence({'b': datasets.Value('string'			)}			)}			)			)
        UpperCamelCase_						=  builder.as_dataset()
        self.assertEqual(dset['train'].num_rows		,		_UpperCAmelCase			)
        self.assertEqual(dset['train'].info.splits['train'].num_examples		,		_UpperCAmelCase			)
        self.assertDictEqual(dset['train'][0]		,		get_test_nested_examples()[0][1]			)
        self.assertDictEqual(
            dset['train'][expected_num_examples - 1]		,		get_test_nested_examples()[expected_num_examples - 1][1]			)
        self.assertTrue(
            os.path.exists(os.path.join(_UpperCAmelCase		,		builder.name		,		'default'		,		'0.0.0'		,		'dataset_info.json'			)			)			)
        del dset





