o
    iO"                     @   s   d dl Z d dlZd dlmZ d dlmZmZmZmZ d dl	Z	ddl
mZ dZG dd dZG d	d
 d
ZG dd dZG dd dZG dd dZdS )    N)
expanduser)DictListOptionalTuple   )
REPO_TYPESzhttps://huggingface.coc                   @   s   e Zd ZdZdd ZdS )RepoObjzl
    HuggingFace git-based system, data structure that represents a file belonging to the current user.
    c                 K   s"   |  D ]
\}}t| || qd S N)itemssetattr)selfkwargskv r   Q/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/huggingface_hub/hf_api.py__init__#   s   zRepoObj.__init__N)__name__
__module____qualname____doc__r   r   r   r   r   r	      s    r	   c                   @   s   e Zd ZdZdefddZdS )	ModelFileze
    Data structure that represents a public file inside a model, accessible from huggingface.co
    	rfilenamec                 K   s(   || _ | D ]
\}}t| || qd S r
   )r   r   r   )r   r   r   r   r   r   r   r   r   -   s   zModelFile.__init__N)r   r   r   r   strr   r   r   r   r   r   (   s    r   c                   @   sR   e Zd ZdZddg ddfdee dee dee dee deee  f
dd	ZdS )
	ModelInfozB
    Info about a public model accessible from huggingface.co
    NmodelIdshatagspipeline_tagsiblingsc           	      K   sV   || _ || _|| _|| _|d urdd |D nd | _| D ]
\}}t| || qd S )Nc                 S      g | ]	}t d i |qS r   )r   .0xr   r   r   
<listcomp>H       z&ModelInfo.__init__.<locals>.<listcomp>)r   r   r   r   r    r   r   )	r   r   r   r   r   r    r   r   r   r   r   r   r   8   s   zModelInfo.__init__)	r   r   r   r   r   r   r   r   r   r   r   r   r   r   3   s(    r   c                   @   s<  e Zd Zd#ddZdededefddZd	edeeee f fd
dZd	eddfddZ	d#de
e dee fddZdee fddZ	d$dede
e d	e
e defddZ	d#d	ede
e dee fddZ					d%d	edede
e de
e de
e de
e defdd Z		d$d	edede
e de
e fd!d"ZdS )&HfApiNc                 C   s   |d ur	|| _d S t | _d S r
   )ENDPOINTendpoint)r   r*   r   r   r   r   O   s   zHfApi.__init__usernamepasswordreturnc                 C   s8   d | j}tj|||dd}|  | }|d S )z
        Call HF API to sign in a user and get a token if credentials are valid.

        Outputs: token if credentials are valid

        Throws: requests.exceptions.HTTPError if credentials are invalid
        z{}/api/login)r+   r,   )jsontoken)formatr*   requestspostraise_for_statusr.   )r   r+   r,   pathrdr   r   r   loginR   s
   zHfApi.loginr/   c                 C   sD   d | j}tj|dd |id}|  | }|d |d fS )z.
        Call HF API to know "whoami"
        z{}/api/whoamiauthorization	Bearer {}headersuserorgsr0   r*   r1   getr3   r.   )r   r/   r4   r5   r6   r   r   r   whoami`   s
   zHfApi.whoamic                 C   s0   d | j}tj|dd |id}|  dS )z)
        Call HF API to log out.
        z{}/api/logoutr8   r9   r:   N)r0   r*   r1   r2   r3   )r   r/   r4   r5   r   r   r   logoutj   s   zHfApi.logoutfilterc                 C   sN   d | j}|dur|ddnd}tj||d}|  | }dd |D S )zI
        Get the public list of all the models on huggingface.co
        z{}/api/modelsNT)rB   full)paramsc                 S   r!   r"   )r   r#   r   r   r   r&   {   r'   z%HfApi.list_models.<locals>.<listcomp>r>   )r   rB   r4   rD   r5   r6   r   r   r   list_modelsr   s   zHfApi.list_modelsc                 C   s   t d |  S )z
        Deprecated method name, renamed to `list_models`.

        Get the public list of all the models on huggingface.co
        zfThis method has been renamed to `list_models` for consistency and will be removed in a future version.)warningswarnrE   )r   r   r   r   
model_list}   s   zHfApi.model_listrepo_idrevisionc                 C   sp   |du rdj | j|dndj | j||d}|dur dd |ind}tj||d}|  | }td	i |S )
z
        Get info on one specific model on huggingface.co

        Model can be private if you pass an acceptable token.
        Nz{}/api/models/{repo_id})rI   z+{}/api/models/{repo_id}/revision/{revision})rI   rJ   r8   r9   r:   r   )r0   r*   r1   r?   r3   r.   r   )r   rI   rJ   r/   r4   r;   r5   r6   r   r   r   
model_info   s   
zHfApi.model_infoorganizationc                 C   sX   d | j}|durd|ind}tj||dd |id}|  | }dd |D S )	z
        HuggingFace git-based system, used for models.

        Call HF API to list all stored files for user (or one of their organizations).
        z{}/api/repos/lsNrL   r8   r9   )rD   r;   c                 S   r!   r"   )r	   r#   r   r   r   r&      r'   z)HfApi.list_repos_objs.<locals>.<listcomp>r>   )r   r/   rL   r4   rD   r5   r6   r   r   r   list_repos_objs   s   zHfApi.list_repos_objsFnameprivate	repo_typelfsmultipartthreshc                 C   s   d | j}|tvrtd|||d}	|dur||	d< |dur$||	d< tj|dd |i|	d	}
|r@|
jd
kr@|
 }|d S |
  |
 }|d S )a  
        HuggingFace git-based system, used for models.

        Call HF API to create a whole repo.

        Params:
            private: Whether the model repo should be private (requires a paid huggingface.co account)

            repo_type: Set to "dataset" if creating a dataset, default is model

            exist_ok: Do not raise an error if repo already exists

            lfsmultipartthresh: Optional: internal param for testing purposes.

        Returns:
            URL to the newly created repo.
        z{}/api/repos/createInvalid repo type)rN   rL   rO   NtyperQ   r8   r9   r;   r.   i  url)	r0   r*   r   
ValueErrorr1   r2   status_coder.   r3   )r   r/   rN   rL   rO   rP   exist_okrQ   r4   r.   r5   r6   r   r   r   create_repo   s&   zHfApi.create_repoc                 C   s\   d | j}|tvrtd||d}|dur||d< tj|dd |i|d}|  dS )	z
        HuggingFace git-based system, used for models.

        Call HF API to delete a whole repo.

        CAUTION(this is irreversible).
        z{}/api/repos/deleterR   )rN   rL   NrS   r8   r9   rT   )r0   r*   r   rV   r1   deleter3   )r   r/   rN   rL   rP   r4   r.   r5   r   r   r   delete_repo   s   
zHfApi.delete_repor
   )NN)NNNFN)r   r   r   r   r   r7   r   r   r@   rA   r   r   rE   rH   rK   r	   rM   boolintrY   r[   r   r   r   r   r(   N   sp    



	
5r(   c                   @   s8   e Zd ZedZedd Zedd Zedd ZdS )	HfFolderz~/.huggingface/tokenc                 C   sT   t jt j| jdd t| jd}|| W d   dS 1 s#w   Y  dS )z8
        Save token, creating folder as needed.
        T)rX   zw+N)osmakedirsr4   dirname
path_tokenopenwrite)clsr/   fr   r   r   
save_token  s   "zHfFolder.save_tokenc                 C   sR   zt | jd}| W  d   W S 1 sw   Y  W dS  ty(   Y dS w )z4
        Get token or None if not existent.
        r5   N)rc   rb   readFileNotFoundError)re   rf   r   r   r   	get_token  s   (zHfFolder.get_tokenc                 C   s(   z	t | j W dS  ty   Y dS w )zD
        Delete token. Do not fail if token does not exist.
        N)r_   removerb   ri   )re   r   r   r   delete_token  s
   zHfFolder.delete_tokenN)	r   r   r   r   rb   classmethodrg   rj   rl   r   r   r   r   r^      s    


r^   )r_   rF   os.pathr   typingr   r   r   r   r1   	constantsr   r)   r	   r   r   r(   r^   r   r   r   r   <module>   s   
 3