|
1 |
| -from star_align.utils import read_jsonl, write_jsonl |
2 |
| -import sys |
3 |
| -import re |
| 1 | +import ast |
| 2 | +import random |
| 3 | +from dataclasses import dataclass, field |
| 4 | +from pathlib import Path |
| 5 | +from typing import cast |
4 | 6 |
|
5 |
| -dataset = read_jsonl(sys.argv[1]) |
| 7 | +from tqdm.auto import tqdm |
| 8 | +from transformers import HfArgumentParser |
6 | 9 |
|
7 |
| -def contains_chinese(s): |
8 |
| - return bool(re.search(r'[\u4e00-\u9fff]', s)) |
| 10 | +from star_align.utils import find_code_blocks, read_jsonl, write_jsonl |
9 | 11 |
|
10 |
| -chosen = [] |
11 |
| -rejected = [] |
12 |
| -for example in dataset: |
13 |
| - if "code snippet" in example["instruction"] or contains_chinese(example["instruction"] + example["response"]): |
14 |
| - rejected.append(example) |
15 |
| - else: |
16 |
| - chosen.append(example) |
17 | 12 |
|
18 |
| -print(f"Removed {len(dataset) - len(chosen)} examples") |
19 |
| -write_jsonl(sys.argv[2], chosen) |
| 13 | +@dataclass(frozen=True) |
| 14 | +class Args: |
| 15 | + data_files: list[str] |
| 16 | + output_file: str |
| 17 | + diversify_func_names: bool = field(default=False) |
| 18 | + |
| 19 | + |
| 20 | +def extract_and_concat_function_names(python_content): |
| 21 | + """ |
| 22 | + Extracts all function names from a given Python content string and concatenates them into a single string. |
| 23 | +
|
| 24 | + Parameters: |
| 25 | + - python_content: A string containing the Python code to analyze. |
| 26 | +
|
| 27 | + Returns: |
| 28 | + - A string containing all function names defined in the content, concatenated. |
| 29 | + """ |
| 30 | + tree = ast.parse(python_content) |
| 31 | + function_names = [] |
| 32 | + |
| 33 | + # Define a node visitor that adds the name of each function definition it visits |
| 34 | + class FunctionDefVisitor(ast.NodeVisitor): |
| 35 | + def visit_FunctionDef(self, node): |
| 36 | + function_names.append(node.name) |
| 37 | + # Process the subtree for this node |
| 38 | + self.generic_visit(node) |
| 39 | + |
| 40 | + def visit_AsyncFunctionDef(self, node): |
| 41 | + function_names.append(node.name) |
| 42 | + self.generic_visit(node) |
| 43 | + |
| 44 | + # Create a node visitor and walk through the AST |
| 45 | + visitor = FunctionDefVisitor() |
| 46 | + visitor.visit(tree) |
| 47 | + |
| 48 | + # Concatenate all function names into a single string |
| 49 | + return " ".join(function_names) |
| 50 | + |
| 51 | + |
| 52 | +def main(): |
| 53 | + args = cast(Args, HfArgumentParser(Args).parse_args_into_dataclasses()[0]) |
| 54 | + raw_data: list[dict] = [] |
| 55 | + for data_file in args.data_files: |
| 56 | + data = read_jsonl(Path(data_file)) |
| 57 | + # language = data_file.split("-")[1] |
| 58 | + # assert language in ALL_LANGS, f"Unknown language {language}" |
| 59 | + # raw_data.extend(dict(lang=language, **d) for d in data) |
| 60 | + raw_data.extend(data) |
| 61 | + # common keys for all d in data |
| 62 | + common_keys = set.intersection(*(set(d.keys()) for d in raw_data)) |
| 63 | + raw_data = [{k: d[k] for k in common_keys} for d in raw_data] |
| 64 | + print(f"Common keys: {common_keys}") |
| 65 | + # counter = defaultdict[str, int](int) |
| 66 | + |
| 67 | + def mk_key(instruction: str) -> str: |
| 68 | + return "".join(instruction.split()) |
| 69 | + |
| 70 | + random.seed(0) |
| 71 | + random.shuffle(raw_data) |
| 72 | + |
| 73 | + seen_keys = set[str]() |
| 74 | + new_data = list[dict]() |
| 75 | + for d in tqdm(raw_data): |
| 76 | + key_i, key_r = mk_key(d["instruction"]), mk_key(d["response"]) |
| 77 | + if key_i in seen_keys or key_r in seen_keys: |
| 78 | + continue |
| 79 | + if args.diversify_func_names: |
| 80 | + code_block = find_code_blocks(d["response"])[0] |
| 81 | + try: |
| 82 | + fn_names = extract_and_concat_function_names(code_block) |
| 83 | + except SyntaxError: |
| 84 | + continue |
| 85 | + if fn_names in seen_keys: |
| 86 | + continue |
| 87 | + seen_keys.add(fn_names) |
| 88 | + new_data.append(d) |
| 89 | +
616B
seen_keys.add(key_i) |
| 90 | + seen_keys.add(key_r) |
| 91 | + |
| 92 | + print(f"Chose {len(new_data)} out of {len(raw_data)}") |
| 93 | + write_jsonl(Path(args.output_file), new_data) |
| 94 | + |
| 95 | + |
| 96 | +if __name__ == "__main__": |
| 97 | + main() |
0 commit comments