o
    ei3                     @   sR  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Z7e8ee!Z9ee#Z:ej;ddd e:D e<dZ=eee< ej>dd?e9 de4e9df Z@e6ddZAeAjBdddgddddddde-jCddf	de0d e)d!e*d"eee= ej>d#df d$e,d%e@d&e+d'e.d(e1d)dfd*d+ZDeAjBd,d-d.gd			dMd/ee<ejEd0df d1e/d%e@d(e1d)df
d2d3ZFeAjBd4g d5d					dNd/ee<ejEd0df d6eee< ejEd7df d8eee< ej>d9d:d;df d<eeGej>d=df d>eeGej>d?df d(e1d)dfd@dAZHdBe"d/e<dCe<dDee< d(ee< d)dfdEdFZIdGZJejKd)ee< fdHdIZLdDe<d)eeMedJ f fdKdLZNdS )Oa  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                 C   s   i | ]}||qS  r&   ).0sr&   r&   X/home/ubuntu/transcripts/venv/lib/python3.10/site-packages/huggingface_hub/cli/spaces.py
<dictcomp>H   s    r*   )typezMComma-separated properties to expand. Example: '--expand=likes,tags'. Valid: , .)helpcallbackz Interact with spaces on the Hub.)r.   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	              	   C   sJ   t |d}	|r
|jnd}
dd |	j||| |
||dD }t|||d dS )zList spaces on the Hub.r;   Nc                 S   s   g | ]}t |qS r&   )r    )r'   
space_infor&   r&   r)   
<listcomp>o   s    zspaces_ls.<locals>.<listcomp>)r5   r4   r3   r6   r7   r8   )r9   r:   )r!   valuelist_spacesr#   )r3   r4   r5   r6   r7   r8   r9   r:   r;   apisort_keyresultsr&   r&   r)   	spaces_lsW   s   
rE   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              
   C   s   t |d}z
|j| ||d}W n, ty$ } z	td|  d|d}~w ty; } ztd| d|  d|d}~ww ttjt|d	d
 dS )z"Get info about a space on the Hub.r=   )repo_idrH   r8   zSpace 'z' not found.Nz
Revision 'z' not found on 'z'.   )indent)	r!   r>   r   r   r   printjsondumpsr    )rG   rH   r8   r;   rB   rF   er&   r&   r)   spaces_infox   s   
rP   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              
   C   s  t jdt jjd t|d}|sU|| }|jdkr#td|j d|j }du r1td|  |j	 }	du r@td	|  d
t
|	t
tk rUtdt d|	 d|rb|}
|du r_|n|}nt|r|sz
|jd| dd W n ty } z	td|  d|d}~ww t }tj|j|}
t  }st  ztd| ||jd W |st  n|st  w w t|
}|dkrtd|dkrtd|dkrtd|
 ntd|jd| |
|dd}|st|| |j|r|
ntj|
|d 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)fgr=   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_typerI   writezWrite access check to z8 repository failed. Make sure that you are authenticated)rY   rI   rQ   	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)rY   rI   path_or_fileobjpath_in_repo_hot_reload)rB   rG   
commit_sha
local_pathr;   )typersechocolorsBRIGHT_BLACKr!   r>   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)rG   rQ   rR   rS   rT   r;   rB   r>   rh   ri   rb   rO   temp_dirpbar_disabled
editor_rescommit_infor&   r&   r)   spaces_hot_reload   s   .






r}   rB   ra   rb   c                    sD  |  |}|j }d u rtd| d|j }d u r#td| d|jdkr3td|j d S |j }d u r>td|j	 }	d u rItdd	t
d
tf fdddtd
d ffdd}
t|||	dd |jD |dD ]1}|d dkr}|
|d  qn|d dkrtd|d  d qn|d dkrtd qnt| qnd 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                    sh   d} d ur|  d7 }| d | d kr"|d| d d  7 }|S |d| d d  d| d  7 }|S )	N r,   	startLineendLinezline r   zlines -r&   )r   res)rb   r&   r)   render_region  s    z1_spaces_hot_reload_summary.<locals>.render_regioneventc                    s  | d d dkrt jddd t j| d d dd d S | d d d	krAt jd
 | d d  dd t j| d d dd d S | d d dkr`t jd| d d  d| d d  dd d S | d d dkrt jd| d d  d| d d  dd d S | d d dkrt jd| d d  d| d d  dd d S | d d dkrt jd | d d  dd t j| d d dd d S | d d dkr| d d rt jddd d S t jd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)rc   rd   r   )r   )r   r&   r)   display_event%  s(    ... z1_spaces_hot_reload_summary.<locals>.display_eventc                 S   s   g | ]\}}|qS r&   r&   )r'   hash_r&   r&   r)   r?   A  s    z._spaces_hot_reload_summary.<locals>.<listcomp>)ra   host	subdomainreplica_hashesr;   r   replicaHashz---- Replica r   z ----	fullMatchu   ✔︎ Same as first replica)r>   runtimer   hot_reloadingstatusrc   echoreplica_statusesr   r   r   strr
   r	   rd   r   )rB   rG   ra   rb   r;   r>   r   r   
space_hostspace_subdomainr   replica_stream_eventr&   )rb   r   r)   rv     s:   




rv   ))codezcode --wait)nvimr   )nanor   )vimr   )vir   c                  C   sN   dD ]} t | d  }r|  S qtD ]\}}t|d ur$|  S qd S )N)	HF_EDITORVISUALEDITORr   )rp   getenvstripPREFERRED_EDITORSshutilwhich)envcommandbinary_patheditor_commandr&   r&   r)   _get_editor_commandW  s   r   )r\   r]   c                 C   sP   t j r
t j sdS t  }d u rdS g t|| }tj|dd}|j	S )Nr\   r]   T)start_new_session)
sysstdinisattystdoutr   shlexsplit
subprocessr   
returncode)rb   r   r   r   r&   r&   r)   rt   b  s   rt   )NNN)NNFFN)O__doc__enum	functoolsrM   rp   r   r   r   r   rn   typingr   r   r   r   r   rc   	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$   rl   sorted_EXPAND_PROPERTIES_SORT_OPTIONSEnumr   r%   Optionrr   	ExpandOpt
spaces_clir   tablerE   ArgumentrP   boolr}   rv   r   cacher   intrt   r&   r&   r&   r)   <module>   s6  @
	
	
	n
H	"
