
"""simple docstring"""


import argparse
import io

import requests
import torch
from omegaconf import OmegaConf

from diffusers import AutoencoderKL
from diffusers.pipelines.stable_diffusion.convert_from_ckpt import (
    assign_to_checkpoint,
    conv_attn_to_linear,
    create_vae_diffusers_config,
    renew_vae_attention_paths,
    renew_vae_resnet_paths,
)






def    lowerCAmelCase_   (		snake_case_		:			List[str]							,    snake_case_		:			Optional[int]			)   ->Any:
			lowerCamelCase__	:     Optional[int]              =checkpoint

			lowerCamelCase__	:     List[Any]              ={}

			lowerCamelCase__	:     Tuple              =vae_state_dict['encoder.conv_in.weight']
			lowerCamelCase__	:     Union[str, Any]              =vae_state_dict['encoder.conv_in.bias']
			lowerCamelCase__	:     Tuple              =vae_state_dict['encoder.conv_out.weight']
			lowerCamelCase__	:     List[Any]              =vae_state_dict['encoder.conv_out.bias']
			lowerCamelCase__	:     Any              =vae_state_dict['encoder.norm_out.weight']
			lowerCamelCase__	:     str              =vae_state_dict['encoder.norm_out.bias']

			lowerCamelCase__	:     Optional[int]              =vae_state_dict['decoder.conv_in.weight']
			lowerCamelCase__	:     int              =vae_state_dict['decoder.conv_in.bias']
			lowerCamelCase__	:     List[Any]              =vae_state_dict['decoder.conv_out.weight']
			lowerCamelCase__	:     Dict              =vae_state_dict['decoder.conv_out.bias']
			lowerCamelCase__	:     List[str]              =vae_state_dict['decoder.norm_out.weight']
			lowerCamelCase__	:     Optional[int]              =vae_state_dict['decoder.norm_out.bias']

			lowerCamelCase__	:     Optional[Any]              =vae_state_dict['quant_conv.weight']
			lowerCamelCase__	:     int              =vae_state_dict['quant_conv.bias']
			lowerCamelCase__	:     str              =vae_state_dict['post_quant_conv.weight']
			lowerCamelCase__	:     str              =vae_state_dict['post_quant_conv.bias']

			# Retrieves the keys for the encoder down blocks only
			lowerCamelCase__	:     Optional[Any]              =len({'.'.join(layer.split('.'			)[:3]			) for layer in vae_state_dict if 'encoder.down' in layer}			)
			lowerCamelCase__	:     Tuple              ={
			    layer_id: [key for key in vae_state_dict if f"""down.{layer_id}""" in key] for layer_id in range(__lowerCAmelCase			)
			}

			# Retrieves the keys for the decoder up blocks only
			lowerCamelCase__	:     int              =len({'.'.join(layer.split('.'			)[:3]			) for layer in vae_state_dict if 'decoder.up' in layer}			)
			lowerCamelCase__	:     Optional[int]              ={
			    layer_id: [key for key in vae_state_dict if f"""up.{layer_id}""" in key] for layer_id in range(__lowerCAmelCase			)
			}

			for i in range(__lowerCAmelCase			):
						lowerCamelCase__	:     List[Any]              =[key for key in down_blocks[i] if f"""down.{i}""" in key and f"""down.{i}.downsample""" not in key]

						if f"""encoder.down.{i}.downsample.conv.weight""" in vae_state_dict:
									lowerCamelCase__	:     Any              =vae_state_dict.pop(
									    f"""encoder.down.{i}.downsample.conv.weight"""			)
									lowerCamelCase__	:     Optional[Any]              =vae_state_dict.pop(
									    f"""encoder.down.{i}.downsample.conv.bias"""			)

						lowerCamelCase__	:     Tuple              =renew_vae_resnet_paths(__lowerCAmelCase			)
						lowerCamelCase__	:     List[Any]              ={'old': f"""down.{i}.block""", 'new': f"""down_blocks.{i}.resnets"""}
						assign_to_checkpoint(__lowerCAmelCase							,    __lowerCAmelCase							,    __lowerCAmelCase							,    additional_replacements=[meta_path]							,    config=__lowerCAmelCase			)

			lowerCamelCase__	:     Dict              =[key for key in vae_state_dict if 'encoder.mid.block' in key]
			lowerCamelCase__	:     Dict              =2
			for i in range(1							,    num_mid_res_blocks + 1			):
						lowerCamelCase__	:     Optional[int]              =[key for key in mid_resnets if f"""encoder.mid.block_{i}""" in key]

						lowerCamelCase__	:     str              =renew_vae_resnet_paths(__lowerCAmelCase			)
						lowerCamelCase__	:     int              ={'old': f"""mid.block_{i}""", 'new': f"""mid_block.resnets.{i - 1}"""}
						assign_to_checkpoint(__lowerCAmelCase							,    __lowerCAmelCase							,    __lowerCAmelCase							,    additional_replacements=[meta_path]							,    config=__lowerCAmelCase			)

			lowerCamelCase__	:     int              =[key for key in vae_state_dict if 'encoder.mid.attn' in key]
			lowerCamelCase__	:     int              =renew_vae_attention_paths(__lowerCAmelCase			)
			lowerCamelCase__	:     Any              ={'old': 'mid.attn_1', 'new': 'mid_block.attentions.0'}
			assign_to_checkpoint(__lowerCAmelCase							,    __lowerCAmelCase							,    __lowerCAmelCase							,    additional_replacements=[meta_path]							,    config=__lowerCAmelCase			)
			conv_attn_to_linear(__lowerCAmelCase			)

			for i in range(__lowerCAmelCase			):
						lowerCamelCase__	:     Any              =num_up_blocks - 1 - i
						lowerCamelCase__	:     str              =[
						    key for key in up_blocks[block_id] if f"""up.{block_id}""" in key and f"""up.{block_id}.upsample""" not in key
						]

						if f"""decoder.up.{block_id}.upsample.conv.weight""" in vae_state_dict:
									lowerCamelCase__	:     List[Any]              =vae_state_dict[
									    f"""decoder.up.{block_id}.upsample.conv.weight"""
									]
									lowerCamelCase__	:     int              =vae_state_dict[
									    f"""decoder.up.{block_id}.upsample.conv.bias"""
									]

						lowerCamelCase__	:     Any              =renew_vae_resnet_paths(__lowerCAmelCase			)
						lowerCamelCase__	:     List[Any]              ={'old': f"""up.{block_id}.block""", 'new': f"""up_blocks.{i}.resnets"""}
						assign_to_checkpoint(__lowerCAmelCase							,    __lowerCAmelCase							,    __lowerCAmelCase							,    additional_replacements=[meta_path]							,    config=__lowerCAmelCase			)

			lowerCamelCase__	:     Dict              =[key for key in vae_state_dict if 'decoder.mid.block' in key]
			lowerCamelCase__	:     Union[str, Any]              =2
			for i in range(1							,    num_mid_res_blocks + 1			):
						lowerCamelCase__	:     Optional[Any]              =[key for key in mid_resnets if f"""decoder.mid.block_{i}""" in key]

						lowerCamelCase__	:     Dict              =renew_vae_resnet_paths(__lowerCAmelCase			)
						lowerCamelCase__	:     List[str]              ={'old': f"""mid.block_{i}""", 'new': f"""mid_block.resnets.{i - 1}"""}
						assign_to_checkpoint(__lowerCAmelCase							,    __lowerCAmelCase							,    __lowerCAmelCase							,    additional_replacements=[meta_path]							,    config=__lowerCAmelCase			)

			lowerCamelCase__	:     List[Any]              =[key for key in vae_state_dict if 'decoder.mid.attn' in key]
			lowerCamelCase__	:     Union[str, Any]              =renew_vae_attention_paths(__lowerCAmelCase			)
			lowerCamelCase__	:     List[str]              ={'old': 'mid.attn_1', 'new': 'mid_block.attentions.0'}
			assign_to_checkpoint(__lowerCAmelCase							,    __lowerCAmelCase							,    __lowerCAmelCase							,    additional_replacements=[meta_path]							,    config=__lowerCAmelCase			)
			conv_attn_to_linear(__lowerCAmelCase			)
			return new_checkpoint






def    lowerCAmelCase_   (		snake_case_		:			str							,    snake_case_		:			str							,    )   ->List[str]:
			# Only support V1
			lowerCamelCase__	:     int              =requests.get(
			    ' https://raw.githubusercontent.com/CompVis/stable-diffusion/main/configs/stable-diffusion/v1-inference.yaml'			)
			lowerCamelCase__	:     Optional[Any]              =io.BytesIO(r.content			)

			lowerCamelCase__	:     Optional[int]              =OmegaConf.load(__lowerCAmelCase			)
			lowerCamelCase__	:     Any              =5_1_2
			lowerCamelCase__	:     Optional[int]              ='cuda' if torch.cuda.is_available() else 'cpu'
			if checkpoint_path.endswith('safetensors'			):
						from safetensors import safe_open

						lowerCamelCase__	:     Tuple              ={}
						with safe_open(__lowerCAmelCase							,    framework='pt'							,    device='cpu'			) as f:
									for key in f.keys():
												lowerCamelCase__	:     Any              =f.get_tensor(__lowerCAmelCase			)
			else:
						lowerCamelCase__	:     List[str]              =torch.load(__lowerCAmelCase							,    map_location=__lowerCAmelCase			)['state_dict']

			# Convert the VAE model.
			lowerCamelCase__	:     Any              =create_vae_diffusers_config(__lowerCAmelCase							,    image_size=__lowerCAmelCase			)
			lowerCamelCase__	:     str              =custom_convert_ldm_vae_checkpoint(__lowerCAmelCase							,    __lowerCAmelCase			)

			lowerCamelCase__	:     Dict              =AutoencoderKL(**__lowerCAmelCase			)
			vae.load_state_dict(__lowerCAmelCase			)
			vae.save_pretrained(__lowerCAmelCase			)


if __name__ == "__main__":
							lowerCAmelCase								=						argparse.ArgumentParser()

							parser.add_argument("""--vae_pt_path""", default=None, type=str, required=True, help="""Path to the VAE.pt to convert.""")
							parser.add_argument("""--dump_path""", default=None, type=str, required=True, help="""Path to the VAE.pt to convert.""")

							lowerCAmelCase								=						parser.parse_args()

							vae_pt_to_vae_diffuser(args.vae_pt_path, args.dump_path)

import pprint

import requests

SCREAMING_SNAKE_CASE__   :			str												=    "https://zenquotes.io/api"




def  __magic_name__ (   )			->   list:
				return requests.get(API_ENDPOINT_URL + '''/today'''							).json()




def  __magic_name__ (   )			->   list:
				return requests.get(API_ENDPOINT_URL + '''/random'''							).json()


if __name__ == "__main__":
					SCREAMING_SNAKE_CASE__   :			int												=    random_quotes()
					pprint.pprint(response)



