o
    5tiR                     @   s   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 eeZedZdd Zd	d
 Zdd Zdd Zdd Zdd ZdS )    N)Path)Value   )
get_loggerz$---[\n\r]+([\S\s]*?)[\n\r]+---[\n\r]c                 C   sV   g }| D ]$}d}t |tr#|jds|jdrd}n|jdkr#d}|| q|S )a  
    Maps metric feature types to input types for gradio Dataframes:
        - float/int -> numbers
        - string -> strings
        - any other -> json
    Note that json is not a native gradio type but will be treated as string that
    is then parsed as a json.
    jsonintfloatnumberstringstr)
isinstancer   dtype
startswithappend)feature_typesinput_typesfeature_type
input_type r   I/home/ubuntu/.local/lib/python3.10/site-packages/evaluate/utils/gradio.pyinfer_gradio_input_types   s   	

r   c                 C   s   dd | D S )zMaps json input type to str.c                 S   s   g | ]
}|d kr
dn|qS )r   r   r   .0ir   r   r   
<listcomp>)   s    z'json_to_string_type.<locals>.<listcomp>r   )r   r   r   r   json_to_string_type'   s   r   c                 C   sv   t j| sdS t| d$}| }t|}|r)|| d }W d   |S W d   |S 1 s4w   Y  |S )z(Parses a repositories README and removeszNo README.md found.rN)ospathexistsopenreadREGEX_YAML_BLOCKsearchend)filepathftextmatchr   r   r   parse_readme,   s   


r)   c                 C   s   i }| j dtjdd | jdd t| |D ].\}}|dkr,dd | |  D ||< q|dkr>dd | |  D ||< q| | ||< q|S )	z4Parses data from gradio Dataframe for use in metric. T)inplacer   c                 S   s   g | ]}t |qS r   )r   loadsr   dr   r   r   r   ?       z%parse_gradio_data.<locals>.<listcomp>r   c                 S   s   g | ]}| d qS ")stripr-   r   r   r   r   A   r/   )replacenpnandropnazipto_list)datar   metric_inputsfeature_namer   r   r   r   parse_gradio_data8   s   r<   c                 C   s   t | dkrdS g }| D ]C}g }t||D ]-\}}|dkr*|dd || D  q|dkr;|dd || D  q|||  q|dd t| D  q|S )	z
    Parses test cases to be used in gradio Dataframe. Note that an apostrophe is added
    to strings to follow the format in json.
    r   Nr   c                 S      g | ]}t |qS r   )r   r   elementr   r   r   r   S       z$parse_test_cases.<locals>.<listcomp>r   c                 S   s   g | ]}d | d  qS r0   r   r>   r   r   r   r   U   s    c                 S   r=   r   )listr   r   r   r   r   X   r@   )lenr7   r   )
test_casesfeature_namesr   examples	test_caseparsed_casesfeatr   r   r   r   parse_test_casesG   s   rI   c              
      s   zddl }W n ty } ztd |d}~ww ttjd }tjt	r3t
jd   \}}n	t
j  \}}t|  fdd}|j||jj|t|dt d|jjjdjjd	 d
j t|d d}|  dS )z%Launches `metric` widget with Gradio.r   NzDTo create a metric widget with Gradio make sure gradio is installed.c                    s   j di t|  S )Nr   )computer<   )r9   gradio_input_typesmetricr   r   rJ   m   s   z%launch_gradio_widget.<locals>.computer   )headers	col_count	row_countdatatype)labelz
If this is a text-based metric, make sure to wrap you input in double quotes. Alternatively you can use a JSON-formatted list as input.zMetric: z	README.md)fninputsoutputsdescriptiontitlearticle)gradioImportErrorloggererrorr   sysr   r   featuresrA   r7   itemsr   	InterfacerT   	DataframerB   r   rU   TextboxnameinforV   r)   launch)rM   grr\   
local_pathrD   r   rJ   ifacer   rK   r   launch_gradio_widget\   s6   



ri   )r   r   rer]   pathlibr   numpyr4   datasetsr   loggingr   __name__r[   compiler"   r   r   r)   r<   rI   ri   r   r   r   r   <module>   s     
