rasdani/github-patches-debug-genesys
import re import json from datasets import load_dataset PROMPT_TEMPLATE = """\ We are currently solving the following issue within our repository. Here is the issue text: --- BEGIN ISSUE --- {issue} --- END ISSUE --- Below are some code segments, each from a relevant file. One or more of these files may contain bugs. --- BEGIN FILES --- {file_context} --- END FILES --- Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format… See the full description on the dataset page: https://huggingface.co/datasets/rasdani/github-patches-debug-genesys.
064
1---2dataset_info:3 features:4 - name: problem_id5 dtype: string6 - name: source7 dtype: string8 - name: task_type9 dtype: string10 - name: in_source_id11 dtype: string12 - name: prompt13 dtype: string14 - name: golden_diff15 dtype: string16 - name: verification_info17 dtype: string18 splits:19 - name: train20 num_bytes: 7368295321 num_examples: 164122 download_size: 2541322823 dataset_size: 7368295324configs:25- config_name: default26 data_files:27 - split: train28 path: data/train-*29---30```python31import re32import json33from datasets import load_dataset34 35PROMPT_TEMPLATE = """\36We are currently solving the following issue within our repository. Here is the issue text:37--- BEGIN ISSUE ---38{issue}39--- END ISSUE ---40 41Below are some code segments, each from a relevant file. One or more of these files may contain bugs.42--- BEGIN FILES ---43{file_context}44--- END FILES ---45 46Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.47 48Here is an example:49 50\```diff51diff --git a/examples/server_async.py b/examples/server_async.py52--- a/examples/server_async.py53+++ b/examples/server_async.py54@@ -313,4 +313,4 @@55 56 57 if __name__ == "__main__":58- asyncio.run(run_async_server("."), debug=True)59+ asyncio.run(run_async_server(), debug=True)60diff --git a/examples/server_sync.py b/examples/server_sync.py61--- a/examples/server_sync.py62+++ b/examples/server_sync.py63@@ -313,5 +313,5 @@64 65 66 if __name__ == "__main__":67- server = run_sync_server(".")68+ server = run_sync_server()69 server.shutdown()70 71\```72"""73 74ds = load_dataset("rasdani/github-patches-10k-sample-sorted", split="train")75 76 77def prepend_line_numbers(file_content: str) -> str:78 if not file_content:79 return ""80 lines = file_content.split('\n')81 lines = [f"{i+1} {line}" for i, line in enumerate(lines)]82 ret = '\n'.join(lines)83 ret = ret.strip() + "\n"84 return ret85 86def normalize_diff(diff_text: str) -> str:87 diff_text = re.sub(r'(?m)^index [^\n]*\n', '', diff_text)88 diff_text = re.sub(r'(?m)^(@@[^@]*@@).*', r'\1', diff_text)89 diff_text = diff_text.strip() + "\n"90 return diff_text91 92def filter_diff_by_files(diff: str, touched_files: set) -> str:93 """Filter a git diff to only include changes for specific files."""94 if not touched_files:95 return diff96 97 lines = diff.split('\n')98 filtered_lines = []99 include_section = False100 101 for line in lines:102 if line.startswith('diff --git'):103 # Check if this file should be included104 # Extract the file path from "diff --git a/path b/path"105 match = re.match(r'diff --git a/(.*?) b/', line)106 if match:107 file_path = match.group(1)108 include_section = file_path in touched_files109 else:110 include_section = False111 112 if include_section:113 filtered_lines.append(line)114 115 return '\n'.join(filtered_lines)116 117def create_golden_diff(example):118 before_paths = [b["path"] for b in example["before_files"]]119 after_paths = [a["path"] for a in example["after_files"]]120 touched_files = set(before_paths) | set(after_paths)121 filtered_diff = filter_diff_by_files(example["pr_diff"], touched_files)122 golden_diff = normalize_diff(filtered_diff)123 for path in touched_files:124 assert path in golden_diff, f"Path {path} not found in golden diff {golden_diff}"125 verification_info_dict = {126 "golden_diff": golden_diff,127 "issue": example["issue"],128 "before_files": example["before_files"],129 "after_files": example["after_files"],130 }131 verification_info = json.dumps(verification_info_dict)132 return {"golden_diff": golden_diff, "verification_info": verification_info}133 134def create_prompt(example):135 golden_diff = example["golden_diff"]136 issue = example["issue"]137 before_files = example["before_files"]138 file_context = [f"Path: `{x['path']}`\nContent:\n```\n{prepend_line_numbers(x['content'])}```" for x in before_files]139 file_context = "\n\n".join(file_context)140 prompt = PROMPT_TEMPLATE.format(issue=issue, file_context=file_context, golden_diff=golden_diff)141 print(prompt)142 print("="*100)143 return {"prompt": prompt}144 145 146 147ds_up = ds.map(lambda x, idx: {"problem_id": f"gh_patches_debug_{idx}"}, with_indices=True)148ds_up = ds_up.map(lambda x: {"source": "rasdani/github-patches", "task_type": "git_diff"})149 150ds_up = ds_up.map(create_golden_diff, num_proc=10)151# example = ds_up[0]152# create_prompt(example)153ds_up = ds_up.map(create_prompt, num_proc=10)154 155ds_up = ds_up.select_columns(["problem_id", "source", "task_type", "in_source_id", "prompt", "golden_diff", "verification_info"])156 157# ds_up.push_to_hub("rasdani/github-patches-debug")158ds_up.push_to_hub("rasdani/github-patches-debug-genesys")159```