Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
326 lines (274 loc) · 11.7 KB
/
Copy pathapp.py
File metadata and controls
326 lines (274 loc) · 11.7 KB
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
import streamlit as st
from dotenv import load_dotenv
load_dotenv()
# Set up page configuration - must be the first Streamlit command
st.set_page_config(
page_title="PrivacyChatBoX",
page_icon="assets/logo.png",
layout="wide",
initial_sidebar_state="expanded",
)
# Import and apply custom CSS
from style import apply_custom_css
apply_custom_css()
from datetime import datetime
import os
import uuid
import pandas as pd
import importlib
# Import custom modules
from auth import authenticate, create_user, get_users, init_auth, ALLOW_SELF_REGISTRATION, validate_password_strength
from database import init_db, get_session
from models import User, Settings, DetectionEvent, Conversation, Message, File
from privacy_scanner import scan_text, anonymize_text
from ai_providers import get_ai_response, get_available_models
from utils import save_uploaded_file
import azure_auth
# Initialize the database
init_db()
# Initialize session state variables if they don't exist
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 "user_id" not in st.session_state:
st.session_state.user_id = None
if "role" not in st.session_state:
st.session_state.role = None
if "current_conversation_id" not in st.session_state:
st.session_state.current_conversation_id = None
if "conversations" not in st.session_state:
st.session_state.conversations = []
if "dark_mode" not in st.session_state:
st.session_state.dark_mode = False
if "must_change_password" not in st.session_state:
st.session_state.must_change_password = False
# Initialize authentication
init_auth()
# Define function to toggle dark mode
def toggle_dark_mode():
st.session_state.dark_mode = not st.session_state.dark_mode
# Use st.rerun() to apply changes
st.rerun()
# Import shared sidebar component
import shared_sidebar
# Clear sidebar state for fresh creation on each page load
if "sidebar_created" in st.session_state:
del st.session_state.sidebar_created
# Create shared sidebar - the function will ensure it's only created once
shared_sidebar.create_sidebar("main_app")
# Main page sidebar content
with st.sidebar:
if not st.session_state.authenticated:
# Add a nice logo/icon at the top - centered and bigger
col1, col2, col3 = st.columns([1, 3, 1])
with col2:
st.image("assets/logo.png", width=180)
st.caption("Secure AI Assistant with Privacy Protection")
# Create fancy tabs with icons
st.markdown("""
<style>
/* Tab styling */
.stTabs [data-baseweb="tab-list"] {
gap: 0;
background-color: #f0f8ff;
border-radius: 10px 10px 0 0;
padding: 5px;
}
.stTabs [data-baseweb="tab"] {
border-radius: 8px 8px 0 0;
border: none;
padding: 10px 16px;
font-weight: 600;
font-size: 15px;
background-color: transparent;
color: #555;
}
.stTabs [data-baseweb="tab"][aria-selected="true"] {
background-color: white;
border-bottom: 3px solid #1E88E5;
color: #1E88E5;
}
/* Tab content area */
.stTabs [data-baseweb="tab-panel"] {
background-color: white;
border-radius: 0 0 10px 10px;
padding: 20px;
box-shadow: 0 10px 20px rgba(0,0,0,0.1);
}
/* Fancy login button */
button[kind="primaryFormSubmit"] {
width: 100%;
height: 50px;
font-size: 16px;
font-weight: 600;
border-radius: 8px;
background: linear-gradient(90deg, #1E88E5 0%, #42a5f5 100%);
border: none;
margin-top: 10px;
transition: all 0.3s ease;
}
button[kind="primaryFormSubmit"]:hover {
background: linear-gradient(90deg, #1565C0 0%, #1E88E5 100%);
box-shadow: 0 5px 15px rgba(30, 136, 229, 0.3);
transform: translateY(-2px);
}
/* Register button */
#register_button {
background: linear-gradient(90deg, #66BB6A 0%, #81c784 100%);
}
#register_button:hover {
background: linear-gradient(90deg, #43A047 0%, #66BB6A 100%);
box-shadow: 0 5px 15px rgba(102, 187, 106, 0.3);
}
/* Form fields */
[data-testid="stTextInput"] > div:first-child {
background-color: #f8fafe;
border-radius: 8px;
border: 1px solid #e0e0e0;
padding: 5px;
box-shadow: inset 0 1px 2px rgba(0,0,0,0.05);
}
</style>
""", unsafe_allow_html=True)
# Create tabs with emoji icons
login_tab, register_tab = st.tabs(["👤 Login", "📝 Register"])
with login_tab:
login_container = st.container()
with login_container:
st.header("Welcome Back!")
st.write("Sign in to access your secure AI assistant")
login_username = st.text_input(
"Username",
key="login_username",
placeholder="Enter your username"
)
login_password = st.text_input(
"Password",
type="password",
key="login_password",
placeholder="Enter your password"
)
login_button = st.button(
"🔐 Sign In",
key="login_button",
use_container_width=True
)
if login_button:
success, user_id, role = authenticate(login_username, login_password)
if success:
st.session_state.authenticated = True
st.session_state.username = login_username
st.session_state.user_id = user_id
st.session_state.role = role
st.session_state.must_change_password = st.session_state.get("user_info", {}).get("must_change_password", False)
st.success(f"Welcome back, {login_username}!")
st.rerun()
else:
st.error("Invalid username or password")
# Add Azure AD login button
st.markdown("---")
st.write("Or sign in with:")
azure_auth.show_azure_login_button()
with register_tab:
register_container = st.container()
with register_container:
st.header("Create Account")
st.write("Join PrivacyChatBoX to access all privacy-focused AI features")
if not ALLOW_SELF_REGISTRATION:
st.info("Self-registration is disabled. Contact an administrator to create an account.")
reg_username = st.text_input(
"Choose a Username",
key="reg_username",
placeholder="Enter a unique username"
)
reg_password = st.text_input(
"Create Password",
type="password",
key="reg_password",
placeholder="Create a secure password"
)
reg_password_confirm = st.text_input(
"Confirm Password",
type="password",
key="reg_password_confirm",
placeholder="Confirm your password"
)
register_button = st.button(
"✅ Create Account",
key="register_button",
use_container_width=True
)
if register_button:
if not ALLOW_SELF_REGISTRATION:
st.error("Self-registration is disabled.")
elif not reg_username or not reg_password:
st.error("Username and password are required")
elif reg_password != reg_password_confirm:
st.error("Passwords do not match")
elif validate_password_strength(reg_password):
st.error(validate_password_strength(reg_password))
else:
success = create_user(reg_username, reg_password, role="user")
if success:
st.success("Registration successful! You can now login.")
else:
st.error("Username already exists or an error occurred during registration")
# The shared sidebar is already created earlier, no need to create it again
else:
pass
# Main content area
if not st.session_state.authenticated:
# Landing page for non-authenticated users
st.title("Welcome to PrivacyChatBoX")
st.write("### AI-powered privacy protection platform")
st.markdown("""
PrivacyChatBoX is an intelligent privacy protection platform that safeguards your sensitive information
when interacting with AI models. Our platform scans and anonymizes confidential data before sending it to AI services.
### Key Features:
- 🔒 Privacy scanning for sensitive information
- 🔄 Automatic anonymization
- 🤖 Multiple AI model integrations (OpenAI, Claude, Gemini)
- 📁 Support for multiple file formats
- 📊 Comprehensive logging
- 📑 PDF export capabilities
Please log in or register to get started.
""")
# Show feature columns
col1, col2, col3 = st.columns(3)
with col1:
st.header("🔍 Privacy Scanning")
st.write("Detect sensitive information like credit card numbers, SSNs, and more using advanced pattern matching.")
with col2:
st.header("🤖 Multiple AI Models")
st.write("Use your preferred AI model from OpenAI, Anthropic Claude, Google Gemini, or local LLMs.")
with col3:
st.header("🔄 Anonymization")
st.write("Automatically replace or mask detected sensitive information before sending to AI models.")
else:
# Welcome dashboard for authenticated users
st.title(f"Welcome back, {st.session_state.username}!")
st.write("### Please select an option from the sidebar menu")
if st.session_state.get("must_change_password"):
st.warning("Your password is temporary. Change it in Settings > Account before using the system.")
st.info("👈 Use the sidebar navigation on the left to access different features.")
# Quick access cards
col1, col2, col3 = st.columns(3)
with col1:
st.info("💬 **Chat**")
st.write("Start a new conversation with AI")
if st.button("Open Chat", key="home_chat_btn"):
st.switch_page("pages/chat.py")
with col2:
st.info("📜 **History**")
st.write("View your past conversations")
if st.button("Open History", key="home_history_btn"):
st.switch_page("pages/history.py")
with col3:
st.info("⚙️ **Settings**")
st.write("Configure your AI and privacy settings")
if st.button("Open Settings", key="home_settings_btn"):
st.switch_page("pages/settings.py")
# Footer
st.markdown("---")
st.markdown("PrivacyChatBoX © 2025 | Secure AI Communication")