Spaces:
Running
Running
File size: 27,789 Bytes
2b6d96a dc59b01 4f2cd24 dc59b01 2b6d96a dc59b01 e997eb1 4f2cd24 aa73117 dc59b01 2b6d96a 4f2cd24 aa2c432 aa73117 4f2cd24 aa73117 4f2cd24 aa73117 4f2cd24 dc59b01 4f2cd24 dc59b01 2b6d96a dc59b01 e997eb1 dc59b01 2b6d96a e997eb1 4f2cd24 dc59b01 4f2cd24 aa73117 4f2cd24 aa73117 4f2cd24 dc59b01 4f2cd24 e997eb1 4f2cd24 aa73117 4f2cd24 aa73117 4f2cd24 aa73117 4f2cd24 dc59b01 4f2cd24 dc59b01 e997eb1 4f2cd24 dc59b01 e997eb1 4f2cd24 e997eb1 dc59b01 4f2cd24 dc59b01 2b6d96a 4f2cd24 dc59b01 2b6d96a e997eb1 2b6d96a 4f2cd24 2b6d96a 4f2cd24 2b6d96a 4f2cd24 dc59b01 2b6d96a dc59b01 2b6d96a e997eb1 2b6d96a e997eb1 2b6d96a 4f2cd24 e997eb1 2b6d96a e997eb1 dc59b01 2b6d96a e997eb1 dc59b01 e997eb1 4f2cd24 2b6d96a 4f2cd24 2b6d96a 4f2cd24 aa73117 4f2cd24 aa73117 4f2cd24 aa73117 4f2cd24 aa73117 4f2cd24 aa73117 4f2cd24 aa73117 4f2cd24 aa73117 4f2cd24 aa73117 4f2cd24 aa73117 4f2cd24 aa73117 4f2cd24 aa73117 4f2cd24 aa73117 4f2cd24 aa73117 4f2cd24 aa73117 4f2cd24 aa73117 4f2cd24 aa73117 4f2cd24 aa73117 4f2cd24 aa73117 4f2cd24 aa73117 4f2cd24 aa73117 4f2cd24 aa73117 4f2cd24 aa73117 4f2cd24 aa73117 4f2cd24 aa73117 4f2cd24 aa73117 4f2cd24 aa73117 4f2cd24 aa73117 4f2cd24 aa73117 4f2cd24 aa73117 4f2cd24 aa73117 4f2cd24 aa73117 4f2cd24 aa73117 4f2cd24 | 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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 | # import sqlite3
# import torch
# import re
# import time
# from pathlib import Path
# from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
# from peft import PeftModel
# from src.sql_validator import SQLValidator
# from src.schema_encoder import SchemaEncoder
# PROJECT_ROOT = Path(__file__).resolve().parents[1]
# # ================================
# # DATABASE PATH AUTO DETECTION
# # ================================
# if (PROJECT_ROOT / "data/database").exists():
# DB_ROOT = PROJECT_ROOT / "data/database"
# else:
# DB_ROOT = PROJECT_ROOT / "final_databases"
# def normalize_question(q: str):
# q = q.lower().strip()
# q = re.sub(r"distinct\s+(\d+)", r"\1 distinct", q)
# q = re.sub(r"\s+", " ", q)
# return q
# def semantic_fix(question, sql):
# q = question.lower().strip()
# s = sql.lower()
# num_match = re.search(r'\b(?:show|list|top|limit|get|first|last)\s+(\d+)\b', q)
# if num_match and "limit" not in s and "count(" not in s:
# limit_val = num_match.group(1)
# sql = sql.rstrip(";")
# sql = f"{sql.strip()} LIMIT {limit_val}"
# return sql
# class Text2SQLEngine:
# def __init__(self,
# adapter_path=None,
# base_model_name="Salesforce/codet5-base",
# use_lora=True):
# self.device = "mps" if torch.backends.mps.is_available() else (
# "cuda" if torch.cuda.is_available() else "cpu"
# )
# self.validator = SQLValidator(DB_ROOT)
# self.schema_encoder = SchemaEncoder(DB_ROOT)
# self.dml_keywords = r'\b(delete|update|insert|drop|alter|truncate)\b'
# print("Loading base model...")
# base = AutoModelForSeq2SeqLM.from_pretrained(base_model_name)
# if not use_lora:
# self.tokenizer = AutoTokenizer.from_pretrained(base_model_name)
# self.model = base.to(self.device)
# self.model.eval()
# return
# if (PROJECT_ROOT / "checkpoints/best_rlhf_model").exists():
# adapter_path = PROJECT_ROOT / "checkpoints/best_rlhf_model"
# else:
# adapter_path = PROJECT_ROOT / "best_rlhf_model"
# adapter_path = adapter_path.resolve()
# print("Loading tokenizer and LoRA adapter...")
# try:
# self.tokenizer = AutoTokenizer.from_pretrained(
# str(adapter_path),
# local_files_only=True
# )
# except Exception:
# self.tokenizer = AutoTokenizer.from_pretrained(base_model_name)
# self.model = PeftModel.from_pretrained(base, str(adapter_path)).to(self.device)
# self.model.eval()
# print("β
RLHF model ready\n")
# def build_prompt(self, question, schema):
# return f"""You are an expert SQL generator.
# Database schema:
# {schema}
# Generate a valid SQLite query for the question.
# Question:
# {question}
# SQL:
# """
# def get_schema(self, db_id):
# return self.schema_encoder.structured_schema(db_id)
# def extract_sql(self, text: str):
# text = text.strip()
# if "SQL:" in text:
# text = text.split("SQL:")[-1]
# match = re.search(r"select[\s\S]*", text, re.IGNORECASE)
# if match:
# text = match.group(0)
# return text.split(";")[0].strip()
# def clean_sql(self, sql: str):
# sql = sql.replace('"', "'")
# sql = re.sub(r"\s+", " ", sql)
# return sql.strip()
# def generate_sql(self, prompt):
# inputs = self.tokenizer(
# prompt,
# return_tensors="pt",
# truncation=True,
# max_length=512
# ).to(self.device)
# with torch.no_grad():
# outputs = self.model.generate(
# **inputs,
# max_new_tokens=128,
# num_beams=5,
# early_stopping=True
# )
# decoded = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
# return self.clean_sql(self.extract_sql(decoded))
# def execute_sql(self, question, sql, db_id):
# if re.search(self.dml_keywords, sql, re.IGNORECASE):
# return sql, [], [], "β Security Alert"
# # FIXED DATABASE PATH
# db_path = DB_ROOT / f"{db_id}.sqlite"
# sql = self.clean_sql(sql)
# sql = semantic_fix(question, sql)
# try:
# conn = sqlite3.connect(db_path)
# cursor = conn.cursor()
# cursor.execute(sql)
# rows = cursor.fetchall()
# columns = [d[0] for d in cursor.description] if cursor.description else []
# conn.close()
# return sql, columns, rows, None
# except Exception as e:
# return sql, [], [], str(e)
# def ask(self, question, db_id):
# question = normalize_question(question)
# if re.search(self.dml_keywords, question, re.IGNORECASE):
# return {
# "question": question,
# "sql": "-- BLOCKED",
# "columns": [],
# "rows": [],
# "error": "Malicious prompt"
# }
# schema = self.get_schema(db_id)
# prompt = self.build_prompt(question, schema)
# raw_sql = self.generate_sql(prompt)
# final_sql, cols, rows, error = self.execute_sql(question, raw_sql, db_id)
# return {
# "question": question,
# "sql": final_sql,
# "columns": cols,
# "rows": rows,
# "error": error
# }
# _engine = None
# def get_engine():
# global _engine
# if _engine is None:
# _engine = Text2SQLEngine()
# return _engine
import sqlite3
import torch
import re
import time
from pathlib import Path
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
from peft import PeftModel
from src.sql_validator import SQLValidator
from src.schema_encoder import SchemaEncoder, build_schema_graph # Added build_schema_graph
PROJECT_ROOT = Path(__file__).resolve().parents[1]
# ================================
# DATABASE PATH AUTO DETECTION
# ================================
if (PROJECT_ROOT / "data/database").exists():
DB_ROOT = PROJECT_ROOT / "data/database"
else:
DB_ROOT = PROJECT_ROOT / "final_databases"
# ==========================================
# INPUT VALIDATION & RELEVANCE (From Code 1)
# ==========================================
def is_valid_question(q: str):
"""Extremely relaxed valid question checker. As long as there is 1 word, it passes."""
words = re.findall(r"[a-zA-Z0-9]+", q)
return len(words) >= 1
def is_relevant_to_db(question: str, schema_graph: dict):
"""
Lexical heuristic to block completely out-of-domain questions
while allowing valid plurals.
"""
q_words = set(re.findall(r'\b[a-z]{3,}\b', question.lower()))
stop_words = {"show", "list", "all", "and", "the", "get", "find", "how", "many", "what", "where", "which", "who", "give", "display", "count", "from", "for", "with", "that", "have", "has", "are", "there"}
q_words = q_words - stop_words
if not q_words:
return True
schema_words = set()
for table, cols in schema_graph.items():
schema_words.update(re.findall(r'\b[a-z]{3,}\b', table.lower()))
for col in cols:
schema_words.update(re.findall(r'\b[a-z]{3,}\b', col.lower()))
synonyms = {
"customer": ["client", "buyer", "shopper", "person", "people", "user"],
"employee": ["staff", "worker", "boss", "manager", "person", "people"],
"track": ["song", "music", "audio", "tune"],
"album": ["record", "cd", "music"],
"artist": ["singer", "band", "musician", "creator"],
"invoice": ["bill", "receipt", "purchase", "sale", "order", "buy", "bought", "cost"],
"city": ["town", "location", "place"],
"country": ["nation", "location", "place"],
"flight": ["plane", "airline", "trip", "fly", "airport"],
"student": ["pupil", "learner", "kid", "child"],
"club": ["group", "organization", "team"],
"course": ["class", "subject"],
"cinema": ["movie", "film", "theater", "screen"]
}
extended_schema_words = set(schema_words)
for db_word in schema_words:
if db_word in synonyms:
extended_schema_words.update(synonyms[db_word])
extended_schema_words.update({"id", "name", "total", "sum", "average", "avg", "min", "max", "number", "amount", "record", "data", "info", "information", "detail", "first", "last", "most", "least", "cheapest", "expensive", "best"})
# Check if the word OR its singular form is in the schema
for qw in q_words:
qw_singular = qw[:-1] if qw.endswith('s') else qw
if qw in extended_schema_words or qw_singular in extended_schema_words:
return True
return False
# ==========================================
# SCHEMA CONSTRAINTS (From Code 1)
# ==========================================
def apply_schema_constraints(sql, schema_graph):
sql = sql.lower()
used_tables = [t[1] for t in re.findall(r'(from|join)\s+(\w+)', sql)]
for t in used_tables:
if t not in schema_graph:
return None
valid_columns = set()
for cols in schema_graph.values():
valid_columns.update(cols)
col_blocks = re.findall(r'select\s+(.*?)\s+from', sql)
for block in col_blocks:
for c in block.split(","):
c = c.strip().split()[-1]
if "." in c:
c = c.split(".")[-1]
if c != "*" and "(" not in c and c != "":
if c not in valid_columns:
return None
return sql
def normalize_question(q: str):
q = q.lower().strip()
q = re.sub(r"distinct\s+(\d+)", r"\1 distinct", q)
q = re.sub(r"\s+", " ", q)
return q
def semantic_fix(question, sql):
q = question.lower().strip()
s = sql.lower()
num_match = re.search(r'\b(?:show|list|top|limit|get|first|last)\s+(\d+)\b', q)
if num_match and "limit" not in s and "count(" not in s:
limit_val = num_match.group(1)
sql = sql.rstrip(";")
sql = f"{sql.strip()} LIMIT {limit_val}"
return sql
class Text2SQLEngine:
def __init__(self,
adapter_path=None,
base_model_name="Salesforce/codet5-base",
use_lora=True,
use_constrained_decoding=True): # Added constrained decoding flag
self.device = "mps" if torch.backends.mps.is_available() else (
"cuda" if torch.cuda.is_available() else "cpu"
)
self.validator = SQLValidator(DB_ROOT)
self.schema_encoder = SchemaEncoder(DB_ROOT)
self.use_constrained_decoding = use_constrained_decoding
self.dml_keywords = r'\b(delete|update|insert|drop|alter|truncate)\b'
print("Loading base model...")
base = AutoModelForSeq2SeqLM.from_pretrained(base_model_name)
if not use_lora:
self.tokenizer = AutoTokenizer.from_pretrained(base_model_name)
self.model = base.to(self.device)
self.model.eval()
return
if (PROJECT_ROOT / "checkpoints/best_rlhf_model").exists():
adapter_path = PROJECT_ROOT / "checkpoints/best_rlhf_model"
else:
adapter_path = PROJECT_ROOT / "best_rlhf_model"
adapter_path = adapter_path.resolve()
print("Loading tokenizer and LoRA adapter...")
try:
self.tokenizer = AutoTokenizer.from_pretrained(
str(adapter_path),
local_files_only=True
)
except Exception:
self.tokenizer = AutoTokenizer.from_pretrained(base_model_name)
self.model = PeftModel.from_pretrained(base, str(adapter_path)).to(self.device)
self.model.eval()
print("β
RLHF model ready\n")
def build_prompt(self, question, schema):
return f"""You are an expert SQL generator.
Database schema:
{schema}
Generate a valid SQLite query for the question.
Question:
{question}
SQL:
"""
def get_schema(self, db_id):
return self.schema_encoder.structured_schema(db_id)
def extract_sql(self, text: str):
text = text.strip()
if "SQL:" in text:
text = text.split("SQL:")[-1]
match = re.search(r"select[\s\S]*", text, re.IGNORECASE)
if match:
text = match.group(0)
return text.split(";")[0].strip()
def clean_sql(self, sql: str):
sql = sql.replace('"', "'")
sql = re.sub(r"\s+", " ", sql)
return sql.strip()
def generate_sql(self, prompt):
inputs = self.tokenizer(
prompt,
return_tensors="pt",
truncation=True,
max_length=512
).to(self.device)
with torch.no_grad():
outputs = self.model.generate(
**inputs,
max_new_tokens=128,
num_beams=5,
early_stopping=True
)
decoded = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
return self.clean_sql(self.extract_sql(decoded))
def execute_sql(self, question, sql, db_id):
if re.search(self.dml_keywords, sql, re.IGNORECASE):
return sql, [], [], "β Security Alert"
# FIXED DATABASE PATH (From Code 2)
db_path = DB_ROOT / f"{db_id}.sqlite"
sql = self.clean_sql(sql)
sql = semantic_fix(question, sql)
try:
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
cursor.execute(sql)
rows = cursor.fetchall()
columns = [d[0] for d in cursor.description] if cursor.description else []
conn.close()
return sql, columns, rows, None
except Exception as e:
return sql, [], [], str(e)
def ask(self, question, db_id):
# 1. Normalize
question_norm = normalize_question(question)
question_context = f"Database question: {question_norm}"
# 2. Block dangerous inputs
if re.search(self.dml_keywords, question_context, re.IGNORECASE):
return {
"question": question_norm,
"sql": "-- BLOCKED",
"columns": [],
"rows": [],
"error": "β Malicious prompt"
}
# 3. Check basic validity of question
if not is_valid_question(question_context):
return {"sql": "", "error": "β Invalid input. Please type words."}
schema = self.get_schema(db_id)
schema_graph = build_schema_graph(schema)
# 4. LEXICAL RELEVANCE GUARDRAIL
if not is_relevant_to_db(question_norm, schema_graph):
return {"sql": "", "error": "β Question is completely out of domain for the selected database."}
# 5. INITIAL GENERATION
prompt = self.build_prompt(question_context, schema)
raw_sql = self.generate_sql(prompt)
# 6. STRONGER CONSTRAINT LOGIC
if self.use_constrained_decoding:
filtered_sql = apply_schema_constraints(raw_sql, schema_graph)
if filtered_sql is None:
constraint_prompt = f"""Use ONLY valid schema.
Database schema:
{schema}
Generate a valid SQLite query for the question.
Question:
{question_context}
SQL:
"""
sql_retry = self.generate_sql(constraint_prompt)
filtered_sql = apply_schema_constraints(sql_retry, schema_graph)
if filtered_sql:
raw_sql = filtered_sql
else:
raw_sql = sql_retry
# 7. EXECUTION
final_sql, cols, rows, error = self.execute_sql(question_norm, raw_sql, db_id)
return {
"question": question_norm,
"sql": final_sql,
"columns": cols,
"rows": rows,
"error": error
}
_engine = None
def get_engine(use_constrained=True): # Added parameter to control constraints
global _engine
if _engine is None:
_engine = Text2SQLEngine(use_constrained_decoding=use_constrained)
return _engine
# import sqlite3
# import torch
# import re
# import os
# from pathlib import Path
# from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
# from peft import PeftModel
# from src.sql_validator import SQLValidator
# from src.schema_encoder import SchemaEncoder # Removed build_schema_graph import
# PROJECT_ROOT = Path(__file__).resolve().parents[1]
# # ================================
# # DATABASE PATH AUTO DETECTION
# # ================================
# if (PROJECT_ROOT / "data/database").exists():
# DB_ROOT = PROJECT_ROOT / "data/database"
# else:
# DB_ROOT = PROJECT_ROOT / "final_databases"
# # ==========================================
# # SCHEMA PARSING
# # ==========================================
# def build_schema_graph(schema_text):
# """
# Parses a structured schema text string into a dictionary graph.
# Matches formats like: table_name(col1, col2, col3)
# """
# tables = {}
# for match in re.findall(r'(\w+)\s*\((.*?)\)', schema_text):
# table = match[0]
# cols = [c.strip().split()[0] for c in match[1].split(",")]
# tables[table] = cols
# return tables
# # ==========================================
# # INPUT VALIDATION & RELEVANCE
# # ==========================================
# def is_valid_question(q: str):
# q = q.strip().lower()
# if len(q) < 3:
# return False
# words = re.findall(r"[a-zA-Z]+", q)
# if len(words) < 1:
# return False
# return True
# def is_relevant_to_db(question: str, schema_graph: dict):
# q_words = set(re.findall(r'\b[a-z]{3,}\b', question.lower()))
# stop_words = {"show", "list", "all", "and", "the", "get", "find", "how", "many", "what", "where", "which", "who", "give", "display", "count", "from", "for", "with", "that", "have", "has", "are", "there"}
# q_words = q_words - stop_words
# if not q_words:
# return True
# schema_words = set()
# for table, cols in schema_graph.items():
# schema_words.update(re.findall(r'\b[a-z]{3,}\b', table.lower()))
# for col in cols:
# schema_words.update(re.findall(r'\b[a-z]{3,}\b', col.lower()))
# synonyms = {
# "customer": ["client", "buyer", "shopper", "person", "people", "user"],
# "employee": ["staff", "worker", "boss", "manager", "person", "people"],
# "track": ["song", "music", "audio", "tune"],
# "album": ["record", "cd", "music"],
# "artist": ["singer", "band", "musician", "creator"],
# "invoice": ["bill", "receipt", "purchase", "sale", "order", "buy", "bought", "cost"],
# "city": ["town", "location", "place"],
# "country": ["nation", "location", "place"],
# "flight": ["plane", "airline", "trip", "fly", "airport"],
# "student": ["pupil", "learner", "kid", "child"],
# "club": ["group", "organization", "team"],
# "course": ["class", "subject"],
# "cinema": ["movie", "film", "theater", "screen"]
# }
# extended_schema_words = set(schema_words)
# for db_word in schema_words:
# if db_word in synonyms:
# extended_schema_words.update(synonyms[db_word])
# extended_schema_words.update({"id", "name", "total", "sum", "average", "avg", "min", "max", "number", "amount", "record", "data", "info", "information", "detail", "first", "last", "most", "least", "cheapest", "expensive", "best"})
# for qw in q_words:
# qw_singular = qw[:-1] if qw.endswith('s') else qw
# if qw in extended_schema_words or qw_singular in extended_schema_words:
# return True
# return False
# def normalize_question(q: str):
# return re.sub(r"\s+", " ", q.lower().strip())
# def semantic_fix(question, sql):
# q = question.lower()
# num_match = re.search(r'\b(?:show|list|top|get)\s+(\d+)\b', q)
# if num_match and "limit" not in sql.lower():
# sql = f"{sql} LIMIT {num_match.group(1)}"
# return sql
# # ==========================================
# # SCHEMA CONSTRAINTS (SIMULATED LOGIT BLOCKING)
# # ==========================================
# def apply_schema_constraints(sql, schema_graph):
# sql = sql.lower()
# used_tables = [t[1] for t in re.findall(r'(from|join)\s+(\w+)', sql)]
# for t in used_tables:
# if t not in schema_graph:
# return None
# valid_columns = set()
# for cols in schema_graph.values():
# valid_columns.update(cols)
# col_blocks = re.findall(r'select\s+(.*?)\s+from', sql)
# for block in col_blocks:
# for c in block.split(","):
# c = c.strip().split()[-1]
# if "." in c:
# c = c.split(".")[-1]
# if c != "*" and "(" not in c and c != "":
# if c not in valid_columns:
# return None
# return sql
# # ==========================================
# # ENGINE
# # ==========================================
# class Text2SQLEngine:
# def __init__(self,
# adapter_path="checkpoints/best_rlhf_model_2",
# base_model_name="Salesforce/codet5-base",
# use_lora=True,
# use_constrained_decoding=False):
# self.device = "mps" if torch.backends.mps.is_available() else (
# "cuda" if torch.cuda.is_available() else "cpu"
# )
# self.validator = SQLValidator(DB_ROOT)
# self.schema_encoder = SchemaEncoder(DB_ROOT)
# self.use_constrained_decoding = use_constrained_decoding
# self.dml_keywords = r'\b(delete|update|insert|drop|alter|truncate|create)\b'
# print(f"\nπ¦ Loading model on {self.device}...")
# base = AutoModelForSeq2SeqLM.from_pretrained(base_model_name)
# # Override the redundant special tokens to prevent the tokenizer crash
# self.tokenizer = AutoTokenizer.from_pretrained(
# base_model_name,
# use_fast=False,
# additional_special_tokens=[]
# )
# # π₯ FIXED LOADA ADAPTER PATH LOGIC
# if use_lora:
# if adapter_path and (PROJECT_ROOT / adapter_path).exists():
# adapter_path = PROJECT_ROOT / adapter_path
# elif (PROJECT_ROOT / "checkpoints/best_rlhf_model_2").exists():
# adapter_path = PROJECT_ROOT / "checkpoints/best_rlhf_model_2"
# else:
# adapter_path = PROJECT_ROOT / "best_rlhf_model_2"
# adapter_path = adapter_path.resolve()
# if adapter_path.exists():
# try:
# self.model = PeftModel.from_pretrained(
# base,
# str(adapter_path),
# local_files_only=True
# ).to(self.device)
# print(f"β
LoRA loaded from {adapter_path}")
# except Exception as e:
# print(f"β οΈ LoRA load failed: {e}")
# self.model = base.to(self.device)
# else:
# print(f"β οΈ Adapter not found at {adapter_path}, using base model")
# self.model = base.to(self.device)
# else:
# self.model = base.to(self.device)
# self.model.eval()
# def build_prompt(self, question, schema):
# return f"""
# You are an expert SQL generator.
# IMPORTANT:
# - Use correct tables and columns
# - Use JOINs when needed
# Schema:
# {schema}
# Question:
# {question}
# SQL:
# """
# def get_schema(self, db_id):
# return self.schema_encoder.structured_schema(db_id)
# def extract_sql(self, text):
# match = re.search(r"(select|with)[\s\S]*", text, re.IGNORECASE)
# return match.group(0).split(";")[0].strip() if match else ""
# def clean_sql(self, sql):
# return re.sub(r"\s+", " ", sql.replace('"', "'")).strip()
# def generate_sql(self, prompt):
# inputs = self.tokenizer(prompt, return_tensors="pt").to(self.device)
# with torch.no_grad():
# outputs = self.model.generate(
# **inputs,
# max_new_tokens=128,
# num_beams=8,
# length_penalty=0.8,
# early_stopping=True
# )
# decoded = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
# return self.clean_sql(self.extract_sql(decoded))
# def execute_sql(self, question, sql, db_id):
# if re.search(self.dml_keywords, sql, re.IGNORECASE):
# return "", [], [], "β Blocked malicious SQL (Contains INSERT/UPDATE/DELETE/DROP)"
# # π₯ FIXED DATABASE PATH
# db_path = DB_ROOT / f"{db_id}.sqlite"
# sql = semantic_fix(question, sql)
# try:
# conn = sqlite3.connect(db_path)
# cursor = conn.cursor()
# cursor.execute(sql)
# rows = cursor.fetchall()
# columns = [d[0] for d in cursor.description] if cursor.description else []
# conn.close()
# return sql, columns, rows, None
# except Exception as e:
# return sql, [], [], str(e)
# def ask(self, question, db_id):
# question = normalize_question(question)
# question_context = f"Database question: {question}"
# if re.search(self.dml_keywords, question_context, re.IGNORECASE):
# return {"sql": "", "error": "β Blocked dangerous query from input text."}
# if not is_valid_question(question_context):
# return {"sql": "", "error": "β Invalid input. Please type words."}
# schema = self.get_schema(db_id)
# schema_graph = build_schema_graph(schema)
# if not is_relevant_to_db(question, schema_graph):
# return {"sql": "", "error": "β Question is completely out of domain for the selected database."}
# sql = self.generate_sql(self.build_prompt(question_context, schema))
# if self.use_constrained_decoding:
# filtered_sql = apply_schema_constraints(sql, schema_graph)
# if filtered_sql is None:
# constraint_prompt = f"""
# Use ONLY valid schema.
# Schema:
# {schema}
# Question:
# {question_context}
# SQL:
# """
# sql_retry = self.generate_sql(constraint_prompt)
# filtered_sql = apply_schema_constraints(sql_retry, schema_graph)
# if filtered_sql:
# sql = filtered_sql
# else:
# sql = sql_retry
# final_sql, cols, rows, error = self.execute_sql(question_context, sql, db_id)
# return {
# "question": question_context,
# "sql": final_sql,
# "columns": cols,
# "rows": rows,
# "error": error
# }
# def get_engine(
# adapter_path="checkpoints/best_rlhf_model_2",
# base_model_name="Salesforce/codet5-base",
# use_lora=True,
# use_constrained=True
# ):
# return Text2SQLEngine(
# adapter_path=adapter_path,
# base_model_name=base_model_name,
# use_lora=use_lora,
# use_constrained_decoding=use_constrained
# ) |