-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTask5.py
More file actions
129 lines (103 loc) · 4.93 KB
/
Copy pathTask5.py
File metadata and controls
129 lines (103 loc) · 4.93 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
import streamlit as st
import pandas as pd
from Tasks.Task1 import get_gemini_response
from Tasks.Task2 import execute_sql_query
from Tasks.Task3 import plot_data
from Tasks.Task4 import download_link
# Predefined SQL commands
sql_commands = {
"Retrieve all employees": "SELECT * FROM EMPLOYEE;",
"Retrieve employees with a specific salary range": "SELECT * FROM EMPLOYEE WHERE SALARY BETWEEN 50000 AND 70000;",
"High Salary Employees": "SELECT * FROM EMPLOYEE WHERE SALARY > 80000;",
"Female Employees": "SELECT * FROM EMPLOYEE WHERE GENDER = 'Female';",
"Young Employees (Age < 30)": "SELECT * FROM EMPLOYEE WHERE AGE < 30;",
"Top 10 Working Hours": "SELECT * FROM EMPLOYEE ORDER BY WORKING_HOURS DESC LIMIT 10;",
"Top 10 Monthly Lunch Bills": "SELECT * FROM EMPLOYEE ORDER BY MONTHLY_LUNCH_BILL DESC LIMIT 10;",
"Employees in IT Department": "SELECT * FROM EMPLOYEE WHERE DEPARTMENT = 'IT';",
"Senior Managers": "SELECT * FROM EMPLOYEE WHERE DESIGNATION = 'Senior Manager';",
"Lowest Salary Employees": "SELECT * FROM EMPLOYEE ORDER BY SALARY ASC LIMIT 10;",
"Employees with Bonus": "SELECT * FROM EMPLOYEE WHERE BONUS > 0;"
}
# Streamlit app configuration
st.set_page_config(page_title="I can Retrieve Any SQL query", layout="wide")
# Sidebar for input and predefined SQL commands
st.sidebar.title("Features")
# Ask question input
question = st.sidebar.text_input("Your Question:", key="input")
ask_question_button = st.sidebar.button("Ask SQL.AI")
# Dropdown menu for predefined SQL commands
selected_command = st.sidebar.selectbox("Select a predefined SQL command:", list(sql_commands.keys()))
# Button to execute the selected command
execute_command_button = st.sidebar.button("Execute Command")
# Widgets for custom search
st.sidebar.header("Custom Search Options")
selected_columns = st.sidebar.multiselect("Select Columns for Visualization:", ["SALARY", "AGE", "WORKING_HOURS", "BONUS"])
date_range = st.sidebar.date_input("Select Date Range:", [pd.to_datetime("2024-01-01"), pd.to_datetime("2024-12-31")])
st.sidebar.header("Download CSV")
# Main content area for data plot
st.title("SQL.AI - Retrieve SQL Data")
if ask_question_button:
response = get_gemini_response(question)
st.subheader("SQL.AI's Response:")
st.info(response)
if "Error executing SQL query" in response:
st.error(response)
else:
try:
# Step 1: Execute the SQL query and fetch data
data = execute_sql_query(response, "company.db")
if "Error executing SQL query" in data:
st.error(data)
else:
# Display the retrieved data in a table
st.subheader("All Data:")
st.table(data)
if data:
columns = ["ID", "NAME", "SALARY", "AGE", "GENDER", "DESIGNATION", "WORKING_HOURS", "MONTHLY_LUNCH_BILL", "BONUS"]
df = pd.DataFrame(data, columns=columns)
# Print the DataFrame
st.write("DataFrame:", df)
# Step 2: Plot data
plot_data(df)
# Step 3: Generate download link for CSV
st.markdown(download_link(df), unsafe_allow_html=True)
else:
st.warning("No data to plot.")
except Exception as e:
st.error(f"Error processing data: {e}")
elif execute_command_button:
if selected_command in sql_commands:
sql_query = sql_commands[selected_command]
response = f"Executing predefined SQL command: {selected_command}"
else:
response = get_gemini_response(question)
sql_query = response
st.subheader("Gemini's Response:")
st.info(response)
if "Error executing SQL query" in response:
st.error(response)
else:
try:
# Step 1: Execute the SQL query and fetch data
data = execute_sql_query(sql_query, "company.db")
if "Error executing SQL query" in data:
st.error(data)
else:
# Display the retrieved data in a table
st.subheader("All Data:")
st.table(data)
if data:
columns = ["ID", "NAME", "SALARY", "AGE", "GENDER", "DESIGNATION", "WORKING_HOURS", "MONTHLY_LUNCH_BILL", "BONUS"]
df = pd.DataFrame(data, columns=columns)
# Print the DataFrame
st.write("DataFrame:", df)
# Step 2: Plot data
plot_data(df)
# Step 3: Generate download link for CSV
st.markdown(download_link(df), unsafe_allow_html=True)
else:
st.warning("No data to plot.")
except Exception as e:
st.error(f"Error processing data: {e}")
# Display a default message when no command is executed
st.info("Enter a question or select a predefined SQL command to execute.")