-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
78 lines (66 loc) · 2.86 KB
/
Copy pathapp.py
File metadata and controls
78 lines (66 loc) · 2.86 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
import streamlit as st
import streamlit_authenticator as stauth
from ddgs import DDGS
# --- 1. PAGE SETUP ---
st.set_page_config(page_title="Nexus AI", page_icon="⚡")
# --- 2. INITIALIZE SESSION STATE ---
# We store credentials in session state so new registrations persist during the current session
if 'credentials' not in st.session_state:
st.session_state['credentials'] = {
"usernames": {
"youness_admin": {
"name": "Youness",
"password": "nexus2026",
"email": "youness@example.com"
}
}
}
# --- 3. SECURITY LAYER ---
# Modern way to hash the dictionary
stauth.Hasher.hash_passwords(st.session_state['credentials'])
def main():
# --- 4. INITIALIZE AUTHENTICATOR ---
authenticator = stauth.Authenticate(
st.session_state['credentials'],
"nexus_secure_cookie_v4", # Changed version name to force a fresh cookie
"nexus_signature_key",
cookie_expiry_days=30
)
# --- 5. AUTHENTICATION UI ---
if not st.session_state.get("authentication_status"):
tab1, tab2 = st.tabs(["Login", "Register"])
with tab2:
try:
# pre_authorized=None allows ANYONE to register without a whitelist
result = authenticator.register_user(location='main', pre_authorized=None, key='reg_form')
if result:
# 'result' is only truthy if the form was actually submitted successfully
st.success('Registration successful! Please switch to the Login tab.')
except Exception as e:
st.error(f"Registration Error: {e}")
with tab1:
try:
authenticator.login(location='main')
except Exception as e:
# This catches that 'User not authorized' bug specifically
st.info("Nexus is ready for login.")
# --- 6. AUTHORIZED APP AREA ---
if st.session_state.get("authentication_status"):
st.sidebar.title(f"Welcome, {st.session_state['name']}")
authenticator.logout('Logout', 'sidebar')
st.title("⚡ NEXUS AI")
st.markdown("---")
query = st.text_input("Enter your command:", key="nexus_search")
if query:
with st.spinner("Nexus is scanning..."):
with DDGS() as ddgs:
results = [r for r in ddgs.text(query, max_results=3)]
for res in results:
st.subheader(res['title'])
st.write(res['body'])
st.caption(f"Source: {res['href']}")
st.divider()
elif st.session_state.get("authentication_status") is False:
st.error('Username/password is incorrect')
if __name__ == "__main__":
main()