File size: 5,082 Bytes
dc59b01
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

import json
import subprocess
import sys
import argparse
import re
import sqlite3
from pathlib import Path

import torch
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
from peft import PeftModel
from prompting import encode_prompt


# ---------------- PARSE ACC ----------------
def _parse_exec_accuracy(stdout: str) -> float | None:
    for line in stdout.splitlines():
        if line.strip().startswith("execution"):
            try:
                return float(line.split()[-1])
            except:
                return None
    return None


# ---------------- CLEAN SQL ----------------
def clean_prediction(pred_sql: str) -> str:
    pred_sql = pred_sql.strip()

    if "SQL:" in pred_sql:
        pred_sql = pred_sql.split("SQL:")[-1]

    pred_sql = pred_sql.replace('"', "'")
    pred_sql = re.sub(r"\s+", " ", pred_sql).strip()

    if not pred_sql.endswith(";"):
        pred_sql += ";"

    return pred_sql


def main():

    parser = argparse.ArgumentParser()
    parser.add_argument("--adapter", type=str, default="checkpoints/sft_t5")
    parser.add_argument("--num_samples", type=int, default=1000)
    args = parser.parse_args()

    project_root = Path(__file__).resolve().parents[1]
    adapter_dir = project_root / args.adapter

    db_root = project_root / "data/database"
    table_json = project_root / "data/tables.json"
    dev_json = project_root / "data/dev.json"
    gold_sql = project_root / "data/dev_gold.sql"
    pred_path = project_root / "pred.sql"

    if not adapter_dir.exists():
        raise FileNotFoundError(f"Missing adapter dir: {adapter_dir}")

    # ---------------- DEVICE ----------------
    device = "mps" if torch.backends.mps.is_available() else (
        "cuda" if torch.cuda.is_available() else "cpu"
    )
    print("Using device:", device)

    # ---------------- LOAD MODEL ----------------
    BASE_MODEL = "t5-small"

    tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL)
    base = AutoModelForSeq2SeqLM.from_pretrained(BASE_MODEL).to(device)

    model = PeftModel.from_pretrained(base, str(adapter_dir)).to(device)
    model = model.merge_and_unload()
    model.eval()

    if tokenizer.pad_token_id is None:
        tokenizer.pad_token = tokenizer.eos_token

    # ---------------- LOAD DATA ----------------
    with dev_json.open() as f:
        dev = json.load(f)[: args.num_samples]

    print("Generating predictions...\n")

    correct = 0
    total = len(dev)

    # ---------------- GENERATE + LIVE EXEC ----------------
    with pred_path.open("w") as out_f, torch.no_grad():

        for i, ex in enumerate(dev, start=1):

            db_id = ex["db_id"]
            question = ex["question"]
            gold_query = ex["query"]

            prompt_ids = encode_prompt(
                tokenizer,
                question,
                db_id,
                device=device,
                max_input_tokens=512,
            )

            input_ids = prompt_ids.unsqueeze(0).to(device)
            attention_mask = (input_ids != tokenizer.pad_token_id).long().to(device)

            outputs = model.generate(
                input_ids=input_ids,
                attention_mask=attention_mask,
                max_new_tokens=160,
                num_beams=4,
                do_sample=False,
                early_stopping=True,
            )

            pred_sql = tokenizer.decode(outputs[0], skip_special_tokens=True)
            pred_sql = clean_prediction(pred_sql)

            out_f.write(pred_sql + "\n")

            # -------- LIVE EXECUTION CHECK --------
            try:
                db_path = db_root / db_id / f"{db_id}.sqlite"

                conn = sqlite3.connect(db_path)
                cursor = conn.cursor()

                cursor.execute(pred_sql)
                pred_rows = cursor.fetchall()

                cursor.execute(gold_query)
                gold_rows = cursor.fetchall()

                conn.close()

                if sorted(pred_rows) == sorted(gold_rows):
                    correct += 1

            except Exception:
                pass  # execution failed

            # 🔥 PRINT EVERY 10
            if i % 10 == 0 or i == total:
                current_acc = correct / i
                print(f"{i}/{total} | Acc: {current_acc:.3f}")

    print("\nGeneration finished.\n")

    # ---------------- SPIDER EVAL ----------------
    eval_script = project_root / "spider_eval/evaluation.py"

    cmd = [
        sys.executable,
        str(eval_script),
        "--gold", str(gold_sql),
        "--pred", str(pred_path),
        "--etype", "exec",
        "--db", str(db_root),
        "--table", str(table_json),
    ]

    print("Running Spider evaluation...")
    proc = subprocess.run(cmd, capture_output=True, text=True)

    print(proc.stdout)

    exec_acc = _parse_exec_accuracy(proc.stdout)
    if exec_acc is not None:
        print(f"\n🎯 Official Execution Accuracy: {exec_acc*100:.2f}%")
    else:
        print("Could not parse accuracy.")


if __name__ == "__main__":
    main()