File size: 12,729 Bytes
9a8a9c5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
"""Code reasoning / completion benchmark adapters (CRUXEval, SAFIM, HumanEval-X)."""

from __future__ import annotations

import re
from typing import Any

from adapters import DatasetAdapter

# Injected at runtime by _set_helpers()
_highlight_code = None
_code_offset = None
_extract_test_classes = None


# ---------------------------------------------------------------------------
# CRUXEval adapter  (HuggingFace: cruxeval-org/cruxeval)
# ---------------------------------------------------------------------------


class CRUXEvalAdapter(DatasetAdapter):
    slug = "cruxeval"
    display_name = "CRUXEval"
    has_ground_truth = False
    has_tasks = True

    def __init__(self, hf_dataset):
        self._ds = hf_dataset

    def problem_count(self) -> int:
        return len(self._ds)

    def get_problem_summary(self, idx: int) -> dict[str, Any]:
        row = self._ds[idx]
        return {
            "idx": idx,
            "task_id": row["id"],
            "entry_point": "f",
            "num_inputs": 1,
            "source": "CRUXEval",
        }

    def get_problem_detail(self, idx: int) -> dict[str, Any]:
        row = self._ds[idx]
        code = row["code"]
        return {
            "idx": idx,
            "task_id": row["id"],
            "entry_point": "f",
            "code": code,
            "highlighted_code": _highlight_code(code),
            "inputs": [row["input"]],
            "outputs": [row["output"]],
            "test": None,
            "tasks": [
                {
                    "name": "Output Prediction",
                    "description": "Given the code and input, predict the output.",
                    "given": "input",
                    "predict": "output",
                    "input": row["input"],
                    "output": row["output"],
                },
                {
                    "name": "Input Prediction",
                    "description": "Given the code and output, predict the input.",
                    "given": "output",
                    "predict": "input",
                    "input": row["input"],
                    "output": row["output"],
                },
            ],
            "source": "CRUXEval",
            "has_ground_truth": False,
            "has_tasks": True,
        }


# ---------------------------------------------------------------------------
# SAFIM adapter  (HuggingFace: gonglinyuan/safim)
# ---------------------------------------------------------------------------


class SAFIMAdapter(DatasetAdapter):
    slug = "safim"
    display_name = "SAFIM"
    has_ground_truth = False
    has_tasks = False

    def __init__(self, hf_dataset):
        self._ds = hf_dataset

    def problem_count(self) -> int:
        return len(self._ds)

    def get_problem_summary(self, idx: int) -> dict[str, Any]:
        row = self._ds[idx]
        return {
            "idx": idx,
            "task_id": row.get("task_id", str(idx)),
            "entry_point": row.get("task_id", f"safim_{idx}"),
            "num_inputs": 0,
            "source": row.get("lang", "unknown"),
        }

    # Patterns that mark where the completion should be inserted
    _HOLE_MARKERS = [
        "{{completion}}",
        "/* TODO: Your code here */",
        "// TODO: Your code here",
        "# TODO: Your code here",
    ]

    def _find_hole_marker(self, prompt: str) -> str | None:
        """Return the first matching hole marker found in the prompt, or None."""
        for marker in self._HOLE_MARKERS:
            if marker in prompt:
                return marker
        return None

    def get_problem_detail(self, idx: int) -> dict[str, Any]:
        row = self._ds[idx]
        prompt = row.get("prompt", "")
        ground_truth = row.get("ground_truth", "")
        lang = row.get("lang", "python")

        marker = self._find_hole_marker(prompt)

        if marker:
            display_code = prompt.replace(marker, "/* [HOLE] */")
            before_hole = prompt.split(marker)[0]
            merged_code = prompt.replace(marker, ground_truth)
        else:
            display_code = prompt + "\n/* [HOLE] */\n"
            before_hole = prompt + "\n"
            merged_code = prompt + "\n" + ground_truth + "\n"

        # Compute 1-indexed line range of the inserted ground truth
        gt_start_line = before_hole.count("\n") + 1
        gt_line_count = ground_truth.count("\n") + (1 if ground_truth else 0)
        gt_end_line = gt_start_line + gt_line_count - 1

        lang_key = {"Python": "python", "Java": "java", "C++": "cpp", "C#": "csharp"}.get(
            lang, lang.lower()
        )

        return {
            "idx": idx,
            "task_id": row.get("task_id", str(idx)),
            "entry_point": row.get("task_id", f"safim_{idx}"),
            "code": display_code,
            "highlighted_code": _highlight_code(display_code, language=lang_key),
            "inputs": [],
            "outputs": [],
            "test": None,
            "tasks": [],
            "source": lang,
            "has_ground_truth": False,
            "has_tasks": False,
            "fim_prefix": prompt,
            "fim_ground_truth": ground_truth,
            "fim_ground_truth_highlighted": _highlight_code(ground_truth, language=lang_key),
            "fim_merged_code": merged_code,
            "fim_merged_highlighted": _highlight_code(
                merged_code,
                highlight_lines=list(range(gt_start_line, gt_end_line + 1)),
                language=lang_key,
            ),
            "fim_gt_start_line": gt_start_line,
            "fim_gt_end_line": gt_end_line,
            "language": lang,
        }


# ---------------------------------------------------------------------------
# HumanEval-X adapter  (HuggingFace: THUDM/humaneval-x)
# ---------------------------------------------------------------------------


def _extract_func_name(declaration: str) -> str:
    """Extract the function/method name from a code declaration string."""
    m = re.search(r"def\s+(\w+)\s*\(", declaration)
    if m:
        return m.group(1)
    m = re.search(r"(\w+)\s*\(", declaration)
    if m:
        return m.group(1)
    return ""


# ---------------------------------------------------------------------------
# HumanEvalPack adapter  (HuggingFace: bigcode/humanevalpack)
# ---------------------------------------------------------------------------


class HumanEvalPackAdapter(DatasetAdapter):
    slug = "humanevalpack"
    display_name = "HumanEvalPack"
    has_ground_truth = False
    has_tasks = False

    LANGUAGES = ["python", "js", "cpp", "go", "java", "rust"]

    def __init__(self, datasets_by_lang: dict[str, Any]):
        self._by_lang = datasets_by_lang
        first_lang = next(iter(self._by_lang))
        self._count = len(self._by_lang[first_lang])

    def problem_count(self) -> int:
        return self._count

    def get_problem_summary(self, idx: int) -> dict[str, Any]:
        first_lang = next(iter(self._by_lang))
        row = self._by_lang[first_lang][idx]
        return {
            "idx": idx,
            "task_id": row["task_id"],
            "entry_point": row.get("entry_point", f"problem_{idx}"),
            "num_inputs": len(self._by_lang),
            "source": "HumanEvalPack",
        }

    def get_problem_detail(self, idx: int) -> dict[str, Any]:
        first_lang = next(iter(self._by_lang))
        row = self._by_lang[first_lang][idx]

        lang_labels = {
            "python": "Python",
            "js": "JavaScript",
            "cpp": "C++",
            "go": "Go",
            "java": "Java",
            "rust": "Rust",
        }
        lang_pygments = {
            "python": "python",
            "js": "javascript",
            "cpp": "cpp",
            "go": "go",
            "java": "java",
            "rust": "rust",
        }

        lang_solutions = []
        for lang in self.LANGUAGES:
            if lang not in self._by_lang:
                continue
            lrow = self._by_lang[lang][idx]
            canonical = lrow.get("prompt", "") + lrow.get("canonical_solution", "")
            buggy = lrow.get("prompt", "") + lrow.get("buggy_solution", "")
            lang_key = lang_pygments.get(lang, lang)
            lang_solutions.append(
                {
                    "language": lang,
                    "language_label": lang_labels.get(lang, lang),
                    "code": canonical,
                    "highlighted_code": _highlight_code(canonical, language=lang_key),
                    "buggy_code": buggy,
                    "buggy_highlighted_code": _highlight_code(buggy, language=lang_key),
                    "test": lrow.get("test", ""),
                    "example_test": lrow.get("example_test", ""),
                    "bug_type": lrow.get("bug_type", ""),
                    "failure_symptoms": lrow.get("failure_symptoms", ""),
                }
            )

        py_row = self._by_lang.get("python", self._by_lang[first_lang])[idx]
        default_code = py_row.get("prompt", "") + py_row.get("canonical_solution", "")

        return {
            "idx": idx,
            "task_id": row["task_id"],
            "entry_point": row.get("entry_point", f"problem_{idx}"),
            "code": default_code,
            "highlighted_code": _highlight_code(default_code),
            "inputs": [],
            "outputs": [],
            "test": py_row.get("test", ""),
            "tasks": [],
            "source": "HumanEvalPack",
            "has_ground_truth": False,
            "has_tasks": False,
            "description": row.get("instruction", row.get("docstring", "")),
            "lang_solutions": lang_solutions,
            "bug_type": py_row.get("bug_type", ""),
            "failure_symptoms": py_row.get("failure_symptoms", ""),
        }


# ---------------------------------------------------------------------------
# HumanEval-X adapter  (HuggingFace: THUDM/humaneval-x)
# ---------------------------------------------------------------------------


class HumanEvalXAdapter(DatasetAdapter):
    slug = "humanevalx"
    display_name = "HumanEval-X"
    has_ground_truth = False
    has_tasks = False

    LANGUAGES = ["python", "cpp", "java", "go", "js"]

    def __init__(self, datasets_by_lang: dict[str, Any]):
        """datasets_by_lang maps language name -> HF dataset split."""
        self._by_lang = datasets_by_lang
        first_lang = next(iter(self._by_lang))
        self._count = len(self._by_lang[first_lang])

    def problem_count(self) -> int:
        return self._count

    def get_problem_summary(self, idx: int) -> dict[str, Any]:
        first_lang = next(iter(self._by_lang))
        row = self._by_lang[first_lang][idx]
        task_id = row["task_id"].split("/")[-1]
        decl = row.get("declaration", row.get("prompt", ""))
        entry = _extract_func_name(decl) or f"problem_{task_id}"
        return {
            "idx": idx,
            "task_id": f"HumanEval/{task_id}",
            "entry_point": entry,
            "num_inputs": len(self._by_lang),
            "source": "HumanEval-X",
        }

    def get_problem_detail(self, idx: int) -> dict[str, Any]:
        first_lang = next(iter(self._by_lang))
        row = self._by_lang[first_lang][idx]
        task_id = row["task_id"].split("/")[-1]
        decl = row.get("declaration", row.get("prompt", ""))
        entry = _extract_func_name(decl) or f"problem_{task_id}"

        lang_solutions = []
        for lang in self.LANGUAGES:
            if lang not in self._by_lang:
                continue
            lrow = self._by_lang[lang][idx]
            code = lrow["prompt"] + lrow["canonical_solution"]
            lang_solutions.append(
                {
                    "language": lang,
                    "code": code,
                    "highlighted_code": _highlight_code(code, language=lang),
                    "test": lrow.get("test", ""),
                    "example_test": lrow.get("example_test", ""),
                }
            )

        py_row = self._by_lang.get("python", self._by_lang[first_lang])[idx]
        default_code = py_row["prompt"] + py_row["canonical_solution"]

        return {
            "idx": idx,
            "task_id": f"HumanEval/{task_id}",
            "entry_point": entry,
            "code": default_code,
            "highlighted_code": _highlight_code(default_code),
            "inputs": [],
            "outputs": [],
            "test": py_row.get("test", ""),
            "tasks": [],
            "source": "HumanEval-X",
            "has_ground_truth": False,
            "has_tasks": False,
            "lang_solutions": lang_solutions,
        }