Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
109 lines (87 loc) · 3.08 KB
/
Copy pathapp.py
File metadata and controls
109 lines (87 loc) · 3.08 KB
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
import os
import joblib
import warnings
import streamlit as st
from dotenv import load_dotenv
from supabase import create_client
# ------------------ setup ------------------
warnings.filterwarnings("ignore")
load_dotenv()
# load ML (backup, optional)
model = joblib.load("query_intent_model.pkl")
vectorizer = joblib.load("tfidf_vectorizer.pkl")
# supabase client
supabase = create_client(
os.getenv("SUPABASE_URL"),
os.getenv("SUPABASE_KEY")
)
# session memory
if "history" not in st.session_state:
st.session_state.history = []
# ------------------ UI ------------------
st.set_page_config(page_title="SQL AI Agent", layout="centered")
st.title("🧠 Natural Language → SQL AI Agent")
query = st.text_input("Ask your question (English):")
# ------------------ logic ------------------
if st.button("Run Query") and query:
q = query.lower()
# -------- intent detection --------
if any(w in q for w in ["average", "avg", "mean"]):
intent = "AVERAGE"
elif any(w in q for w in ["lowest", "minimum", "min"]):
intent = "LOWEST"
elif any(w in q for w in ["highest", "maximum", "max", "top"]):
intent = "HIGHEST"
else:
intent = "SELECT"
# -------- department detection --------
department = None
departments = ["engineering", "sales", "hr", "marketing", "support"]
for dept in departments:
if dept in q:
department = dept
break
# -------- fetch data --------
data = supabase.table("employees").select("*").execute().data
if not data:
st.warning("No data found")
st.stop()
# -------- apply department filter FIRST --------
if department:
data = [
d for d in data
if d["department"].lower() == department
]
if not data:
st.warning(f"No data found for {department.capitalize()} department")
st.stop()
# -------- handle intents --------
if intent == "HIGHEST":
emp = max(data, key=lambda x: x["salary"])
result = f"Highest paid employee in {emp['department']} is {emp['name']} with salary {emp['salary']}"
st.success(result)
elif intent == "LOWEST":
emp = min(data, key=lambda x: x["salary"])
result = f"Lowest paid employee in {emp['department']} is {emp['name']} with salary {emp['salary']}"
st.success(result)
elif intent == "AVERAGE":
avg_salary = sum(e["salary"] for e in data) // len(data)
if department:
result = f"Average salary in {department.capitalize()} department is {avg_salary}"
else:
result = f"Average salary is {avg_salary}"
st.success(result)
else:
result = data[:5]
st.write(result)
# -------- save to memory --------
st.session_state.history.append({
"query": query,
"result": result
})
# ------------------ memory UI ------------------
st.subheader("🧠 History")
for item in reversed(st.session_state.history[-5:]):
st.markdown(f"**Q:** {item['query']}")
st.markdown(f"**A:** {item['result']}")
st.markdown("---")