o
    ۷i+                     @  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mZ d dlZd dl	m
Z
 d dlmZ d dlZe ZejedZejejedZdddZdddZdd Zdd Z			dd ddZdS )!    )annotationsN)	Annotated)print)Optionz	README.mdzdeploy_space_action.yamltitle
str | Noneapp_filereturndictc              	   C  s  i }t jt}| d u rtd| dp|} t| }|| kr&td| d ||d< |d u rt tD ]?}t jt|}t j	|rG|
dsHq3t|ddd	}| }d
|v rc|}	 W d     nW d    n1 smw   Y  q3td|r}d| dnd dp|}|rt j|std||d< d|d< tj|d< tt| tdddd tjD  dpd|d< i }	tddkr	 td}
|
sntd |
 d}||	|
< q|	|d!< t jtd"}t j|std# dkr	 td$}|sn t|d%dd&}||d'  W d    n	1 sw   Y  qtd( dkr}td)p(d*}t jtd+}t jt j|dd, ttdd&}| }W d    n	1 sQw   Y  |d-|}t|d.dd&}|| W d    n	1 stw   Y  td/ |S )0NzEnter Spaces app title [z]: zFormatted to z. r   z.pyutf-8ignore)encodingerrorszimport gradiozEnter Gradio app file [] z: zFailed to find Gradio app file.r   gradiosdksdk_versionzEnter Spaces hardware (z, c                 s  s    | ]}|j V  qd S )N)value).0hardware r   V/home/ubuntu/vllm_env/lib/python3.10/site-packages/gradio/cli/commands/deploy_space.py	<genexpr>>   s    z.add_configuration_to_readme.<locals>.<genexpr>z) [cpu-basic]: z	cpu-basicr   zAny Spaces secrets (y/n) [n]: yTz(Enter secret name (leave blank to end): zEnter secret value for secretsrequirements.txtz)Create requirements.txt file? (y/n) [n]: z)Enter a dependency (leave blank to end): ar   
zGCreate Github Action to automatically update Space on 'git push'? [n]: zEnter branch to track [main]: mainz".github/workflows/update_space.yml)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_titler   listdirjoinisfileendswithopenreadexistsFileNotFoundErrorgr__version__huggingface_hubmetadata_savereadme_fileSpaceHardwarelowerwritemakedirsdirnamegithub_action_templatereplace)r   r   configurationdir_nameformatted_titlefile	file_pathfcontentr   secret_namesecret_valuerequirements_filerequirementtrack_branchgithub_action_filegithub_action_contentr   r   r   add_configuration_to_readme   s    


rL   strc                 C  sL   |  dd} tdd| } tdd| } | dr$| dd  } | ds| S )	N _z[^a-zA-Z0-9\-._]r   z-+-.   )r=   resub
startswith)r   r   r   r   r)   q   s   

r)   c                  C  sx  zat jg ddddd} | j s"td td t jddgdd t jg d	dddd}|j sFtd
 td}|sCtd W dS |W S td| j  d td|j  d |j W S  t jy } z@td| d td zt jddgdd W n! t jy } ztd| d W Y d}~W Y d}~dS d}~ww W Y d}~dS d}~w ty   td Y dS w )zFCheck if user is logged in to Google Cloud and has a project selected.)gcloudauthlistz--filter=status:ACTIVETchecktextcapture_outputzA[bold yellow]You are not logged in to Google Cloud.[/bold yellow]z1Running 'gcloud init' to set up authentication...rV   init)rZ   )rV   configz	get-valueprojectz?[bold yellow]No Google Cloud project is selected.[/bold yellow]z$Enter your Google Cloud project ID: z1[red]Project ID is required for deployment.[/red]Nu   [green]✓ Authenticated as: z[/green]u   [green]✓ Project: z5[bold red]Error checking Google Cloud configuration: z[/bold red]z0Running 'gcloud init' to set up configuration...z"[red]Failed to run 'gcloud init': [/red]FzK[bold red]gcloud CLI not found. Please install Google Cloud SDK.[/bold red])
subprocessrunstdoutstripr   r(   CalledProcessErrorr1   )auth_resultproject_result
project_ide
init_errorr   r   r   check_gcloud_authz   s\   



rk   c               
   C  s  t dstd dS t } | std dS tjds(tjds(td dS d}tj|smtd	tj	 d

 dkrgt|ddd}|dtj	 d W d   n1 sYw   Y  tdtj	  n[td dS t|dd}| }W d   n1 sw   Y  d|vrtdtj	 d
 dkrt|ddd}|dtj	 d W d   n1 sw   Y  tdtj	 d td zg d}| r|d| g tj|dddd td W dS  tjy } ztd | d! W Y d}~dS d}~w ty   td Y dS w )"zODeploy a Gradio app to Google Cloud Run. Always uses app.py as the entry point.rV   z[bold red]gcloud CLI is not installed.[/bold red]
Please install the Google Cloud SDK from: [link]https://cloud.google.com/sdk/docs/install[/link]NzZ[bold red]Google Cloud configuration failed. Please run 'gcloud init' manually.[/bold red]app.pyzmain.pyz[bold red]Error:[/bold red] app.py and main.py not found. Google Cloud Run deployment requires app.py or main.py as the entry point.r   zkA requirements.txt file is necessary for Google Cloud Run deployment. Create requirements.txt with gradio==z? (y/n) [y]: nr#   r   r   zgradio==r    z&Created requirements.txt with gradio==z'
[yellow]Deployment cancelled.[/yellow]r   zAdd gradio==z! to requirements.txt? (y/n) [y]: r   zAdded gradio==z to requirements.txtz-[bold]Deploying to Google Cloud Run...[/bold])rV   rb   deployz
--source=.z--labels=created-by=gradioz	--projectTFrY   u'   [green]✓ Deployment complete![/green]z[red]Deployment failed: r`   )shutilwhichr   rk   r$   r%   r0   r(   r2   r3   r8   r.   r9   r/   extendra   rb   re   KeyboardInterrupt)rh   rG   rC   requirements_contentdeploy_commandri   r   r   r   deploy_to_gcloud   s   

ru   spaces6Annotated[str | None, Option(help='Spaces app title')]DAnnotated[str | None, Option(help='File containing the Gradio app')]providerLAnnotated[str | None, Option(help='Deployment provider (spaces or gcloud)')]c           
      C  s  |dkr|r|dkrt d t  d S |dkr!t d| d d S tddkr*d S t }d }d}z| }|d	 d
 d dkrCd}W n tyO   d}Y nw |r`t d tjdd | }d }tj	
tryztt}W n	 tyx   Y nw |d u rt dt d t| |}|j|d ddd|ddj}|j|dtd |dr|d  D ]\}}	||||	 qt d|  d S )NrV   rl   zm[yellow]Warning:[/yellow] --app-file is ignored for Google Cloud Run deployment. Using app.py as entry point.rv   z[red]Unknown provider: z!. Use 'spaces' or 'gcloud'.[/red]SYSTEMFrW   accessTokenroler9   Tz2Need 'write' access token to create a Spaces repo.)add_to_git_credentialzCreating new Spaces Repo in 'z<'. Collecting metadata, press Enter to accept default value.r   r   spacer   )	space_sdk	repo_typer"   space_hardware)repo_idr   folder_pathr   z1Space available at https://huggingface.co/spaces/)r   ru   r$   getenvr4   HfApiwhoamiOSErrorloginr%   r0   r6   metadata_load
ValueErrorr'   rL   create_repogetr   upload_folderitemsadd_space_secret)
r   r   ry   hf_apir   r   r>   space_idrE   rF   r   r   r   rn      sx   	

rn   )r   r   r   r   r	   r
   )r   rM   )NNrv   )r   rw   r   rx   ry   rz   )
__future__r   r$   rS   ro   ra   typingr   r4   richr   typerr   r   r2   getcwdr'   r%   r+   r6   r;   __file__r<   rL   r)   rk   ru   rn   r   r   r   r   <module>   s.    

[	3P