
    rh=                     ~   d Z ddlZddlZddlmZ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mZmZmZmZmZ d	d
lmZ d	dlmZmZ 	 ddlmZ ddlm Z  ddl!m"Z" dZ#ed   Z%dZ&dedefdZ' G d de      Z(dddede)de*fdZ+d1dedee%   fdZ,e'	 d1dedee*   dee%   dee*   fd        Z-e'd2d!e*d"e.de.fd#       Z/	 d1d$ee   dee*   dee%   defd%Z0	 d1dedee*   dee%   dee*   fd&Z1d2d!e*d"e.de.fd'Z2ded(ee*   de*fd)Z3d*e*dee*   fd+Z4d,e& d-e& d.jk                         Z6d/edefd0Z7y# e$$ r dZ#Y w xY w)3zKContains the 'hf cache' command group with 'scan' and 'delete' subcommands.    N)	Namespace_SubParsersActionwraps)mkstemp)AnyCallableIterableListLiteralOptionalUnion   )CachedRepoInfoCachedRevisionInfoCacheNotFoundHFCacheInfoscan_cache_dir   )BaseHuggingfaceCLICommand)ANSItabulate)inquirer)Choice)	SeparatorTFalphabeticallastUpdatedlastUsedsizeCANCEL_DELETIONfnreturnc                 .     t                fd       }|S )Nc                  6    t         st        d       | i |S )NzThe 'cache delete' command requires extra dependencies for the TUI.
Please run 'pip install huggingface_hub[cli]' to install them.
Otherwise, disable TUI using the '--disable-tui' flag.)_inquirer_py_availableImportError)argskwargsr"   s     l/var/www/html/ai-insurance-compliance-backend/venv/lib/python3.12/site-packages/huggingface_hub/cli/cache.py_innerz#require_inquirer_py.<locals>._inner2   s+    %I 
 4"6""    r   )r"   r+   s   ` r*   require_inquirer_pyr-   1   s     
2Y# # Mr,   c                   D    e Zd Zedefd       ZdeddfdZd Zd Z	d	 Z
y)
CacheCommandparserc                 
   | j                  dd      j                  dd      }j                  fd       |j                  d	d
      }|j                  dt        d d       |j                  ddddd       |j                  t
        d	       |j                  dd      }|j                  dt        d d       |j                  ddd       |j                  ddg dd        |j                  t
        d       y )!NcachezManage local cache directory.)helpcache_commandzCache subcommands)destr3   c                 $    j                         S N)
print_help)r(   cache_parsers    r*   <lambda>z2CacheCommand.register_subcommand.<locals>.<lambda>F   s    L4K4K4M r,   )funcscanzScan cache directory.z--dirzMcache directory to scan (optional). Default to the default HuggingFace cache.)typedefaultr3   z-vz	--verbosecountr   zshow a more verbose output)actionr>   r3   )r;   r4   deletez*Delete revisions from the cache directory.zEcache directory (optional). Default to the default HuggingFace cache.z--disable-tui
store_truezrDisable Terminal User Interface (TUI) mode. Useful if your platform/terminal doesn't support the multiselect menu.)r@   r3   z--sort?r   zSort repositories by the specified criteria. Options: 'alphabetical' (A-Z), 'lastUpdated' (newest first), 'lastUsed' (most recent first), 'size' (largest first).)nargschoicesr3   )
add_parseradd_subparsersset_defaultsadd_argumentstrr/   )r0   cache_subparsersscan_parserdelete_parserr9   s       @r*   register_subcommandz CacheCommand.register_subcommand@   sB   ((7V(W'66ORe6f 	!!'M!N '11&?V1W  `	 	! 	
 	  - 	! 	
 	  l& I(33HCo3p""X	 	# 	
 	"" E	 	# 	
 	""G* 	# 	
 	""H"Mr,   r(   r#   Nc                     || _         t        |dd      | _        t        |dd       | _        t        |dd      | _        t        |dd       | _        t        |dd       | _        y )Nverboser   dirdisable_tuiFsortr4   )r(   getattr	verbosity	cache_dirrR   sort_byr4   )selfr(   s     r*   __init__zCacheCommand.__init__u   s[    	%dIq9(/eT(B!(}e!D29$2M,3D/4,Pr,   c                     | j                   dk(  r| j                          y | j                   dk(  r| j                          y t        d       y )Nr<   rA   zDPlease specify a cache subcommand (scan or delete). Use -h for help.)r4   	_run_scan_run_deleteprint)rX   s    r*   runzCacheCommand.run}   s=    'NN8+XYr,   c           
      $   	 t        j                          }t        | j                        }t        j                          }t	        t        || j                               t	        dt        ||z
  d       dt        |j                         dt        j                  |j                         d       t        |j                        dkD  rd	t        |j                         d
}| j                  dk\  rNt	        t        j                  |             |j                  D ]   }t	        t        j                  |             " y t	        t        j                  |dz                y y # t        $ r$}|j                  }t	        d|        Y d }~y d }~ww xY w)NzCache directory not found: rU   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.)timer   rV   r   r]   	get_tablerU   roundlenreposr   redsize_on_disk_strwarningsgray)rX   t0hf_cache_infot1excrV   messagewarnings           r*   r[   zCacheCommand._run_scan   sO   	B*4>>:MB
 	i@ArBw*+;s=;N;N7O6P Q#xx(F(FGHK	
 }%%&*S!7!7899TUG~~"dii(),55 .G$))G,-. dii*G GHI +  	I/	{;<	s   =E" "	F+F

Fc           	      2   t        | j                        }| j                  rt        |g | j                        }nt        |g | j                        }t        |      dkD  rt        |vrt        ||      dz   }| j                  rt        |      }nt        |      }|rp |j                  | }t        d       |j                          t        dt        |j                         dt        |j                         d|j                    d       y t        d	       y )
N)preselectedrW   r   z Confirm deletion ?zStart deletion.zDone. Deleted z repo(s) and z revision(s) for a total of ra   z"Deletion is cancelled. Do nothing.)r   rV   rR   _manual_review_no_tuirW   _manual_review_tuirf   _CANCEL_DELETION_STR_get_expectations_str_ask_for_confirmation_no_tui_ask_for_confirmation_tuidelete_revisionsr]   executerg   	snapshotsexpected_freed_size_str)rX   rm   selected_hashesconfirm_message	confirmedstrategys         r*   r\   zCacheCommand._run_delete   s   &t~~63Mr[_[g[ghO0BX\XdXdeO!#(<O(S3M?SVkkO8I	5oF	9=99?K'(  "$S%8$9 :H../0 1 889<
 23r,   )__name__
__module____qualname__staticmethodr   rN   r   rY   r^   r[   r\    r,   r*   r/   r/   ?   sG    2N$5 2N 2NhQY Q4 QZJ.4r,   r/   r`   rm   rU   c                L   |dk(  rt        t        | j                  d       D cg c]  }|j                  |j                  dj                  |j                        |j                  |j                  |j                  dj                  t        |j                              t        |j                        g c}g d      S t        t        | j                  d       D cg c]  }t        |j                  d	       D ]  }|j                  |j                  |j                  dj                  |j                        |j                  |j                  dj                  t        |j                              t        |j                         g  c}}g d
      S c c}w c c}}w )Nr   c                     | j                   S r7   	repo_pathrepos    r*   r:   zget_table.<locals>.<lambda>   
     r,   keyz{:>12}, )REPO ID	REPO TYPESIZE ON DISKNB FILESLAST_ACCESSEDLAST_MODIFIEDREFS
LOCAL PATH)rowsheadersc                     | j                   S r7   r   r   s    r*   r:   zget_table.<locals>.<lambda>   r   r,   c                     | j                   S r7   )commit_hashrevisions    r*   r:   zget_table.<locals>.<lambda>   s    HL`L` r,   )r   r   REVISIONr   r   r   r   r   )r   sortedrg   repo_id	repo_typeformatri   nb_fileslast_accessed_strlast_modified_strjoinrefsrJ   r   	revisionsr   snapshot_path)rm   rU   r   r   s       r*   rd   rd      sh   A~ #=#6#6<WX  LLNNOOD$9$9:MM****IIfTYY/0'		
 	
4  #=#6#6<WX  &t~~;` a  LLNN((OOH$=$=>%%..IIfX]]34../			
 	
34s   BF!B.F 
r   rW   c                    |dk(  r&| j                   | j                  j                         fS |dk(  rt        d | j                  D               S |dk(  r| j
                   S |dk(  r| j                   S | j                   | j                  fS )Nr   r   c              3   4   K   | ]  }|j                     y wr7   last_modified).0revs     r*   	<genexpr>z(_get_repo_sorting_key.<locals>.<genexpr>   s     @#C%%@s   r   r    )r   r   lowermaxr   last_accessedsize_on_diskr   rW   s     r*   _get_repo_sorting_keyr      s    .  2 2 455	M	!@@@@@	J	""""	F	!!!!--r,   rs   c                     t         j                  ||      }t        j                  d|ddt	         |D cg c],  }t        |t              s|j                  s!|j                  . c}      dd        fd	}j                  d
   j                  d|i       	 j                         S c c}w # t        $ r g cY S w xY w)N)rg   rs   rW   zSelect revisions to delete:Fd   r~   zWPress <space> to select, <enter> to validate and <ctrl+c> to quit without modification.c                     t        |        dS )Nz revision(s) selected.)rf   )results    r*   r:   z$_manual_review_tui.<locals>.<lambda>  s    c&k]2H#I r,   )rp   rE   cycleheightinstructionlong_instructiontransformerc                     t        j                  j                  D cg c]  }|d   s	|d    c}      _        y c c}w )Nenabledvaluer   )rw   content_controlrE   _instruction)_choicecheckboxrm   s     r*   _update_expectationsz0_manual_review_tui.<locals>._update_expectations  s=     5;C;S;S;[;[q_efo_pVG_q!
qs
   
A 
A 
toggler;   )_get_tui_choices_from_scanrg   r   r   rw   
isinstancer   r   r   kb_func_lookupappendr{   KeyboardInterrupt)rm   rs   rW   rE   cr   r   s   `     @r*   ru   ru      s     )}/B/BP[elmG  -)W+l
STV\H]bcbkbkAGG+l
 sI
H
 H%,,f6J-KL!! ,m  	s#   B0B0B0 B5 5CCrp   r>   c                 L    t        j                  | |      j                         S )Nr>   )r   confirmr{   )rp   r>   s     r*   ry   ry     s    GW5==??r,   rg   c                 f   g }|j                  t        t        dd             t        | fd      }|D ]  }|j                  t	        d|j
                  j                          d|j                   d|j                   d	|j                   d
	             t        |j                  t              D ]z  }|j                  t        |j                  |j                  d d  ddj                  t        |j                              xs d d|j                   |j                  |v              |  |S )Nz=None of the following (if selected, nothing will be deleted).F)namer   c                     t        |       S r7   r   r   s    r*   r:   z,_get_tui_choices_from_scan.<locals>.<lambda>&  s    2Gg2V r,   r   
  (, used )   z: r   
(detached) # modified )r   r   rv   r   r   r   
capitalizer   ri   r   r   _revision_sorting_orderr   r   r   r   )rg   rs   rW   rE   sorted_reposr   r   s     `    r*   r   r     s<    35GNN 'fpu	

 %%VWL T^^..014<<.4CXCXBYY`aeawaw`xxyz	

 t~~3JK 		HNN((#//34BtyyAV7W7g[g6hhtu}  vP  vP  uQ  R$00K?		  Nr,   c                 0   t        d      \  }}t        j                  |       g }t        | j                  fd      }|D ]  }|j                  d|j                  j                          d|j                   d|j                   d|j                   d		       t        |j                  t              D ]h  }|j                  |j                  |v rd
nd d|j                   ddj                  t        |j                              xs d d|j                           j  t#        |d      5 }	|	j%                  t&               |	j%                  dj                  |             d d d        dt)        j*                  |       d}
t-        dj                  d |
j/                         j1                  d      D                     	 t3        |      }t5        t7        | |      dz   d      rn't        j8                  |       t        |      S # 1 sw Y   xY w)Nz.txt)suffixc                     t        |       S r7   r   r   s    r*   r:   z'_manual_review_no_tui.<locals>.<lambda>@  s    @UVZ\c@d r,   r   z
# r   r   r   r    #z   z	 # Refs: r   r   r   wr   a*  
    TUI is disabled. In order to select which revisions you want to delete, please edit
    the following file using the text editor of your choice. Instructions for manual
    editing are located at the beginning of the file. Edit the file, save it and confirm
    to continue.
    File to edit: z
    c              3   <   K   | ]  }|j                           y wr7   )strip)r   lines     r*   r   z(_manual_review_no_tui.<locals>.<genexpr>S  s     NTDJJLNs   z Continue ?Fr   )r   oscloser   rg   r   r   r   r   ri   r   r   r   r   r   r   r   openwrite"_MANUAL_REVIEW_NO_TUI_INSTRUCTIONSr   boldr]   r   split_read_manual_review_tmp_filerx   rw   remove)rm   rs   rW   fdtmp_pathlinesr   r   r   finstructionsr~   s     `         r*   rt   rt   :  s+    &)LBHHRLE---3deL 4>>,,./qbAVAV@WW^_c_u_u^vvwx	
 t~~3JK 	HLL!--<2#Fc(J^J^I__himirirsy  {C  {H  {H  tI  jJ  jZ  NZ  i[  [g  hp  hB  hB  gC  D		 
h	 "	23			% !"
 99X&' (L 
$))N\-?-?-A-G-G-MN
NO
6x@'!-AMQ[`
   IIh/""%" "s   /6HHc                     d}d}d}||z   |fz   }| |rdndz   }	 t        |      j                         }||k(  r|S ||v ry||v ryt        d|        9)	N)yyes1)nno0r   z (Y/n) z (y/N) TFzInvalid input. Must be one of )inputr   r]   )rp   r>   YESNODEFAULTALLfull_messageanswers           r*   rx   rx   ^  s{    
C	BG
(gZ
C7i	BL
|$**,WNS=R<.se45 r,   r~   c                 h    t         |v ry | j                  | }t        |       d|j                   dS )NzNothing will be deleted.z! revisions selected counting for ra   )rv   rz   rf   r}   )rm   r~   r   s      r*   rw   rw   o  sA    .)-}--?H/"##DXEeEeDffghhr,   r   c                    t        |       5 }|j                         }d d d        j                  d      D cg c]  }|j                          }}|D cg c]  }|j	                  d      r| }}|D cg c]$  }|j                  d      d   j                         & }}|D cg c]  }t        |      dkD  s| c}S # 1 sw Y   xY wc c}w c c}w c c}w c c}w )Nr   r   r   )r   readr   r   
startswithrf   )r   r   contentr   r   selected_linesr~   hashs           r*   r   r   v  s    	h 1&&(&-mmD&9:dTZZ\:E:',ItDOOC4HdINI>LMdtzz#q)//1MOM,>TD	AD>> :IM>s.   C CC,C6)C%C9C C	a  
# INSTRUCTIONS
# ------------
# This is a temporary file created by running `hf cache delete --disable-tui`. It contains a set of revisions that can be deleted from your local cache directory.
#
# Please manually review the revisions you want to delete:
#   - Revision hashes can be commented out with '#'.
#   - Only non-commented revisions in this file will be deleted.
#   - Revision hashes that are removed from this file are ignored as well.
#   - If `aY  ` line is uncommented, the all cache deletion is cancelled and no changes will be applied.
#
# Once you've manually reviewed this file, please confirm deletion in the terminal. This file will be automatically removed once done.
# ------------

# KILL SWITCH
# ------------
# Un-comment following line to completely cancel the deletion process
# z,
# ------------

# REVISIONS
# ------------
r   c                     | j                   S r7   r   r   s    r*   r   r     s    !!!r,   r7   )T)8__doc__r   rc   argparser   r   	functoolsr   tempfiler   typingr   r	   r
   r   r   r   r   utilsr   r   r   r   r   r   r   
_cli_utilsr   r   
InquirerPyr   InquirerPy.base.controlr   InquirerPy.separatorr   r&   r'   SortingOption_Trv   r-   r/   intrJ   rd   r   ru   boolry   r   rt   rx   rw   r   r   r   r   r   r,   r*   <module>r     sP   R 	  1   J J J  ( &##..! KL( H  s4, s4l ?@ 5
[ 5
 5
C 5
p
. 
./9R 
. ]a-1#YAI/AZ	#Y : @s @T @T @ @
 cgN#26s)FNF_	< ^b!#!#-1#Y!#AI/AZ!#	#Y!#H6# 6 6 6"i itCy iUX i?3 ?49 ?	*  
  !  #&, 
EG- #2"&8 "S "]  #"#s   D2 2D<;D<