Spaces:
Sleeping
Sleeping
Fix generate(): unpack chat-template BatchEncoding instead of passing positionally
Browse files- src/text2sql.py +10 -4
src/text2sql.py
CHANGED
|
@@ -130,20 +130,26 @@ def generate_sql(
|
|
| 130 |
else:
|
| 131 |
messages = [{"role": "user", "content": build_instruction(question, schema, evidence)}]
|
| 132 |
|
| 133 |
-
|
|
|
|
|
|
|
|
|
|
| 134 |
messages,
|
| 135 |
return_tensors="pt",
|
| 136 |
add_generation_prompt=True,
|
| 137 |
-
|
|
|
|
|
|
|
|
|
|
| 138 |
|
| 139 |
with torch.no_grad():
|
| 140 |
outputs = model.generate(
|
| 141 |
-
|
| 142 |
max_new_tokens=max_new_tokens if cot else min(max_new_tokens, 256),
|
| 143 |
do_sample=False,
|
| 144 |
pad_token_id=tokenizer.eos_token_id,
|
| 145 |
)
|
| 146 |
-
raw = tokenizer.decode(outputs[0][
|
| 147 |
|
| 148 |
if cot:
|
| 149 |
return extract_final_sql(raw), raw.strip()
|
|
|
|
| 130 |
else:
|
| 131 |
messages = [{"role": "user", "content": build_instruction(question, schema, evidence)}]
|
| 132 |
|
| 133 |
+
# return_dict=True + unpacking is version-robust: apply_chat_template returns a
|
| 134 |
+
# BatchEncoding (dict) in newer transformers, and passing that positionally to
|
| 135 |
+
# generate() makes it treat the dict as input_ids and fail on `.shape`.
|
| 136 |
+
enc = tokenizer.apply_chat_template(
|
| 137 |
messages,
|
| 138 |
return_tensors="pt",
|
| 139 |
add_generation_prompt=True,
|
| 140 |
+
return_dict=True,
|
| 141 |
+
)
|
| 142 |
+
enc = {k: v.to(model.device) for k, v in enc.items()}
|
| 143 |
+
input_len = enc["input_ids"].shape[-1]
|
| 144 |
|
| 145 |
with torch.no_grad():
|
| 146 |
outputs = model.generate(
|
| 147 |
+
**enc,
|
| 148 |
max_new_tokens=max_new_tokens if cot else min(max_new_tokens, 256),
|
| 149 |
do_sample=False,
|
| 150 |
pad_token_id=tokenizer.eos_token_id,
|
| 151 |
)
|
| 152 |
+
raw = tokenizer.decode(outputs[0][input_len:], skip_special_tokens=True)
|
| 153 |
|
| 154 |
if cot:
|
| 155 |
return extract_final_sql(raw), raw.strip()
|