Update streamlit_app.py
Browse files- streamlit_app.py +211 -203
streamlit_app.py
CHANGED
@@ -1,204 +1,212 @@
|
|
1 |
-
# import streamlit as st
|
2 |
-
# import requests
|
3 |
-
# import json
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
# st.
|
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 |
-
# st.
|
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 |
-
st.markdown("
|
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 |
-
st.
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
204 |
st.success("Chat history cleared!")
|
|
|
1 |
+
# import streamlit as st
|
2 |
+
# import requests
|
3 |
+
# import json
|
4 |
+
# import pandas as pd
|
5 |
+
|
6 |
+
# st.set_page_config(page_title="QueryMate: Text to SQL & CSV")
|
7 |
+
|
8 |
+
# st.markdown("# QueryMate: Text to SQL & CSV π¬ποΈ")
|
9 |
+
# st.markdown('''Welcome to QueryMate, your friendly assistant for converting natural language queries into SQL statements and CSV outputs!
|
10 |
+
# Let's get started with your data queries!''')
|
11 |
+
|
12 |
+
# # Load chat history
|
13 |
+
# def load_chat_history():
|
14 |
+
# try:
|
15 |
+
# with open('chat_history.json', 'r') as f:
|
16 |
+
# return json.load(f)
|
17 |
+
# except FileNotFoundError:
|
18 |
+
# return []
|
19 |
+
|
20 |
+
# def save_chat_history(history):
|
21 |
+
# with open('chat_history.json', 'w') as f:
|
22 |
+
# json.dump(history, f)
|
23 |
+
|
24 |
+
# chat_history = load_chat_history()
|
25 |
+
|
26 |
+
# # Data source selection
|
27 |
+
# data_source = st.radio("Select Data Source:", ('SQL Database', 'Employee CSV'))
|
28 |
+
|
29 |
+
# # Predefined queries
|
30 |
+
# predefined_queries = {
|
31 |
+
# 'SQL Database': [
|
32 |
+
# 'Print all students',
|
33 |
+
# 'Count total number of students',
|
34 |
+
# 'List students in Data Science class'
|
35 |
+
# ],
|
36 |
+
# 'Employee CSV': [
|
37 |
+
# 'Print employees having the department id equal to 100',
|
38 |
+
# 'Count total number of employees',
|
39 |
+
# 'List Top 5 employees according to salary in descending order'
|
40 |
+
# ]
|
41 |
+
# }
|
42 |
+
|
43 |
+
# st.markdown(f"### Predefined Queries for {data_source}")
|
44 |
+
|
45 |
+
# # Create buttons for predefined queries
|
46 |
+
# for query in predefined_queries[data_source]:
|
47 |
+
# if st.button(query):
|
48 |
+
# st.session_state.predefined_query = query
|
49 |
+
|
50 |
+
# st.markdown("### Enter Your Question")
|
51 |
+
# question = st.text_input("Input: ", key="input", value=st.session_state.get('predefined_query', ''))
|
52 |
+
|
53 |
+
# # Submit button
|
54 |
+
# submit = st.button("Submit")
|
55 |
+
|
56 |
+
# if submit:
|
57 |
+
# # Send request to FastAPI backend
|
58 |
+
# response = requests.post("http://localhost:8000/query",
|
59 |
+
# json={"question": question, "data_source": data_source})
|
60 |
+
# if response.status_code == 200:
|
61 |
+
# data = response.json()
|
62 |
+
# st.markdown(f"## Generated {'SQL' if data_source == 'SQL Database' else 'Pandas'} Query")
|
63 |
+
# st.code(data['query'])
|
64 |
+
|
65 |
+
# st.markdown("## Query Results")
|
66 |
+
# result = data['result']
|
67 |
+
|
68 |
+
# if isinstance(result, list) and len(result) > 0:
|
69 |
+
# if isinstance(result[0], dict):
|
70 |
+
# # For CSV queries that return a list of dictionaries
|
71 |
+
# df = pd.DataFrame(result)
|
72 |
+
# st.dataframe(df)
|
73 |
+
# elif isinstance(result[0], list):
|
74 |
+
# # For SQL queries that return a list of lists
|
75 |
+
# df = pd.DataFrame(result)
|
76 |
+
# st.dataframe(df)
|
77 |
+
# else:
|
78 |
+
# # For single column results
|
79 |
+
# st.dataframe(pd.DataFrame(result, columns=['Result']))
|
80 |
+
# elif isinstance(result, dict):
|
81 |
+
# # For single row results
|
82 |
+
# st.table(result)
|
83 |
+
# else:
|
84 |
+
# # For scalar results or empty results
|
85 |
+
# st.write(result)
|
86 |
+
|
87 |
+
# if data_source == 'Employee CSV':
|
88 |
+
# st.markdown("## Available CSV Columns")
|
89 |
+
# st.write(data['columns'])
|
90 |
+
|
91 |
+
# # Update chat history
|
92 |
+
# chat_history.append(f"π¨βπ»({data_source}): {question}")
|
93 |
+
# chat_history.append(f"π€: {data['query']}")
|
94 |
+
# save_chat_history(chat_history)
|
95 |
+
# else:
|
96 |
+
# st.error(f"Error processing your request: {response.text}")
|
97 |
+
|
98 |
+
# # Clear the predefined query from session state
|
99 |
+
# st.session_state.pop('predefined_query', None)
|
100 |
+
|
101 |
+
# # Display chat history
|
102 |
+
# st.markdown("## Chat History")
|
103 |
+
# for message in chat_history:
|
104 |
+
# st.text(message)
|
105 |
+
|
106 |
+
# # Option to clear chat history
|
107 |
+
# if st.button("Clear Chat History"):
|
108 |
+
# chat_history.clear()
|
109 |
+
# save_chat_history(chat_history)
|
110 |
+
# st.success("Chat history cleared!")
|
111 |
+
|
112 |
+
|
113 |
+
|
114 |
+
|
115 |
+
|
116 |
+
import streamlit as st
|
117 |
+
import requests
|
118 |
+
import pandas as pd
|
119 |
+
|
120 |
+
st.set_page_config(page_title="QueryMate: Text to SQL & CSV")
|
121 |
+
|
122 |
+
st.markdown("# QueryMate: Text to SQL & CSV π¬ποΈ")
|
123 |
+
st.markdown('''Welcome to QueryMate, your friendly assistant for converting natural language queries into SQL statements and CSV outputs!
|
124 |
+
Let's get started with your data queries!''')
|
125 |
+
|
126 |
+
# Initialize chat history in session state if it doesn't exist
|
127 |
+
if 'chat_history' not in st.session_state:
|
128 |
+
st.session_state.chat_history = []
|
129 |
+
|
130 |
+
# Data source selection
|
131 |
+
data_source = st.radio("Select Data Source:", ('SQL Database', 'Employee CSV'))
|
132 |
+
|
133 |
+
# Predefined queries
|
134 |
+
predefined_queries = {
|
135 |
+
'SQL Database': [
|
136 |
+
'Print all students',
|
137 |
+
'Count total number of students',
|
138 |
+
'List students in Data Science class'
|
139 |
+
],
|
140 |
+
'Employee CSV': [
|
141 |
+
'Print employees having the department id equal to 100',
|
142 |
+
'Count total number of employees',
|
143 |
+
'List Top 5 employees according to salary in descending order'
|
144 |
+
]
|
145 |
+
}
|
146 |
+
|
147 |
+
st.markdown(f"### Predefined Queries for {data_source}")
|
148 |
+
|
149 |
+
# Create buttons for predefined queries
|
150 |
+
for query in predefined_queries[data_source]:
|
151 |
+
if st.button(query):
|
152 |
+
st.session_state.predefined_query = query
|
153 |
+
|
154 |
+
st.markdown("### Enter Your Question")
|
155 |
+
question = st.text_input("Input: ", key="input", value=st.session_state.get('predefined_query', ''))
|
156 |
+
|
157 |
+
# Submit button
|
158 |
+
submit = st.button("Submit")
|
159 |
+
|
160 |
+
if submit:
|
161 |
+
# Send request to FastAPI backend
|
162 |
+
response = requests.post("http://localhost:8000/query",
|
163 |
+
json={"question": question, "data_source": data_source})
|
164 |
+
if response.status_code == 200:
|
165 |
+
data = response.json()
|
166 |
+
st.markdown(f"## Generated {'SQL' if data_source == 'SQL Database' else 'Pandas'} Query")
|
167 |
+
st.code(data['query'])
|
168 |
+
|
169 |
+
st.markdown("## Query Results")
|
170 |
+
result = data['result']
|
171 |
+
|
172 |
+
if isinstance(result, list) and len(result) > 0:
|
173 |
+
if isinstance(result[0], dict):
|
174 |
+
# For CSV queries that return a list of dictionaries
|
175 |
+
df = pd.DataFrame(result)
|
176 |
+
st.dataframe(df)
|
177 |
+
elif isinstance(result[0], list):
|
178 |
+
# For SQL queries that return a list of lists
|
179 |
+
df = pd.DataFrame(result)
|
180 |
+
st.dataframe(df)
|
181 |
+
else:
|
182 |
+
# For single column results
|
183 |
+
st.dataframe(pd.DataFrame(result, columns=['Result']))
|
184 |
+
elif isinstance(result, dict):
|
185 |
+
# For single row results
|
186 |
+
st.table(result)
|
187 |
+
else:
|
188 |
+
# For scalar results or empty results
|
189 |
+
st.write(result)
|
190 |
+
|
191 |
+
if data_source == 'Employee CSV':
|
192 |
+
st.markdown("## Available CSV Columns")
|
193 |
+
st.write(data['columns'])
|
194 |
+
|
195 |
+
# Update chat history in session state
|
196 |
+
st.session_state.chat_history.append(f"π¨βπ»({data_source}): {question}")
|
197 |
+
st.session_state.chat_history.append(f"π€: {data['query']}")
|
198 |
+
else:
|
199 |
+
st.error(f"Error processing your request: {response.text}")
|
200 |
+
|
201 |
+
# Clear the predefined query from session state
|
202 |
+
st.session_state.pop('predefined_query', None)
|
203 |
+
|
204 |
+
# Display chat history
|
205 |
+
st.markdown("## Chat History")
|
206 |
+
for message in st.session_state.chat_history:
|
207 |
+
st.text(message)
|
208 |
+
|
209 |
+
# Option to clear chat history
|
210 |
+
if st.button("Clear Chat History"):
|
211 |
+
st.session_state.chat_history = []
|
212 |
st.success("Chat history cleared!")
|