o
    پi"L                     @   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 d dlm	Z	 d dl
mZmZmZmZmZmZmZ d dlZd dlmZmZmZ zd dlmZ W n eyY   d dlmZ Y nw zd dlZd	ZW n eym   d
ZY nw zd dl
mZ W n ey   d dlmZ Y nw d dlm Z  d dl!m"Z" z!d dl#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z) d dl*m+Z+ ee&de dZ&d	Z,W n ey   dZ&d
Z,Y nw e-e.Z/g dZ0dZ1dZ2dZ3dZ4d^de5fddZ6			
	d_dee5ee5 ee5e5f f de7de7deee5ef  fdd Z8			d`dee5ee5 ee5e5f f de7deee5ef  fd!d"Z9dad#e7fd$d%Z:d&e5fd'd(Z;d)ee5ef fd*d+Z<	dbd,e5d-e5deee5ef  fd.d/Z=d0ee5ef d1ee5ef d2eee5ef e5ee5ef f fd3d4Z>	dbd,e5deee5ef  fd5d6Z?d7ee5ef fd8d9Z@e1d
dfd,e5d-e5d:e7deee5ef  fd;d<ZAd=ZBd>ZC	
dad?e5d:e7fd@dAZD	dbd,e5d-e5dBejEjFdeee5ef  fdCdDZG		dcdBejEjFdEe5dFeeH dGeeH fdHdIZI			
dddBejEjFdJe5dFeeH dGeeH dKee7edL f f
dMdNZJ	O			
	
				LdedBejEjFdPe5dQe5dRee5 dSee5 dTe7dUe7dFeeH dVeeH dGeeH dKee7edL f fdWdXZKdVeHdYe5fdZd[ZLd-e5d2ee5 fd\d]ZMdS )f    N)partial)Path)TemporaryDirectory)AnyDictIterableListOptionalTupleUnion)
HASH_REGEXdownload_url_to_fileurlparse)get_dir)_get_torch_homeTF)Literal)__version__)filter_pretrained_cfg)create_repoget_hf_file_metadatahf_hub_download
hf_hub_urlrepo_type_and_id_from_hf_idupload_folder)EntryNotFoundErrortimm)library_namelibrary_version)get_cache_dirdownload_cached_file
has_hf_hubhf_splitload_model_config_from_hfload_state_dict_from_hfsave_for_hfpush_to_hf_hubpytorch_model.binmodel.safetensorsopen_clip_pytorch_model.binopen_clip_model.safetensors 	child_dirc                 C   sP   t dr
td t }| sdn| f} t jj|dg| R  }t j|dd |S )zf
    Returns the location of the directory where models are cached (and creates it if necessary).
    TORCH_MODEL_ZOOz@TORCH_MODEL_ZOO is deprecated, please use env TORCH_HOME instead checkpointsTexist_ok)osgetenv_loggerwarningr   pathjoinmakedirs)r+   hub_dir	model_dirr-   r-   D/home/ubuntu/.local/lib/python3.10/site-packages/timm/models/_hub.pyr   8   s   

r   url
check_hashprogress	cache_dirc           	      C   s   t | ttfr| \} }nt| }tj|j}|r!tj|dd nt }tj	||}tj
|sTtd| | d }|rLt|}|rJ|dnd }t| |||d |S )NTr/   zDownloading: "{}" to {}
   )r=   )
isinstancelisttupler   r1   r5   basenamer7   r   r6   existsr3   infoformatr   searchgroupr   )	r;   r<   r=   r>   filenamepartscached_filehash_prefixrr-   r-   r:   r   G   s    

r   c           
      C   s   t | ttfr| \} }nt| }tj|j}|st }tj||}tj	|ro|rmt
|}|r7|dnd }|rmt|d%}t|  }	|	d t| |kr]	 W d    dS W d    dS 1 shw   Y  dS dS )Nr?   rbFT)r@   rA   rB   r   r1   r5   rC   r   r6   rD   r   rG   rH   openhashlibsha256read	hexdigestlen)
r;   r<   r>   rI   rJ   rK   rM   rL   fhdr-   r-   r:   check_cached_filea   s.   


rW   	necessaryc                 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_hubRuntimeError)rX   r-   r-   r:   r    {   s
   r    hf_idc                 C   sZ   |  d}dt|  k rdksJ d J d|d }t|dkr'|d nd }||fS )N@r      zChf_hub id should only contain one @ character to identify revision.r?   )splitrT   )r[   	rev_splithf_model_idhf_revisionr-   r-   r:   r!      s
   
(r!   	json_filec                 C   s@   t | ddd}| }W d    n1 sw   Y  t|S )NrM   zutf-8)encoding)rO   rR   jsonloads)rc   readertextr-   r-   r:   load_cfg_from_json   s   

ri   model_idrI   c                 C   s   t | \}}t||||dS )N)revisionr>   )r!   r   )rj   rI   r>   ra   rb   r-   r-   r:   download_from_hf   s   rl   cfgextra_fieldsreturnc                 C   s   d| vr| }| d| dd|d} d|v r| d|d< | d }|| d| v r1| d |d< d| v r<|  d|d< d	| v rG|  d	|d	< | d
i }| d }|||fS )r*   pretrained_cfgarchitecturenum_featuresN)rq   rr   rp   labelslabel_namesnum_classeslabel_descriptions
model_args)popupdateget)rm   rn   rp   rw   
model_namer-   r-   r:   _parse_model_cfg   s&   


r|   c                 C   s2   t dsJ t| d|d}t|}t|| ddS )u>   Original HF‑Hub loader (unchanged download, shared parsing).Tconfig.json)r>   zhf-hub)	hf_hub_idsource)r    rl   ri   r|   )rj   r>   cfg_pathrm   r-   r-   r:   r"      s   r"   
model_pathc                 C   sH   t | } | d }| std| t|}t| dd}t||dS )z?Load from ``<model_path>/config.json`` on the local filesystem.r}   zConfig file not found: z	local-dir)filer   )rn   )r   is_fileFileNotFoundErrorri   strr|   )r   cfg_filerm   rn   r-   r-   r:   load_model_config_from_path   s   r   weights_onlyc           
   
   C   s   t dsJ t| \}}tr@t|D ]-}z"t||||d}td|  d| d| d tjj	|ddW   S  t
y?   Y qw t||||d	}td|  d
| d ztj|d|d}	W |	S  typ   tj|dd}	Y |	S w )NT)repo_idrI   rk   r>   [z"] Safe alternative available for 'z' (as 'z&'). Loading weights using safetensors.cpu)devicerI   rk   r>   z"] Safe alternative not found for 'z)'. Loading weights using default pytorch.map_locationr   r   )r    r!   _has_safetensors_get_safe_alternativesr   r3   rE   safetensorstorch	load_filer   debugload	TypeError)
rj   rI   r   r>   ra   rb   safe_filenamecached_safe_filerK   
state_dictr-   r-   r:   r#      sD   r#   )r'   r&   zpytorch_model.pthz	model.pthr)   z#open_clip_pytorch_model.safetensorsr(   zopen_clip_pytorch_model.pth).safetensorsz.pthz.pth.tar.binr5   c                 C   s   d }t D ]}| | }| rtd|j  |} nqtD ]-}t| d| }|rKt|dkrGt	d| d|  dt
 d|d j d		 |d }q|sVtd
|  dztj|d|d}W |S  tyr   tj|dd}Y |S w )NzFound preferred checkpoint: *r?   z	Multiple z checkpoints in : z	. Using 'r   z'.z!No suitable checkpoints found in .r   r   r   )_PREFERRED_FILESrD   loggingrE   name_EXT_PRIORITYsortedglobrT   r4   namesrZ   r   r   r   )r5   r   
found_filefnamepextfilesr   r-   r-   r:   load_state_dict_from_path  s8   r   modelc                 C   s2   t dsJ t| \}}t||||d}||S )NTr   )r    r!   r   load_pretrained)rj   rI   r   r>   ra   rb   rK   r-   r-   r:   load_custom_from_hf.  s   
r   config_pathmodel_configrw   c           
      C   sL  |pi }i }t | jddd}|d|d< |d| j|d< |d| j|d< |dt| dd }t|tr<|r<||d< d|v rNt	d |
d	|d |d	d }|rdt|tttfs`J ||d	< |d
d }|rwt|tssJ ||d
< |r}||d< ||d< || |d}	tj||	dd W d    d S 1 sw   Y  d S )NT)remove_sourceremove_nullrq   ru   rr   global_poolrs   z'labels' as a config field for is deprecated. Please use 'label_names' and 'label_descriptions'. Renaming provided 'labels' field to 'label_names'.rt   rv   rw   rp   wr]   )indent)r   rp   rx   ru   rr   getattrr@   r   r3   r4   
setdefaultdictrA   rB   ry   rO   re   dump)
r   r   r   rw   	hf_configrp   global_pool_typert   rv   rU   r-   r-   r:   save_config_for_hf?  s:   
"r   save_directorysafe_serializationbothc                 C   s   t dsJ t|}|jddd |  }|du s|dkr,ts#J dtj||t  |du s4|dkr<t	||t
  |d }t| |||d d S )NT)r0   parentsr   z-`pip install safetensors` to use .safetensorsFr}   )r   rw   )r    r   mkdirr   r   r   r   	save_fileHF_SAFE_WEIGHTS_NAMEsaveHF_WEIGHTS_NAMEr   )r   r   r   rw   r   tensorsr   r-   r-   r:   r$   p  s    
r$   	Add modelr   commit_messagetokenrk   private	create_pr
model_cardc                 C   s   t |||dd}t|\}}}| d| }ztt|d|d d}W n ty.   d}Y nw t 7}t| |||	|
d |sY|pAi }|dd }t|d }t	||}|
| t|||||d	W  d
   S 1 slw   Y  d
S )a5  
    Arguments:
        (...)
        safe_serialization (`bool` or `"both"`, *optional*, defaults to `False`):
            Whether to save the model using `safetensors` or the traditional PyTorch way (that uses `pickle`).
            Can be set to `"both"` in order to push both safe and unsafe weights.
    T)r   r   r0   /z	README.md)r   rI   rk   F)r   rw   r   r^   )r   folder_pathrk   r   r   N)r   r   r   r   r   r   r$   r_   r   generate_readme
write_textr   )r   r   r   r   rk   r   r   r   r   rw   r   repo_url_
repo_owner	repo_name
has_readmetmpdirr{   readme_pathreadme_textr-   r-   r:   r%     s>   	

$r%   r{   c                 C   s$  |  dd p	g d}d}|r|d7 }|D ]
}|d| d7 }q|d|  dd	 d7 }|d
|  dd d7 }d| v rF|d|  d d7 }d| v rU|d|  d d7 }d| v rd| d v r|d7 }t| d d ttfr| d d D ]}|d|  d7 }qtn|d| d d   d7 }d| d v rt| d d ttfr| d d D ]}|d|  d7 }qn|d| d d   d7 }|d7 }|d| d7 }d| v r|d| d  d7 }d| v r?|d7 }| d  D ]R\}}t|ttfr|d| d7 }|D ]}|d| d7 }qqt|tr3|d| d7 }| D ]\}	}|d|	 d| d7 }q!q|d| d| d7 }qd| v rR|d7 }|| d 7 }|d7 }d| v re|d 7 }|| d 7 }|d7 }d!| v r|d"7 }t| d! ttfs~| d! g}
n| d! }
|
D ]}|d#| d$7 }q|S )%Ntags)zimage-classificationr   transformersz---
ztags:
z- 
zlibrary_name: r   r   z	license: licensez
apache-2.0license_namezlicense_name: license_linkzlicense_link: detailsDatasetz
datasets:
zPretrain Datasetz# Model card for descriptionz
## Model Details
z- **z:**
z  - r   z:** usagez
## Model Usage

comparisonz
## Model Comparison
citationz
## Citation
z
```bibtex
z
```
)rz   r@   rB   rA   loweritemsr   )r   r{   r   r   tdkvviki	citationscr-   r-   r:   r     sv   



r   c                 c   sR    | t krtV  | tkrtV  | t tfvr%| dr'| dd d V  dS dS dS )aI  Returns potential safetensors alternatives for a given filename.

    Use case:
        When downloading a model from the Huggingface Hub, we first look if a .safetensors file exists and if yes, we use it.
        Main use case is filename "pytorch_model.bin" => check for "model.safetensors" or "pytorch_model.safetensors".
    r   Nr   )r   r   HF_OPEN_CLIP_WEIGHTS_NAMEHF_OPEN_CLIP_SAFE_WEIGHTS_NAMEendswith)rI   r-   r-   r:   r     s   r   )r*   )TFN)TN)F)N)NN)NNF)	r   NNFFNNNr   )NrP   re   r   r1   	functoolsr   pathlibr   tempfiler   typingr   r   r   r   r	   r
   r   r   	torch.hubr   r   r   r   ImportErrorr   safetensors.torchr   r   r   typing_extensionsr   r   timm.models._pretrainedr   huggingface_hubr   r   r   r   r   r   huggingface_hub.utilsr   rY   	getLogger__name__r3   __all__r   r   r   r   r   r   boolr   rW   r    r!   ri   rl   r|   r"   r   r#   r   r   r   nnModuler   r   r   r$   r%   r   r   r-   r-   r-   r:   <module>   sj   $ 


		



"



)

%

4
	

@@