-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
291 lines (233 loc) Β· 11.3 KB
/
app.py
File metadata and controls
291 lines (233 loc) Β· 11.3 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
from dotenv import load_dotenv
load_dotenv()
import streamlit as st
import os
import sqlite3
import re
import pandas as pd
import google.generativeai as genai
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
def get_sql_queries(question, prompt):
model = genai.GenerativeModel('gemini-1.5-flash')
response = model.generate_content([prompt[0], question])
sql_query = response.text.strip()
sql_query = re.sub(r'^```sql\s*', '', sql_query, flags=re.IGNORECASE)
sql_query = re.sub(r'^```\s*', '', sql_query)
sql_query = re.sub(r'```\s*$', '', sql_query)
sql_query = sql_query.strip()
return sql_query
def debug_date_data(db):
"""Debug function to check date ranges in the database"""
try:
conn = sqlite3.connect(db)
cur = conn.cursor()
# Check date range
cur.execute("SELECT MIN(order_date) as min_date, MAX(order_date) as max_date, COUNT(*) as total_records FROM sales")
date_info = cur.fetchone()
# Check specific dates
cur.execute("SELECT order_date, COUNT(*) FROM sales WHERE order_date < '2025-02-15' GROUP BY order_date ORDER BY order_date")
before_feb_15 = cur.fetchall()
# Check July records
cur.execute("SELECT order_date, profit FROM sales WHERE strftime('%m', order_date) = '07' ORDER BY order_date")
july_records = cur.fetchall()
conn.close()
return date_info, before_feb_15, july_records
except Exception as e:
return None, None, None
def read_sql_queries(sql, db):
try:
conn = sqlite3.connect(db)
cur = conn.cursor()
cur.execute(sql)
rows = cur.fetchall()
# Get column names
columns = [description[0] for description in cur.description]
conn.close()
return rows, columns
except Exception as e:
st.error(f"Error executing SQL query: {e}")
return [], []
# CORRECTED PROMPT - Now correctly references the 'sales' table
prompt = [
"""
You are an expert in converting English questions to SQL Query!
The SQL database has a table named 'sales' and has the following columns - id, order_date, region, product_name, unit_price, quantity_sold, discount_percent, profit
The order_date column stores dates in 'YYYY-MM-DD' format (e.g., '2025-01-15', '2025-07-20').
For example:
Example 1 - "Show all records from north region"
SQL: SELECT * FROM sales WHERE region = 'North';
Example 2 - "How many records are in north region?"
SQL: SELECT COUNT(*) FROM sales WHERE region = 'North';
Example 3 - "What is the total profit by region?"
SQL: SELECT region, SUM(profit) as total_profit FROM sales GROUP BY region;
Example 4 - "Show top 10 most profitable orders"
SQL: SELECT * FROM sales ORDER BY profit DESC LIMIT 10;
Example 5 - "List all smartphone sales"
SQL: SELECT * FROM sales WHERE product_name = 'Smartphone';
Example 6 - "How much total profit before 2025-02-15?"
SQL: SELECT SUM(profit) as total_profit FROM sales WHERE order_date < '2025-02-15';
Example 7 - "How much total profit in month of July?"
SQL: SELECT SUM(profit) as total_profit FROM sales WHERE strftime('%m', order_date) = '07';
Example 8 - "Show sales in January 2025"
SQL: SELECT * FROM sales WHERE strftime('%Y-%m', order_date) = '2025-01';
Example 9 - "Total profit by month"
SQL: SELECT strftime('%Y-%m', order_date) as month, SUM(profit) as total_profit FROM sales GROUP BY strftime('%Y-%m', order_date) ORDER BY month;
Important notes for DATE queries:
- Use strftime() function for extracting parts of dates
- For specific month: strftime('%m', order_date) = '07' (July)
- For year-month: strftime('%Y-%m', order_date) = '2025-01'
- For year: strftime('%Y', order_date) = '2025'
- For day: strftime('%d', order_date) = '15'
- Always use two-digit format for months: '01', '02', '07', '12'
General notes:
- Table name is 'sales' (lowercase)
- Use single quotes for string values in SQL
- When user asks to "show", "display", "list" records, use SELECT * to return all columns
- When user asks "how many", "count", use COUNT(*)
- When user asks for "total profit", "sum of profit", use SUM(profit)
- When user asks for "all records", "all data", always use SELECT * not COUNT(*)
- Return only the SQL query without any markdown formatting
- Do not include ```sql or ``` in your response
- Column names are case-sensitive: id, order_date, region, product_name, unit_price, quantity_sold, discount_percent, profit
Remember:
- "Show all records" = SELECT * (not COUNT)
- "How many records" = SELECT COUNT(*)
- "List all" = SELECT * (not COUNT)
- "Display records" = SELECT * (not COUNT)
- For date-based queries, always use strftime() for month/year extraction
"""
]
def load_all_sales_data(db):
try:
conn = sqlite3.connect(db)
df = pd.read_sql_query("SELECT * FROM sales ORDER BY order_date DESC", conn)
conn.close()
return df
except Exception as e:
st.error(f"Error loading data: {e}")
return pd.DataFrame()
# Streamlit app
st.set_page_config(page_title="SQL Query Generator with Gemini", layout="wide")
st.header("π Data Query Assistant to Retrieve SQL Data")
st.subheader("Ask questions about your sales data in natural language!")
# Create tabs
tab1, tab2 = st.tabs(["π€ AI Query", "π View All Data"])
with tab2:
st.header("π Complete Sales Data")
# Load and display all data
all_data = load_all_sales_data("salesDummyData.db")
if not all_data.empty:
# Display summary statistics
col1, col2, col3, col4 = st.columns(4)
with col1:
st.metric("Total Records", len(all_data))
with col2:
st.metric("Total Profit", f"${all_data['profit'].sum():,.2f}")
with col3:
st.metric("Average Order Value", f"${(all_data['unit_price'] * all_data['quantity_sold']).mean():,.2f}")
with col4:
st.metric("Products", all_data['product_name'].nunique())
# Add filters
st.subheader("π Filter Data")
col1, col2, col3 = st.columns(3)
with col1:
selected_regions = st.multiselect(
"Select Region(s)",
options=all_data['region'].unique(),
default=all_data['region'].unique()
)
with col2:
selected_products = st.multiselect(
"Select Product(s)",
options=all_data['product_name'].unique(),
default=all_data['product_name'].unique()
)
with col3:
profit_range = st.slider(
"Profit Range",
min_value=float(all_data['profit'].min()),
max_value=float(all_data['profit'].max()),
value=(float(all_data['profit'].min()), float(all_data['profit'].max()))
)
# Apply filters
filtered_data = all_data[
(all_data['region'].isin(selected_regions)) &
(all_data['product_name'].isin(selected_products)) &
(all_data['profit'] >= profit_range[0]) &
(all_data['profit'] <= profit_range[1])
]
st.subheader(f"Filtered Results ({len(filtered_data)} records)")
st.dataframe(filtered_data, use_container_width=True)
csv_all = filtered_data.to_csv(index=False)
st.download_button(
label="π₯ Download Filtered Data as CSV",
data=csv_all,
file_name="sales_data_filtered.csv",
mime="text/csv"
)
# Quick insights
if not filtered_data.empty:
st.subheader("π Quick Insights")
col1, col2 = st.columns(2)
with col1:
top_products = filtered_data.groupby('product_name')['profit'].sum().sort_values(ascending=False).head(5)
st.write("**Top 5 Products by Total Profit:**")
for product, profit in top_products.items():
st.write(f"β’ {product}: ${profit:,.2f}")
with col2:
region_sales = filtered_data.groupby('region')['profit'].sum().sort_values(ascending=False)
st.write("**Total Profit by Region:**")
for region, profit in region_sales.items():
st.write(f"β’ {region}: ${profit:,.2f}")
with tab1:
st.sidebar.header("π Example Questions")
st.sidebar.write("β’ Show all records from North region")
st.sidebar.write("β’ What is the total profit by region?")
st.sidebar.write("β’ Which product has the highest unit price?")
st.sidebar.write("β’ Show orders with discount more than 20%")
st.sidebar.write("β’ What is the average quantity sold per product?")
st.sidebar.write("β’ Show top 5 most profitable orders")
st.sidebar.write("β’ How many orders were placed in each region?")
st.sidebar.write("β’ How much total profit before 2025-02-15?")
st.sidebar.write("β’ How much total profit in month of July?")
st.sidebar.write("β’ Show all sales in January 2025")
question = st.text_input("Input your question:", key="input", placeholder="e.g., Show me all smartphone sales")
submit = st.button("Ask Question")
if submit:
if question:
with st.spinner("Generating SQL query..."):
sql_query = get_sql_queries(question, prompt)
st.subheader("Generated SQL Query:")
st.code(sql_query, language="sql")
with st.spinner("Executing query..."):
data, columns = read_sql_queries(sql_query, "salesDummyData.db")
if data:
st.subheader(f"Results ({len(data)} rows):")
df = pd.DataFrame(data, columns=columns)
st.dataframe(df, use_container_width=True)
if len(data) > 0:
st.subheader("π Quick Stats:")
col1, col2, col3 = st.columns(3)
with col1:
st.metric("Total Rows", len(data))
if 'profit' in columns:
profit_idx = columns.index('profit')
profits = [row[profit_idx] for row in data if isinstance(row[profit_idx], (int, float))]
if profits:
with col2:
st.metric("Total Profit", f"${sum(profits):,.2f}")
with col3:
st.metric("Avg Profit", f"${sum(profits)/len(profits):,.2f}")
csv = df.to_csv(index=False)
st.download_button(
label="π₯ Download results as CSV",
data=csv,
file_name="query_results.csv",
mime="text/csv"
)
else:
st.info("No results found or there was an error executing the query.")
else:
st.warning("Please enter a question first!")
st.markdown("---")
st.markdown("*Powered by Google Gemini AI*")