
    Pi+                    L   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j                    Zej                            ed          Zej                            ej                            e          d          ZddZddZd Zd Z	 	 	 dddZdS )    )annotationsN)	Annotated)print)Optionz	README.mdzdeploy_space_action.yamltitle
str | Noneapp_filereturndictc                b   i }t           j                            t                    }| t	          d| d          p|} t          |           }|| k    rt          d| d           ||d<   |t          j        t                    D ]}t           j                            t          |          }t           j        	                    |          r|
                    d          s\t          |dd	          5 }|                                }d
|v r|}	 d d d             n	 d d d            n# 1 swxY w Y   t	          d|rd| dnd d          p|}|rt           j                            |          st          d          ||d<   d|d<   t          j        |d<   t#          j        t&          |           t	          dd                    d t"          j        D                        d          pd|d<   i }	t	          d          dk    r,	 t	          d          }
|
snt	          d|
 d          }||	|
<   +|	|d <   t           j                            t          d!          }t           j                            |          s{t	          d"                                          dk    rV	 t	          d#          }|snCt          |d$d%          5 }|                    |d&z              d d d            n# 1 swxY w Y   Ut	          d'                                          dk    rt	          d(          pd)}t           j                            t          d*          }t          j        t           j                            |          d+           t          t2          d%          5 }|                                }d d d            n# 1 swxY w Y   |                    d,|          }t          |d-d%          5 }|                    |           d d d            n# 1 swxY w Y   t          d.           |S )/Nz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              3  $   K   | ]}|j         V  d S )N)value).0hardwares     t/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/gradio/cli/commands/deploy_space.py	<genexpr>z.add_configuration_to_readme.<locals>.<genexpr>>   s$      /m/m8/m/m/m/m/m/m    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_contents                    r   add_configuration_to_readmerO      s|    Mw//H}>>>>??K8"5))O%1o111222,M'J~.. 		 		D^T::I7>>),, DMM%4H4H i'(CCC q&&(("g--#H	       -               Rh+N?x????BRRRSS  	  C27>>(33 C ABBB (M*#M%#%>M- !+}=== 	~dii/m/mOl/m/m/m&m&m~~~	
 	
 	 	 * G-..#55	0 JKKK  !J;!J!J!JKKL#/GK 	0  'M)^5GHHGNN,--	,=>>DDFF#MM	, KLLK 'w??? ,1d*+++, , , , , , , , , , , , , , ,		, 	U	
 	

%''	 	
 =>>H&W\\@
 
 	BGOO$677$GGGG(7;;; 	-q$%FFHH!	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 5 = =i V V$cG<<< 	+GG)***	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	 j	
 	
 	
 sH   4D**D.	1D.		K..K25K2O  OO3PPPstrc                    |                      dd          } t          j        dd|           } t          j        dd|           } |                     d          r| dd          } |                     d          | S )	N _z[^a-zA-Z0-9\-._]r   z-+-.   )r@   resub
startswith)r   s    r   r,   r,   q   s|    MM#s##EF&E22EF4e$$E


3

 abb	 

3

 Lr   c                    	 t          j        g dddd          } | j                                        s6t	          d           t	          d           t          j        ddgd           t          j        g d	ddd          }|j                                        s3t	          d
           t          d          }|st	          d           dS |S t	          d| j                                         d           t	          d|j                                         d           |j                                        S # t           j        $ rz}t	          d| d           t	          d           	 t          j        ddgd           n4# t           j        $ r"}t	          d| d           Y d}~Y d}~dS d}~ww xY wY d}~n#d}~wt          $ r t	          d           Y dS w xY wdS )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...r[   init)r_   )r[   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+   CalledProcessErrorr4   )auth_resultproject_result
project_ide
init_errors        r   check_gcloud_authrp   z   so   , n@@@	
 
 
 !'')) 	;UVVVEFFFNHf-T::::#888	
 
 
 $**,, 	STTTEFFJ IJJJtRk.@.F.F.H.HRRRSSSL^%:%@%@%B%BLLLMMM$**,,,(   TaTTTUUU@AAA	NHf-T:::::, 	 	 	IzIIIJJJ555555555	 ;::::    Y	
 	
 	
 uu	 4sU   CD> D> A,D> >G""G0F	G	F:F5+G5F::GG"!G"c                    t          j        d          st          d           dS t                      } | st          d           dS t          j                            d          s0t          j                            d          st          d           dS d}t          j                            |          st          d	t          j	         d
          
                                dk    rjt          |dd          5 }|                    dt          j	         d           ddd           n# 1 swxY w Y   t          dt          j	                    nt          d           dS t          |d          5 }|                                }ddd           n# 1 swxY w Y   d|vrt          dt          j	         d          
                                dk    rjt          |dd          5 }|                    dt          j	         d           ddd           n# 1 swxY w Y   t          dt          j	         d           t          d           	 g d}| r|                    d| g           t          j        |ddd           t          d           dS # t          j        $ r}t          d | d!           Y d}~dS d}~wt$          $ r t          d           Y dS w xY w)"zODeploy a Gradio app to Google Cloud Run. Always uses app.py as the entry point.r[   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])r[   rg   deployz
--source=.z--labels=created-by=gradioz	--projectTFr^   u'   [green]✓ Deployment complete![/green]z[red]Deployment failed: re   )shutilwhichr   rp   r'   r(   r3   r+   r5   r6   r;   r1   r<   r2   extendrf   rg   rj   KeyboardInterrupt)rm   rJ   rF   requirements_contentdeploy_commandrn   s         r   deploy_to_gcloudr{      s   <!! E	
 	
 	

 	"$$J h	
 	
 	
 	7>>(## BGNN9,E,E  S	
 	
 	
 	*7>>+,, I \  A  M  \  \  \ egg 
 'w??? 7152>5556667 7 7 7 7 7 7 7 7 7 7 7 7 7 7K2>KKLLLL<===F#g666 	,!#$6688 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, ///Pr~PPP egg 
 'w??? 7152>5556667 7 7 7 7 7 7 7 7 7 7 7 7 7 7G2>GGGHHH	
9:::
 
 
  	=!!;
";<<< 		
 	
 	
 	
 	788888(   2222333   8999sU   <$D,,D03D06FFF+$HH"HAJ K!(KK! K!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           	     L   |dk    r'|r|dk    rt          d           t                       d S |dk    rt          d| d           d S t          j        d          dk    rd S t	          j                    }d }d}	 |                                }|d	         d
         d         dk    rd}n# t          $ r d}Y nw xY w|r8t          d           t	          j        d           |                                }d }t          j	        
                    t                    r+	 t	          j        t                    }n# t          $ r Y nw xY w|(t          dt           d           t          | |          }|                    |d         ddd|                    d                    j        }|                    |dt                     |                    d          r7|d                                         D ]\  }}	|                    |||	           t          d|            d S )Nr[   rr   zm[yellow]Warning:[/yellow] --app-file is ignored for Google Cloud Run deployment. Using app.py as entry point.r|   z[red]Unknown provider: z!. Use 'spaces' or 'gcloud'.[/red]SYSTEMFr\   accessTokenroler<   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   r{   r'   getenvr7   HfApiwhoamiOSErrorloginr(   r3   r9   metadata_load
ValueErrorr*   rO   create_repogetr   upload_folderitemsadd_space_secret)
r   r	   r   hf_apir   r   rA   space_idrH   rI   s
             r   rt   rt      s    8 	H,,   	8SSSSTTT	yh&&"$$FFE&>-(0G;;E    !BCCCE::::!%M	w~~k"" 	+9+FFMM 	 	 	D	 xNxxx	
 	
 	
 4
 

 !!g$((44 "     "    
 ## I)6y)A)G)G)I)I 	I 	I%K##Hk<HHHH	
Hh
H
HIIIIIs$   ;.B* *B98B9D7 7
EE)r   r   r	   r   r
   r   )r   rP   )NNr|   )r   r}   r	   r~   r   r   )
__future__r   r'   rW   ru   rf   typingr   r7   richr   typerr   r   r5   getcwdr*   r(   r.   r9   r>   __file__r?   rO   r,   rp   r{   rt    r   r   <module>r      sj   " " " " " " 				 				                               gll>;77GOOH9  
X X X Xv   0 0 0fL L L` EI 	 	EJ EJ EJ EJ EJ EJ EJr   