-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
175 lines (124 loc) · 4.32 KB
/
app.py
File metadata and controls
175 lines (124 loc) · 4.32 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
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
import streamlit as st
import pandas as pd
import uuid
from core.ai_sql_generator import (
generate_sql,
verify_sql,
explain_query,
clean_sql_display,
apply_join_correction
)
from core.validator import validate_query, clean_sql
from core.visualization import auto_visualize
from database.snowflake_connector import upload_dataframe, run_query, drop_table, convert_logical_to_physical
st.set_page_config(page_title="AI Analytics Assistant", layout="wide")
st.title("AI-Powered Natural Language Analytics Assistant")
# ---------------------------
# SESSION INIT
# ---------------------------
if "session_id" not in st.session_state:
st.session_state.session_id = str(uuid.uuid4())[:8]
if "tables" not in st.session_state:
st.session_state.tables = {}
if "schema_text" not in st.session_state:
st.session_state.schema_text = ""
if "last_sql" not in st.session_state:
st.session_state.last_sql = None
# ---------------------------
# FILE UPLOAD
# ---------------------------
uploaded_files = st.file_uploader(
"Upload Dataset",
type=["csv", "xlsx"],
accept_multiple_files=True
)
if uploaded_files:
st.session_state.schema_text = ""
st.session_state.tables = {}
for file in uploaded_files:
try:
df = pd.read_csv(file) if file.name.endswith(".csv") else pd.read_excel(file)
except Exception as e:
st.error(f"File read error: {str(e)}")
st.stop()
df.columns = [c.lower().strip() for c in df.columns]
logical_name = file.name.split('.')[0].upper()
physical_name = f"{logical_name}_{st.session_state.session_id}"
try:
upload_dataframe(df, physical_name)
except Exception as e:
st.error(str(e))
st.stop()
st.session_state.tables[logical_name] = physical_name
st.success(f"{file.name} uploaded successfully")
#metadata cache
st.session_state.schema_text += (
f"\nTable: {logical_name}\n"
f"Columns: {', '.join(df.columns)}\n"
)
# ---------------------------
# QUESTION
# ---------------------------
user_question = st.text_input("Ask your business question")
if st.button("Generate Insights"):
if not user_question:
st.warning("Enter a question")
st.stop()
if not st.session_state.tables:
st.warning("Upload a dataset first")
st.stop()
# 1. Generate SQL
raw_sql = generate_sql(user_question, schema=st.session_state.schema_text)
if not raw_sql:
st.error("SQL generation failed.")
st.stop()
# 2. Apply join correction rule
corrected_sql = apply_join_correction(user_question, raw_sql)
# 3. Verify semantic correctness
if not verify_sql(user_question, corrected_sql, st.session_state.schema_text):
st.error("Generated SQL does not fully match the question intent.")
st.stop()
# 4. Clean
sql_query = clean_sql(corrected_sql)
if not validate_query(sql_query):
st.error("Unsafe or invalid query detected.")
st.stop()
# 5. Convert logical → physical
physical_sql = convert_logical_to_physical(
sql_query,
st.session_state.tables
)
# 6. Execute
try:
result_df = run_query(physical_sql)
except Exception as e:
st.error(str(e))
st.stop()
# 7. Display
display_sql = clean_sql_display(corrected_sql)
st.subheader("Generated SQL (Logical)")
st.code(display_sql, language="sql")
st.session_state.last_sql = display_sql
st.subheader("Results")
st.dataframe(result_df, use_container_width=True, hide_index=True)
fig = auto_visualize(result_df)
if fig:
st.plotly_chart(fig, use_container_width=True)
# ---------------------------
# EXPLAIN
# ---------------------------
if st.session_state.last_sql:
if st.button("Explain Query"):
explanation = explain_query(st.session_state.last_sql)
st.subheader("Query Explanation")
st.write(explanation)
# ---------------------------
# CLEAR
# ---------------------------
if st.button("Clear Session Data"):
for physical in st.session_state.tables.values():
drop_table(physical)
st.session_state.tables = {}
st.session_state.schema_text = ""
st.session_state.last_sql = None
st.success("All temporary tables deleted")