File size: 9,463 Bytes
a095974
a23241d
 
a095974
 
 
 
 
 
 
 
c0f0dd5
a095974
 
 
 
 
 
 
a23241d
a095974
 
 
 
a23241d
a095974
 
 
 
 
 
 
 
a23241d
a095974
 
a23241d
a095974
 
 
 
a23241d
 
a095974
a23241d
 
 
 
 
a095974
a23241d
a095974
a23241d
 
 
a095974
 
a23241d
a095974
 
 
 
a23241d
 
a095974
a23241d
a095974
 
a23241d
a095974
a23241d
 
 
 
 
a095974
 
a23241d
a095974
 
 
 
a23241d
a095974
 
a23241d
a095974
a23241d
a095974
 
a23241d
a095974
a23241d
 
 
 
a095974
a23241d
 
a095974
 
 
 
 
 
 
 
a23241d
 
a095974
 
a23241d
a095974
 
 
a23241d
a095974
 
 
 
a23241d
 
a095974
 
 
 
a23241d
a095974
 
 
a23241d
a095974
 
a23241d
a095974
 
 
a23241d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a095974
 
a23241d
a095974
a23241d
a095974
a23241d
 
a095974
 
 
 
 
 
 
a23241d
a095974
 
a23241d
a095974
 
 
 
 
 
a23241d
a095974
 
 
a23241d
a095974
 
 
 
a23241d
a095974
a23241d
a095974
 
a23241d
a095974
a23241d
a095974
a23241d
 
 
 
 
 
 
a095974
a23241d
a095974
 
 
 
 
 
 
 
 
 
a23241d
 
 
 
 
 
 
 
a095974
 
 
 
a23241d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
"""
Evaluation module: loads models, computes metrics, and creates visualizations.
Lightweight, CPU-friendly, no Java required.
"""

import re
import math
import uuid
from typing import List, Dict, Tuple

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import torch
from transformers import AutoTokenizer, AutoModelForSequenceClassification
from sentence_transformers import SentenceTransformer, util

# --------------------------
# MODEL LOADING
# --------------------------
NLI_MODEL = "textattack/roberta-base-MNLI"
EMBED_MODEL = "sentence-transformers/all-MiniLM-L6-v2"

# Load NLI model & tokenizer
nli_tokenizer = AutoTokenizer.from_pretrained(NLI_MODEL)
nli_model = AutoModelForSequenceClassification.from_pretrained(NLI_MODEL)
nli_model.to("cpu")
nli_model.eval()

# Load embedding model
embed_model = SentenceTransformer(EMBED_MODEL)

# Label mapping from config
id2label = {int(k): v.upper() for k, v in nli_model.config.id2label.items()}


# --------------------------
# METRIC FUNCTIONS
# --------------------------
def check_instruction_following(prompt: str, response: str) -> float:
    """Embedding-based similarity between prompt and response."""
    if not prompt or not response:
        return 0.0
    p_emb = embed_model.encode(prompt, convert_to_tensor=True)
    r_emb = embed_model.encode(response, convert_to_tensor=True)
    sim = float(util.cos_sim(p_emb, r_emb).item())
    return round(max(0.0, min(1.0, sim)), 3)


def check_hallucination(reference: str, response: str) -> float:
    """
    Single hallucination score:
    Entailment prob - Contradiction prob (normalized to [0,1]).
    Higher = less hallucination.
    """
    if not reference or not response:
        return 0.0
    with torch.no_grad():
        inputs = nli_tokenizer.encode_plus(reference, response, return_tensors="pt", truncation=True)
        outputs = nli_model(**inputs)
        probs = torch.softmax(outputs.logits, dim=-1).cpu().numpy()[0]

    entail_prob, contra_prob = 0.0, 0.0
    for idx, p in enumerate(probs):
        label = id2label.get(idx, "")
        if "ENTAIL" in label:
            entail_prob = float(p)
        elif "CONTRA" in label:
            contra_prob = float(p)

    score = entail_prob - contra_prob
    score = (score + 1) / 2  # normalize [-1,1] β†’ [0,1]
    return round(max(0.0, min(1.0, score)), 3)


def check_assumption(response: str) -> float:
    """Detect speculative/hedging terms."""
    if not response:
        return 0.0
    speculative_terms = ["maybe", "probably", "might", "perhaps", "i guess", "seems", "could"]
    count = sum(1 for t in speculative_terms if t in response.lower())
    score = 1.0 - min(count / 5.0, 1.0)  # smoother decay
    return round(score, 3)


def check_coherence(response: str) -> float:
    """Heuristic coherence metric: penalizes very short/long, rewards sentence balance."""
    if not response:
        return 0.0
    words = len(re.findall(r"\w+", response))
    sents = max(1, len(re.split(r"[.!?]+", response)) - 1)
    if words < 5:
        return 0.3
    if words > 200:
        return 0.5
    base = min(1.0, (words / 50.0) + (sents / 5.0))
    return round(max(0.4, min(base, 0.95)), 3)


def check_accuracy(reference: str, response: str) -> float:
    """Semantic similarity between reference and response via embeddings (cosine)."""
    if not reference or not response:
        return 0.0
    ref_emb = embed_model.encode(reference, convert_to_tensor=True)
    resp_emb = embed_model.encode(response, convert_to_tensor=True)
    sim = float(util.cos_sim(ref_emb, resp_emb).item())
    return round(max(0.0, min(1.0, sim)), 3)


# --------------------------
# SCORING PIPELINE
# --------------------------
def compute_row_scores(prompt, response, reference) -> Dict:
    instr = check_instruction_following(prompt, response)
    halluc = check_hallucination(reference, response)
    assum = check_assumption(response)
    coh = check_coherence(response)
    acc = check_accuracy(reference, response)

    # Final score: average
    components = [instr, halluc, assum, coh, acc]
    final = round(float(sum(components) / len(components)), 3)

    return {
        "InstructionFollowing": instr,
        "Hallucination": halluc,
        "AssumptionControl": assum,
        "Coherence": coh,
        "Accuracy": acc,
        "FinalScore": final,
    }


# --------------------------
# VISUALIZATION HELPERS
# --------------------------
# def spider_net_multi(labels: List[str], rows: List[Dict], title: str, fill_alpha: float = 0.12):
#     """Radar chart for multiple agents."""
#     N = len(labels)
#     angles = [n / float(N) * 2 * math.pi for n in range(N)]
#     angles += angles[:1]

#     fig = plt.figure(figsize=(6.5, 6.5))
#     ax = plt.subplot(111, polar=True)
#     ax.set_xticks(angles[:-1])
#     ax.set_xticklabels(labels, fontsize=9)
#     ax.set_ylim(0, 100)
#     ax.set_yticks([0, 25, 50, 75, 100])

#     for r in rows:
#         values = r["values"]
#         values_closed = values + values[:1]
#         ax.plot(angles, values_closed, linewidth=1.5, label=r["name"])
#         ax.fill(angles, values_closed, alpha=fill_alpha)

#     ax.set_title(title, y=1.08, fontsize=12)
#     ax.legend(loc="upper right", bbox_to_anchor=(1.25, 1.1))
#     return fig


# def heatmap_plot(df: pd.DataFrame, metric_cols: List[str], title: str = "Metric Correlations"):
#     fig, ax = plt.subplots(figsize=(7, 5))
#     sns.heatmap(df[metric_cols].corr(), annot=True, fmt=".2f", cmap="coolwarm", ax=ax)
#     ax.set_title(title)
#     return fig


# --------------------------
# HIGH-LEVEL EVALUATION
# --------------------------
def evaluate_dataframe(df: pd.DataFrame) -> Tuple[pd.DataFrame, List[Tuple[str, str]], pd.DataFrame]:
    """
    df must contain: prompt, response, task, agent, reference
    Returns: metrics_df, [(image_path, caption)], leaderboard_df
    """
    df = df.rename(columns={c: c.strip() for c in df.columns})

    rows = []
    for _, r in df.iterrows():
        prompt = r.get("prompt", "")
        response = r.get("response", "")
        reference = r.get("reference", "")
        agent = r.get("agent", "Unknown")
        task = r.get("task", "Unknown")

        scores = compute_row_scores(prompt, response, reference)
        entry = {
            "Task": str(task).strip(),
            "Agent": str(agent),
            "Prompt": prompt,
            "Response": response,
            "Reference": reference,
        }
        entry.update(scores)
        rows.append(entry)

    metrics_df = pd.DataFrame(rows)

    # Visualization artifacts
    images = []
    metric_labels = ["InstructionFollowing", "Hallucination", "AssumptionControl", "Coherence", "Accuracy"]

    # Per-task radar and bar charts
    for task, g in metrics_df.groupby("Task"):
        series = []
        for a in g["Agent"].unique():
            subset = g[g["Agent"] == a]
            vals = [round(float(subset[m].mean()) * 100, 2) for m in metric_labels]
            series.append({"name": a, "values": vals})
        if series:
            fig = spider_net_multi(metric_labels, series, title=f"{task} β€” Agent Comparison")
            fname = f"/tmp/{uuid.uuid4().hex}_{task}_radar.png"
            fig.savefig(fname, bbox_inches="tight")
            plt.close(fig)
            images.append((fname, f"{task} - radar"))

            fig2, ax = plt.subplots(figsize=(8, 4))
            avg = g.groupby("Agent")[metric_labels].mean()
            avg.plot(kind="bar", ax=ax)
            ax.set_title(f"{task} β€” Average Metrics by Agent")
            ax.set_ylabel("Score (0-1)")
            plt.xticks(rotation=45)
            fname2 = f"/tmp/{uuid.uuid4().hex}_{task}_bar.png"
            fig2.savefig(fname2, bbox_inches="tight")
            plt.close(fig2)
            images.append((fname2, f"{task} - bar"))

    # Global heatmap
    metric_cols = metric_labels + ["FinalScore"]
    figh = heatmap_plot(metrics_df, metric_cols)
    fnameh = f"/tmp/{uuid.uuid4().hex}_heatmap.png"
    figh.savefig(fnameh, bbox_inches="tight")
    plt.close(figh)
    images.append((fnameh, "Metric Correlations Heatmap"))

    # Leaderboard
    lb = metrics_df.groupby(["Agent", "Task"])["FinalScore"].mean().reset_index()
    lb = lb.sort_values(["FinalScore"], ascending=False)

    return metrics_df, images, lb


# --------------------------
# DEMO USAGE
# --------------------------
if __name__ == "__main__":
    # Sample dataset
    data = [
        {"task": "Math QA", "agent": "AgentA", "prompt": "What is 2+2?", "response": "The answer is 4.", "reference": "2+2=4"},
        {"task": "Math QA", "agent": "AgentB", "prompt": "What is 2+2?", "response": "It might be 5, but usually 4.", "reference": "2+2=4"},
        {"task": "Summarization", "agent": "AgentA", "prompt": "Summarize: 'The cat sat on the mat. The dog barked.'", "response": "A cat sat while a dog barked.", "reference": "Cat on mat, dog barking."},
    ]
    df = pd.DataFrame(data)

    metrics_df, images, leaderboard = evaluate_dataframe(df)

    print("\n=== Metrics per response ===")
    print(metrics_df[["Task", "Agent", "InstructionFollowing", "Hallucination", "AssumptionControl", "Coherence", "Accuracy", "FinalScore"]])

    print("\n=== Leaderboard (average per task & agent) ===")
    print(leaderboard)

    print("\nVisualization files saved in /tmp/:")
    for path, caption in images:
        print(f"{caption}: {path}")