dvwn commited on
Commit
723d980
·
1 Parent(s): b33b249

Streamlit UI Frontend version 1.0.0

Browse files
frontend/app.py CHANGED
@@ -1,2 +1,27 @@
1
  # Path: frontend/app.py
2
- # Main application file for the frontend, setting up the app and routing.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  # Path: frontend/app.py
2
+ # Main application file for the frontend, setting up the app and routing.
3
+ import streamlit as st
4
+ from components import auth, sidebar, chat
5
+
6
+ st.set_page_config(page_icon=":material/sql:", page_title="NL2SQL Assistant", layout="wide")
7
+
8
+ # Initialize global session state variables
9
+ if 'user_id' not in st.session_state:
10
+ st.session_state['user_id'] = None
11
+ if 'guest_mode' not in st.session_state:
12
+ st.session_state['guest_mode'] = False
13
+ if 'current_session_id' not in st.session_state:
14
+ st.session_state['current_session_id'] = None
15
+ if 'messages' not in st.session_state:
16
+ st.session_state['messages'] = []
17
+
18
+ def main():
19
+ if st.session_state['user_id'] is None and not st.session_state['guest_mode']:
20
+ # User not logged in, show authentication page
21
+ auth.render_auth()
22
+ else:
23
+ sidebar.render_sidebar()
24
+ chat.render_chat_interface()
25
+
26
+ if __name__ == "__main__":
27
+ main()
frontend/components/__init__.py DELETED
File without changes
frontend/components/__pycache__/__init__.cpython-313.pyc ADDED
Binary file (166 Bytes). View file
 
frontend/components/__pycache__/auth.cpython-313.pyc ADDED
Binary file (2.48 kB). View file
 
frontend/components/__pycache__/chat.cpython-313.pyc ADDED
Binary file (3.17 kB). View file
 
frontend/components/__pycache__/sidebar.cpython-313.pyc ADDED
Binary file (3.42 kB). View file
 
frontend/components/auth.py CHANGED
@@ -1,2 +1,33 @@
1
  # Path: frontend/components/auth.py
2
- # Authentication component for handling user login and registration.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  # Path: frontend/components/auth.py
2
+ # Authentication component for handling user login and registration.
3
+ import streamlit as st
4
+ from utils import api
5
+
6
+ def render_auth():
7
+ st.title("Welcome to NL2SQL Assistant Demo")
8
+ tab_login, tab_signup = st.tabs(["Login", "Sign Up"])
9
+
10
+ with tab_login:
11
+ with st.form("login_form"):
12
+ email = st.text_input("Email")
13
+ password = st.text_input("Password", type="password")
14
+ if st.form_submit_button("Login"):
15
+ response = api.user_login(email, password)
16
+ if response.get("success"):
17
+ st.session_state['user_id'] = response.get("user_id")
18
+ st.rerun()
19
+ else:
20
+ st.error("Invalid email or password. Please try again.")
21
+
22
+ with tab_signup:
23
+ with st.form("signup_form"):
24
+ email = st.text_input("Email")
25
+ new_password = st.text_input("Password", type="password")
26
+ if st.form_submit_button("Sign Up"):
27
+ st.success("Account created successfully! Plase login to continue.")
28
+
29
+ st.divider()
30
+ st.markdown("Or continue as a guest:")
31
+ if st.button("Continue as Guest", use_container_width=True):
32
+ st.session_state['guest_mode'] = True
33
+ st.rerun()
frontend/components/chat.py CHANGED
@@ -1,2 +1,51 @@
1
  # Path: frontend/components/chat.py
2
- # Chat component for handling user interactions and displaying chat history.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  # Path: frontend/components/chat.py
2
+ # Chat component for handling user interactions and displaying chat history.
3
+ import streamlit as st
4
+ import pandas as pd
5
+ from utils import api
6
+
7
+ def render_chat_interface():
8
+ st.title(":material/sql: NL2SQL Assistant")
9
+
10
+ # Render existing messages
11
+ for msg in st.session_state['messages']:
12
+ with st.chat_message(msg["role"]):
13
+ st.markdown(msg["content"])
14
+ if "sql" in msg and msg["sql"]:
15
+ st.code(msg["sql"], language="sql")
16
+ if "data" in msg and msg["data"]:
17
+ st.dataframe(pd.DataFrame(msg["data"]))
18
+
19
+ # Input area for new messages
20
+ if prompt := st.chat_input("Ask a question about your database (e.g., 'Show me top revenue in 2020')"):
21
+ # Display user prompt
22
+ st.session_state['messages'].append({"role": "user", "content": prompt})
23
+ with st.chat_message("user"):
24
+ st.markdown(prompt)
25
+
26
+ # Process and get assistant response
27
+ with st.chat_message("assistant"):
28
+ with st.spinner("Executing LangChain pipeline for RAG-based schema retrieval..."):
29
+ response = api.send_chat_message(
30
+ user_id = st.session_state['user_id'],
31
+ session_id = st.session_state.get('current_session_id'),
32
+ message = prompt
33
+ )
34
+
35
+ # Render results
36
+ st.markdown(response.get("answer", "Generated SQL based on the query:"))
37
+ st.code(response["sql"], language="sql")
38
+ if response.get("data"):
39
+ st.dataframe(pd.DataFrame(response["data"]))
40
+
41
+ # Save conversations to session state
42
+ st.session_state['messages'].append({
43
+ "role": "assistant",
44
+ "content": response.get("answer", ""),
45
+ "sql": response["sql"],
46
+ "data": response.get("data")
47
+ })
48
+
49
+ # Save session ID automatically if user is authenticated and it's a new session
50
+ if not st.session_state['guest_mode'] and st.session_state['current_session_id'] is None:
51
+ st.session_state['current_session_id'] = response.get("session_id")
frontend/components/sidebar.py CHANGED
@@ -1,2 +1,49 @@
1
  # Path: frontend/components/sidebar.py
2
- # Sidebar component for navigation and additional options.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  # Path: frontend/components/sidebar.py
2
+ # Sidebar component for navigation and additional options.
3
+ import streamlit as st
4
+ from utils import api
5
+
6
+ def render_sidebar():
7
+ with st.sidebar:
8
+ if st.button(":material/add: New Chat", use_container_width=True):
9
+ st.session_state['current_session_id'] = None
10
+ st.session_state['messages'] = []
11
+ st.rerun()
12
+ st.divider()
13
+
14
+ # Guest View
15
+ if st.session_state['guest_mode']:
16
+ st.info("You are in Guest Mode. Your conversation history will not be saved.")
17
+ st.write("Create an account to save your queries.")
18
+ if st.button("Log In / Sign Up", use_container_width=True):
19
+ st.session_state['guest_mode'] = False
20
+ st.rerun()
21
+ # Authenticated User View
22
+ else:
23
+ st.header(":material/history: History")
24
+ # Fetch history
25
+ history = api.get_user_history(st.session_state['user_id'])
26
+
27
+ for time_label, sessions in history.items():
28
+ if sessions:
29
+ st.caption(time_label)
30
+ for session in sessions:
31
+ col1, col2 = st.columns([5, 1])
32
+
33
+ with col1:
34
+ if st.button(f":material/chat_bubble: {session['title'][:20]}", key=f"sel_{session['id']}", use_container_width=True):
35
+ st.session_state['current_session_id'] = session['id']
36
+ st.session_state['messages'] = api.get_session_messages(session['id'])
37
+ st.rerun()
38
+ with col2:
39
+ if st.button(":material/delete:", key=f"del_{session['id']}", help="Delete Chat History"):
40
+ if api.del_conversation(session['id']):
41
+ if st.session_state['current_session_id'] == session['id']:
42
+ st.session_state['current_session_id'] = None
43
+ st.session_state['messages'] = []
44
+ st.rerun()
45
+ st.divider()
46
+ if st.button(":material/logout: Logout", use_container_width=True):
47
+ st.session_state['user_id'] = None
48
+ st.session_state['messages'] = []
49
+ st.rerun()
frontend/utils/__init__.py DELETED
File without changes
frontend/utils/__pycache__/__init__.cpython-313.pyc ADDED
Binary file (161 Bytes). View file
 
frontend/utils/__pycache__/api.cpython-313.pyc ADDED
Binary file (156 Bytes). View file