louiecerv commited on
Commit
6a0b651
1 Parent(s): 2be8e4e

added the authentication

Browse files
Files changed (3) hide show
  1. app.py +152 -0
  2. requirements.txt +2 -0
  3. user_auth.db +0 -0
app.py ADDED
@@ -0,0 +1,152 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import sqlite3
3
+ import bcrypt
4
+ import os
5
+
6
+ # Database file path
7
+ DB_FILE = "user_auth.db"
8
+
9
+ # Initialize the database
10
+ def initialize_database():
11
+ if not os.path.exists(DB_FILE):
12
+ conn = sqlite3.connect(DB_FILE)
13
+ c = conn.cursor()
14
+ # Create users table
15
+ c.execute("""
16
+ CREATE TABLE users (
17
+ username TEXT PRIMARY KEY,
18
+ password_hash TEXT
19
+ )
20
+ """)
21
+ # Create settings table
22
+ c.execute("""
23
+ CREATE TABLE user_settings (
24
+ username TEXT PRIMARY KEY,
25
+ topic TEXT,
26
+ instructions 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
70
+ def save_settings(username, settings):
71
+ conn = sqlite3.connect(DB_FILE)
72
+ c = conn.cursor()
73
+ c.execute("""
74
+ INSERT INTO user_settings (username, topic, instructions)
75
+ VALUES (?, ?, ?)
76
+ ON CONFLICT(username)
77
+ DO UPDATE SET topic = excluded.topic, instructions = excluded.instructions
78
+ """, (username, settings["topic"], settings["instructions"]))
79
+ conn.commit()
80
+ conn.close()
81
+
82
+ # Dummy function to simulate prompt generation
83
+ def generate_prompt(topic, instructions):
84
+ return f"Generated prompt based on topic '{topic}' and instructions: '{instructions}'."
85
+
86
+ # Initialize the database
87
+ initialize_database()
88
+
89
+ # Multi-page Streamlit App
90
+ def main():
91
+ # State management
92
+ if "authenticated" not in st.session_state:
93
+ st.session_state.authenticated = False
94
+ st.session_state.username = None
95
+
96
+ def login_page():
97
+ st.title("Login")
98
+ username = st.text_input("Username", key="login_username")
99
+ password = st.text_input("Password", type="password", key="login_password")
100
+ if st.button("Login"):
101
+ if authenticate_user(username, password):
102
+ st.session_state.authenticated = True
103
+ st.session_state.username = username
104
+ else:
105
+ st.error("Invalid username or password.")
106
+
107
+ st.write("Don't have an account? [Sign Up](#)")
108
+
109
+ def sign_up_page():
110
+ st.title("Sign Up")
111
+ username = st.text_input("Username", key="signup_username")
112
+ password = st.text_input("Password", type="password", key="signup_password")
113
+ if st.button("Sign Up"):
114
+ if add_user(username, password):
115
+ st.success("Account created successfully. Please login.")
116
+ else:
117
+ st.error("Username already exists. Please choose another.")
118
+
119
+ def settings_page():
120
+ st.title("Manage Your Settings")
121
+ username = st.session_state.username
122
+ settings = load_settings(username)
123
+
124
+ # User-specific settings form
125
+ topic = st.text_area("Topic", value=settings.get("topic", ""), key="topic")
126
+ instructions = st.text_area("Custom Instructions", value=settings.get("instructions", ""), key="instructions")
127
+
128
+ if st.button("Save Settings"):
129
+ settings = {"topic": topic, "instructions": instructions}
130
+ save_settings(username, settings)
131
+ st.success("Settings saved successfully!")
132
+
133
+ if st.button("Generate Prompt"):
134
+ result = generate_prompt(topic, instructions)
135
+ st.success(result)
136
+
137
+ # Navigation between pages
138
+ st.sidebar.title("Navigation")
139
+ page = st.sidebar.radio("Go to", ["Login", "Sign Up", "Settings"])
140
+
141
+ if page == "Login":
142
+ login_page()
143
+ elif page == "Sign Up":
144
+ sign_up_page()
145
+ elif page == "Settings":
146
+ if st.session_state.authenticated:
147
+ settings_page()
148
+ else:
149
+ st.error("You must log in to access this page.")
150
+
151
+ if __name__ == "__main__":
152
+ main()
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ streamlit
2
+ bcrypt
user_auth.db ADDED
Binary file (20.5 kB). View file