
    +sg                    L    d dl mZ d dlZd dlmZmZ erd dlmZ  G d d      Zy)    )annotationsN)TYPE_CHECKINGAny)SentenceTransformerc                  V    e Zd ZdZd Z	 d	 	 	 	 	 	 	 	 	 d	dZd
dZddZedd       Z	y)SentenceEvaluatorzl
    Base class for all evaluators

    Extend this class and implement __call__ for custom evaluators.
    c                     d| _         d| _        y)a  
        Base class for all evaluators. Notably, this class introduces the ``greater_is_better`` and ``primary_metric``
        attributes. The former is a boolean indicating whether a higher evaluation score is better, which is used
        for choosing the best checkpoint if ``load_best_model_at_end`` is set to ``True`` in the training arguments.

        The latter is a string indicating the primary metric for the evaluator. This has to be defined whenever
        the evaluator returns a dictionary of metrics, and the primary metric is the key pointing to the primary
        metric, i.e. the one that is used for model selection and/or logging.
        TN)greater_is_betterprimary_metric)selfs    e/var/www/html/venv/lib/python3.12/site-packages/sentence_transformers/evaluation/SentenceEvaluator.py__init__zSentenceEvaluator.__init__   s     "&"    Nc                     y)a  
        This is called during training to evaluate the model.
        It returns a score for the evaluation with a higher score indicating a better result.

        Args:
            model: the model to evaluate
            output_path: path where predictions and metrics are written
                to
            epoch: the epoch where the evaluation takes place. This is
                used for the file prefixes. If this is -1, then we
                assume evaluation on test data.
            steps: the steps in the current epoch at time of the
                evaluation. This is used for the file prefixes. If this
                is -1, then we assume evaluation at the end of the
                epoch.

        Returns:
            Either a score for the evaluation with a higher score
            indicating a better result, or a dictionary with scores. If
            the latter is chosen, then `evaluator.primary_metric` must
            be defined
        N )r   modeloutput_pathepochstepss        r   __call__zSentenceEvaluator.__call__   s    2 	r   c                    |s|S |j                         D ci c]  \  }}|dz   |z   | }}}t        | d      r5| j                  j                  |dz         s|dz   | j                  z   | _        |S c c}}w )N_r   )itemshasattrr   
startswith)r   metricsnamekeyvalues        r   prefix_name_to_metricsz(SentenceEvaluator.prefix_name_to_metrics9   s|    N=D]]_MzsE4#:#U*MM4)*43F3F3Q3QRVY\R\3]"&*t/B/B"BD Ns   A1c                <    |j                   j                  | |       y )N)model_card_dataset_evaluation_metrics)r   r   r   s      r    store_metrics_in_model_card_dataz2SentenceEvaluator.store_metrics_in_model_card_dataA   s    44T7Cr   c                    | j                   j                  }	 |j                  d      }|d| }t	        j
                  dd|      S # t        $ r Y "w xY w)z
        Returns a human-readable description of the evaluator: BinaryClassificationEvaluator -> Binary Classification

        1. Remove "Evaluator" from the class name
        2. Add a space before every capital letter
        	EvaluatorNz([a-z])([A-Z])z\g<1> \g<2>)	__class____name__index
IndexErrorresub)r   
class_namer)   s      r   descriptionzSentenceEvaluator.descriptionD   s^     ^^,,
	$$[1E#FU+J vv'DD  		s   A 	AA)Nr/   )
r   r   r   strr   intr   r1   returnzfloat | dict[str, float])r   dict[str, float]r   r0   r2   r3   )r   r   r   zdict[str, Any]r2   None)r2   r0   )
r(   
__module____qualname____doc__r   r   r    r$   propertyr.   r   r   r   r   r   
   sa    # bd(7:JM[^	!6D E Er   r   )	
__future__r   r+   typingr   r   )sentence_transformers.SentenceTransformerr   r   r   r   r   <module>r<      s"    " 	 %MIE IEr   