o
    5ti                     @   s   d dl mZ d dlmZmZmZmZmZmZm	Z	 d dl
mZmZ d dlmZ ddlmZ ddlmZmZ dd	lmZmZmZ dd
lmZ erTd dlmZmZmZmZmZ dZ G dd deZ!dS )    )Number)TYPE_CHECKINGAnyCallableDictOptionalTupleUnion)Datasetload_dataset)Literal   )EvaluationModule)add_end_docstringsadd_start_docstrings   )"EVALUATOR_COMPUTE_RETURN_DOCSTRING EVALUTOR_COMPUTE_START_DOCSTRING	Evaluator)DatasetColumnPair)FeatureExtractionMixinPipelinePreTrainedModelPreTrainedTokenizerTFPreTrainedModelaD  
    Examples:
    ```python
    >>> from evaluate import evaluator
    >>> from datasets import load_dataset
    >>> task_evaluator = evaluator("text-classification")
    >>> data = load_dataset("imdb", split="test[:2]")
    >>> results = task_evaluator.compute(
    >>>     model_or_pipeline="huggingface/prunebert-base-uncased-6-finepruned-w-distil-mnli",
    >>>     data=data,
    >>>     metric="accuracy",
    >>>     label_mapping={"LABEL_0": 0.0, "LABEL_1": 1.0},
    >>>     strategy="bootstrap",
    >>>     n_resamples=10,
    >>>     random_state=0
    >>> )
    ```
c                (       s<  e Zd ZdZddiZd* fdd	Zdeeef d	ed
edefddZ	dd Z
eeeee																d+deededdf deeef dee dee deeef deeedf  deeedf  d ed! d"ed#ed$ed%ee d	ed
ee ded&eeeef  d'eeeef ef f"d(d)Z  ZS ),TextClassificationEvaluatora  
    Text classification evaluator.
    This text classification evaluator can currently be loaded from [`evaluator`] using the default task name
    `text-classification` or with a `"sentiment-analysis"` alias.
    Methods in this class assume a data format compatible with the [`~transformers.TextClassificationPipeline`] - a single textual
    feature as input and a categorical label as output.
    
truncationTtext-classificationNc                    s   t  j||d d S )N)default_metric_name)super__init__)selftaskr   	__class__ Z/home/ubuntu/.local/lib/python3.10/site-packages/evaluate/evaluator/text_classification.pyr    >   s   z$TextClassificationEvaluator.__init__datainput_columnsecond_input_columnlabel_columnc                 C   sl   |d u rt d| |||d |d ur| |d|i t|tr&t|n|}d|| it|||ddfS )NzXPlease specify a valid `data` object - either a `str` with a name or a `Dataset` object.)r(   r*   r)   
referencestext	text_pair)
ValueErrorcheck_required_columns
isinstancestrr   r   )r!   r'   r(   r)   r*   r%   r%   r&   prepare_dataA   s   
z(TextClassificationEvaluator.prepare_datac                    s    fdd|D }d|iS )Nc                    s(   g | ]} d ur |d  n|d qS )Nlabelr%   ).0elementlabel_mappingr%   r&   
<listcomp>S   s    zETextClassificationEvaluator.predictions_processor.<locals>.<listcomp>predictionsr%   )r!   r9   r7   r%   r6   r&   predictions_processorR   s   
z1TextClassificationEvaluator.predictions_processorsimpleffffff?'  r,   r3   model_or_pipeliner   r   r   subsetsplitmetric	tokenizerr   feature_extractorr   strategy)r;   	bootstrapconfidence_leveln_resamplesdevicerandom_stater7   returnc                 C   s   i }|  || | j|||d}| j||||d\}}| j||||d}| |}| ||\}}| ||}|| | j||||	|
|d}|| || |S )a#  
        input_column (`str`, *optional*, defaults to `"text"`):
            The name of the column containing the text feature in the dataset specified by `data`.
        second_input_column (`str`, *optional*, defaults to `None`):
            The name of the second column containing the text features. This may be useful for classification tasks
            as MNLI, where two columns are used.
        label_column (`str`, defaults to `"label"`):
            The name of the column containing the labels in the dataset specified by `data`.
        label_mapping (`Dict[str, Number]`, *optional*, defaults to `None`):
            We want to map class labels defined by the model in the pipeline to values consistent with those
            defined in the `label_column` of the `data` dataset.
        )r'   r?   r@   )r'   r(   r)   r*   )r>   rB   rC   rH   )rA   metric_inputsrD   rF   rG   rI   )	"check_for_mismatch_in_device_setup	load_datar2   prepare_pipelineprepare_metriccall_pipeliner:   updatecompute_metric)r!   r>   r'   r?   r@   rA   rB   rC   rD   rF   rG   rH   rI   r(   r)   r*   r7   resultrK   pipe_inputspiper9   perf_resultsmetric_resultsr%   r%   r&   computeY   s6   $



	
z#TextClassificationEvaluator.compute)r   N)NNNNNNNr;   r<   r=   NNr,   Nr3   N)__name__
__module____qualname____doc__PIPELINE_KWARGSr    r	   r1   r
   r2   r:   r   r   r   r   TASK_DOCUMENTATIONr   r   r   r   floatintr   r   r   r   rX   __classcell__r%   r%   r#   r&   r   3   sz    "

	
r   N)"numbersr   typingr   r   r   r   r   r   r	   datasetsr
   r   typing_extensionsr   moduler   utils.file_utilsr   r   baser   r   r   utilsr   transformersr   r   r   r   r   r^   r   r%   r%   r%   r&   <module>   s   $