adelelsayed1991 commited on
Commit
19f0c5b
·
verified ·
1 Parent(s): ecd77d9

Fix: use sft/seed_42/best (not rl/), correct plan-then-SQL prompt, 4-bit NF4 quantization, and SQL extraction from fenced block, per MODEL_CARD.md

Browse files
Files changed (2) hide show
  1. app.py +88 -42
  2. requirements.txt +1 -0
app.py CHANGED
@@ -1,39 +1,68 @@
1
  """Gradio ZeroGPU Space serving the fhirsql-reasoning-sql-adapters model.
2
 
3
- Reproduces the exact prompt format used during fine-tuning (see
4
- C:\\dev\\fhirsql\\sft_train.ipynb, SYSTEM_PROMPT_TEMPLATE / build_messages /
5
- load_schema_ddl): a system message containing the abstention token and the
6
- DuckDB schema DDL, a user message containing the raw question, and greedy
7
- decoding for a single SQL statement (or the literal string UNANSWERABLE).
 
 
8
  """
9
 
 
 
10
  import spaces
11
  import gradio as gr
12
- from transformers import AutoModelForCausalLM, AutoTokenizer
 
13
  from peft import PeftModel
14
 
15
  BASE_MODEL = "Qwen/Qwen2.5-Coder-14B-Instruct"
16
  ADAPTER = "adelelsayed1991/fhirsql-reasoning-sql-adapters"
17
- # The adapters repo holds six checkpoints (sft/rl x seed_42/43/44), each in
18
- # its own subfolder rather than at the repo root. rl/seed_42/best is served
19
- # here as the project's most advanced trained artifact (SFT+RL); change
20
- # this to any of sft|rl/seed_{42,43,44}/best to serve a different one.
21
- ADAPTER_SUBFOLDER = "rl/seed_42/best"
22
  ABSTENTION_TOKEN = "UNANSWERABLE"
23
- MAX_NEW_TOKENS = 512
24
 
25
  SYSTEM_PROMPT_TEMPLATE = """You are a clinical data analyst who translates natural-language hospital \
26
- questions into a single DuckDB SQL statement, run against the schema below.
 
 
 
 
 
 
 
 
 
 
27
 
28
- Rules:
29
- - Output ONLY the SQL statement. No markdown fences, no explanation, no leading/trailing text.
30
- - If the question cannot be answered from this schema -- the data it needs genuinely doesn't \
31
- exist here -- output exactly the single word: {abstention_token}
32
- - Do not guess or approximate an answer using unrelated columns when the real field is absent.
33
 
34
  Schema:
35
  {schema_ddl}"""
36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
  def load_schema_ddl(schema_path: str) -> str:
39
  """Load the DuckDB schema DDL, dropping the changelog header above the first CREATE TABLE."""
@@ -48,33 +77,39 @@ SYSTEM_PROMPT = SYSTEM_PROMPT_TEMPLATE.format(
48
  )
49
 
50
  tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL)
51
- base_model = AutoModelForCausalLM.from_pretrained(BASE_MODEL, torch_dtype="auto")
52
- # torch_device="cpu" is required here: PeftModel.from_pretrained forwards
53
- # unrecognized kwargs to load_adapter(), whose `torch_device` parameter
54
- # controls what device load_peft_weights()/safetensors loads the adapter
55
- # weights onto. Left unset, it calls infer_device(), which sees
56
- # torch.cuda.is_available() == True (ZeroGPU's emulation layer reports
57
- # this as True even at module-load time, before a real GPU is allocated)
58
- # and picks "cuda" -- a direct-to-device safetensors load that the
59
- # emulation layer does not cover, causing "RuntimeError: No CUDA GPUs are
60
- # available". Forcing torch_device="cpu" here and moving the whole
61
- # composed model to CUDA afterward avoids that code path entirely. Note
62
- # this is NOT the same kwarg as transformers' `device_map` (which is
63
- # unrelated to this PEFT-internal loading path and is silently ignored if
64
- # passed here instead).
65
  model = PeftModel.from_pretrained(
66
  base_model, ADAPTER, subfolder=ADAPTER_SUBFOLDER, torch_device="cpu"
67
  )
68
  model.to("cuda")
69
  model.eval()
70
 
 
 
 
 
 
 
 
71
 
72
- @spaces.GPU(duration=30)
73
- def generate_sql(question: str) -> str:
74
- """Generate a single DuckDB SQL statement for a natural-language question.
75
 
76
- Returns the literal string "UNANSWERABLE" if the model determines the
77
- question cannot be answered from the schema it was shown.
 
 
 
78
  """
79
  messages = [
80
  {"role": "system", "content": SYSTEM_PROMPT},
@@ -92,18 +127,29 @@ def generate_sql(question: str) -> str:
92
  do_sample=False,
93
  pad_token_id=tokenizer.eos_token_id,
94
  )
95
- return tokenizer.decode(output_ids[0][prompt_len:], skip_special_tokens=True).strip()
 
 
 
 
 
 
 
96
 
97
 
98
  demo = gr.Interface(
99
  fn=generate_sql,
100
  inputs=gr.Text(label="Clinical question", lines=2),
101
- outputs=gr.Text(label="Generated SQL"),
 
 
 
102
  title="fhirsql-reasoning-sql",
103
  description=(
104
- "Translates a natural-language hospital question into DuckDB SQL "
105
- "against the fhirsql schema. Returns UNANSWERABLE if the question "
106
- "cannot be answered from that schema."
 
107
  ),
108
  )
109
 
 
1
  """Gradio ZeroGPU Space serving the fhirsql-reasoning-sql-adapters model.
2
 
3
+ Reproduces the exact prompt/loading contract documented in the adapters
4
+ repo's MODEL_CARD.md and C:\\dev\\fhirsql-phase2\\sft_train.ipynb: a plan-then-SQL
5
+ system prompt (SYSTEM_PROMPT_TEMPLATE / build_messages), 4-bit NF4
6
+ quantized loading, the `sft/seed_42/best` checkpoint (the model card
7
+ explicitly recommends `sft/`, not `rl/` -- RL did not improve on its SFT
8
+ starting point for this task), and extraction of the SQL from the model's
9
+ fenced ```sql block (extract_sql_from_completion), not the raw completion.
10
  """
11
 
12
+ import re
13
+
14
  import spaces
15
  import gradio as gr
16
+ import torch
17
+ from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
18
  from peft import PeftModel
19
 
20
  BASE_MODEL = "Qwen/Qwen2.5-Coder-14B-Instruct"
21
  ADAPTER = "adelelsayed1991/fhirsql-reasoning-sql-adapters"
22
+ # MODEL_CARD.md: "sft/ -- supervised fine-tuning only... Use these." RL
23
+ # checkpoints are published for reproducibility only; they do not improve
24
+ # on the SFT starting point for this task (PAPER.md Section 5.3).
25
+ ADAPTER_SUBFOLDER = "sft/seed_42/best"
 
26
  ABSTENTION_TOKEN = "UNANSWERABLE"
27
+ MAX_NEW_TOKENS = 768
28
 
29
  SYSTEM_PROMPT_TEMPLATE = """You are a clinical data analyst who translates natural-language hospital \
30
+ questions into DuckDB SQL, run against the schema below, via an explicit query plan first.
31
+
32
+ Output exactly two parts, in this order, and nothing else:
33
+ 1. A JSON object describing the query plan: which clinical concepts the question refers to (and \
34
+ whether each needs a terminology lookup against the schema's `valuesets` table), what \
35
+ additional tables must be joined and why, what filters apply, and what the final aggregation \
36
+ computes.
37
+ 2. The compiled SQL statement for that plan, in a fenced code block:
38
+ ```sql
39
+ <the SQL statement>
40
+ ```
41
 
42
+ If the question cannot be answered from this schema -- the data it needs genuinely doesn't \
43
+ exist here -- the plan should be {{"abstain": true}}, and the fenced SQL block should contain \
44
+ exactly the single word: {abstention_token}
45
+
46
+ Do not guess or approximate an answer using unrelated columns when the real field is absent.
47
 
48
  Schema:
49
  {schema_ddl}"""
50
 
51
+ _SQL_FENCE_RE = re.compile(r"```sql\s*\n(.*?)\n```", re.IGNORECASE | re.DOTALL)
52
+
53
+
54
+ def extract_sql_from_completion(text: str) -> str | None:
55
+ """Pull the SQL out of a plan-JSON + fenced ```sql block completion.
56
+
57
+ Takes the LAST matching fence, not the first, mirroring the training
58
+ notebooks' own extraction logic. Returns None (not a crash, not an
59
+ empty string) when no fence is found at all.
60
+ """
61
+ matches = _SQL_FENCE_RE.findall(text)
62
+ if not matches:
63
+ return None
64
+ return matches[-1].strip()
65
+
66
 
67
  def load_schema_ddl(schema_path: str) -> str:
68
  """Load the DuckDB schema DDL, dropping the changelog header above the first CREATE TABLE."""
 
77
  )
78
 
79
  tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL)
80
+ base_model = AutoModelForCausalLM.from_pretrained(
81
+ BASE_MODEL,
82
+ dtype=torch.bfloat16,
83
+ quantization_config=BitsAndBytesConfig(
84
+ load_in_4bit=True,
85
+ bnb_4bit_compute_dtype=torch.bfloat16,
86
+ bnb_4bit_use_double_quant=True,
87
+ bnb_4bit_quant_type="nf4",
88
+ ),
89
+ )
 
 
 
 
90
  model = PeftModel.from_pretrained(
91
  base_model, ADAPTER, subfolder=ADAPTER_SUBFOLDER, torch_device="cpu"
92
  )
93
  model.to("cuda")
94
  model.eval()
95
 
96
+ print("model type:", type(model))
97
+ print("peft_config:", getattr(model, "peft_config", "NO ADAPTER"))
98
+ print(
99
+ "active_adapters:",
100
+ model.active_adapters if hasattr(model, "active_adapters") else "-",
101
+ )
102
+
103
 
104
+ @spaces.GPU(duration=45)
105
+ def generate_sql(question: str) -> tuple[str, str]:
106
+ """Generate a plan+SQL completion for a natural-language question.
107
 
108
+ Returns (extracted_sql, raw_completion). extracted_sql is
109
+ "UNANSWERABLE" if the model determines the question cannot be
110
+ answered from the schema, or a literal "[NO SQL FENCE FOUND]"
111
+ placeholder if the completion did not contain a parseable ```sql
112
+ block at all (a genuine model failure, surfaced rather than hidden).
113
  """
114
  messages = [
115
  {"role": "system", "content": SYSTEM_PROMPT},
 
127
  do_sample=False,
128
  pad_token_id=tokenizer.eos_token_id,
129
  )
130
+ completion = tokenizer.decode(
131
+ output_ids[0][prompt_len:], skip_special_tokens=True
132
+ ).strip()
133
+
134
+ extracted = extract_sql_from_completion(completion)
135
+ if extracted is None:
136
+ extracted = "[NO SQL FENCE FOUND]"
137
+ return extracted, completion
138
 
139
 
140
  demo = gr.Interface(
141
  fn=generate_sql,
142
  inputs=gr.Text(label="Clinical question", lines=2),
143
+ outputs=[
144
+ gr.Text(label="Extracted SQL"),
145
+ gr.Text(label="Raw completion (plan JSON + fenced SQL)", lines=10),
146
+ ],
147
  title="fhirsql-reasoning-sql",
148
  description=(
149
+ "Translates a natural-language hospital question into a query "
150
+ "plan and DuckDB SQL against the fhirsql-phase2 schema. Returns "
151
+ "UNANSWERABLE if the question cannot be answered from that "
152
+ "schema. sft/seed_42/best checkpoint, per MODEL_CARD.md."
153
  ),
154
  )
155
 
requirements.txt CHANGED
@@ -3,3 +3,4 @@ transformers>=4.45
3
  peft>=0.13
4
  torch>=2.8
5
  accelerate>=0.34
 
 
3
  peft>=0.13
4
  torch>=2.8
5
  accelerate>=0.34
6
+ bitsandbytes>=0.43