Spaces:
Sleeping
Sleeping
Upload 4 files
Browse files- model.py +45 -31
- sql_templates.py +18 -0
model.py
CHANGED
|
@@ -1,52 +1,60 @@
|
|
| 1 |
-
# model.py
|
| 2 |
from sentence_transformers import SentenceTransformer, util
|
| 3 |
-
from sql_templates import sql_templates, sql_keyword_aliases, fuzzy_aliases
|
|
|
|
| 4 |
import torch
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
| 8 |
rules = load_rules()
|
| 9 |
|
| 10 |
-
# Load
|
| 11 |
model = SentenceTransformer("sentence-transformers/paraphrase-MiniLM-L6-v2")
|
| 12 |
-
|
| 13 |
-
# Pre-compute embeddings of training prompts
|
| 14 |
train_prompts = list(rules.keys())
|
| 15 |
train_embeddings = model.encode(train_prompts, convert_to_tensor=True)
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
def oracle_sql_suggester(prompt):
|
| 18 |
prompt_clean = prompt.strip().lower()
|
| 19 |
|
| 20 |
-
#
|
| 21 |
if prompt_clean in rules:
|
| 22 |
return rules[prompt_clean]
|
| 23 |
|
| 24 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
for word in prompt_clean.split():
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
| 30 |
for key, template in sql_templates.items():
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
if word in sql_keyword_aliases:
|
| 36 |
-
return sql_templates[sql_keyword_aliases[word]]
|
| 37 |
-
|
| 38 |
-
# Try fuzzy alias matches
|
| 39 |
for fuzzy_phrase, mapped_key in fuzzy_aliases.items():
|
| 40 |
-
|
| 41 |
-
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
# Semantic matching
|
| 47 |
user_embedding = model.encode(prompt_clean, convert_to_tensor=True)
|
| 48 |
cosine_scores = util.cos_sim(user_embedding, train_embeddings)
|
| 49 |
-
|
| 50 |
top_match_index = torch.argmax(cosine_scores).item()
|
| 51 |
top_score = cosine_scores[0][top_match_index].item()
|
| 52 |
|
|
@@ -54,6 +62,12 @@ def oracle_sql_suggester(prompt):
|
|
| 54 |
matched_prompt = train_prompts[top_match_index]
|
| 55 |
return rules[matched_prompt]
|
| 56 |
|
| 57 |
-
#
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from sentence_transformers import SentenceTransformer, util
|
| 2 |
+
from sql_templates import sql_templates, sql_keyword_aliases, fuzzy_aliases, conflicting_phrases, greeting_templates
|
| 3 |
+
from data_loader import load_rules
|
| 4 |
import torch
|
| 5 |
|
| 6 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
| 7 |
+
|
| 8 |
+
# π Load rules
|
| 9 |
rules = load_rules()
|
| 10 |
|
| 11 |
+
# π Load semantic model
|
| 12 |
model = SentenceTransformer("sentence-transformers/paraphrase-MiniLM-L6-v2")
|
|
|
|
|
|
|
| 13 |
train_prompts = list(rules.keys())
|
| 14 |
train_embeddings = model.encode(train_prompts, convert_to_tensor=True)
|
| 15 |
|
| 16 |
+
# π€ Load local LLM model
|
| 17 |
+
llm_name = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
|
| 18 |
+
tokenizer = AutoTokenizer.from_pretrained(llm_name)
|
| 19 |
+
llm_model = AutoModelForCausalLM.from_pretrained(llm_name, torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32)
|
| 20 |
+
llm_pipeline = pipeline("text-generation", model=llm_model, tokenizer=tokenizer, device=0 if torch.cuda.is_available() else -1)
|
| 21 |
+
|
| 22 |
def oracle_sql_suggester(prompt):
|
| 23 |
prompt_clean = prompt.strip().lower()
|
| 24 |
|
| 25 |
+
# β
Exact rule
|
| 26 |
if prompt_clean in rules:
|
| 27 |
return rules[prompt_clean]
|
| 28 |
|
| 29 |
+
# β
Greeting handling
|
| 30 |
+
for greet_key, greet_reply in greeting_templates.items():
|
| 31 |
+
if greet_key in prompt_clean:
|
| 32 |
+
return greet_reply
|
| 33 |
+
|
| 34 |
+
# β
Conflicting phrase
|
| 35 |
+
for terms, response in conflicting_phrases.items():
|
| 36 |
+
if all(term in prompt_clean for term in terms):
|
| 37 |
+
return response
|
| 38 |
+
|
| 39 |
+
# β
Keyword alias
|
| 40 |
for word in prompt_clean.split():
|
| 41 |
+
if word in sql_keyword_aliases:
|
| 42 |
+
mapped_key = sql_keyword_aliases[word]
|
| 43 |
+
return sql_templates.get(mapped_key)
|
| 44 |
+
|
| 45 |
+
# β
Template match
|
| 46 |
for key, template in sql_templates.items():
|
| 47 |
+
if key in prompt_clean or key.replace("_", " ") in prompt_clean:
|
| 48 |
+
return template
|
| 49 |
+
|
| 50 |
+
# β
Fuzzy match
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
for fuzzy_phrase, mapped_key in fuzzy_aliases.items():
|
| 52 |
+
if fuzzy_phrase in prompt_clean:
|
| 53 |
+
return sql_templates.get(mapped_key)
|
| 54 |
|
| 55 |
+
# β
Semantic match
|
|
|
|
|
|
|
|
|
|
| 56 |
user_embedding = model.encode(prompt_clean, convert_to_tensor=True)
|
| 57 |
cosine_scores = util.cos_sim(user_embedding, train_embeddings)
|
|
|
|
| 58 |
top_match_index = torch.argmax(cosine_scores).item()
|
| 59 |
top_score = cosine_scores[0][top_match_index].item()
|
| 60 |
|
|
|
|
| 62 |
matched_prompt = train_prompts[top_match_index]
|
| 63 |
return rules[matched_prompt]
|
| 64 |
|
| 65 |
+
# β
Local LLM fallback
|
| 66 |
+
try:
|
| 67 |
+
prompt_text = f"Generate an Oracle SQL query or guidance for the following request:\n{prompt}\n\nSQL:"
|
| 68 |
+
output = llm_pipeline(prompt_text, max_new_tokens=256, do_sample=True, temperature=0.5)[0]["generated_text"]
|
| 69 |
+
return "π€ (Local LLM): " + output.split("SQL:")[-1].strip()
|
| 70 |
+
except Exception as e:
|
| 71 |
+
print("β οΈ Local LLM error:", e)
|
| 72 |
+
return "π€ Sorry, I couldnβt process that locally. Please try a simpler prompt."
|
| 73 |
+
|
sql_templates.py
CHANGED
|
@@ -39,3 +39,21 @@ fuzzy_aliases = {
|
|
| 39 |
"change row": "update",
|
| 40 |
"erase record": "delete"
|
| 41 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
"change row": "update",
|
| 40 |
"erase record": "delete"
|
| 41 |
}
|
| 42 |
+
|
| 43 |
+
conflicting_phrases = {
|
| 44 |
+
("modify", "new"): "β οΈ You cannot modify a new record. Consider using INSERT instead.",
|
| 45 |
+
("update", "new"): "β οΈ You cannot update new data. Did you mean to INSERT?",
|
| 46 |
+
("delete", "new"): "β οΈ You cannot delete something that doesn't exist yet.",
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
# π€ Greeting phrases and responses
|
| 50 |
+
greeting_templates = {
|
| 51 |
+
"hello": "π Hello! How can I assist you with SQL or PL/SQL today?",
|
| 52 |
+
"hi": "π Hi there! Need help with Oracle SQL or PL/SQL?",
|
| 53 |
+
"hey": "π Hey! Let me know your SQL or PLSQL question.",
|
| 54 |
+
"good morning": "π
Good morning! What Oracle task can I help with?",
|
| 55 |
+
"good afternoon": "π Good afternoon! Ready to write some SQL?",
|
| 56 |
+
"good evening": "π Good evening! Need SQL/PLSQL guidance?",
|
| 57 |
+
"how are you": "π€ I'm a bot but I'm always ready to help! What do you need?",
|
| 58 |
+
"what can you do": "π‘ I can help you generate Oracle SQL/PLSQL from descriptions, explain syntax, suggest queries, and more.",
|
| 59 |
+
}
|