a
    d                  	   @   s
  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mZmZm	Z	 ddl
Z
ddlZddlmZ eeeee f dddZeeee f e	ee eee  f dd	d
Zeeeeeeeee f f f dddZeedddZe
jedddZeedddZdS )z'Utility function for gradio/external.py    N)DictListTuple)
components)
model_namereturnc                 C   s   t d|  d}|jdkr4td|  t i }nPtd|j}|d u rPi }n4t	t
|jd | d  }|di di }|std	|  d
| D ]2}t|D ]$\}}t|trt|rd||< qq|S )Nzhttps://huggingface.co/z/resolve/main/README.md   z%Cannot load examples from README for z,(?:^|[
])---[
]+([\S\s]*?)[
]+---([
]|$)ZwidgetZstructuredDataz&No example data found in README.md of z - Cannot build gradio demo. See the README.md here: https://huggingface.co/scikit-learn/tabular-playground/blob/main/README.md for a reference on how to provide example data to your model.NaN)requestsgetstatus_codewarningswarnUserWarningresearchtextnextyamlZsafe_load_allspan
ValueErrorvalues	enumerate
isinstancefloatmathisnan)r   Zreadmeexample_dataZ
yaml_regexZexample_yamldataival r"   ^/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/gradio/external_utils.pyget_tabular_examples   s,    

r$   )r   r   c                    s   t   }t fdd|D }g }t|D ]N}g }|D ]6} | pHg }|t|krb|d q:|||  q:|| q.||fS )Nc                 3   s   | ]}t  | pg V  qd S )N)len).0headerr   r"   r#   	<genexpr>6       zcols_to_rows.<locals>.<genexpr>r
   )listkeysmaxranger%   append)r   headersZn_rowsr   Z	row_indexZrow_datar'   colr"   r(   r#   cols_to_rows2   s    r2   )incoming_datar   c                    s@   i }t | d D ]"\ } fdd| d D ||< qdd|iiS )Nr0   c                    s   g | ]}t |  qS r"   )str)r&   rowr    r"   r#   
<listcomp>G   r*   z rows_to_cols.<locals>.<listcomp>r   inputs)r   )r3   Zdata_column_wiser'   r"   r6   r#   rows_to_colsD   s    r9   )scoresr   c                 C   s4   t |  tddd}|d d dd |D dS )N   T)keyreverser   c                 S   s   g | ]}|d  |d dqS )r   r;   )label
confidencer"   )r&   predr"   r"   r#   r7   T   s   z%postprocess_label.<locals>.<listcomp>)r>   Zconfidences)sorteditemsoperator
itemgetter)r:   Zsorted_predr"   r"   r#   postprocess_labelP   s    
rE   )rr   c              
   C   s   t | jd}d}||v r"|S | jd}|dkrz |  d }|d }|d }W q ty } ztd|W Y d }~qd }~0 0 n d| d| }|S d S )	Nzutf-8z;base64,zcontent-typezapplication/jsonr   Zblobz7Cannot determine content type returned by external API.zdata:)	base64	b64encodecontentdecoder0   r   jsonKeyErrorr   )rF   Zbase64_reprZdata_prefixcontent_typer   ZkeZ
new_base64r"   r"   r#   encode_to_base64Z   s$    rN   )configr   c                    sJ   dd  d D  d< dd  d D  d< h d} fd	d
|D   S )zGStreamlines the interface config dictionary to remove unnecessary keys.c                 S   s   g | ]}t |qS r"   r   Zget_component_instancer&   	componentr"   r"   r#   r7   {   s   z/streamline_spaces_interface.<locals>.<listcomp>Zinput_componentsr8   c                 S   s   g | ]}t |qS r"   rP   rQ   r"   r"   r#   r7      s   Zoutput_componentsoutputs>   descriptiontitleZflagging_optionsrS   articler8   c                    s   i | ]}| | qS r"   r"   )r&   krO   r"   r#   
<dictcomp>   r*   z/streamline_spaces_interface.<locals>.<dictcomp>r"   )rO   
parametersr"   rX   r#   streamline_spaces_interfacey   s    

r[   )__doc__rG   r   rC   r   r   typingr   r   r   r   r   Zgradior   r4   r   r$   r2   r9   rE   ResponserN   r[   r"   r"   r"   r#   <module>   s"   ,
