Spaces:
Sleeping
Sleeping
Upload 6 files
Browse files- app.py +12 -22
- data_loader.py +10 -0
- model.py +50 -0
- my_gradio_demo.py +22 -0
- requirements.txt +3 -0
- sql_templates.py +10 -0
app.py
CHANGED
|
@@ -1,22 +1,12 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
interface = gr.Interface(
|
| 15 |
-
fn=oracle_plsql_autocomplete,
|
| 16 |
-
inputs=gr.Textbox(lines=2, placeholder="Start typing PL/SQL... (e.g. select, insert)"),
|
| 17 |
-
outputs=gr.Textbox(),
|
| 18 |
-
title="Oracle LLM Autocomplete (Mock)",
|
| 19 |
-
description="Start typing a PL/SQL keyword and get a suggested code snippet."
|
| 20 |
-
)
|
| 21 |
-
|
| 22 |
-
interface.launch()
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from model import oracle_sql_suggester
|
| 3 |
+
|
| 4 |
+
interface = gr.Interface(
|
| 5 |
+
fn=oracle_sql_suggester,
|
| 6 |
+
inputs=gr.Textbox(lines=3, placeholder="Describe the SQL query you want..."),
|
| 7 |
+
outputs=gr.Textbox(),
|
| 8 |
+
title="Oracle SQL Generator (Phase 1 - Rule Based)",
|
| 9 |
+
description="Describe your requirement in text, get a suggested SQL."
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
interface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
data_loader.py
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# data_loader.py
|
| 2 |
+
|
| 3 |
+
def load_rules(file_path="data/train_data.txt"):
|
| 4 |
+
data = {}
|
| 5 |
+
with open(file_path, "r", encoding="utf-8") as file:
|
| 6 |
+
for line in file:
|
| 7 |
+
if "=" in line:
|
| 8 |
+
key, value = line.strip().split("=", 1)
|
| 9 |
+
data[key.strip().lower()] = value.strip()
|
| 10 |
+
return data
|
model.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# model.py
|
| 2 |
+
from sentence_transformers import SentenceTransformer, util
|
| 3 |
+
from sql_templates import sql_templates # new import
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
# Load training rules (string-to-SQL map)
|
| 7 |
+
from data_loader import load_rules # you can split this for cleanliness
|
| 8 |
+
rules = load_rules()
|
| 9 |
+
|
| 10 |
+
# Load embedding model (lightweight, fast)
|
| 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 |
+
# Try direct rule match
|
| 21 |
+
if prompt_clean in rules:
|
| 22 |
+
return rules[prompt_clean]
|
| 23 |
+
|
| 24 |
+
# Semantic matching
|
| 25 |
+
user_embedding = model.encode(prompt_clean, convert_to_tensor=True)
|
| 26 |
+
cosine_scores = util.cos_sim(user_embedding, train_embeddings)
|
| 27 |
+
|
| 28 |
+
top_match_index = torch.argmax(cosine_scores).item()
|
| 29 |
+
top_score = cosine_scores[0][top_match_index].item()
|
| 30 |
+
|
| 31 |
+
if top_score >= 0.7:
|
| 32 |
+
matched_prompt = train_prompts[top_match_index]
|
| 33 |
+
return rules[matched_prompt]
|
| 34 |
+
|
| 35 |
+
# Check template keywords
|
| 36 |
+
for key in sql_templates:
|
| 37 |
+
if key.replace("_", " ") in prompt_clean or key in prompt_clean:
|
| 38 |
+
return sql_templates[key]
|
| 39 |
+
|
| 40 |
+
# Semantic match
|
| 41 |
+
user_embedding = model.encode(prompt_clean, convert_to_tensor=True)
|
| 42 |
+
cosine_scores = util.cos_sim(user_embedding, train_embeddings)
|
| 43 |
+
top_match_index = torch.argmax(cosine_scores).item()
|
| 44 |
+
top_score = cosine_scores[0][top_match_index].item()
|
| 45 |
+
|
| 46 |
+
if top_score >= 0.7:
|
| 47 |
+
matched_prompt = train_prompts[top_match_index]
|
| 48 |
+
return rules[matched_prompt]
|
| 49 |
+
|
| 50 |
+
return "🤖 Sorry, I couldn’t understand that. Please try rephrasing your request."
|
my_gradio_demo.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
|
| 3 |
+
def oracle_plsql_autocomplete(prompt):
|
| 4 |
+
suggestions = {
|
| 5 |
+
"select": "SELECT * FROM employees WHERE ROWNUM <= 10;",
|
| 6 |
+
"insert": "INSERT INTO employees (emp_id, name) VALUES (101, 'John');",
|
| 7 |
+
"create": "CREATE TABLE departments (dept_id NUMBER, dept_name VARCHAR2(50));",
|
| 8 |
+
"begin": "BEGIN\n NULL;\nEND;",
|
| 9 |
+
"if": "IF salary > 10000 THEN\n DBMS_OUTPUT.PUT_LINE('High salary');\nEND IF;"
|
| 10 |
+
}
|
| 11 |
+
key = prompt.lower().strip().split()[0]
|
| 12 |
+
return suggestions.get(key, "-- No matching Oracle PL/SQL snippet found.")
|
| 13 |
+
|
| 14 |
+
interface = gr.Interface(
|
| 15 |
+
fn=oracle_plsql_autocomplete,
|
| 16 |
+
inputs=gr.Textbox(lines=2, placeholder="Start typing PL/SQL... (e.g. select, insert)"),
|
| 17 |
+
outputs=gr.Textbox(),
|
| 18 |
+
title="Oracle LLM Autocomplete (Mock)",
|
| 19 |
+
description="Start typing a PL/SQL keyword and get a suggested code snippet."
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
interface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
torch
|
| 3 |
+
sentence-transformers
|
sql_templates.py
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
sql_templates = {
|
| 2 |
+
"basic_select": "SELECT column1, column2 FROM table_name;",
|
| 3 |
+
"select_where": "SELECT column1 FROM table_name WHERE condition;",
|
| 4 |
+
"join_example": "SELECT a.col1, b.col2 FROM table1 a JOIN table2 b ON a.id = b.a_id;",
|
| 5 |
+
"group_by": "SELECT dept, COUNT(*) FROM employees GROUP BY dept;",
|
| 6 |
+
"having": "SELECT dept, COUNT(*) FROM employees GROUP BY dept HAVING COUNT(*) > 5;",
|
| 7 |
+
"insert": "INSERT INTO table_name (col1, col2) VALUES (val1, val2);",
|
| 8 |
+
"update": "UPDATE table_name SET col1 = val1 WHERE condition;",
|
| 9 |
+
"delete": "DELETE FROM table_name WHERE condition;"
|
| 10 |
+
}
|