a
    dIE                  %   @   s&  d dl Z d dlZd dlZd dlZd dlmZ d dlmZmZ d dl	m	Z	 ddl
mZ z"d dlmZ eeded	Zd
ZW n ey   dZdZY n0 dddZeedededdZeedededdZeededdZeededdZeeddZeeddZeeddZeedededededded ded!ded"ded#ded$ded%ded&ded'ded(ded)ded*ded+ded,ded-dd.Zeedededd/Zeed0ed1ed2ed3ded4ded5ded6ded7ded8ded9ded:dd;Zeed<ed=d>Zeed?ed@edAedBdCdCdDedEdedFdedGdedHddIZeedJdZeedKddLZeedMdedNddOZ eedPddQZ!eedRddSZ"eedTddUZ#eedVddWZ$eedXddYZ%eedZded[ded\dd]Z&eed^ded_dd`Z'eedaddbZ(eedcdeddddeZ)eedfdedgdedhddiZ*eedjdedkddlZ+eedmdednddlZ,eeeeeeeeeeeeeee e!e"e#e$e%e&e'e(e)e*e+e,eedoddpeedqddreedsddteeduddveedwddxeedyddzeed{dd|d}"Z-e.d~ddZ/de0dddZ1e.d~ddZ2e.dddZ3e.e.dddZ4e.e.dddZ5e.e.dddZ6de.ee.df dddZ7dddZ8de.e.ee.df dddZ9dee0ee.df dddZ:dS )    N)partial)DictUnion)tqdm   )__version__)hf_hub_downloadZ	open_clip)Zlibrary_nameZlibrary_versionTF c                 C   s   t | |||dS )Nurlhf_hubmeanstd)dictr
    r   ]/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/open_clip/pretrained.py_pcfg   s    r   zwhttps://openaipublic.azureedge.net/clip/models/afeb0e10f9e5a86da6080e35cf09123aca3b358a0c3e3b6c78a7b63bc04b6762/RN50.ptzlhttps://github.com/mlfoundations/open_clip/releases/download/v0.2-weights/rn50-quickgelu-yfcc15m-455df137.ptzjhttps://github.com/mlfoundations/open_clip/releases/download/v0.2-weights/rn50-quickgelu-cc12m-f000538c.pt)openaiyfcc15mZcc12mzxhttps://openaipublic.azureedge.net/clip/models/8fa8567bab74a42d41c5915025a8e4538c3bdbe8804a470a72f30b0d94fab599/RN101.ptzmhttps://github.com/mlfoundations/open_clip/releases/download/v0.2-weights/rn101-quickgelu-yfcc15m-3e04b30e.pt)r   r   zyhttps://openaipublic.azureedge.net/clip/models/7e526bd135e493cef0776de27d5f42653e6b4c8bf9e0f653bb11773263205fdd/RN50x4.pt)r   zzhttps://openaipublic.azureedge.net/clip/models/52378b407f34354e150460fe41077663dd5b39c54cd0bfd2b27167a4a06ec9aa/RN50x16.ptzzhttps://openaipublic.azureedge.net/clip/models/be1cfb55d75a9666199fb2206c106743da0f6468c9d327f3e0d0a543a9919d9c/RN50x64.ptz{https://openaipublic.azureedge.net/clip/models/40d365715913c9da98579312b702a82c18be219cc2a73407c4526f58eba950af/ViT-B-32.ptzvhttps://github.com/mlfoundations/open_clip/releases/download/v0.2-weights/vit_b_32-quickgelu-laion400m_e31-d867053b.ptzvhttps://github.com/mlfoundations/open_clip/releases/download/v0.2-weights/vit_b_32-quickgelu-laion400m_e32-46683a32.ptzkhttps://github.com/mlfoundations/open_clip/releases/download/v0.2-weights/vit_b_32-laion2b_e16-af8dbd0c.pthz&laion/CLIP-ViT-B-32-laion2B-s34B-b79K/)r   z)laion/CLIP-ViT-B-32-DataComp.M-s128M-b4K/z0laion/CLIP-ViT-B-32-CommonPool.M.clip-s128M-b4K/z1laion/CLIP-ViT-B-32-CommonPool.M.laion-s128M-b4K/z1laion/CLIP-ViT-B-32-CommonPool.M.image-s128M-b4K/z0laion/CLIP-ViT-B-32-CommonPool.M.text-s128M-b4K/z1laion/CLIP-ViT-B-32-CommonPool.M.basic-s128M-b4K/z+laion/CLIP-ViT-B-32-CommonPool.M-s128M-b4K/z(laion/CLIP-ViT-B-32-DataComp.S-s13M-b4K/z/laion/CLIP-ViT-B-32-CommonPool.S.clip-s13M-b4K/z0laion/CLIP-ViT-B-32-CommonPool.S.laion-s13M-b4K/z0laion/CLIP-ViT-B-32-CommonPool.S.image-s13M-b4K/z/laion/CLIP-ViT-B-32-CommonPool.S.text-s13M-b4K/z0laion/CLIP-ViT-B-32-CommonPool.S.basic-s13M-b4K/z*laion/CLIP-ViT-B-32-CommonPool.S-s13M-b4K/)r   laion400m_e31laion400m_e32Zlaion2b_e16Zlaion2b_s34b_b79kZdatacomp_m_s128m_b4kZcommonpool_m_clip_s128m_b4kZcommonpool_m_laion_s128m_b4kZcommonpool_m_image_s128m_b4kZcommonpool_m_text_s128m_b4kZcommonpool_m_basic_s128m_b4kZcommonpool_m_s128m_b4kZdatacomp_s_s13m_b4kZcommonpool_s_clip_s13m_b4kZcommonpool_s_laion_s13m_b4kZcommonpool_s_image_s13m_b4kZcommonpool_s_text_s13m_b4kZcommonpool_s_basic_s13m_b4kZcommonpool_s_s13m_b4k)r   r   r   z{https://openaipublic.azureedge.net/clip/models/5806e77cd80f8b59890b7e101eabd078d9fb84e6937f9e85e4ecb61988df416f/ViT-B-16.ptzlhttps://github.com/mlfoundations/open_clip/releases/download/v0.2-weights/vit_b_16-laion400m_e31-00efa78f.ptzlhttps://github.com/mlfoundations/open_clip/releases/download/v0.2-weights/vit_b_16-laion400m_e32-55e67d44.ptz&laion/CLIP-ViT-B-16-laion2B-s34B-b88K/z'laion/CLIP-ViT-B-16-DataComp.L-s1B-b8K/z.laion/CLIP-ViT-B-16-CommonPool.L.clip-s1B-b8K/z/laion/CLIP-ViT-B-16-CommonPool.L.laion-s1B-b8K/z/laion/CLIP-ViT-B-16-CommonPool.L.image-s1B-b8K/z.laion/CLIP-ViT-B-16-CommonPool.L.text-s1B-b8K/z/laion/CLIP-ViT-B-16-CommonPool.L.basic-s1B-b8K/z)laion/CLIP-ViT-B-16-CommonPool.L-s1B-b8K/)r   r   r   laion2b_s34b_b88kZdatacomp_l_s1b_b8kZcommonpool_l_clip_s1b_b8kZcommonpool_l_laion_s1b_b8kZcommonpool_l_image_s1b_b8kZcommonpool_l_text_s1b_b8kZcommonpool_l_basic_s1b_b8kZcommonpool_l_s1b_b8kzuhttps://github.com/mlfoundations/open_clip/releases/download/v0.2-weights/vit_b_16_plus_240-laion400m_e31-8fb26589.ptzuhttps://github.com/mlfoundations/open_clip/releases/download/v0.2-weights/vit_b_16_plus_240-laion400m_e32-699c4b84.pt)r   r   z{https://openaipublic.azureedge.net/clip/models/b8cca3fd41ae0c99ba7e8951adf17d267cdb84cd88be6f7c2e0eca1737a03836/ViT-L-14.ptzlhttps://github.com/mlfoundations/open_clip/releases/download/v0.2-weights/vit_l_14-laion400m_e31-69988bb6.ptzlhttps://github.com/mlfoundations/open_clip/releases/download/v0.2-weights/vit_l_14-laion400m_e32-3d133497.ptz&laion/CLIP-ViT-L-14-laion2B-s32B-b82K/)      ?r   r   )r   r   r   z*laion/CLIP-ViT-L-14-DataComp.XL-s13B-b90K/z1laion/CLIP-ViT-L-14-CommonPool.XL.clip-s13B-b90K/z2laion/CLIP-ViT-L-14-CommonPool.XL.laion-s13B-b90K/z,laion/CLIP-ViT-L-14-CommonPool.XL-s13B-b90K/)r   r   r   Zlaion2b_s32b_b82kZdatacomp_xl_s13b_b90kZcommonpool_xl_clip_s13b_b90kZcommonpool_xl_laion_s13b_b90kZcommonpool_xl_s13b_b90kzhttps://openaipublic.azureedge.net/clip/models/3035c92b350959924f9f00213499208652fc7ea050643e8b385c2dac08641f02/ViT-L-14-336px.ptz&laion/CLIP-ViT-H-14-laion2B-s32B-b79K/)Zlaion2b_s32b_b79kz&laion/CLIP-ViT-g-14-laion2B-s12B-b42K/z&laion/CLIP-ViT-g-14-laion2B-s34B-b88K/)Zlaion2b_s12b_b42kr   z)laion/CLIP-ViT-bigG-14-laion2B-39B-b160k/)Zlaion2b_s39b_b160kz3laion/CLIP-ViT-B-32-roberta-base-laion2B-s12B-b32k/)Zlaion2b_s12b_b32kz7laion/CLIP-ViT-B-32-xlm-roberta-base-laion5B-s13B-b90k/)Zlaion5b_s13b_b90kz?laion/CLIP-ViT-H-14-frozen-xlm-roberta-large-laion5B-s13B-b90k/)Zfrozen_laion5b_s13b_b90kz-laion/CLIP-convnext_base-laion400M-s13B-b51K/)Zlaion400m_s13b_b51kz-laion/CLIP-convnext_base_w-laion2B-s13B-b82K/z4laion/CLIP-convnext_base_w-laion2B-s13B-b82K-augreg/z5laion/CLIP-convnext_base_w-laion_aesthetic-s13B-b82K/)Zlaion2b_s13b_b82kZlaion2b_s13b_b82k_augreglaion_aesthetic_s13b_b82kz9laion/CLIP-convnext_base_w_320-laion_aesthetic-s13B-b82K/z@laion/CLIP-convnext_base_w_320-laion_aesthetic-s13B-b82K-augreg/)r   Z laion_aesthetic_s13b_b82k_augregz6laion/CLIP-convnext_large_d.laion2B-s26B-b102K-augreg/)Zlaion2b_s26b_b102k_augregz6laion/CLIP-convnext_large_d_320.laion2B-s29B-b131K-ft/z;laion/CLIP-convnext_large_d_320.laion2B-s29B-b131K-ft-soup/)Zlaion2b_s29b_b131k_ftZlaion2b_s29b_b131k_ft_soupz5laion/CLIP-convnext_xxlarge-laion2B-s34B-b82K-augreg/z<laion/CLIP-convnext_xxlarge-laion2B-s34B-b82K-augreg-rewind/z:laion/CLIP-convnext_xxlarge-laion2B-s34B-b82K-augreg-soup/)Zlaion2b_s34b_b82k_augregZlaion2b_s34b_b82k_augreg_rewindZlaion2b_s34b_b82k_augreg_soupz&laion/CoCa-ViT-B-32-laion2B-s13B-b90k/z7laion/mscoco_finetuned_CoCa-ViT-B-32-laion2B-s13B-b90k/)Zlaion2b_s13b_b90kZ"mscoco_finetuned_laion2b_s13b_b90kz&laion/CoCa-ViT-L-14-laion2B-s13B-b90k/z7laion/mscoco_finetuned_CoCa-ViT-L-14-laion2B-s13B-b90k/z4timm/eva_giant_patch14_clip_224.laion400m_s11b_b41k/)Zlaion400m_s11b_b41kz9timm/eva_giant_patch14_plus_clip_224.merged2b_s11b_b114k/)Zmerged2b_s11b_b114kz4timm/eva02_base_patch16_clip_224.merged2b_s8b_b131k/)Zmerged2b_s8b_b131kz5timm/eva02_large_patch14_clip_224.merged2b_s4b_b131k/)Zmerged2b_s4b_b131kz4timm/eva02_large_patch14_clip_336.merged2b_s6b_b61k/)Zmerged2b_s6b_b61kz7timm/eva02_enormous_patch14_clip_224.laion2b_s4b_b115k/)Zlaion2b_s4b_b115kz<timm/eva02_enormous_patch14_plus_clip_224.laion2b_s9b_b144k/)Zlaion2b_s9b_b144k)"ZRN50zRN50-quickgeluZRN101zRN101-quickgeluZRN50x4ZRN50x16ZRN50x64zViT-B-32zViT-B-32-quickgeluzViT-B-16zViT-B-16-plus-240zViT-L-14zViT-L-14-336zViT-H-14zViT-g-14zViT-bigG-14zroberta-ViT-B-32zxlm-roberta-base-ViT-B-32zxlm-roberta-large-ViT-H-14Zconvnext_baseZconvnext_base_wZconvnext_base_w_320Zconvnext_large_dZconvnext_large_d_320Zconvnext_xxlargezcoca_ViT-B-32zcoca_ViT-L-14z
EVA01-g-14zEVA01-g-14-plusz
EVA02-B-16z
EVA02-L-14zEVA02-L-14-336z
EVA02-E-14zEVA02-E-14-plustagc                 C   s   |   ddS )N-_)lowerreplacer   r   r   r   
_clean_tag  s    r    Zas_strc                    s    fddt  D S )z returns list of pretrained models
    Returns a tuple (model_name, pretrain_tag) by default or 'name:tag' if as_str == True
    c                    s8   g | ]0}t |  D ]} r*d ||gn||fqqS ):)_PRETRAINEDkeysjoin).0ktr!   r   r   
<listcomp>'      z#list_pretrained.<locals>.<listcomp>)r#   r$   r!   r   r!   r   list_pretrained#  s    r+   c                 C   s4   g }t | } t D ]}| t| v r|| q|S )z5 return all models having the specified pretrain tag )r    r#   r$   append)r   modelsr'   r   r   r   list_pretrained_models_by_tag*  s    r.   )modelc                 C   s"   g }| t v r|t |    |S )z? return all pretrain tags for the specified model architecture )r#   extendr$   )r/   tagsr   r   r   list_pretrained_tags_by_model4  s    r2   r/   r   c                 C   s   | t vrdS t|t |  v S )NF)r#   r    r3   r   r   r   is_pretrained_cfg<  s    r4   c                 C   s$   | t vri S t |  }|t|i S )N)r#   getr    )r/   r   Zmodel_pretrainedr   r   r   get_pretrained_cfgB  s    r6   c                 C   s   t | t|}|ddS )Nr   r	   )r6   r    r5   )r/   r   cfgr   r   r   get_pretrained_urlI  s    r8   )r   	cache_dirc           	   
   C   s  |st jd}t j|dd t j| }d| v rB| dd }n(d| v rft j|d d	d
 }nd}t j||}t j|rt j	|st
| dt j	|r|rtt|d  |r|S t| d n|S tj| }t|d~}tt|jddddd@}|d}|s2qN|| |t| qW d    n1 sd0    Y  W d    n1 s0    Y  W d    n1 s0    Y  |rtt|d  |st
d|S )Nz~/.cache/clipT)exist_okZopenaipublic/Zmlfoundationsr   r   r	   z! exists and is not a regular filerbzH exists, but the SHA256 checksum does not match; re-downloading the filewbzContent-LengthP   ZiB)totalZncolsunitZ
unit_scalei    zDModel has been downloaded but the SHA256 checksum does not not match)ospath
expandusermakedirsbasenamesplitsplitextr%   existsisfileRuntimeErrorhashlibsha256openread	hexdigest
startswithwarningswarnurllibrequesturlopenr   intheadersr5   writeupdatelen)	r   r9   filenameZexpected_sha256Zdownload_targetsourceoutputZloopbufferr   r   r   download_pretrained_from_urlN  s8    

r&ra   c                 C   s   t s| rtdt S )Nz^Hugging Face hub model specified but package not installed. Run `pip install huggingface_hub`.)_has_hf_hubrL   )Z	necessaryr   r   r   
has_hf_hub|  s
    rc   open_clip_pytorch_model.bin)model_idr]   r9   c                 C   s   t d t| |||d}|S )NT)revisionr9   )rc   r   )re   r]   rf   r9   Zcached_filer   r   r   download_pretrained_from_hf  s    rg   )r7   force_hf_hubr9   c                 C   s   d}| s|S |  dd}|  dd}|r0|r0d}|rBt||d}n<|r~td tj|\}}|rrt|||d}nt||d}|S )Nr	   r   r   )r9   T)r]   r9   )r5   ra   rc   rC   rD   rH   rg   )r7   rh   r9   targetdownload_urlZdownload_hf_hubre   r]   r   r   r   download_pretrained  s     rk   )r	   r	   NN)F)N)F)rd   NN)FN);rM   rC   rU   rS   	functoolsr   typingr   r   r   versionr   Zhuggingface_hubr   rb   ImportErrorr   r   Z_RN50Z_RN50_quickgeluZ_RN101Z_RN101_quickgeluZ_RN50x4Z_RN50x16Z_RN50x64Z_VITB32Z_VITB32_quickgeluZ_VITB16Z_VITB16_PLUS_240Z_VITL14Z_VITL14_336Z_VITH14Z_VITg14Z
_VITbigG14Z_robertaViTB32Z_xlmRobertaBaseViTB32Z_xlmRobertaLargeFrozenViTH14Z_convnext_baseZ_convnext_base_wZ_convnext_base_w_320Z_convnext_large_dZ_convnext_large_d_320Z_convnext_xxlargeZ_coca_VITB32Z_coca_VITL14r#   strr    boolr+   r.   r2   r4   r6   r8   ra   rc   rg   rk   r   r   r   r   <module>   s>  

				;
 
.

   
  
