File size: 850 Bytes
4b112ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# metrics/bleurt.py
import os
# disable GPU (and XLA/PTX) so BLEURT runs on CPU and avoids the libdevice error
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"


"""

BLEURT metric wrappers using HuggingFace evaluate.

"""
from evaluate import load

_hf_bleurt = None


def get_hf_bleurt():
    global _hf_bleurt
    if _hf_bleurt is None:
        _hf_bleurt = load("bleurt", module_type="metric", checkpoint="BLEURT-20")
    return _hf_bleurt


def compute_bleurt_single(reference: str, prediction: str) -> str:
    """

    Compute and format BLEURT score for a single pair.

    """
    if not reference or not prediction:
        return "Please provide both texts."
    bleurt = get_hf_bleurt()
    result = bleurt.compute(predictions=[prediction], references=[reference])
    return f"BLEURT Score: {result['scores'][0]:.4f}"