




'''simple docstring'''


import argparse

import torch

from transformers import ChineseCLIPConfig, ChineseCLIPModel



def    _SCREAMING_SNAKE_CASE    (			UpperCamelCase__						:				int   ,						UpperCamelCase__						:				Union[str, Any]   ,						UpperCamelCase__						:				str							):




     """simple docstring"""


     a_    :       Union[str, Any]    	=     pt_weights[F"{prefix}.in_proj_weight"].chunk(3   ,						dim=0							)
     a_    :       Any    	=     pt_weights[F"{prefix}.in_proj_bias"].chunk(3   ,						dim=0							)

     a_    :       List[Any]    	=     pt_weights[F"{prefix}.out_proj.weight"]
     a_    :       str    	=     pt_weights[F"{prefix}.out_proj.bias"]

     a_    :       str    	=     q_proj
     a_    :       Optional[Any]    	=     q_proj_bias

     a_    :       List[Any]    	=     k_proj
     a_    :       str    	=     k_proj_bias

     a_    :       Tuple    	=     v_proj
     a_    :       List[Any]    	=     v_proj_bias

     a_    :       Dict    	=     out_proj_weights
     a_    :       Optional[int]    	=     out_proj_bias



def    _SCREAMING_SNAKE_CASE    (			UpperCamelCase__						:				Any   ,						UpperCamelCase__						:				List[str]   ,						UpperCamelCase__						:				Any							):




     """simple docstring"""


     copy_linear(hf_mlp.fca   ,						lowercase__   ,						F"{prefix}.c_fc"							)
     copy_linear(hf_mlp.fca   ,						lowercase__   ,						F"{prefix}.c_proj"							)



def    _SCREAMING_SNAKE_CASE    (			UpperCamelCase__						:				List[Any]   ,						UpperCamelCase__						:				Dict   ,						UpperCamelCase__						:				List[Any]							):




     """simple docstring"""


     a_    :       List[Any]    	=     pt_weights[F"{prefix}.weight"].data
     a_    :       Optional[int]    	=     pt_weights[F"{prefix}.bias"].data



def    _SCREAMING_SNAKE_CASE    (			UpperCamelCase__						:				Dict   ,						UpperCamelCase__						:				List[Any]   ,						UpperCamelCase__						:				List[Any]							):




     """simple docstring"""


     copy_linear(hf_layer.layer_norma   ,						lowercase__   ,						F"{prefix}.ln_1"							)
     copy_linear(hf_layer.layer_norma   ,						lowercase__   ,						F"{prefix}.ln_2"							)

     # copy MLP
     copy_mlp(hf_layer.mlp   ,						lowercase__   ,						F"{prefix}.mlp"							)

     # copy attn
     copy_attn_layer(hf_layer.self_attn   ,						lowercase__   ,						F"{prefix}.attn"							)



def    _SCREAMING_SNAKE_CASE    (			UpperCamelCase__						:				List[Any]   ,						UpperCamelCase__						:				Optional[int]   ,						UpperCamelCase__						:				Optional[int]							):




     """simple docstring"""


     for layer_id, hf_layer in enumerate(lowercase__							):
          copy_layer(lowercase__   ,						lowercase__   ,						F"{prefix}.{layer_id}"							)



def    _SCREAMING_SNAKE_CASE    (			UpperCamelCase__						:				Tuple   ,						UpperCamelCase__						:				List[Any]							):




     """simple docstring"""


     a_    :       List[Any]    	=     pt_weights["text_projection"].data.T

     # copy text encoder
     for name, param in hf_model.text_model.named_parameters():
          a_    :       List[str]    	=     pt_weights[F"bert.{name}"].data



def    _SCREAMING_SNAKE_CASE    (			UpperCamelCase__						:				Dict   ,						UpperCamelCase__						:				Optional[Any]							):




     """simple docstring"""


     a_    :       Any    	=     pt_weights["visual.proj"].data.T

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

     # copy embeddings
     a_    :       Dict    	=     pt_weights["visual.conv1.weight"].data
     a_    :       Optional[int]    	=     pt_weights["visual.class_embedding"].data
     a_    :       Optional[Any]    	=     pt_weights["visual.positional_embedding"].data

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



@torch.no_grad()
def    _SCREAMING_SNAKE_CASE    (			UpperCamelCase__						:				str   ,						UpperCamelCase__						:				Any   ,						UpperCamelCase__						:				Tuple=None							):




     """simple docstring"""


     assert config_path is not None, "Please specify the ChineseCLIP model config of the corresponding model size."
     a_    :       Union[str, Any]    	=     ChineseCLIPConfig.from_pretrained(lowercase__							)

     a_    :       Any    	=     ChineseCLIPModel(lowercase__							).eval()

     a_    :       int    	=     torch.load(lowercase__   ,						map_location="""cpu"""							)["state_dict"]
     a_    :       Dict    	=     {(name[7:] if name.startswith("""module."""							) else name): value for name, value in pt_weights.items()}

     copy_text_model_and_projection(lowercase__   ,						lowercase__							)
     copy_vision_model_and_projection(lowercase__   ,						lowercase__							)
     a_    :       Dict    	=     pt_weights["logit_scale"].data

     hf_model.save_pretrained(lowercase__							)


if __name__ == "__main__":
       lowerCAmelCase_			:     List[str]							    =     argparse.ArgumentParser()
       parser.add_argument(
           '--pytorch_dump_folder_path',
           default=None,
           type=str,
           help='Path to the output folder storing converted hf PyTorch model.',
       )
       parser.add_argument(
           '--checkpoint_path', default=None, type=str, help='Path to original github format ChineseCLIP checkpoint.'
       )
       parser.add_argument(
           '--config_path', default=None, required=True, type=str, help='Path to hf config.json of model to convert.'
       )
       lowerCAmelCase_			:     Optional[int]							    =     parser.parse_args()

       convert_chinese_clip_checkpoint(args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path)
       print('The conversion is finished!')






'''simple docstring'''


import shutil

import requests



def    _SCREAMING_SNAKE_CASE    (			UpperCamelCase__						:				str   ,						UpperCamelCase__						:				bool = False   ,						UpperCamelCase__						:				str = "."							):




     """simple docstring"""


     a_    :       Any    	=     """https://api.nasa.gov/planetary/apod"""
     return requests.get(UpperCamelCase__   ,						params={"""api_key""": api_key}							).json()



def    _SCREAMING_SNAKE_CASE    (			UpperCamelCase__						:				str   ,						UpperCamelCase__						:				str = "."							):




     """simple docstring"""


     a_    :       Tuple    	=     get_apod_data(UpperCamelCase__							)
     a_    :       Tuple    	=     apod_data["""url"""]
     a_    :       Any    	=     img_url.split("""/"""							)[-1]
     a_    :       Tuple    	=     requests.get(UpperCamelCase__   ,						stream=UpperCamelCase__							)

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



def    _SCREAMING_SNAKE_CASE    (			UpperCamelCase__						:				str							):




     """simple docstring"""


     a_    :       str    	=     """https://images-api.nasa.gov/search"""
     return requests.get(UpperCamelCase__   ,						params={"""q""": query}							).json()


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