pratham0011 commited on
Commit
6274649
β€’
1 Parent(s): be9dcd8

Update streamlit_app.py

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