rasdani's picture
Update README.md
a421d28 verified
metadata
dataset_info:
  features:
    - name: problem_id
      dtype: string
    - name: source
      dtype: string
    - name: task_type
      dtype: string
    - name: in_source_id
      dtype: string
    - name: prompt
      dtype: string
    - name: golden_diff
      dtype: string
    - name: verification_info
      dtype: string
  splits:
    - name: train
      num_bytes: 73682953
      num_examples: 1641
  download_size: 25413228
  dataset_size: 73682953
configs:
  - config_name: default
    data_files:
      - split: train
        path: data/train-*
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 fenced by three backticks.

Here is an example:

\```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

\```
"""

ds = load_dataset("rasdani/github-patches-10k-sample-sorted", split="train")


def prepend_line_numbers(file_content: str) -> str:
    if not file_content:
        return ""
    lines = file_content.split('\n')
    lines = [f"{i+1} {line}" for i, line in enumerate(lines)]
    ret = '\n'.join(lines)
    ret = ret.strip() + "\n"
    return ret

def normalize_diff(diff_text: str) -> str:
    diff_text = re.sub(r'(?m)^index [^\n]*\n', '', diff_text)
    diff_text = re.sub(r'(?m)^(@@[^@]*@@).*', r'\1', diff_text)
    diff_text = diff_text.strip() + "\n"
    return diff_text

def filter_diff_by_files(diff: str, touched_files: set) -> str:
    """Filter a git diff to only include changes for specific files."""
    if not touched_files:
        return diff
    
    lines = diff.split('\n')
    filtered_lines = []
    include_section = False
    
    for line in lines:
        if line.startswith('diff --git'):
            # Check if this file should be included
            # Extract the file path from "diff --git a/path b/path"
            match = re.match(r'diff --git a/(.*?) b/', line)
            if match:
                file_path = match.group(1)
                include_section = file_path in touched_files
            else:
                include_section = False
        
        if include_section:
            filtered_lines.append(line)
    
    return '\n'.join(filtered_lines)

def create_golden_diff(example):
    before_paths = [b["path"] for b in example["before_files"]]
    after_paths = [a["path"] for a in example["after_files"]]
    touched_files = set(before_paths) | set(after_paths)
    filtered_diff = filter_diff_by_files(example["pr_diff"], touched_files)
    golden_diff = normalize_diff(filtered_diff)
    for path in touched_files:
        assert path in golden_diff, f"Path {path} not found in golden diff {golden_diff}"
    verification_info_dict = {
        "golden_diff": golden_diff,
        "issue": example["issue"],
        "before_files": example["before_files"],
        "after_files": example["after_files"],
    }
    verification_info = json.dumps(verification_info_dict)
    return {"golden_diff": golden_diff, "verification_info": verification_info}

def create_prompt(example):
    golden_diff = example["golden_diff"]
    issue = example["issue"]
    before_files = example["before_files"]
    file_context = [f"Path: `{x['path']}`\nContent:\n```\n{prepend_line_numbers(x['content'])}```" for x in before_files]
    file_context = "\n\n".join(file_context)
    prompt = PROMPT_TEMPLATE.format(issue=issue, file_context=file_context, golden_diff=golden_diff)
    print(prompt)
    print("="*100)
    return {"prompt": prompt}

    

ds_up = ds.map(lambda x, idx: {"problem_id": f"gh_patches_debug_{idx}"}, with_indices=True)
ds_up = ds_up.map(lambda x: {"source": "rasdani/github-patches", "task_type": "git_diff"})

ds_up = ds_up.map(create_golden_diff, num_proc=10)
# example = ds_up[0]
# create_prompt(example)
ds_up = ds_up.map(create_prompt, num_proc=10)

ds_up = ds_up.select_columns(["problem_id", "source", "task_type", "in_source_id", "prompt", "golden_diff", "verification_info"])

# ds_up.push_to_hub("rasdani/github-patches-debug")
ds_up.push_to_hub("rasdani/github-patches-debug-genesys")