-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
359 lines (296 loc) Β· 12.6 KB
/
app.py
File metadata and controls
359 lines (296 loc) Β· 12.6 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
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
import streamlit as st
import pandas as pd
from src.llm.nl_to_sql import NLToSQLConverter
from src.database.db_manager import DatabaseManager
from src.visualization.chart_generator import ChartGenerator
from config.settings import settings
import logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
# Page configuration
st.set_page_config(
page_title="Easy SQL - NL to SQL Visualizer",
page_icon="π",
layout="wide",
initial_sidebar_state="expanded"
)
# Custom CSS
st.markdown("""
<style>
.main-header {
font-size: 2.5rem;
color: #1f77b4;
text-align: center;
margin-bottom: 2rem;
}
.sub-header {
font-size: 1.2rem;
color: #666;
text-align: center;
margin-bottom: 2rem;
}
.sql-box {
background-color: #f0f2f6;
padding: 1rem;
border-radius: 0.5rem;
border-left: 4px solid #1f77b4;
}
</style>
""", unsafe_allow_html=True)
@st.cache_resource
def init_database():
"""Initialize database manager"""
try:
db = DatabaseManager()
success, message = db.test_connection()
if not success:
st.error(f"Database connection failed: {message}")
return None
return db
except Exception as e:
st.error(f"Failed to initialize database: {str(e)}")
return None
def init_llm(provider, model):
"""Initialize LLM converter"""
try:
converter = NLToSQLConverter(provider=provider, model=model)
return converter
except Exception as e:
st.error(f"Failed to initialize LLM: {str(e)}")
print("INIT ERROR:", e)
return None
def main():
# Header
st.markdown('<h1 class="main-header">π Easy SQL</h1>', unsafe_allow_html=True)
st.markdown(
'<p class="sub-header">AI-powered tool to convert natural language to SQL and visualize results</p>',
unsafe_allow_html=True
)
# Sidebar configuration
with st.sidebar:
st.header("βοΈ Settings")
# LLM Configuration
st.subheader("LLM Configuration")
llm_provider = st.selectbox(
"LLM Provider",
["openai", "anthropic"],
index=0 if settings.DEFAULT_LLM_PROVIDER == "openai" else 1
)
model_options = {
"openai": ["gpt-4o-mini"],
"anthropic": ["claude-3-haiku-20240307"]
}
llm_model = st.selectbox(
"Model",
model_options[llm_provider],
index=0
)
# Database info
st.subheader("π Database Information")
db = init_database()
if db:
tables = db.get_tables()
st.write(f"**Number of Tables:** {len(tables)}")
selected_table = st.selectbox("Select Table", tables)
if selected_table:
stats = db.get_table_stats(selected_table)
st.write(f"**Row Count:** {stats['row_count']}")
st.write(f"**Column Count:** {stats['column_count']}")
if st.checkbox("Show Sample Data"):
sample_df = db.get_sample_data(selected_table, limit=5)
st.dataframe(sample_df)
# Example queries
st.subheader("π‘ Example Questions")
example_queries = [
"Show me total sales by category",
"What are the top 5 customers by order amount?",
"Show monthly revenue trend",
"Which products have low stock?",
"Show customer distribution by city"
]
for query in example_queries:
if st.button(query, key=f"example_{query}"):
st.session_state.example_query = query
# Main content
if not db:
st.error("Database connection failed. Please check your .env file.")
return
# Initialize conversation history in session state
if "conversation_history" not in st.session_state:
st.session_state.conversation_history = []
if "query_results" not in st.session_state:
st.session_state.query_results = []
# Query input
st.subheader("π Enter Question")
# Use example query if set
user_query = st.text_area(
"Enter your question in natural language",
value=st.session_state.get("example_query", ""),
height=100,
placeholder="Example: Show me the total revenue by category"
)
col1, col2 = st.columns([1, 5])
with col1:
submit_button = st.button("π Execute", type="primary", use_container_width=True)
with col2:
clear_button = st.button("ποΈ Clear", use_container_width=True)
if clear_button:
st.session_state.conversation_history = []
st.session_state.query_results = []
st.rerun()
if submit_button and user_query:
if "example_query" in st.session_state:
del st.session_state.example_query
with st.spinner("Generating SQL query..."):
logger.info(f"User query: {user_query}")
logger.info(f"LLM Provider: {llm_provider}")
# Initialize LLM
converter = init_llm(llm_provider, llm_model)
if not converter:
st.error("Failed to initialize LLM. Please check your API key.")
return
# Get schema info
schema_info = db.get_schema_info()
# Convert to SQL
try:
sql_query = converter.convert(
user_query,
schema_info,
st.session_state.conversation_history
)
# Validate query
if not converter.validate_query(sql_query):
st.error("β οΈ Generated query is not safe or valid.")
return
# Display generated SQL
st.subheader("π Generated SQL Query")
st.markdown(f'<div class="sql-box"><code>{sql_query}</code></div>', unsafe_allow_html=True)
# Execute query
with st.spinner("Executing query..."):
success, result = db.execute_query(sql_query)
if success:
st.success(f"β
Query executed successfully! ({len(result)} rows)")
# Save to history
st.session_state.conversation_history.append({
"role": "user",
"content": user_query
})
st.session_state.conversation_history.append({
"role": "assistant",
"content": sql_query
})
st.session_state.query_results.append({
"query": user_query,
"sql": sql_query,
"result": result
})
else:
st.error(f"β Query execution failed: {result}")
return
except Exception as e:
st.error(f"β Error occurred: {str(e)}")
return
# Display results
if st.session_state.query_results:
latest_result = st.session_state.query_results[-1]
df = latest_result["result"]
if not df.empty:
# Tabs for different views
tab1, tab2, tab3 = st.tabs(["π Visualization", "π Data", "π Chart Settings"])
with tab1:
st.subheader("Data Visualization")
# Auto-detect chart type
chart_gen = ChartGenerator()
suggested_chart = chart_gen.auto_detect_chart_type(df)
# Display metric if single value
if len(df) == 1 and len(df.columns) == 1:
st.metric(
label=df.columns[0],
value=df.iloc[0, 0]
)
elif len(df.columns) == 1 and df[df.columns[0]].dtype in ['int64', 'float64']:
col1, col2, col3 = st.columns(3)
with col1:
st.metric("Sum", f"{df[df.columns[0]].sum():,.2f}")
with col2:
st.metric("Average", f"{df[df.columns[0]].mean():,.2f}")
with col3:
st.metric("Maximum", f"{df[df.columns[0]].max():,.2f}")
# Display chart
if len(df) > 1 and suggested_chart != "table":
try:
fig = chart_gen.create_chart(df, suggested_chart)
st.plotly_chart(fig, use_container_width=True)
except Exception as e:
st.warning(f"Chart generation failed: {str(e)}")
st.dataframe(df, use_container_width=True)
else:
st.dataframe(df, use_container_width=True)
with tab2:
st.subheader("Data Table")
st.dataframe(df, use_container_width=True)
# Download button
csv = df.to_csv(index=False).encode('utf-8')
st.download_button(
label="π₯ Download CSV",
data=csv,
file_name="query_result.csv",
mime="text/csv"
)
# Statistics
if not df.empty:
st.subheader("Statistical Information")
numeric_cols = df.select_dtypes(include=['number']).columns
if len(numeric_cols) > 0:
st.dataframe(df[numeric_cols].describe())
with tab3:
st.subheader("Chart Customization")
chart_type = st.selectbox(
"Chart Type",
["bar", "line", "pie", "scatter", "histogram"],
index=["bar", "line", "pie", "scatter", "histogram"].index(suggested_chart)
if suggested_chart in ["bar", "line", "pie", "scatter", "histogram"] else 0
)
# Column selection based on chart type
if chart_type in ["bar", "line", "scatter"]:
x_column = st.selectbox("X-axis", df.columns.tolist(), index=0)
numeric_cols = chart_gen.get_numeric_columns(df)
if chart_type == "line":
y_columns = st.multiselect("Y-axis (multiple selection)", numeric_cols, default=numeric_cols[:1])
else:
y_column = st.selectbox("Y-axis", numeric_cols, index=0 if numeric_cols else 0)
elif chart_type == "pie":
names_column = st.selectbox("Names Column", df.columns.tolist(), index=0)
numeric_cols = chart_gen.get_numeric_columns(df)
values_column = st.selectbox("Values Column", numeric_cols, index=0 if numeric_cols else 0)
elif chart_type == "histogram":
numeric_cols = chart_gen.get_numeric_columns(df)
column = st.selectbox("Select Column", numeric_cols, index=0 if numeric_cols else 0)
if st.button("Generate Chart"):
try:
config = {}
if chart_type in ["bar", "scatter"]:
config = {"x_column": x_column, "y_column": y_column}
elif chart_type == "line":
config = {"x_column": x_column, "y_columns": y_columns}
elif chart_type == "pie":
config = {"names_column": names_column, "values_column": values_column}
elif chart_type == "histogram":
config = {"column": column}
fig = chart_gen.create_chart(df, chart_type, config)
st.plotly_chart(fig, use_container_width=True)
except Exception as e:
st.error(f"Chart generation failed: {str(e)}")
# Query history
if st.session_state.query_results:
with st.expander("π Query History"):
for idx, item in enumerate(reversed(st.session_state.query_results)):
st.write(f"**{len(st.session_state.query_results) - idx}. {item['query']}**")
st.code(item['sql'], language='sql')
st.write(f"Results: {len(item['result'])} rows")
st.divider()
if __name__ == "__main__":
main()