File size: 9,360 Bytes
030876e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
import argparse
import json
import os
from pathlib import Path
from typing import Any, Dict, Tuple

from tqdm import tqdm

from reward_new_v5 import (
    compute_score,
    compute_completeness_reward,
    compute_hallucination_score_vs_input,
    _compute_classifier_reward,
)


# ---------------------------------------------------------------------------
# Optional external metadata: verified_combined_0-80_clean200.json
# ---------------------------------------------------------------------------

VERIFIED_COMBINED_PATH = (
    "/home/mshahidul/readctrl/code/readctrl_rl_inference/verified_combined_0-80_clean200.json"
)

_VERIFIED_INDEX: Dict[Tuple[int, str], Dict[str, Any]] = {}
_VERIFIED_LOADED = False


def _load_verified_index() -> None:
    global _VERIFIED_LOADED, _VERIFIED_INDEX
    if _VERIFIED_LOADED:
        return
    _VERIFIED_LOADED = True
    if not os.path.exists(VERIFIED_COMBINED_PATH):
        return
    try:
        with open(VERIFIED_COMBINED_PATH, "r", encoding="utf-8") as f:
            data = json.load(f)
    except Exception:
        return

    index: Dict[Tuple[int, str], Dict[str, Any]] = {}
    for row in data:
        try:
            doc_id = int(row.get("doc_id"))
        except Exception:
            continue
        label = str(row.get("label", "")).strip()
        if not label:
            continue
        key = (doc_id, label)
        index[key] = {
            "summary": row.get("summary", ""),
            "fulltext": row.get("fulltext", ""),
        }
    _VERIFIED_INDEX = index


def _lookup_verified(doc_id: Any, label: str) -> Dict[str, Any]:
    """
    Try to fetch (summary, fulltext) for a given (doc_id, label) pair
    from verified_combined_0-80_clean200.json. Returns {} if not found.
    """
    if doc_id is None or not label:
        return {}
    _load_verified_index()
    try:
        doc_id_int = int(doc_id)
    except Exception:
        return {}
    key = (doc_id_int, label.strip())
    return _VERIFIED_INDEX.get(key, {})


def build_solution_str(prediction_text: str, target_level: str) -> str:
    payload = {target_level: prediction_text}
    return f"```json\n{json.dumps(payload, ensure_ascii=False)}\n```"


def build_ground_truth(example: Dict[str, Any]) -> Dict[str, Any]:
    """
    Build ground_truth dict for compute_score from a JSONL row.

    Priority:
    1. Use external metadata from verified_combined_0-80_clean200.json
       (matched by doc_id + label).
    2. Fallback: parse summary / source text from the prompt field.
    """
    summary_text = ""
    input_text = ""

    # 1) Try to get from verified_combined_0-80_clean200.json
    doc_id = example.get("doc_id")
    gold_label = str(example.get("gold_label", "")).strip()
    meta = _lookup_verified(doc_id, gold_label)
    if meta:
        summary_text = str(meta.get("summary", "")).strip()
        input_text = str(meta.get("fulltext", "")).strip()

    # 2) Fallback: parse from prompt if needed
    if not summary_text or not input_text:
        prompt: str = example.get("prompt", "")

        # Very lightweight parsing based on the known template in the prompt.
        marker_summary = "- Gold Summary (the anchor reference summary):"
        marker_source = "- Source Text (detailed content):"

        if marker_summary in prompt and marker_source in prompt:
            before_source = prompt.split(marker_source, 1)[0]
            after_source = prompt.split(marker_source, 1)[1]

            if not summary_text and marker_summary in before_source:
                summary_text = before_source.split(marker_summary, 1)[1].strip()
            if not input_text:
                input_text = after_source.strip()

    return {
        "summary_text": summary_text,
        "input_text": input_text,
    }


def score_row(example: Dict[str, Any]) -> Tuple[float, float, float, float]:
    gold_label = example.get("gold_label", "").strip()
    if not gold_label:
        return float("nan")

    # Prefer explicit JSON in "prediction" if present; otherwise use "generated_text".
    raw_prediction = example.get("prediction")
    if isinstance(raw_prediction, str) and raw_prediction.strip():
        try:
            parsed = json.loads(raw_prediction)
            prediction_text = parsed.get(gold_label, "")
        except Exception:
            prediction_text = example.get("generated_text", "")
    else:
        prediction_text = example.get("generated_text", "")

    if not prediction_text or not prediction_text.strip():
        nan = float("nan")
        return nan, nan, nan, nan

    # Build common pieces
    solution_str = build_solution_str(prediction_text, gold_label)
    ground_truth = build_ground_truth(example)
    extra_info = {"target_level": gold_label}

    # Overall reward (for reference)
    total_reward = compute_score(
        data_source="jsonl_offline_eval",
        solution_str=solution_str,
        ground_truth=ground_truth,
        extra_info=extra_info,
    )

    summary_text = ground_truth.get("summary_text", "")
    input_text = ground_truth.get("input_text", "")

    # Component scores
    completeness = None
    if summary_text and summary_text.strip():
        completeness = compute_completeness_reward(
            summary_text=summary_text,
            generated_text=prediction_text,
            threshold=0.5,
            batch_size=128,
        )

    classifier = _compute_classifier_reward(gold_label, prediction_text)

    hallucination = None
    if input_text and input_text.strip():
        hallucination = compute_hallucination_score_vs_input(
            input_text=input_text,
            generated_text=prediction_text,
            threshold=0.5,
            batch_size=128,
        )

    # Normalise None → NaN for easy averaging
    def _to_float(x):
        return float("nan") if x is None else float(x)

    return (
        float(total_reward),
        _to_float(completeness),
        float(classifier),
        _to_float(hallucination),
    )


def compute_avg_scores(path: str) -> Tuple[float, float, float, float]:
    total_reward = 0.0
    total_compl = 0.0
    total_class = 0.0
    total_hallu = 0.0

    n_reward = 0
    n_compl = 0
    n_class = 0
    n_hallu = 0

    with open(path, "r", encoding="utf-8") as f:
        for line in tqdm(f, desc="Scoring examples"):
            line = line.strip()
            if not line:
                continue
            try:
                example = json.loads(line)
            except Exception:
                continue

            reward, compl, clf, hallu = score_row(example)

            # Reward
            if reward == reward:  # not NaN
                total_reward += reward
                n_reward += 1

            # Completeness
            if compl == compl:
                total_compl += compl
                n_compl += 1

            # Classifier
            if clf == clf:
                total_class += clf
                n_class += 1

            # Hallucination
            if hallu == hallu:
                total_hallu += hallu
                n_hallu += 1

    def _avg(total: float, n: int) -> float:
        if n == 0:
            return float("nan")
        return total / n

    return (
        _avg(total_reward, n_reward),
        _avg(total_compl, n_compl),
        _avg(total_class, n_class),
        _avg(total_hallu, n_hallu),
    )


def _parse_args() -> argparse.Namespace:
    parser = argparse.ArgumentParser(
        description=(
            "Compute average reward over a JSONL file "
            "containing GPT-5 inference outputs."
        )
    )
    parser.add_argument(
        "jsonl_path",
        type=str,
        help="Path to JSONL file with GPT-5 inference outputs.",
    )
    return parser.parse_args()


def _save_results(
    jsonl_path: str,
    avg_reward: float,
    avg_compl: float,
    avg_class: float,
    avg_hallu: float,
) -> None:
    """
    Save aggregate metrics to test_result_v5 as a JSON file.
    """
    output_dir = Path("/home/mshahidul/readctrl/code/readctrl_rl_inference/test_result_v5")
    output_dir.mkdir(parents=True, exist_ok=True)

    basename = os.path.basename(jsonl_path)
    stem = os.path.splitext(basename)[0]
    # Save using the input filename stem so the stats file
    # clearly corresponds to the original JSONL.
    out_path = output_dir / f"{stem}.json"

    payload = {
        "input_jsonl": os.path.abspath(jsonl_path),
        "avg_reward": avg_reward,
        "avg_completeness": avg_compl,
        "avg_classifier": avg_class,
        "avg_hallucination": avg_hallu,
    }

    with out_path.open("w", encoding="utf-8") as f:
        json.dump(payload, f, ensure_ascii=False, indent=2)


def main() -> None:
    args = _parse_args()
    avg_reward, avg_compl, avg_class, avg_hallu = compute_avg_scores(args.jsonl_path)

    # Plain-text, easy-to-parse output
    print(f"avg_reward         = {avg_reward:.6f}")
    print(f"avg_completeness   = {avg_compl:.6f}")
    print(f"avg_classifier     = {avg_class:.6f}")
    print(f"avg_hallucination  = {avg_hallu:.6f}")

    # Save to JSON in test_result_v5 for later analysis.
    _save_results(
        jsonl_path=args.jsonl_path,
        avg_reward=avg_reward,
        avg_compl=avg_compl,
        avg_class=avg_class,
        avg_hallu=avg_hallu,
    )


if __name__ == "__main__":
    main()