o
    5tiM                     @   s   d dl Z d dlZd dlmZ d dlmZ d dlmZmZm	Z	m
Z
 d dlmZmZmZmZ d dlmZ ddlmZ dd	lmZ dd
lmZ eeZeG dd dZdd ZG dd dZdS )    N)	dataclass)Path)CallableDictOptionalUnion)DatasetDownloadConfigDownloadModeload_dataset)Version   )	evaluator)evaluation_module_factory)
get_loggerc                   @   sv   e Zd ZU eed< dZeeeef  ed< dZ	ee ed< dZ
ee ed< dZee ed< dZee ed< dd	 ZdS )
SubTask	task_typeNdatasubsetsplitdata_preprocessorargs_for_taskc                 C   s   t | jturtdt | j t | jttfvr$tdt | j | jr8t | jtur8tdt | j | jrLt | jturLtdt | j | jr\t	| js\td| j | j
rpt | j
turrtdt | j
 d S d S )Nz$'task_type' must be type 'str', got zI'data' must be an already-instantiated Dataset object or type 'str', got z!'subset' must be type 'str', got z 'split' must be type 'str', got z-'data_preprocessor' must be a Callable', got z)'args_for_task' must be type 'dict', got )typer   str
ValueErrorr   r   r   r   r   callabler   dictself r   V/home/ubuntu/.local/lib/python3.10/site-packages/evaluate/evaluation_suite/__init__.py__post_init__   s   zSubTask.__post_init__)__name__
__module____qualname__r   __annotations__r   r   r   r   r   r   r   r   r   r   r!   r   r   r   r    r      s   
 r   c                 C   sP   t | }d}|j D ]\}}t|tr%|jdkr%t|r q|} |S q|S )zCImport a module at module_path and return the EvaluationSuite classNSuite)		importlibimport_module__dict__items
isinstancer   r"   inspect
isabstract)module_pathmodulemodule_main_clsnameobjr   r   r    import_main_class,   s   

 r3   c                   @   s   e Zd ZdZdd Ze			ddedee dee	ee
f  dee fd	d
Zdd Zdd Zde	ededdf deeef fddZdS )EvaluationSuitea  
    This class instantiates an evaluation suite made up of multiple tasks, where each task consists of a dataset and
    an associated metric, and runs evaluation on a model or pipeline. Evaluation suites can be a Python script found
    either locally or uploaded as a Space on the Hugging Face Hub.
    Usage:
    ```python
    from evaluate import EvaluationSuite
    suite = EvaluationSuite.load("evaluate/evaluation-suite-ci")
    results = suite.run("lvwerra/distilbert-imdb")
    ```
    c                 C   s
   || _ d S )N)r1   )r   r1   r   r   r    __init__H   s   
zEvaluationSuite.__init__Npathdownload_moderevisiondownload_configc                 C   s@   t |pt j}t| d |||d}t| j}t|j}||}|S )N)module_typer8   r9   r7   )r
   REUSE_DATASET_IF_EXISTSr   r   stemr3   r.   )r6   r7   r8   r9   evaluation_moduler1   evaluation_clsevaluation_instancer   r   r    loadK   s   


zEvaluationSuite.loadc                 C   s(   dd | j D | _d| j d| j dS )Nc                 S   s   g | ]}t |qS r   )r   ).0taskr   r   r    
<listcomp>]   s    z,EvaluationSuite.__repr__.<locals>.<listcomp>zEvaluationSuite name: "z
", Tasks: ))suitetasksr1   r   r   r   r    __repr__\   s   zEvaluationSuite.__repr__c                 C   s   | j stdd S )Nz\No evaluation tasks found. The EvaluationSuite must include at least one SubTask definition.)rE   r   r   r   r   r    assert_suite_nonempty`   s
   z%EvaluationSuite.assert_suite_nonemptymodel_or_pipelinePipelinePreTrainedModelTFPreTrainedModelreturnc           	      C   s   |    g }| jD ]]}|j}|jr"t|j|j|jd}||j|_t|j	}|j
}||d< |j|d< |j|d< |j|d< |jd	i |}|jrO|d |j n||d< |jd ur]t|jnd |d< || q	|S )
N)r1   r   rI   r   r   r   /	task_namer   r   )rH   rE   r   r   r   r   r   mapr   r   r   computer   append)	r   rI   results_allrB   rO   dstask_evaluatorr   resultsr   r   r    runf   s$   




zEvaluationSuite.run)NNN)r"   r#   r$   __doc__r5   staticmethodr   r   r
   r   r   r	   r@   rG   rH   r   r   floatrW   r   r   r   r    r4   ;   s.    
r4   )r'   r,   dataclassesr   pathlibr   typingr   r   r   r   datasetsr   r	   r
   r   datasets.utils.versionr   r   loadingr   utils.loggingr   r"   loggerr   r3   r4   r   r   r   r    <module>   s    