Download scripts/prepare_livecodebench_cache.py from yaro1214/daim: direct link, hf CLI and curl.
- Browser
- Download file 2.28 kB
-
https://huggingface.co/yaro1214/daim/resolve/main/scripts/prepare_livecodebench_cache.py
- Command line
-
hf download hf://yaro1214/daim/scripts/prepare_livecodebench_cache.py
-
curl -L -o prepare_livecodebench_cache.py https://huggingface.co/yaro1214/daim/resolve/main/scripts/prepare_livecodebench_cache.py
2.28 kB
| #!/usr/bin/env python3 | |
| """Build the prompt-only DFlash cache from complete rows in a raw JSONL.""" | |
| from __future__ import annotations | |
| import argparse | |
| import json | |
| import os | |
| from pathlib import Path | |
| def _format_livecodebench(row: dict) -> str: | |
| prompt = ( | |
| "You are an expert Python programmer. You will be given a question " | |
| "(problem specification) and will generate a correct Python program " | |
| "that matches the specification and passes all tests. You will NOT " | |
| "return anything except for the program\n\n" | |
| f"### Question:\n{row['question_content']}\n\n" | |
| ) | |
| if row.get("starter_code"): | |
| prompt += ( | |
| "### Format: Use the following code structure:\n" | |
| f"```python\n{row['starter_code']}\n```\n\n" | |
| ) | |
| else: | |
| prompt += ( | |
| "### Format: Write your code in the following format:\n" | |
| "```python\n# YOUR CODE HERE\n```\n\n" | |
| ) | |
| return prompt + "### Answer: (use the provided format with backticks)" | |
| def main() -> None: | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument("--input", type=Path, required=True) | |
| parser.add_argument("--output", type=Path, required=True) | |
| parser.add_argument("--max-samples", type=int, required=True) | |
| args = parser.parse_args() | |
| args.output.parent.mkdir(parents=True, exist_ok=True) | |
| tmp_path = args.output.with_name(f".{args.output.name}.tmp.{os.getpid()}") | |
| count = 0 | |
| try: | |
| with args.input.open() as source, tmp_path.open("w") as target: | |
| for line in source: | |
| if count >= args.max_samples: | |
| break | |
| try: | |
| row = json.loads(line) | |
| except json.JSONDecodeError: | |
| break | |
| turns = [_format_livecodebench(row)] | |
| target.write(json.dumps({"turns": turns}) + "\n") | |
| count += 1 | |
| if count != args.max_samples: | |
| raise RuntimeError( | |
| f"requested {args.max_samples} complete rows, found {count}" | |
| ) | |
| os.replace(tmp_path, args.output) | |
| finally: | |
| tmp_path.unlink(missing_ok=True) | |
| print(f"[cached] {args.output} ({count} samples)") | |
| if __name__ == "__main__": | |
| main() | |