-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
525 lines (444 loc) · 20.9 KB
/
Copy pathapp.py
File metadata and controls
525 lines (444 loc) · 20.9 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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
"""
IMS AstroBot — Main Streamlit Application
Entry point with sidebar-based login selection and role-based dashboards.
"""
import streamlit as st
import sys
from pathlib import Path
from datetime import datetime
# Ensure project root is in path
sys.path.insert(0, str(Path(__file__).parent))
# ── Page config (must be first Streamlit command) ──
st.set_page_config(
page_title="IMS AstroBot",
page_icon="🤖",
layout="wide",
initial_sidebar_state="expanded",
)
# ── Imports ──
from auth.auth import init_session_state, login, logout, register_user
from views.chat import render_chat_page
from views.admin import render_admin_page, render_ai_settings_page, render_memory_page, render_observability_page
from tests.config import CHROMA_PERSIST_DIR, UPLOAD_DIR, EMBEDDING_MODEL, LLM_MODE
# ═══════════════════════════════════════════════════════
# LIGHTWEIGHT HEALTH CHECK (admin only)
# ═══════════════════════════════════════════════════════
@st.cache_resource(ttl=60) # Cache health check results for 60 seconds
def _check_system_health() -> dict:
"""Lightweight health checks — no external LLM provider calls.
Cached for 60 seconds to reduce startup time and improve page switching performance.
- Removed embedding import check (loads lazily on first query anyway)
- Removed file counting in uploads directory (too slow with many files)
- Kept: SQLite connectivity, ChromaDB directory, LLM mode config
"""
health = {}
try:
from database.db import get_connection
conn = get_connection()
conn.execute("SELECT 1")
u = conn.execute("SELECT COUNT(*) as cnt FROM users").fetchone()["cnt"]
d = conn.execute("SELECT COUNT(*) as cnt FROM documents").fetchone()["cnt"]
conn.close()
health["sqlite"] = {"status": "ok", "message": f"{u} users, {d} documents"}
except Exception as e:
health["sqlite"] = {"status": "error", "message": str(e)}
chroma_path = Path(CHROMA_PERSIST_DIR)
health["chromadb"] = (
{"status": "ok", "message": f"Ready at {chroma_path.name}/"}
if chroma_path.exists()
else {"status": "warning", "message": "Will create on first use"}
)
# LLM mode only (no provider network calls here)
health["llm_mode"] = {"status": "ok", "message": LLM_MODE}
# SKIPPED: Embedding import check → loads lazily on first use, no need to check on startup
# SKIPPED: File counting in uploads directory → too slow if directory has thousands of files
return health
def _check_llm_providers() -> dict:
"""On-demand LLM provider health checks (can be slow / external)."""
from rag.providers.manager import get_manager
mgr = get_manager()
provider_statuses = mgr.get_all_statuses()
provider_statuses.pop("_mode", None)
results = {}
for key in ("ollama", "groq", "gemini"):
if key in provider_statuses:
results[key] = provider_statuses[key]
return results
def render_health_sidebar():
"""Show system health in sidebar (admin only).
Core components are always checked; LLM providers are on-demand.
"""
if "system_health" not in st.session_state:
st.session_state.system_health = _check_system_health()
st.session_state.health_check_time = datetime.now().strftime("%H:%M:%S")
health = st.session_state.system_health
icons = {"ok": "🟢", "warning": "🟡", "error": "🔴"}
ok_count = sum(1 for v in health.values() if v["status"] == "ok")
total = len(health)
st.sidebar.divider()
if ok_count == total:
st.sidebar.success(f"System: {ok_count}/{total} OK", icon="✅")
else:
err = sum(1 for v in health.values() if v["status"] == "error")
st.sidebar.error(f"System: {err} issues", icon="❌") if err else st.sidebar.warning("Warnings", icon="⚠️")
with st.sidebar.expander("🔍 Status Details", expanded=(ok_count < total)):
# Core components (fast, local checks)
core_labels = {
"sqlite": "📦 DB",
"chromadb": "🔮 VectorDB",
"llm_mode": "🔀 Mode",
"embeddings": "🔢 Embed",
"uploads": "📂 Uploads",
}
for key, label in core_labels.items():
info = health.get(key, {"status": "error", "message": "?"})
st.markdown(f"{icons.get(info['status'], '⚪')} **{label}**: {info['message']}")
st.caption(f"Checked {st.session_state.get('health_check_time', '')}")
if st.button("🔄 Re-check Core", use_container_width=True, key="recheck_core"):
st.session_state.system_health = _check_system_health()
st.session_state.health_check_time = datetime.now().strftime("%H:%M:%S")
st.rerun()
# On-demand LLM provider checks (can be slow / external)
st.markdown("---")
st.markdown("**🤖 LLM Providers (on-demand)**")
provider_health = st.session_state.get("llm_provider_health")
provider_labels = {
"ollama": "🖥️ Ollama",
"groq": "⚡ Groq",
"gemini": "💎 Gemini",
}
if provider_health:
for key, info in provider_health.items():
label = provider_labels.get(key, key)
st.markdown(
f"{icons.get(info.get('status', 'warning'), '⚪')} "
f"**{label}**: {info.get('message', '')}"
)
else:
st.caption("Providers not checked yet. Use the button below to run tests.")
if st.button("⚡ Check LLM Providers", use_container_width=True, key="recheck_llm"):
st.session_state.llm_provider_health = _check_llm_providers()
st.session_state.health_check_time = datetime.now().strftime("%H:%M:%S")
st.rerun()
# ═══════════════════════════════════════════════════════
# LOGIN PAGE (with sidebar role selector)
# ═══════════════════════════════════════════════════════
def render_login_page():
"""Login page with sidebar role selection."""
# ── Sidebar: role selector ──
with st.sidebar:
st.markdown(
"""
<div style='text-align: center; padding: 1.5rem 0 0.5rem;'>
<h2>🤖 IMS AstroBot</h2>
<p style='color: gray; font-size: 0.85rem;'>Institutional AI Assistant</p>
</div>
""",
unsafe_allow_html=True,
)
st.divider()
st.markdown("#### Select Login Type")
if "login_mode" not in st.session_state:
st.session_state.login_mode = "student_faculty"
if st.button("🔑 Admin Login", use_container_width=True,
type="primary" if st.session_state.login_mode == "admin" else "secondary"):
st.session_state.login_mode = "admin"
st.rerun()
if st.button("🎓 Student / Faculty", use_container_width=True,
type="primary" if st.session_state.login_mode == "student_faculty" else "secondary"):
st.session_state.login_mode = "student_faculty"
st.rerun()
st.divider()
st.caption("Choose your role, then log in →")
# ── Main area: login form ──
st.markdown(
"""
<div style='text-align: center; padding: 1rem 0;'>
<h1>🤖 IMS AstroBot</h1>
<p style='color: gray; font-size: 1.1rem;'>Institutional AI Assistant — Powered by RAG</p>
</div>
""",
unsafe_allow_html=True,
)
mode = st.session_state.login_mode
col1, col2, col3 = st.columns([1, 2, 1])
with col2:
if mode == "admin":
_render_admin_login()
else:
_render_student_faculty_login()
def _render_admin_login():
"""Admin login form."""
st.markdown("### 🔑 Administrator Login")
st.caption("Access the full admin dashboard — documents, users, analytics, AI settings.")
with st.form("admin_login_form"):
username = st.text_input("Admin Username", placeholder="Enter admin username")
password = st.text_input("Password", type="password", placeholder="Enter password")
submitted = st.form_submit_button("Login as Admin", use_container_width=True, type="primary")
if submitted:
if username and password:
if login(username, password):
if st.session_state.role == "admin":
st.success(f"Welcome, Administrator {st.session_state.username}!")
st.rerun()
else:
logout()
st.error("This account is not an administrator. Use Student/Faculty login.")
else:
st.error("Invalid username or password.")
else:
st.warning("Please enter both username and password.")
def _render_student_faculty_login():
"""Student/Faculty login + registration."""
tab_login, tab_register = st.tabs(["🎓 Login", "📝 Register"])
with tab_login:
st.markdown("### 🎓 Student / Faculty Login")
st.caption("Access the Q&A chatbot to ask questions about institutional documents.")
with st.form("sf_login_form"):
username = st.text_input("Username", placeholder="Enter your username")
password = st.text_input("Password", type="password", placeholder="Enter your password")
submitted = st.form_submit_button("Login", use_container_width=True, type="primary")
if submitted:
if username and password:
if login(username, password):
if st.session_state.role in ("student", "faculty"):
st.success(f"Welcome, {st.session_state.username}!")
st.rerun()
else:
logout()
st.error("Admin accounts should use the Admin Login option in the sidebar.")
else:
st.error("Invalid username or password.")
else:
st.warning("Please enter both username and password.")
with tab_register:
st.markdown("### 📝 Create Account")
with st.form("register_form"):
reg_fullname = st.text_input("Full Name", placeholder="Enter your full name")
reg_username = st.text_input("Username", placeholder="Choose a username", key="reg_user")
reg_password = st.text_input("Password", type="password", placeholder="Choose a password", key="reg_pass")
reg_password2 = st.text_input("Confirm Password", type="password", placeholder="Confirm password")
reg_role = st.selectbox("Role", ["student", "faculty"])
reg_submitted = st.form_submit_button("Register", use_container_width=True)
if reg_submitted:
if not all([reg_fullname, reg_username, reg_password, reg_password2]):
st.warning("Please fill in all fields.")
elif reg_password != reg_password2:
st.error("Passwords do not match.")
elif len(reg_password) < 4:
st.error("Password must be at least 4 characters.")
else:
if register_user(reg_username, reg_password, reg_role, reg_fullname):
st.success("Account created! Switch to the Login tab.")
else:
st.error("Username already taken.")
# ═══════════════════════════════════════════════════════
# ADMIN DASHBOARD FLOW
# ═══════════════════════════════════════════════════════
def run_admin_dashboard():
"""Full admin experience with sidebar navigation."""
with st.sidebar:
st.markdown(
"""
<div style='text-align: center; padding: 1rem 0;'>
<h2>🤖 IMS AstroBot</h2>
<p style='color: gray; font-size: 0.8rem;'>Admin Dashboard</p>
</div>
""",
unsafe_allow_html=True,
)
st.divider()
st.markdown(f"**🔑 {st.session_state.username}** (Admin)")
st.divider()
page = st.radio(
"Navigate",
["📄 Documents", "📊 Analytics", "👥 Users", "🤖 AI Settings", "💾 Memory", "📈 RAG Observability", "💬 Test Chat"],
label_visibility="collapsed",
)
st.divider()
if st.button("🚪 Logout", use_container_width=True):
logout()
st.rerun()
# Health dashboard in sidebar (admin only)
render_health_sidebar()
# Footer
with st.sidebar:
st.markdown(
"<div style='padding:0.5rem;color:gray;font-size:0.75rem;text-align:center;'>"
"IMS AstroBot v2.0<br>Powered by RAG + Ollama/Groq/Gemini</div>",
unsafe_allow_html=True,
)
# ── Page routing ──
try:
if page == "📄 Documents":
render_admin_page()
elif page == "📊 Analytics":
_render_analytics_page()
elif page == "👥 Users":
_render_users_page()
elif page == "🤖 AI Settings":
render_ai_settings_page()
elif page == "💾 Memory":
render_memory_page()
elif page == "📈 RAG Observability":
render_observability_page()
elif page == "💬 Test Chat":
render_chat_page()
except Exception as e:
st.error(f"Error: {e}")
import traceback
st.code(traceback.format_exc(), language="text")
import traceback
st.code(traceback.format_exc(), language="text")
def _render_analytics_page():
"""Analytics dashboard."""
from database.db import get_analytics
from ingestion.embedder import get_collection_stats
import pandas as pd
st.markdown("### 📊 Usage Analytics")
st.divider()
analytics = get_analytics()
c1, c2, c3, c4 = st.columns(4)
c1.metric("Total Queries", analytics["total_queries"])
c2.metric("Total Documents", analytics["total_documents"])
c3.metric("Total Users", analytics["total_users"])
c4.metric("Avg Response Time", f"{analytics['avg_response_ms']}ms")
st.divider()
if analytics["daily_queries"]:
st.subheader("Queries Per Day (Last 7 Days)")
df = pd.DataFrame(analytics["daily_queries"])
df.columns = ["Date", "Queries"]
st.bar_chart(df.set_index("Date"))
else:
st.info("No query data yet. Use the Test Chat to generate some!")
if analytics["top_users"]:
st.subheader("Top Querying Users")
df_u = pd.DataFrame(analytics["top_users"])
df_u.columns = ["Username", "Queries"]
st.dataframe(df_u, use_container_width=True, hide_index=True)
st.divider()
stats = get_collection_stats()
st.metric("Total Chunks in Vector DB", stats["total_chunks"])
# Query Logs
st.divider()
st.subheader("📋 Recent Query Logs")
from database.db import get_query_logs
logs = get_query_logs(limit=50)
if not logs:
st.info("No queries logged yet.")
else:
for log in logs:
with st.expander(
f"🔍 {log['query_text'][:80]}{'...' if len(log['query_text']) > 80 else ''} — "
f"by {log['username']} ({log['created_at'][:16]})"
):
st.markdown(f"**Query:** {log['query_text']}")
st.markdown(f"**Response:** {log['response_text']}")
if log["sources"]:
st.markdown(f"**Sources:** {log['sources']}")
st.caption(f"Response time: {log['response_time_ms']:.0f}ms | Time: {log['created_at']}")
def _render_users_page():
"""User management — pulled from admin.py's existing logic."""
from database.db import get_all_users, create_user, delete_user, toggle_user_active
st.markdown("### 👥 User Management")
st.divider()
st.subheader("Create New User")
with st.form("create_user_form"):
c1, c2 = st.columns(2)
with c1:
new_username = st.text_input("Username")
new_password = st.text_input("Password", type="password")
with c2:
new_fullname = st.text_input("Full Name")
new_role = st.selectbox("Role", ["student", "faculty", "admin"])
if st.form_submit_button("➕ Create User", use_container_width=True):
if new_username and new_password:
if create_user(new_username, new_password, new_role, new_fullname):
st.success(f"User '{new_username}' created as {new_role}.")
st.rerun()
else:
st.error("Username already exists.")
else:
st.warning("Username and password are required.")
st.divider()
st.subheader("Existing Users")
users = get_all_users()
if users:
for user in users:
c1, c2, c3, c4 = st.columns([3, 2, 1, 1])
with c1:
icon = "🟢" if user["is_active"] else "🔴"
st.markdown(f"{icon} **{user['username']}** ({user['full_name']})")
with c2:
badges = {"admin": "🔑 Admin", "faculty": "🎓 Faculty", "student": "📚 Student"}
st.markdown(badges.get(user["role"], user["role"]))
with c3:
active = bool(user["is_active"])
if st.button("Disable" if active else "Enable", key=f"toggle_{user['id']}"):
toggle_user_active(user["id"], not active)
st.rerun()
with c4:
if user["username"] != st.session_state.username:
if st.button("🗑️", key=f"del_user_{user['id']}"):
delete_user(user["id"])
st.rerun()
else:
st.info("No users found.")
# ═══════════════════════════════════════════════════════
# STUDENT / FACULTY FLOW
# ═══════════════════════════════════════════════════════
def run_student_faculty():
"""Simple chat-only interface for students and faculty."""
with st.sidebar:
st.markdown(
"""
<div style='text-align: center; padding: 1rem 0;'>
<h2>🤖 IMS AstroBot</h2>
<p style='color: gray; font-size: 0.8rem;'>Ask Questions</p>
</div>
""",
unsafe_allow_html=True,
)
st.divider()
role_icons = {"faculty": "🎓", "student": "📚"}
icon = role_icons.get(st.session_state.role, "👤")
st.markdown(f"**{icon} {st.session_state.username}**")
st.caption(f"Role: {st.session_state.role.title()}")
st.divider()
if st.button("🗑️ Clear Chat", use_container_width=True):
st.session_state.chat_history = []
st.session_state.sources_history = []
st.rerun()
st.divider()
if st.button("🚪 Logout", use_container_width=True):
logout()
st.rerun()
st.markdown(
"<div style='padding:0.5rem;color:gray;font-size:0.75rem;text-align:center;'>"
"IMS AstroBot v1.0</div>",
unsafe_allow_html=True,
)
# Render the chat page (no sidebar controls — we handle them above)
try:
render_chat_page()
except Exception as e:
st.error(f"Error: {e}")
import traceback
st.code(traceback.format_exc(), language="text")
# ═══════════════════════════════════════════════════════
# MAIN ENTRY POINT
# ═══════════════════════════════════════════════════════
def main():
"""Main application controller."""
init_session_state()
# Not authenticated → login page with sidebar role selector
if not st.session_state.get("authenticated", False):
render_login_page()
return
# Authenticated → route by role
if st.session_state.role == "admin":
run_admin_dashboard()
else:
run_student_faculty()
if __name__ == "__main__":
main()