
    Zi3                        d Z ddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
mZmZmZmZm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 ddlmZmZmZ dd	lmZ dd
l m!Z!m"Z"m#Z# ddl$m%Z%m&Z&m'Z' ddl(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6 dZ7 e8 ee!                    Z9 ee#          Z: ej;        dd e:D             e<          Z=eee<          ej>        dd?                    e9           d e4e9                    f         Z@ e6d          ZAeAB                    dddg          dddddde-jC        ddf	de0de)d e*d!eee=          ej>        d"          f         d#e,d$e@d%e+d&e.d'e1d(dfd)            ZDeAB                    d*d+d,g          	 	 	 dFd-ee< ejE        d.          f         d/e/d$e@d'e1d(df
d0            ZFeAB                    d1g d2          	 	 	 	 	 dGd-ee< ejE        d.          f         d3eee<          ejE        d4          f         d5eee<          ej>        d6d7d8          f         d9eeG ej>        d:          f         d;eeG ej>        d<          f         d'e1d(dfd=            ZHd>e"d-e<d?e<d@ee<         d'ee<         d(dfdAZIdBZJejK        d(ee<         fdC            ZLd@e<d(eeMedD         f         fdEZNdS )Ha  Contains commands to interact with spaces on the Hugging Face Hub.

Usage:
    # list spaces on the Hub
    hf spaces ls

    # list spaces with a search query
    hf spaces ls --search "chatbot"

    # get info about a space
    hf spaces info enzostvs/deepsite
    N)	AnnotatedLiteralOptionalUnionget_args)version)assert_never)multi_replica_reload_events)ApiGetReloadEventSourceDataReloadRegion)CLIErrorRepositoryNotFoundErrorRevisionNotFoundError)hf_hub_download)ExpandSpaceProperty_THfApiSpaceSort_T)are_progress_bars_disableddisable_progress_barsenable_progress_bars   )	AuthorOpt	FilterOpt	FormatOptLimitOptOutputFormatQuietOptRevisionOpt	SearchOptTokenOptapi_object_to_dict
get_hf_apimake_expand_properties_parserprint_list_outputtyper_factoryz6.1.0SpaceSortEnumc                     i | ]}||S  r(   ).0ss     U/root/projects/butler/venv/lib/python3.11/site-packages/huggingface_hub/cli/spaces.py
<dictcomp>r,   H   s    +H+H+HQAq+H+H+H    )typezMComma-separated properties to expand. Example: '--expand=likes,tags'. Valid: , .)helpcallbackz Interact with spaces on the Hub.)r1   lszhf spaces ls --limit 10z4hf spaces ls --search "chatbot" --author huggingface)examples
   FsearchauthorfiltersortzSort results.limitexpandformatquiettokenreturnc	           	          t          |          }	|r|j        nd}
d |	                    ||| |
||          D             }t          |||           dS )zList spaces on the Hub.r>   Nc                 ,    g | ]}t          |          S r(   )r!   )r)   
space_infos     r+   
<listcomp>zspaces_ls.<locals>.<listcomp>o   s0        	:&&  r-   )r8   r7   r6   r9   r:   r;   )r<   r=   )r"   valuelist_spacesr$   )r6   r7   r8   r9   r:   r;   r<   r=   r>   apisort_keyresultss               r+   	spaces_lsrJ   W   s    , 5
!
!
!C!+tzztH //&he\b * 
 
  G gfE::::::r-   infoz hf spaces info enzostvs/deepsitez>hf spaces info gradio/theme_builder --expand sdk,runtime,likesspace_idz)The space ID (e.g. `username/repo-name`).revisionc                 N   t          |          }	 |                    | ||          }nJ# t          $ r}t          d|  d          |d}~wt          $ r}t          d| d|  d          |d}~ww xY wt          t          j        t          |          d	
                     dS )z"Get info about a space on the Hub.rA   )repo_idrM   r;   zSpace 'z' not found.Nz
Revision 'z' not found on 'z'.   )indent)	r"   rC   r   r   r   printjsondumpsr!   )rL   rM   r;   r>   rG   rK   es          r+   spaces_inforV   x   s     5
!
!
!CS~~h&~QQ" @ @ @777788a?  S S SJHJJhJJJKKQRRS	$*'--a
8
8
899999s    + 
A2A		A2A--A2z
hot-reload)zshf spaces hot-reload username/repo-name app.py               # Open an interactive editor to the remote app.py filezhf spaces hot-reload username/repo-name -f app.py            # Take local version from ./app.py and patch app.py in remote repozhf spaces hot-reload username/repo-name app.py -f src/app.py # Take local version from ./src/app.py and patch app.py in remote repofilenamez~Path to the Python file in the Space repository. Can be omitted when --local-file is specified and path in repository matches.
local_filez--local-filez-fz<Path of local file. Interactive editor mode if not specifiedskip_checksz%Skip hot-reload compatibility checks.skip_summaryz2Skip summary display after hot-reload is triggeredc                 &   t          j        dt           j        j                   t	          |          }|s|                    |           }|j        dk    rt          d|j         d          |j        x}t          d|            |j	        x}	t          d	|  d
          t          j        |	          t          j        t                    k     rt          dt           d|	 d          |r
|}
||n|}n<|r*|s@	 |                    d| d           n&# t          $ r}t          d|  d          |d}~ww xY wt!          j                    }t$          j                            |j        |          }
t-                      x}st/                       	 t1          d| ||j                   |st3                       n# |st3                       w w xY wt5          |
          }|dk    rt          d          |dk    rt          d          |dk    rt          d|
           nt          d          |                    d| |
|d          }|s<t9          || |j        |r|
nt$          j                            |
          |           dS dS )u  
    Hot-reload any Python file of a Space without a full rebuild + restart.

    ⚠ This feature is experimental ⚠

    Only works with Gradio SDK (6.1+)
    Opens an interactive editor unless --local-file/-f is specified.

    This command patches the live Python process using https://github.com/breuleux/jurigged
    (AST-based diffing, in-place function updates, etc.), integrated with Gradio's native hot-reload support
    (meaning that Gradio demo object changes are reflected in the UI)
    z2This feature is experimental and subject to change)fgrA   gradioz5Hot-reloading is only available on Gradio SDK. Found z SDKNz"Unable to read cardData for Space z Unable to read sdk_version from z	 cardDataz!Hot-reloading requires Gradio >= z (found )spaceT)	repo_typerO   writezWrite access check to z8 repository failed. Make sure that you are authenticated)r`   rO   rW   	local_dirno-ttyzICannot open an editor (no TTY). Use -f flag to hot-reload from local path	no-editorzONo editor found in local environment. Use -f flag to hot-reload from local pathr   z>Editor returned a non-zero exit code while attempting to edit z4Either filename or --local-file/-f must be specified)r`   rO   path_or_fileobjpath_in_repo_hot_reload)rG   rL   
commit_sha
local_pathr>   )typersechocolorsBRIGHT_BLACKr"   rC   sdkr   	card_datasdk_versionr   parseVersionHOT_RELOADING_MIN_GRADIO
auth_checkr   tempfileTemporaryDirectoryospathjoinnamer   r   r   r   _editor_openupload_file_spaces_hot_reload_summaryoidbasename)rL   rW   rX   rY   rZ   r>   rG   rC   ro   rp   ri   rU   temp_dirpbar_disabled
editor_rescommit_infos                   r+   spaces_hot_reloadr      sN   \ 
KDIbcccc
5
!
!
!C 	q^^H--
>X%%gS]Saggghhh#--I6JJJKKK$00K9QhQQQRRR=%%8P(Q(QQQo?Wooaloooppp %O
!)!1::x	 "O 
		%$     
 +   oXooo  .00W\\(-::
!;!=!== 	$!###		'! !"-	    ! '$&&& ! '$&&&&'!*--
!!fggg$$lmmm??h\fhhiii  MNNN//" "  K  
""%/QzzRW5E5Ej5Q5Q	
 	
 	
 	
 	
 	

 
s$   D 
D?&D::D?G GrG   rh   ri   c                    |                      |          }|j        x}t          d| d          |j        x}t          d| d          |j        dk    rt          j        d|j                   d S |j        x}t          d          |j	        x}	t          d          d	t          d
t          ffddt          d
d ffd}
t          |||	d |j        D             |          D ]{}|d         dk    r |
|d                     |d         dk    rt          j        d|d          d           K|d         dk    rt          j        d           lt          |           |d S )Nz!Unable to read SpaceRuntime from z infoszSpace z2 current running version has not been hot-reloadedcreatedzDFailed creating hot-reloaded commit. hot_reloading.replica_statuses=z)Unexpected None host on hotReloaded Spacez.Unexpected None subdomain on hotReloaded Spaceregionr?   c                     d}| dz  }| d         | d         k    r|d| d         dz
   z  }n|d| d         dz
   d| d          z  }|S )	N r/   	startLineendLinezline r   zlines -r(   )r   resri   s     r+   render_regionz1_spaces_hot_reload_summary.<locals>.render_region  s    !j$$$$C+&"33346+.2444CCIF;/!3IIfY6GIIIC
r-   eventc                    | d         d         dk    r:t          j        dd           t          j        | d         d         d           d S | d         d         d	k    rRt          j        d
 | d         d                    d           t          j        | d         d         d           d S | d         d         dk    r6t          j        d| d         d          d| d         d          d           d S | d         d         dk    r6t          j        d| d         d          d| d         d          d           d S | d         d         dk    r6t          j        d| d         d          d| d         d          d           d S | d         d         dk    rRt          j        d | d         d                    d           t          j        | d         d         d           d S | d         d         dk    r>| d         d         rt          j        dd           d S t          j        dd           d S t          | d         d                    d S )Ndatakinderroru"   ✘ Unexpected hot-reloading errorT)bold	traceback)italic	exceptionu   ✘ Exception at r   addu   ✔︎ Created 
objectName 
objectTypedeleteu   ∅ Deleted updateu   ✔︎ Updated runu   ▶ Run 	codeLinesuiupdatedu   ⟳ UI updatedu   ∅ UI untouched)rj   rk   r	   )r   r   s    r+   display_eventz1_spaces_hot_reload_summary.<locals>.display_event%  s   = G++K<4HHHHKfk24@@@@@@6]6"k11KTMM%-:Q,R,RTT[_````Kfk24@@@@@@6]6"e++Ke%-*EeefVbHceelpqqqqqq6]6"h..KbuV}\'BbbU6]S_E`bbimnnnnnn6]6"h..Ke%-*EeefVbHceelpqqqqqq6]6"e++KK==vx1H#I#IKKRVWWWWKfk24@@@@@@6]6"d**V}Y' ;,4888888.T::::::vv./////r-   c                     g | ]\  }}|S r(   r(   )r)   hash_s      r+   rD   z._spaces_hot_reload_summary.<locals>.<listcomp>A  s    KKKqKKKr-   )rh   host	subdomainreplica_hashesr>   r   replicaHashz---- Replica r   z ----	fullMatchu   ✔︎ Same as first replica)rC   runtimer   hot_reloadingstatusrj   echoreplica_statusesr   r   r   strr   r
   rk   r	   )rG   rL   rh   ri   r>   rC   r   r   
space_hostspace_subdomainr   replica_stream_eventr   s      `        @r+   r}   r}     s	    ))J%%.K8KKKLLL ..7\\\\]]]y((
\=;Y\\]]] o%
.BCCC%//8GHHHl s      08 0T 0 0 0 0 0 00 !<!KKM,JKKK! ! ! / /  '722M.w78888!&)]::KK(<V(DKKKLLLL!&)[88J56666-..../ /r-   ))codezcode --wait)nvimr   )nanor   )vimr   )vir   c                      dD ]/} t          j        | d                                          x}r|c S 0t          D ]\  }}t	          j        |          |c S d S )N)	HF_EDITORVISUALEDITORr   )rw   getenvstripPREFERRED_EDITORSshutilwhich)envcommandbinary_patheditor_commands       r+   _get_editor_commandr   W  s~    0  iR((..0007 	NNN	'8 " "#^<$$0!!!! 14r-   )rc   rd   c                    t           j                                        rt           j                                        sdS t	                      x}dS g t          j        |          | }t          j        |d          }|j	        S )Nrc   rd   T)start_new_session)
sysstdinisattystdoutr   shlexsplit
subprocessr   
returncode)ri   r   r   r   s       r+   r{   r{   b  s}    I 3:#4#4#6#6 x-///8{8N++8Z8G
.D
9
9
9C>r-   )NNN)NNFFN)O__doc__enum	functoolsrS   rw   r   r   r   r   ru   typingr   r   r   r   r   rj   	packagingr   typing_extensionsr	   "huggingface_hub._hot_reload.clientr
   !huggingface_hub._hot_reload.typesr   r   huggingface_hub.errorsr   r   r   huggingface_hub.file_downloadr   huggingface_hub.hf_apir   r   r   huggingface_hub.utilsr   r   r   
_cli_utilsr   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   rs   sorted_EXPAND_PROPERTIES_SORT_OPTIONSEnumr   r&   Optionry   	ExpandOpt
spaces_clir   tablerJ   ArgumentrV   boolr   r}   r   cacher   intr{   r(   r-   r+   <module>r      s          				       



  @ @ @ @ @ @ @ @ @ @ @ @ @ @        * * * * * * J J J J J J W W W W W W W W [ [ [ [ [ [ [ [ [ [ 9 9 9 9 9 9 L L L L L L L L L L i i i i i i i i i i                               $ #  VHH%:;;<< %%	/+H+H-+H+H+HsSSS SMEL}]a]f]fgy]z]z}}}../ABB  	 ] BCCC
 !>     	$*; ;;; ; /***	,	; ; ; ; ; ; 
; ; ; ;4 
*H    !	: :^U^1\]]]]^:: : 	:
 
: : : :"      ( 	 	_dmr/k
 k
<	
 	
 	
	k
  R	
 	
 	
	k
 O	
 	
 	
	k
* 43Z![![![[\+k
, D,%,4h"i"i"iij-k
. /k
0 
1k
 k
 k
 k
\E/	E/E/ E/ 	E/
 C=E/ 
E/ E/ E/ E/P  Xc]    S U38M0N+N%O      r-   