
    +j                    8   d dl mZ d dlZd dlmZmZ d dlmZ d dlm	Z	 d dl
mZ d dlZ ej                    Zd'd(dZd)dZd Zd*d+dZd,d-dZd,d.dZd Zd Ze                                	 d'd/d            Ze                                e	dfd0d&            ZdS )1    )annotationsN)	AnnotatedAny)Client)DEFAULT_TEMP_DIR)traverseschemar   defsdict[str, Any] | Nonereturnc                   t          | t                    s%t          | t                    rfd| D             S | S |                     di           d| v r?| d         }|                    d          d         }|v rt          |                   S | S i }|                                 D ]\  }}|dk    rt          |          ||<    |S )z5Recursively resolve $ref references and remove $defs.c                0    g | ]}t          |          S  )_resolve_refs).0itemr
   s     Q/home/wildlama/visual-decline/.venv/lib/python3.11/site-packages/hf_gradio/cli.py
<listcomp>z!_resolve_refs.<locals>.<listcomp>   s#    AAA$M$--AAA    Nz$defsz$ref/)
isinstancedictlistgetsplitr   items)r	   r
   ref_pathref_nameresolvedkeyvalues    `     r   r   r      s    fd## fd## 	BAAAA&AAAA|zz'2&&&>>>#&&r*t h666Hllnn 3 3
U'>>%eT22Or   boolc                ~   t          | t                    sdS |                     di           }d|vsd|vrdS |d         }|                    di           }d|v r|d                             d          dk    S |                    di           }t          |t                    r|                    d          dk    S dS )	zPCheck if a schema represents a file type (has path + meta with gradio.FileData).F
propertiespathmeta_typeconstgradio.FileDatadefault)r   r   r   )r	   propsr'   
meta_propsmeta_defaults        r   _is_file_schemar/   &   s    fd## uJJ|R((EUfE11u=D,++J*'"&&w//3DDD88Ir**L,%% >((,===5r   c                    t          | t                    oCt          |                     dd           t                    o|                     d          ddik    S )Nr&   r'   r(   r*   )r   r   r   strds    r   _is_file_dictr4   7   sQ    1d 	:quuVT**C00	:EE&MMg'899r   TFis_inputurl_onlyc                l    t          |           } t          ||          }t          | |t                    S N)r   _make_file_simplifierr   r/   )r	   r5   r6   
simplifiers       r   simplify_json_schemar;   ?   s9    6""F&x::J  r   c                     d fd}|S )Nr	   r   r   c                ~    |                      di           }d|v r!d|v rrrd}nd}d|dz   dS rd	d
dS dddS | S )Nr%   r'   r&   zDMust include {"path": "url", "meta": {"_type": "gradio.FileData"}}. zTMust include {"path": "<local path or url>", "meta": {"_type": "gradio.FileData"}}. filepathzIThe meta key signals that the file will be uploaded to the remote server.)typedescriptionobjectz,JSON object with url key to fetch the outputzThe returned file path on disk.)r   )r	   r,   descr5   r6   s      r   	_simplifyz(_make_file_simplifier.<locals>._simplifyJ   s    

<,,U??v  raDDqD&ef     ('U  
 '#D   r   )r	   r   r   r   r   )r5   r6   rC   s   `` r   r9   r9   I   s/          6 r   infodict[str, Any]c                   i }| d                                          D ]\  }}g g |                    dd          d}|d         D ]K}|d                             |d         |d          |d         t          |d	         d
|          d           L|d         D ]<}|d                             |d         t          |d	         d|          d           =|||<   |S )Nnamed_endpointsr@    )
parametersreturnsr@   rI   parameter_nameparameter_has_defaultparameter_defaultr?   T)r5   r6   )namerequiredr+   r?   rJ   labelF)rN   r?   )r   r   appendr;   )rD   r6   condensed_infoendpointdata_formatendpoint_infoparamoutputs           r   _condense_inforX   h   sH   N!%&7!8!>!>!@!@ 1 1+&??="==
 

 !. 
	 
	E,'..!"23$)*A$B B$%890fx  	 	 	 	 	 "), 	 	F)$++"7O0v       $1x  r   c                >    d |                                  D             S )Nc                "    i | ]\  }}|d v 	||S ))r&   r'   r   )r   kvs      r   
<dictcomp>z _delete_keys.<locals>.<dictcomp>   s)    @@@TQ!/?*?*?Aq*?*?*?r   )r   r2   s    r   _delete_keysr^      s    @@QWWYY@@@@r   c                    i }|                                  D ]J\  }}t          j        d |d         D             d          }d| d| d                    d          ||<   K|S )	Nc                n    i | ]2}|d          t          |d         p|d         t          t                    3S )rK   rM   example_input)r   r^   r4   )r   ps     r   r]   z(generate_cli_snippet.<locals>.<dictcomp>   sU         "#X)*@a.@ !& &  r   rI      indentz
{command} predict {space_id} z 'z'

)r   jsondumpslstrip)original_info	endpointsrS   rD   paramss        r   generate_cli_snippetrm      s    I'--//  $  l+   

 

 

"* .4  
F4LL 	( r   space_id_or_urllAnnotated[str, typer.Argument(help='The space id, e.g. gradio/calculator or URL of the Gradio application')]tokenAnnotated[str | None, typer.Option(help='optional Hugging Face token to use to access private Spaces. By default, the locally saved token is used if there is one.')]c                    t          | |d          }|                    dd          }t          |          }t          t	          j        |d                     dS )zAFetches the expected JSON payload for all of the app's endpoints.F)srcrp   verboser   return_format
print_inforc   rd   N)r   view_apirX   printrg   rh   )rn   rp   clientrj   rR   s        r   rD   rD      s]      ueDDDFOO&UOKKM#M22N	$*^A
.
.
./////r   rS   :Annotated[str, typer.Argument(help='The endpoint to hit')]payloadGAnnotated[str, typer.Argument(help='The payload to send to the space')]download_files  Annotated[str, typer.Option(help='The directory where the files created by the space are downloaded to on your local machine. By default, this is the Gradio temporary directory. If False, a URL pointing to the file on the remote app will be returned instead.')]c                   t          | |d|dk    r|nd          }t          j        |          } |j        di |d|i}|                    dd          }t          |          }d ||         d         D             }	t          |t                    s|f}t          t          |	|                    }
t          t          j        |
d	
                     dS )z4Sends a prediction request to a Gradio app endpoint.FFalse)rs   rp   rt   r~   api_namer   ru   c                    g | ]
}|d          S )rN   r   )r   rs     r   r   zpredict.<locals>.<listcomp>   s    KKK!AfIKKKr   rJ   rc   rd   Nr   )r   rg   loadspredictrx   rX   r   tupler   zipry   rh   )rn   rS   r|   r~   rp   rz   resultrj   rR   return_namesrW   s              r   r   r      s    0 )77)B)B~~	  F j!!GV^99g99999FOO&UOKKM#M22NKK~h'?	'JKKKLfe$$ #lF++,,F	$*VA
&
&
&'''''r   r8   )r	   r   r
   r   r   r   )r	   r   r   r#   )TF)r	   r   r5   r#   r6   r#   )F)r5   r#   r6   r#   )rD   rE   r6   r#   )rn   ro   rp   rq   )
rn   ro   rS   r{   r|   r}   r~   r   rp   rq   )
__future__r   typertypingr   r   gradio_clientr   gradio_client.clientr   gradio_client.utilsr   rg   Typerappr   r/   r4   r;   r9   rX   r^   rm   commandrD   r   r   r   r   <module>r      s   " " " " " "  ! ! ! ! ! ! ! !             1 1 1 1 1 1 ( ( ( ( ( ( ekmm    2   "          >    @A A A  (  	0 0 0 0 0*  	 	)'( '( '( '( '( '( '(r   