a
    h!                     @   s   d Z ddlZddlmZmZ ddlmZ ddlmZm	Z	m
Z
 ddlmZ dd	lmZmZmZ G d
d deZdde	eedddZdS )zContains command to scan the HF cache directory.

Usage:
    huggingface-cli scan-cache
    huggingface-cli scan-cache -v
    huggingface-cli scan-cache -vvv
    huggingface-cli scan-cache --dir ~/.cache/huggingface/hub
    N)	Namespace_SubParsersAction)Optional   )CacheNotFoundHFCacheInfoscan_cache_dir   )BaseHuggingfaceCLICommand)ANSIshow_deprecation_warningtabulatec                   @   sF   e Zd ZeedddZeddddZdd	 Ze	dd
ddZ
dS )ScanCacheCommand)parserc                 C   sD   | j ddd}|jdtd dd |jddd	d
dd |jtd d S )Nz
scan-cachezScan cache directory.)helpz--dirzMcache directory to scan (optional). Default to the default HuggingFace cache.)typedefaultr   z-vz	--verbosecountr   zshow a more verbose output)actionr   r   )func)
add_parseradd_argumentstrset_defaultsr   )r   Zscan_cache_parser r   _/var/www/html/assistant/venv/lib/python3.9/site-packages/huggingface_hub/commands/scan_cache.pyregister_subcommand"   s    z$ScanCacheCommand.register_subcommandN)argsreturnc                 C   s   |j | _|j| _d S N)verbose	verbositydir	cache_dir)selfr   r   r   r   __init__5   s    zScanCacheCommand.__init__c              
   C   s  t dd zt }t| j}t }W n: tyb } z"|j}td|  W Y d }~d S d }~0 0 | | tdt|| d dt|j	 dt
|j d t|jd	krd
t|j d}| jdk rtt
| |jD ]}tt
| qntt
|d  d S )Nzhuggingface-cli scan-cachezhf cache scanzCache directory not found: z	
Done in r	   zs. Scanned z repo(s) for a total of .r   zGot z warning(s) while scanning.   z Use -vvv to print details.)r   timer   r#   r   print_print_hf_cache_info_as_tableroundlenreposr   redsize_on_disk_strwarningsr!   gray)r$   t0hf_cache_infot1excr#   messagewarningr   r   r   run9   s,    




zScanCacheCommand.run)r3   r   c                 C   s   t t|| jd d S )Nr!   )r)   	get_tabler!   )r$   r3   r   r   r   r*   T   s    z.ScanCacheCommand._print_hf_cache_info_as_table)__name__
__module____qualname__staticmethodr   r   r   r%   r8   r   r*   r   r   r   r   r   !   s
   r   r9   )r3   r!   r   c                C   s\   |dkr0t dd t| jdd dD g ddS t d	d t| jd
d dD g ddS dS )a:  Generate a table from the [`HFCacheInfo`] object.

    Pass `verbosity=0` to get a table with a single row per repo, with columns
    "repo_id", "repo_type", "size_on_disk", "nb_files", "last_accessed", "last_modified", "refs", "local_path".

    Pass `verbosity=1` to get a table with a row per repo and revision (thus multiple rows can appear for a single repo), with columns
    "repo_id", "repo_type", "revision", "size_on_disk", "nb_files", "last_modified", "refs", "local_path".

    Example:
    ```py
    >>> from huggingface_hub.utils import scan_cache_dir
    >>> from huggingface_hub.commands.scan_cache import get_table

    >>> hf_cache_info = scan_cache_dir()
    HFCacheInfo(...)

    >>> print(get_table(hf_cache_info, verbosity=0))
    REPO ID                                             REPO TYPE SIZE ON DISK NB FILES LAST_ACCESSED LAST_MODIFIED REFS LOCAL PATH
    --------------------------------------------------- --------- ------------ -------- ------------- ------------- ---- --------------------------------------------------------------------------------------------------
    roberta-base                                        model             2.7M        5 1 day ago     1 week ago    main C:\Users\admin\.cache\huggingface\hub\models--roberta-base
    suno/bark                                           model             8.8K        1 1 week ago    1 week ago    main C:\Users\admin\.cache\huggingface\hub\models--suno--bark
    t5-base                                             model           893.8M        4 4 days ago    7 months ago  main C:\Users\admin\.cache\huggingface\hub\models--t5-base
    t5-large                                            model             3.0G        4 5 weeks ago   5 months ago  main C:\Users\admin\.cache\huggingface\hub\models--t5-large

    >>> print(get_table(hf_cache_info, verbosity=1))
    REPO ID                                             REPO TYPE REVISION                                 SIZE ON DISK NB FILES LAST_MODIFIED REFS LOCAL PATH
    --------------------------------------------------- --------- ---------------------------------------- ------------ -------- ------------- ---- -----------------------------------------------------------------------------------------------------------------------------------------------------
    roberta-base                                        model     e2da8e2f811d1448a5b465c236feacd80ffbac7b         2.7M        5 1 week ago    main C:\Users\admin\.cache\huggingface\hub\models--roberta-base\snapshots\e2da8e2f811d1448a5b465c236feacd80ffbac7b
    suno/bark                                           model     70a8a7d34168586dc5d028fa9666aceade177992         8.8K        1 1 week ago    main C:\Users\admin\.cache\huggingface\hub\models--suno--bark\snapshots\70a8a7d34168586dc5d028fa9666aceade177992
    t5-base                                             model     a9723ea7f1b39c1eae772870f3b547bf6ef7e6c1       893.8M        4 7 months ago  main C:\Users\admin\.cache\huggingface\hub\models--t5-base\snapshots\a9723ea7f1b39c1eae772870f3b547bf6ef7e6c1
    t5-large                                            model     150ebc2c4b72291e770f58e6057481c8d2ed331a         3.0G        4 5 months ago  main C:\Users\admin\.cache\huggingface\hub\models--t5-large\snapshots\150ebc2c4b72291e770f58e6057481c8d2ed331a                                                 ```
    ```

    Args:
        hf_cache_info ([`HFCacheInfo`]):
            The HFCacheInfo object to print.
        verbosity (`int`, *optional*):
            The verbosity level. Defaults to 0.

    Returns:
        `str`: The table as a string.
    r   c                 S   sD   g | ]<}|j |jd |j|j|j|jdt|j	t
|jgqS ){:>12}, )repo_id	repo_typeformatr/   nb_filesZlast_accessed_strlast_modified_strjoinsortedrefsr   	repo_path).0repor   r   r   
<listcomp>   s   
zget_table.<locals>.<listcomp>c                 S   s   | j S r   rI   rK   r   r   r   <lambda>       zget_table.<locals>.<lambda>key)REPO ID	REPO TYPESIZE ON DISKNB FILESZLAST_ACCESSEDLAST_MODIFIEDREFS
LOCAL PATH)rowsheadersc                 S   s\   g | ]T}t |jd d dD ]<}|j|j|jd|j|j|jd	t |j
t|jgqqS )c                 S   s   | j S r   )commit_hash)revisionr   r   r   rO      rP   z&get_table.<locals>.<listcomp>.<lambda>rQ   r?   r@   )rG   Z	revisionsrA   rB   r\   rC   r/   rD   rE   rF   rH   r   Zsnapshot_path)rJ   rK   r]   r   r   r   rL      s   
c                 S   s   | j S r   rM   rN   r   r   r   rO      rP   )rS   rT   ZREVISIONrU   rV   rW   rX   rY   N)r   rG   r-   )r3   r!   r   r   r   r:   X   s    +r:   )__doc__r(   argparser   r   typingr   utilsr   r   r    r
   Z
_cli_utilsr   r   r   r   intr   r:   r   r   r   r   <module>   s   	7