




import argparse

import torch
from clip import load

from transformers import CLIPConfig, CLIPModel

def 			lowerCAmelCase_			(  lowerCamelCase:       Optional[int]     ,				lowerCamelCase:       Union[str, Any]						)					->			Union[str, Any]:


      '''simple docstring'''




      lowerCamelCase__  ,		lowerCamelCase__  ,		lowerCamelCase__        =				pt_attn_layer.in_proj_weight.chunk(3     ,				dim=0						)
      lowerCamelCase__  ,		lowerCamelCase__  ,		lowerCamelCase__        =				pt_attn_layer.in_proj_bias.chunk(3     ,				dim=0						)

      lowerCamelCase__        =				pt_attn_layer.out_proj.weight
      lowerCamelCase__        =				pt_attn_layer.out_proj.bias

      lowerCamelCase__        =				q_proj
      lowerCamelCase__        =				q_proj_bias

      lowerCamelCase__        =				k_proj
      lowerCamelCase__        =				k_proj_bias

      lowerCamelCase__        =				v_proj
      lowerCamelCase__        =				v_proj_bias

      lowerCamelCase__        =				out_proj_weights
      lowerCamelCase__        =				out_proj_bias

def 			lowerCAmelCase_			(  lowerCamelCase:       Union[str, Any]     ,				lowerCamelCase:       int						)					->			List[Any]:


      '''simple docstring'''




      copy_linear(hf_mlp.fca     ,				pt_mlp.c_fc						)
      copy_linear(hf_mlp.fca     ,				pt_mlp.c_proj						)

def 			lowerCAmelCase_			(  lowerCamelCase:       List[Any]     ,				lowerCamelCase:       Union[str, Any]						)					->			Tuple:


      '''simple docstring'''




      lowerCamelCase__        =				pt_linear.weight
      lowerCamelCase__        =				pt_linear.bias

def 			lowerCAmelCase_			(  lowerCamelCase:       int     ,				lowerCamelCase:       Dict						)					->			Optional[Any]:


      '''simple docstring'''




      copy_linear(hf_layer.layer_norma     ,				pt_layer.ln_a						)
      copy_linear(hf_layer.layer_norma     ,				pt_layer.ln_a						)

      # copy MLP
      copy_mlp(hf_layer.mlp     ,				pt_layer.mlp						)

      # copy attn
      copy_attn_layer(hf_layer.self_attn     ,				pt_layer.attn						)

def 			lowerCAmelCase_			(  lowerCamelCase:       Optional[Any]     ,				lowerCamelCase:       Optional[int]						)					->			str:


      '''simple docstring'''




      for hf_layer, pt_layer in zip(lowerCamelCase     ,				lowerCamelCase						):
            copy_layer(lowerCamelCase     ,				lowerCamelCase						)

def 			lowerCAmelCase_			(  lowerCamelCase:       Union[str, Any]     ,				lowerCamelCase:       Union[str, Any]						)					->			Dict:


      '''simple docstring'''




      lowerCamelCase__        =				pt_model.token_embedding.weight
      lowerCamelCase__        =				pt_model.positional_embedding

      # copy layer norm
      copy_linear(hf_encoder.final_layer_norm     ,				pt_model.ln_final						)

      # copy hidden layers
      copy_layers(hf_encoder.encoder.layers     ,				pt_model.transformer.resblocks						)

def 			lowerCAmelCase_			(  lowerCamelCase:       int     ,				lowerCamelCase:       Tuple						)					->			List[Any]:


      '''simple docstring'''




      lowerCamelCase__        =				pt_model.text_projection.data.T

      # copy text encoder
      copy_encoder(hf_model.text_model     ,				lowerCamelCase						)

def 			lowerCAmelCase_			(  lowerCamelCase:       str     ,				lowerCamelCase:       Optional[Any]						)					->			Optional[int]:


      '''simple docstring'''




      lowerCamelCase__        =				pt_model.visual.proj.data.T

      # copy layer norms
      copy_linear(hf_model.vision_model.pre_layrnorm     ,				pt_model.visual.ln_pre						)
      copy_linear(hf_model.vision_model.post_layernorm     ,				pt_model.visual.ln_post						)

      # copy embeds
      lowerCamelCase__        =				pt_model.visual.conva.weight.data
      lowerCamelCase__        =				pt_model.visual.class_embedding
      lowerCamelCase__        =				pt_model.visual.positional_embedding.data

      # copy encoder
      copy_layers(hf_model.vision_model.encoder.layers     ,				pt_model.visual.transformer.resblocks						)

@torch.no_grad()
def 			lowerCAmelCase_			(  lowerCamelCase:       int     ,				lowerCamelCase:       Tuple     ,				lowerCamelCase:       Optional[Any]=None						)					->			Any:


      '''simple docstring'''




      if config_path is not None:
            lowerCamelCase__        =				CLIPConfig.from_pretrained(lowerCamelCase						)
      else:
            lowerCamelCase__        =				CLIPConfig(projection_dim=5_12     ,				text_config={}     ,				vision_config={}						)

      lowerCamelCase__        =				CLIPModel(lowerCamelCase						).eval()

      lowerCamelCase__  ,		lowerCamelCase__        =				load(lowerCamelCase     ,				device='cpu'     ,				jit=lowerCamelCase						)
      lowerCamelCase__        =				pt_model.eval()

      copy_text_model_and_projection(lowerCamelCase     ,				lowerCamelCase						)
      copy_vison_model_and_projection(lowerCamelCase     ,				lowerCamelCase						)
      lowerCamelCase__        =				pt_model.logit_scale

      lowerCamelCase__        =				torch.arange(0     ,				77						).unsqueeze(0						)
      lowerCamelCase__        =				torch.randn(1     ,				3     ,				2_24     ,				2_24						)

      lowerCamelCase__        =				hf_model(input_ids=lowerCamelCase     ,				pixel_values=lowerCamelCase     ,				return_dict=lowerCamelCase						)
      lowerCamelCase__        =				hf_outputs.logits_per_image
      lowerCamelCase__        =				hf_outputs.logits_per_text
      lowerCamelCase__  ,		lowerCamelCase__        =				pt_model(lowerCamelCase     ,				lowerCamelCase						)

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

      hf_model.save_pretrained(lowerCamelCase						)


if __name__ == "__main__":
      __SCREAMING_SNAKE_CASE    :   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 fairseq checkpoint""")
      parser.add_argument("""--config_path""", default=None, type=str, help="""Path to hf config.json of model to convert""")
      __SCREAMING_SNAKE_CASE    :   Any       =parser.parse_args()

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





import shutil

import requests

def 			lowerCAmelCase_			(  lowerCamelCase:       str     ,				lowerCamelCase:       bool = False     ,				lowerCamelCase:       str = "."						)					->			dict:


      '''simple docstring'''




      lowerCamelCase__        =				'https://api.nasa.gov/planetary/apod'
      return requests.get(lowerCamelCase     ,				params={'api_key': api_key}						).json()

def 			lowerCAmelCase_			(  lowerCamelCase:       str     ,				lowerCamelCase:       str = "."						)					->			dict:


      '''simple docstring'''




      lowerCamelCase__        =				get_apod_data(lowerCamelCase						)
      lowerCamelCase__        =				apod_data['url']
      lowerCamelCase__        =				img_url.split('/'						)[-1]
      lowerCamelCase__        =				requests.get(lowerCamelCase     ,				stream=lowerCamelCase						)

      with open(f"""{path}/{img_name}"""     ,				'wb+'						) as img_file:
            shutil.copyfileobj(response.raw     ,				lowerCamelCase						)
      del response
      return apod_data

def 			lowerCAmelCase_			(  lowerCamelCase:       str						)					->			dict:


      '''simple docstring'''




      lowerCamelCase__        =				'https://images-api.nasa.gov/search'
      return requests.get(lowerCamelCase     ,				params={'q': query}						).json()


if __name__ == "__main__":
      print(save_apod("""YOUR API KEY"""))
      __SCREAMING_SNAKE_CASE    :   Tuple       =get_archive_data("""apollo 2011""")["""collection"""]["""items"""]
      print(apollo_aaaa_items[0]["""data"""][0]["""description"""])