Spaces:
Sleeping
Sleeping
File size: 6,326 Bytes
5f097cc 03dba14 5f097cc c74f24a 5f097cc c74f24a 5f097cc c74f24a 5f097cc |
1 2 3 4 5 6 7 8 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 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 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 |
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() |