Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import sqlite3 | |
| from passlib.hash import bcrypt | |
| import pandas as pd | |
| import re | |
| import warnings | |
| warnings.filterwarnings("ignore", message="module 'bcrypt' has no attribute '__about__'") | |
| if "is_starting" not in st.session_state: | |
| st.session_state["is_starting"] = True | |
| if "authenticated" not in st.session_state: | |
| st.session_state["authenticated"] = False | |
| from pages.About import show_about | |
| from pages.Text_prompt import show_text_prompt | |
| from pages.Multimodal import show_multimodal | |
| from pages.Settings import show_settings | |
| if "authenticated" not in st.session_state: | |
| st.session_state["authenticated"] = False | |
| def create_usertable(): | |
| conn = sqlite3.connect('users.db') | |
| c = conn.cursor() | |
| c.execute('CREATE TABLE IF NOT EXISTS userstable(username TEXT, password TEXT)') | |
| c.execute('CREATE TABLE IF NOT EXISTS system_instructions(username TEXT PRIMARY KEY, instruction TEXT)') | |
| c.execute('CREATE TABLE IF NOT EXISTS user_prompts(id INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT, prompt_time TEXT, prompt_type TEXT)') | |
| conn.commit() | |
| conn.close() | |
| def add_userdata(username, password): | |
| conn = sqlite3.connect('users.db') | |
| c = conn.cursor() | |
| c.execute('INSERT INTO userstable(username, password) VALUES (?,?)', (username, password)) | |
| conn.commit() | |
| conn.close() | |
| def login_user(username, password): | |
| conn = sqlite3.connect('users.db') | |
| c = conn.cursor() | |
| c.execute('SELECT password FROM userstable WHERE username =?', (username,)) | |
| stored_hash = c.fetchone() | |
| conn.close() | |
| if stored_hash: | |
| stored_hash = stored_hash[0] | |
| return check_hashes(password, stored_hash) | |
| else: | |
| return False | |
| def view_all_users(): | |
| conn = sqlite3.connect('users.db') | |
| c = conn.cursor() | |
| c.execute('SELECT * FROM userstable') | |
| data = c.fetchall() | |
| conn.close() | |
| return data | |
| # --- Hashing --- | |
| def make_hashes(password): | |
| return bcrypt.hash(password) | |
| def check_hashes(password, hashed_text): | |
| return bcrypt.verify(password, hashed_text) | |
| # --- Authentication --- | |
| def authenticate(username, password): | |
| return login_user(username, password) | |
| def logout(): | |
| del st.session_state["authenticated"] | |
| del st.session_state["username"] | |
| del st.session_state["page"] | |
| # --- Initialize session state --- | |
| if "authenticated" not in st.session_state: | |
| st.session_state["authenticated"] = False | |
| if "username" not in st.session_state: | |
| st.session_state["username"] = None | |
| if "page" not in st.session_state: | |
| st.session_state["page"] = "login" | |
| # --- Login page --- | |
| def login_page(): | |
| st.title("User Authentication System") | |
| st.subheader("Login Section") | |
| username = st.text_input("User Name") | |
| password = st.text_input("Password", type='password') | |
| if st.button("Login"): | |
| result = authenticate(username.lower(), password) | |
| if result: | |
| st.session_state["authenticated"] = True | |
| st.session_state["username"] = username | |
| st.success("Logged In as {}".format(username)) | |
| st.session_state["page"] = "main" | |
| st.session_state["is_starting"] = False | |
| st.rerun() | |
| else: | |
| st.warning("Incorrect Username/Password") | |
| st.write("Don't have an account? Click Signup.") | |
| # --- Signup button --- | |
| if st.button("Signup"): | |
| st.session_state["page"] = "signup" | |
| st.rerun() | |
| # --- Signup page --- | |
| def signup_page(): | |
| st.subheader("Create New Account") | |
| new_user = st.text_input("Username") | |
| new_password = st.text_input("Password", type='password') | |
| # Display password requirements | |
| st.write("Password Requirements:") | |
| st.write("* Minimum length: 8 characters") | |
| st.write("* Mix of uppercase and lowercase letters") | |
| st.write("* At least one number") | |
| st.write("* At least one special character") | |
| # Validate password strength | |
| col1, col2 = st.columns([1, 1]) | |
| if col1.button("Signup"): | |
| password_strength = validate_password(new_password) | |
| if password_strength: | |
| # Check if username already exists | |
| conn = sqlite3.connect('users.db') | |
| c = conn.cursor() | |
| c.execute('SELECT * FROM userstable WHERE username=?', (new_user,)) | |
| existing_user = c.fetchone() | |
| conn.close() | |
| if existing_user: | |
| st.error("Username already exists. Please choose a different username.") | |
| else: | |
| hashed_new_password = make_hashes(new_password) | |
| add_userdata(new_user, hashed_new_password) | |
| st.success("You have successfully created a valid Account") | |
| st.info("Go to Login Menu to login") | |
| st.session_state["page"] = "login" | |
| st.rerun() | |
| else: | |
| st.error("Password does not meet the requirements.") | |
| if col2.button("Cancel"): | |
| st.session_state["page"] = "login" | |
| st.rerun() | |
| # --- Validate password strength --- | |
| def validate_password(password): | |
| # Define password requirements | |
| min_length = 8 | |
| has_uppercase = re.search(r"[A-Z]", password) | |
| has_lowercase = re.search(r"[a-z]", password) | |
| has_number = re.search(r"\d", password) | |
| has_symbol = re.search(r"[!@#$%^&*()_+=-{};:'<>,./?]", password) | |
| # Check if password meets all requirements | |
| if (len(password) >= min_length and | |
| has_uppercase and | |
| has_lowercase and | |
| has_number and | |
| has_symbol): | |
| return True | |
| else: | |
| return False | |
| # --- Manage users page --- | |
| def manage_users_page(): | |
| st.subheader("User Management") | |
| user_result = view_all_users() | |
| clean_db = pd.DataFrame(user_result, columns=["Username", "Password"]) | |
| st.dataframe(clean_db) | |
| # --- Main app --- | |
| def main(): | |
| create_usertable() | |
| if st.session_state["page"] == "login": | |
| login_page() | |
| elif st.session_state["page"] == "signup": | |
| signup_page() | |
| else: | |
| st.title("Welcome") | |
| st.write("This is the Welcome page.") | |
| # Display username and logout button on every page | |
| st.sidebar.write(f"Welcome, {st.session_state['username']}") | |
| if st.sidebar.button("Logout"): | |
| logout() | |
| st.rerun() | |
| if __name__ == "__main__": | |
| main() |