import time
import streamlit as st
import re
import asyncio
import nest_asyncio
from auth import check_login
from chat import load_user_sessions, load_chat_history, save_message
from rag import setup_rag_system
from db import init_connection
nest_asyncio.apply()
def login_page():
# Custom CSS for login page
st.markdown("""
""", unsafe_allow_html=True)
st.markdown("""
Legal Case RAG
Ask me about legal cases (2021â2025). I'll retrieve documents and give citations.
""", unsafe_allow_html=True)
# Center the form
col1, col2, col3 = st.columns([1, 2, 1])
with col2:
with st.form("login_form"):
st.markdown("### Login to Continue")
username = st.text_input("Username", placeholder="Enter your username")
password = st.text_input("Password", type="password", placeholder="Enter your password")
login_button = st.form_submit_button("đ Login", use_container_width=True)
if login_button:
db_conn = init_connection()
users_collection = db_conn["users"]
if check_login(username, password, users_collection):
st.session_state.logged_in = True
st.session_state.username = username
st.success(f"Welcome back, {username}!")
time.sleep(1)
st.rerun()
else:
st.error("Invalid username or password.")
st.markdown("---")
if st.button("Create an account", use_container_width=True):
st.session_state.show_create_account = True
st.rerun()
def create_account_page():
# Custom CSS for create account page
st.markdown("""
""", unsafe_allow_html=True)
st.markdown("""
Create Account
Set up your account to start chatting with the legal RAG system.
""", unsafe_allow_html=True)
db_conn = init_connection()
users_collection = db_conn["users"]
col1, col2, col3 = st.columns([1, 2, 1])
with col2:
with st.form("create_account_form"):
st.markdown("### Account Details")
new_username = st.text_input("Choose a Username", placeholder="Enter desired username")
new_password = st.text_input("Choose a Password", type="password", placeholder="Enter secure password")
create_button = st.form_submit_button("Create Account", use_container_width=True)
if create_button:
import bcrypt
if not new_username or not new_password:
st.error("Username and password cannot be empty.")
elif users_collection.find_one({"username": new_username}):
st.error("Username already exists.")
else:
salt = bcrypt.gensalt()
hashed_password = bcrypt.hashpw(new_password.encode('utf-8'), salt)
users_collection.insert_one({
"username": new_username,
"password": hashed_password.decode('utf-8')
})
st.success("Account created successfully! Please log in.")
st.session_state.show_create_account = False
st.rerun()
if st.button("Back to Login", use_container_width=True):
st.session_state.show_create_account = False
st.rerun()
def main_page():
# Enhanced CSS for chat interface
st.markdown("""