
    rh                    `    d dl mZ d dlZd dlmZmZ erd dlZd dlm	Z	 d dl
mZ  G d d      Zy)    )annotationsN)TYPE_CHECKINGAny)Tensor)SentenceTransformerc                      e Zd ZdZd Z	 d
	 	 	 	 	 	 	 	 	 ddZddZ	 d	 	 	 	 	 	 	 	 	 ddZedd       Z	ddZ
	 	 	 	 	 	 dd	Zy)SentenceEvaluatora  
    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.

    Extend this class and implement __call__ for custom evaluators.
    c                     d| _         d | _        y )NT)greater_is_betterprimary_metricselfs    /var/www/html/ai-insurance-compliance-backend/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                ^   dd}|s*|j                         D ci c]  \  }}| ||       c}}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 c c}}w )Nc                <    	 t        |       S # t        $ r | cY S w xY wN)float
ValueError)values    r   maybe_to_floatz@SentenceEvaluator.prefix_name_to_metrics.<locals>.maybe_to_float:   s%    U|# s   
 _r   )r   r   returnr   )itemshasattrr   
startswith)r   metricsnamer   keyr   s         r   prefix_name_to_metricsz(SentenceEvaluator.prefix_name_to_metrics9   s    	 AHQ:3C..QQMT]]_]zsE4#:#^E%::]]4)*43F3F3Q3QRVY\R\3]"&*t/B/B"BD	 R]s   B#B)c                @    |j                   j                  | |||       y r   )model_card_dataset_evaluation_metrics)r   r   r%   r   steps        r    store_metrics_in_model_card_dataz2SentenceEvaluator.store_metrics_in_model_card_dataG   s     	44T7E4Pr   c                    | j                   j                  }|j                  d      rd|dd z   }	 |j                  d      }|d| }t        j                  dd|      S # t        $ r Y "w xY w)a  
        Returns a human-readable description of the evaluator: BinaryClassificationEvaluator -> Binary Classification

        1. Replace "CE" prefix with "CrossEncoder"
        2. Remove "Evaluator" from the class name
        3. Add a space before every capital letter
        CECrossEncoder   N	Evaluatorz([a-z])([A-Z])z\g<1> \g<2>)	__class____name__r$   index
IndexErrorresub)r   
class_namer5   s      r   descriptionzSentenceEvaluator.descriptionL   s|     ^^,,
  &'*QR.8J	$$[1E#FU+J vv'DD  		s   A 	A*)A*c                    i S )z{
        Return a dictionary with all meaningful configuration values of the evaluator to store in the model card.
        r   r   s    r   get_config_dictz!SentenceEvaluator.get_config_dictb   s	     	r   c                (     |j                   |fi |S )ae  
        Call the encoder method of the model pass

        Args:
            model (SentenceTransformer): Model we are evaluating
            sentences (str | list[str] | np.ndarray): Text that we are embedding

        Returns:
            list[Tensor] | np.ndarray | Tensor | dict[str, Tensor] | list[dict[str, Tensor]]: The associated embedding
        )encode)r   r   	sentenceskwargss       r   embed_inputszSentenceEvaluator.embed_inputsh   s      u||I000r   )NrB   )
r   r   r   z
str | Noner   intr   rC   r!   zfloat | dict[str, float])r%   dict[str, float]r&   strr!   rD   )r   r   )
r   r   r%   dict[str, Any]r   rC   r,   rC   r!   None)r!   rE   )r!   rF   )r   r   r?   zstr | list[str] | np.ndarrayr!   zPlist[Tensor] | np.ndarray | Tensor | dict[str, Tensor] | list[dict[str, Tensor]])r4   
__module____qualname____doc__r   r   r(   r-   propertyr:   r<   rA   r   r   r   r	   r	      s    
#
 ik(7AQTbe	!6 `aQ(Q3AQJMQY\Q	Q
 E E*1"1 01
 
Z1r   r	   )
__future__r   r7   typingr   r   numpynptorchr   )sentence_transformers.SentenceTransformerr   r	   r   r   r   <module>rR      s&    " 	 %Mk1 k1r   