louiecerv commited on
Commit
a18f29d
1 Parent(s): b6cd00d

save changes

Browse files
__pycache__/user_management.cpython-313.pyc ADDED
Binary file (4.48 kB). View file
 
app.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from user_management import add_user, authenticate_user, load_settings, save_settings, generate_prompt, initialize_database
3
+
4
+ # Initialize the database
5
+ initialize_database()
6
+
7
+ def main():
8
+ # State management
9
+ if "authenticated" not in st.session_state:
10
+ st.session_state.authenticated = False
11
+ st.session_state.username = None
12
+ st.session_state.page = "Login"
13
+
14
+ def login_page():
15
+ st.title("Login")
16
+ username = st.text_input("Username", key="login_username")
17
+ password = st.text_input("Password", type="password", key="login_password")
18
+ if st.button("Login"):
19
+ if authenticate_user(username, password):
20
+ st.success(f"Welcome back, {username}!")
21
+ st.session_state.authenticated = True
22
+ st.session_state.username = username
23
+ st.session_state.page = "Main Page"
24
+ else:
25
+ st.error("Invalid username or password.")
26
+ st.write("Don't have an account? [Sign Up](#)")
27
+
28
+ def sign_up_page():
29
+ st.title("Sign Up")
30
+ username = st.text_input("Username", key="signup_username")
31
+ password = st.text_input("Password", type="password", key="signup_password")
32
+ if st.button("Sign Up"):
33
+ if add_user(username, password):
34
+ st.success("Account created successfully. Please login.")
35
+ else:
36
+ st.error("Username already exists. Please choose another.")
37
+
38
+ def settings_page():
39
+ st.title("Chat with AI")
40
+ username = st.session_state.username
41
+ settings = load_settings(username)
42
+
43
+ # User-specific settings form
44
+ topic = st.text_area("Topic", value=settings.get("topic", ""), key="topic")
45
+ instructions = st.text_area("Custom Instructions", value=settings.get("instructions", ""), key="instructions")
46
+
47
+ if st.button("Generate Prompt"):
48
+ settings = {"topic": topic, "instructions": instructions}
49
+ save_settings(username, settings)
50
+ result = generate_prompt(topic, instructions)
51
+ st.success(result)
52
+
53
+ # Navigation between pages
54
+ if st.session_state.page == "Login":
55
+ login_page()
56
+ elif st.session_state.page == "Sign Up":
57
+ sign_up_page()
58
+ elif st.session_state.page == "Main Page":
59
+ if st.session_state.authenticated:
60
+ settings_page()
61
+ else:
62
+ st.error("You must log in to access this page.")
63
+ st.session_state.page = "Login"
64
+
65
+ # Sidebar for navigation
66
+ st.sidebar.title("Navigation")
67
+ if st.session_state.authenticated:
68
+ if st.sidebar.button("Go to Main Page"):
69
+ st.session_state.page = "Main Page"
70
+ if st.sidebar.button("Logout"):
71
+ st.session_state.authenticated = False
72
+ st.session_state.username = None
73
+ st.session_state.page = "Login"
74
+ else:
75
+ st.sidebar.radio(
76
+ "Go to",
77
+ ["Login", "Sign Up"],
78
+ key="sidebar_nav",
79
+ on_change=lambda: st.session_state.update(page=st.session_state.sidebar_nav)
80
+ )
81
+
82
+ if __name__ == "__main__":
83
+ main()
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ streamlit
2
+ bcrypt
user_auth.db ADDED
Binary file (20.5 kB). View file
 
user_management.py ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sqlite3
2
+ import bcrypt
3
+ import os
4
+ from datetime import datetime
5
+
6
+ DB_FILE = "user_auth.db"
7
+
8
+ # Initialize the database
9
+ def initialize_database():
10
+ if not os.path.exists(DB_FILE):
11
+ conn = sqlite3.connect(DB_FILE)
12
+ c = conn.cursor()
13
+ # Create users table
14
+ c.execute("""
15
+ CREATE TABLE users (
16
+ username TEXT PRIMARY KEY,
17
+ password_hash TEXT
18
+ )
19
+ """)
20
+ # Create settings table with timestamp
21
+ c.execute("""
22
+ CREATE TABLE user_settings (
23
+ username TEXT PRIMARY KEY,
24
+ topic TEXT,
25
+ instructions TEXT,
26
+ timestamp TEXT,
27
+ FOREIGN KEY (username) REFERENCES users (username)
28
+ )
29
+ """)
30
+ conn.commit()
31
+ conn.close()
32
+
33
+ # Add a new user to the database
34
+ def add_user(username, password):
35
+ conn = sqlite3.connect(DB_FILE)
36
+ c = conn.cursor()
37
+ password_hash = bcrypt.hashpw(password.encode(), bcrypt.gensalt()).decode()
38
+ try:
39
+ c.execute("INSERT INTO users (username, password_hash) VALUES (?, ?)", (username, password_hash))
40
+ conn.commit()
41
+ except sqlite3.IntegrityError:
42
+ conn.close()
43
+ return False # Username already exists
44
+ conn.close()
45
+ return True
46
+
47
+ # Authenticate a user
48
+ def authenticate_user(username, password):
49
+ conn = sqlite3.connect(DB_FILE)
50
+ c = conn.cursor()
51
+ c.execute("SELECT password_hash FROM users WHERE username = ?", (username,))
52
+ row = c.fetchone()
53
+ conn.close()
54
+ if row and bcrypt.checkpw(password.encode(), row[0].encode()):
55
+ return True
56
+ return False
57
+
58
+ # Load settings for a user
59
+ def load_settings(username):
60
+ conn = sqlite3.connect(DB_FILE)
61
+ c = conn.cursor()
62
+ c.execute("SELECT topic, instructions FROM user_settings WHERE username = ?", (username,))
63
+ row = c.fetchone()
64
+ conn.close()
65
+ if row:
66
+ return {"topic": row[0], "instructions": row[1]}
67
+ return {"topic": "", "instructions": ""}
68
+
69
+ # Save settings for a user with timestamp
70
+ def save_settings(username, settings):
71
+ conn = sqlite3.connect(DB_FILE)
72
+ c = conn.cursor()
73
+ timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
74
+ c.execute("""
75
+ INSERT INTO user_settings (username, topic, instructions, timestamp)
76
+ VALUES (?, ?, ?, ?)
77
+ ON CONFLICT(username)
78
+ DO UPDATE SET topic = excluded.topic, instructions = excluded.instructions, timestamp = excluded.timestamp
79
+ """, (username, settings["topic"], settings["instructions"], timestamp))
80
+ conn.commit()
81
+ conn.close()
82
+
83
+ # Dummy function to simulate prompt generation
84
+ def generate_prompt(topic, instructions):
85
+ return f"Generated prompt based on topic '{topic}' and instructions: '{instructions}'."