a
    d                     @  s   d dl mZ d dlZd dlZd dlZd dlZd dlZe Z	ej
e	dZej
ej
edZdddddd	Zd
dddZdd ZdS )    )annotationsNz	README.mdzdeploy_space_action.yamlz
str | Nonedict)titleapp_filereturnc              	   C  s  i }t jt}| d u r,td| dp*|} t| }|| krLtd| d ||d< |d u rt tD ]|}t jt|}t j	|rh|
dsqht|ddd	4}| }d
|v r|}W d     qW d    qh1 s0    Y  qhtd|rd| dnd dp
|}|r t j|s(td||d< d|d< tj|d< tt| tdddd tjD  dptd|d< i }	tddkrtd}
|
sqtd|
 d}||	|
< q|	|d < t jtd!}t j|s@td" dkr@td#}|sq@t|d$}||d%  W d    n1 s20    Y  qtd& dkr td'p^d(}t jtd)}t jt j|d*d+ tt}| }W d    n1 s0    Y  |d,|}t|d-}|| W d    n1 s0    Y  td. |S )/NzEnter Spaces app title [z]: zFormatted to z. r   z.pyzutf-8ignore)encodingerrorszimport gradiozEnter Gradio app file [] z: zFailed to find Gradio app file.r   gradioZsdkZsdk_versionzEnter Spaces hardware (z, c                 s  s   | ]}|j V  qd S )N)value).0hardware r   \/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/gradio/deploy_space.py	<genexpr>:       z.add_configuration_to_readme.<locals>.<genexpr>z) [cpu-basic]: z	cpu-basicr   zAny Spaces secrets (y/n) [n]: yz(Enter secret name (leave blank to end): zEnter secret value for secretszrequirements.txtz)Create requirements.txt file? (y/n) [n]: z)Enter a dependency (leave blank to end): a
zGCreate Github Action to automatically update Space on 'git push'? [n]: zEnter branch to track [main]: mainz".github/workflows/update_space.ymlT)exist_okz$branchwzGithub Action created. Add your Hugging Face write token (from https://huggingface.co/settings/tokens) as an Actions Secret named 'hf_token' to your GitHub repository. This can be set in your repository's settings page.)ospathbasenamerepo_directoryinputformat_titleprintlistdirjoinisfileendswithopenreadexistsFileNotFoundErrorgr__version__huggingface_hubZmetadata_savereadme_fileZSpaceHardwarelowerwritemakedirsdirnamegithub_action_templatereplace)r   r   configurationdir_nameZformatted_titlefile	file_pathfcontentr   secret_namesecret_valueZrequirements_filerequirementtrack_branchZgithub_action_fileZgithub_action_contentr   r   r   add_configuration_to_readme   s    
0"

2
(*r?   strr   c                 C  sD   |  dd} tdd| } tdd| } | dr@| dd  } q(| S )	N _z[^a-zA-Z0-9\-._]r   z-+-.   )r4   resub
startswithrA   r   r   r   r!   m   s    
r!   c            	      C  s  t ddkrd S tjdd} | d | jdtdd | jd	td
d |  }t }d }d}z$|	 }|d d d dkrd}W n t
y   d}Y n0 |rtd tjdd |	 }d }t jtrztt}W n ty   Y n0 |d u rtdt d t|j|j}tj|d ddd|ddj}|j|dtd |drx|d  D ]\}}t||| q^td|  d S )NZSYSTEMspaceszDeploy to Spaces)descriptiondeployz--titlezSpaces app title)typehelpz
--app-filezFile containing the Gradio appFauthZaccessTokenroler0   Tz2Need 'write' access token to create a Spaces repo.)Zadd_to_git_credentialzCreating new Spaces Repo in 'z<'. Collecting metadata, press Enter to accept default value.r   r   spacer   )Z	space_sdk	repo_typer   Zspace_hardware)repo_idrR   Zfolder_pathr   z1Space available at https://huggingface.co/spaces/)r   getenvargparseArgumentParseradd_argumentr@   
parse_argsr-   ZHfApiwhoamiOSErrorr"   loginr   r)   r.   Zmetadata_load
ValueErrorr   r?   r   r   Zcreate_repogetrS   Zupload_folderitemsZadd_space_secret)	parserargsZhf_apirY   r[   r5   Zspace_idr;   r<   r   r   r   rL   v   sf    



rL   )
__future__r   rU   r   rG   r-   r   r+   getcwdr   r   r$   r.   r2   __file__r3   r?   r!   rL   r   r   r   r   <module>   s   [	