Spaces:
Sleeping
Sleeping
UI redesign: QDB brand theme + design system
Browse files- .streamlit/config.toml +18 -0
- app/core/auth.py +56 -13
- app/ui/_styles.py +614 -0
- app/ui/admin.py +199 -66
- app/ui/streamlit_app.py +116 -73
.streamlit/config.toml
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[theme]
|
| 2 |
+
base = "light"
|
| 3 |
+
primaryColor = "#8B1538"
|
| 4 |
+
backgroundColor = "#FAFAFA"
|
| 5 |
+
secondaryBackgroundColor = "#FFFFFF"
|
| 6 |
+
textColor = "#1A1A1A"
|
| 7 |
+
font = "sans serif"
|
| 8 |
+
|
| 9 |
+
[server]
|
| 10 |
+
maxUploadSize = 200
|
| 11 |
+
enableXsrfProtection = true
|
| 12 |
+
|
| 13 |
+
[browser]
|
| 14 |
+
gatherUsageStats = false
|
| 15 |
+
|
| 16 |
+
[client]
|
| 17 |
+
toolbarMode = "minimal"
|
| 18 |
+
showErrorDetails = false
|
app/core/auth.py
CHANGED
|
@@ -57,8 +57,9 @@ def _config_ok() -> bool:
|
|
| 57 |
|
| 58 |
|
| 59 |
def _login_screen(require_admin: bool) -> None:
|
| 60 |
-
|
| 61 |
-
|
|
|
|
| 62 |
|
| 63 |
if not _config_ok():
|
| 64 |
missing = []
|
|
@@ -69,26 +70,68 @@ def _login_screen(require_admin: bool) -> None:
|
|
| 69 |
if not settings.ms_client_secret:
|
| 70 |
missing.append("MS_CLIENT_SECRET")
|
| 71 |
st.error(
|
| 72 |
-
f"⚠️ إعدادات Microsoft auth ناقصة. الـ secrets الناقصة: **{', '.join(missing)}**.
|
| 73 |
-
"اتأكد إنها موجودة في HF Space → Settings → Variables and secrets."
|
| 74 |
-
)
|
| 75 |
-
st.caption(
|
| 76 |
-
f"Debug: tenant={'set' if settings.ms_tenant_id else 'MISSING'} "
|
| 77 |
-
f"client={'set' if settings.ms_client_id else 'MISSING'} "
|
| 78 |
-
f"secret={'set' if settings.ms_client_secret else 'MISSING'} "
|
| 79 |
-
f"redirect={settings.ms_redirect_uri or 'MISSING'} "
|
| 80 |
-
f"domain={settings.allowed_email_domain or 'MISSING'}"
|
| 81 |
)
|
| 82 |
st.stop()
|
| 83 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
auth_url = _msal_app().get_authorization_request_url(
|
| 85 |
SCOPES,
|
| 86 |
redirect_uri=settings.ms_redirect_uri,
|
| 87 |
prompt="select_account",
|
| 88 |
)
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
if require_admin:
|
| 91 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
st.stop()
|
| 93 |
|
| 94 |
|
|
|
|
| 57 |
|
| 58 |
|
| 59 |
def _login_screen(require_admin: bool) -> None:
|
| 60 |
+
# Inject styles + branded login screen
|
| 61 |
+
from app.ui._styles import inject_styles
|
| 62 |
+
inject_styles()
|
| 63 |
|
| 64 |
if not _config_ok():
|
| 65 |
missing = []
|
|
|
|
| 70 |
if not settings.ms_client_secret:
|
| 71 |
missing.append("MS_CLIENT_SECRET")
|
| 72 |
st.error(
|
| 73 |
+
f"⚠️ إعدادات Microsoft auth ناقصة. الـ secrets الناقصة: **{', '.join(missing)}**."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
)
|
| 75 |
st.stop()
|
| 76 |
|
| 77 |
+
st.markdown(
|
| 78 |
+
"""
|
| 79 |
+
<div class="qdb-login-hero">
|
| 80 |
+
<div class="qdb-login-logo">QDB</div>
|
| 81 |
+
<div class="qdb-login-title">مساعد المعرفة الداخلي</div>
|
| 82 |
+
<div class="qdb-login-subtitle">إجابات فورية من المراجع الرسمية للشركة</div>
|
| 83 |
+
</div>
|
| 84 |
+
""",
|
| 85 |
+
unsafe_allow_html=True,
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
auth_url = _msal_app().get_authorization_request_url(
|
| 89 |
SCOPES,
|
| 90 |
redirect_uri=settings.ms_redirect_uri,
|
| 91 |
prompt="select_account",
|
| 92 |
)
|
| 93 |
+
|
| 94 |
+
col1, col2, col3 = st.columns([1, 2, 1])
|
| 95 |
+
with col2:
|
| 96 |
+
st.link_button(
|
| 97 |
+
"🔐 تسجيل الدخول بحساب Microsoft",
|
| 98 |
+
auth_url,
|
| 99 |
+
type="primary",
|
| 100 |
+
use_container_width=True,
|
| 101 |
+
)
|
| 102 |
+
st.markdown(
|
| 103 |
+
f'<div style="text-align:center; color:#6B7280; font-size:12px; margin-top:8px;">'
|
| 104 |
+
f'الدخول مقصور على إيميلات <b>@{settings.allowed_email_domain}</b></div>',
|
| 105 |
+
unsafe_allow_html=True,
|
| 106 |
+
)
|
| 107 |
+
|
| 108 |
if require_admin:
|
| 109 |
+
st.markdown(
|
| 110 |
+
'<div style="background:#FEF3C7; border-left:3px solid #F59E0B; padding:12px 16px; '
|
| 111 |
+
'border-radius:8px; margin-top:1.5rem; font-size:13px;">'
|
| 112 |
+
'🛡️ <b>صفحة المشرفين</b> — حسابك يجب أن يكون في قائمة المشرفين.</div>',
|
| 113 |
+
unsafe_allow_html=True,
|
| 114 |
+
)
|
| 115 |
+
|
| 116 |
+
# Feature highlights
|
| 117 |
+
st.markdown('<div style="margin-top: 2.5rem;"></div>', unsafe_allow_html=True)
|
| 118 |
+
cols = st.columns(3)
|
| 119 |
+
features = [
|
| 120 |
+
("📚", "مراجع موثوقة", "إجابات من ملفات الشركة الرسمية فقط"),
|
| 121 |
+
("🔒", "آمن وخاص", "محمي بـ Microsoft SSO"),
|
| 122 |
+
("⚡", "سريع ودقيق", "استرجاع ذكي مع ذكر المصدر"),
|
| 123 |
+
]
|
| 124 |
+
for col, (icon, title, desc) in zip(cols, features):
|
| 125 |
+
with col:
|
| 126 |
+
st.markdown(
|
| 127 |
+
f'<div class="qdb-login-feature">'
|
| 128 |
+
f'<div class="qdb-login-feature-icon">{icon}</div>'
|
| 129 |
+
f'<div><div class="qdb-login-feature-title">{title}</div>'
|
| 130 |
+
f'<div class="qdb-login-feature-desc">{desc}</div></div>'
|
| 131 |
+
f'</div>',
|
| 132 |
+
unsafe_allow_html=True,
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
st.stop()
|
| 136 |
|
| 137 |
|
app/ui/_styles.py
ADDED
|
@@ -0,0 +1,614 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Centralized CSS + UI primitives for the QDB Knowledge Bot.
|
| 2 |
+
|
| 3 |
+
Importing `inject_styles()` once per page applies the full design system.
|
| 4 |
+
"""
|
| 5 |
+
import streamlit as st
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
_CSS = """
|
| 9 |
+
<style>
|
| 10 |
+
@import url('https://fonts.googleapis.com/css2?family=Cairo:wght@400;500;600;700;800&family=Inter:wght@400;500;600;700&display=swap');
|
| 11 |
+
|
| 12 |
+
/* ============== CSS Variables (QDB brand) ============== */
|
| 13 |
+
:root {
|
| 14 |
+
--qdb-primary: #8B1538;
|
| 15 |
+
--qdb-primary-dark: #6B1029;
|
| 16 |
+
--qdb-primary-light: #A53D5A;
|
| 17 |
+
--qdb-gold: #C9A961;
|
| 18 |
+
--qdb-gold-light: #E5C97D;
|
| 19 |
+
--qdb-bg: #FAFAFA;
|
| 20 |
+
--qdb-surface: #FFFFFF;
|
| 21 |
+
--qdb-surface-2: #F5F5F7;
|
| 22 |
+
--qdb-text: #1A1A1A;
|
| 23 |
+
--qdb-text-muted: #6B7280;
|
| 24 |
+
--qdb-border: #E5E7EB;
|
| 25 |
+
--qdb-success: #10B981;
|
| 26 |
+
--qdb-warning: #F59E0B;
|
| 27 |
+
--qdb-error: #EF4444;
|
| 28 |
+
--shadow-sm: 0 1px 2px rgba(0,0,0,0.04);
|
| 29 |
+
--shadow-md: 0 4px 12px rgba(0,0,0,0.08);
|
| 30 |
+
--shadow-lg: 0 12px 32px rgba(0,0,0,0.12);
|
| 31 |
+
--radius-sm: 8px;
|
| 32 |
+
--radius-md: 12px;
|
| 33 |
+
--radius-lg: 16px;
|
| 34 |
+
--radius-xl: 24px;
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
/* ============== Reset / Global ============== */
|
| 38 |
+
html, body, [class*="css"] {
|
| 39 |
+
font-family: 'Inter', 'Cairo', -apple-system, BlinkMacSystemFont, sans-serif;
|
| 40 |
+
color: var(--qdb-text);
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
.arabic, [dir="rtl"] {
|
| 44 |
+
font-family: 'Cairo', 'Inter', sans-serif;
|
| 45 |
+
direction: rtl;
|
| 46 |
+
text-align: right;
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
/* Hide Streamlit default chrome */
|
| 50 |
+
#MainMenu, footer, header[data-testid="stHeader"] {visibility: hidden;}
|
| 51 |
+
.stDeployButton {display: none;}
|
| 52 |
+
|
| 53 |
+
/* ============== App Container ============== */
|
| 54 |
+
.main .block-container {
|
| 55 |
+
padding-top: 2rem;
|
| 56 |
+
padding-bottom: 4rem;
|
| 57 |
+
max-width: 900px;
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
/* ============== Sidebar ============== */
|
| 61 |
+
section[data-testid="stSidebar"] {
|
| 62 |
+
background: linear-gradient(180deg, #ffffff 0%, #fafafa 100%);
|
| 63 |
+
border-right: 1px solid var(--qdb-border);
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
section[data-testid="stSidebar"] > div {
|
| 67 |
+
padding-top: 1rem;
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
/* ============== Brand Header ============== */
|
| 71 |
+
.qdb-brand {
|
| 72 |
+
display: flex;
|
| 73 |
+
align-items: center;
|
| 74 |
+
gap: 12px;
|
| 75 |
+
padding: 16px;
|
| 76 |
+
background: linear-gradient(135deg, var(--qdb-primary) 0%, var(--qdb-primary-dark) 100%);
|
| 77 |
+
border-radius: var(--radius-md);
|
| 78 |
+
margin-bottom: 1.5rem;
|
| 79 |
+
box-shadow: var(--shadow-md);
|
| 80 |
+
}
|
| 81 |
+
.qdb-brand-logo {
|
| 82 |
+
width: 44px; height: 44px;
|
| 83 |
+
background: var(--qdb-gold);
|
| 84 |
+
border-radius: 10px;
|
| 85 |
+
display: flex;
|
| 86 |
+
align-items: center;
|
| 87 |
+
justify-content: center;
|
| 88 |
+
font-weight: 800;
|
| 89 |
+
color: var(--qdb-primary-dark);
|
| 90 |
+
font-size: 18px;
|
| 91 |
+
letter-spacing: -0.5px;
|
| 92 |
+
}
|
| 93 |
+
.qdb-brand-text {
|
| 94 |
+
color: white;
|
| 95 |
+
font-weight: 700;
|
| 96 |
+
font-size: 16px;
|
| 97 |
+
line-height: 1.2;
|
| 98 |
+
}
|
| 99 |
+
.qdb-brand-subtitle {
|
| 100 |
+
color: rgba(255,255,255,0.7);
|
| 101 |
+
font-size: 11px;
|
| 102 |
+
font-weight: 400;
|
| 103 |
+
margin-top: 2px;
|
| 104 |
+
}
|
| 105 |
+
|
| 106 |
+
/* ============== Page Title ============== */
|
| 107 |
+
.qdb-page-title {
|
| 108 |
+
font-size: 28px;
|
| 109 |
+
font-weight: 700;
|
| 110 |
+
color: var(--qdb-text);
|
| 111 |
+
margin-bottom: 4px;
|
| 112 |
+
letter-spacing: -0.5px;
|
| 113 |
+
}
|
| 114 |
+
.qdb-page-subtitle {
|
| 115 |
+
font-size: 14px;
|
| 116 |
+
color: var(--qdb-text-muted);
|
| 117 |
+
margin-bottom: 2rem;
|
| 118 |
+
}
|
| 119 |
+
|
| 120 |
+
/* ============== Chat Messages ============== */
|
| 121 |
+
[data-testid="stChatMessage"] {
|
| 122 |
+
background: transparent !important;
|
| 123 |
+
padding: 0 !important;
|
| 124 |
+
margin-bottom: 16px !important;
|
| 125 |
+
}
|
| 126 |
+
[data-testid="stChatMessageContent"] {
|
| 127 |
+
background: var(--qdb-surface);
|
| 128 |
+
border: 1px solid var(--qdb-border);
|
| 129 |
+
border-radius: var(--radius-md);
|
| 130 |
+
padding: 16px 18px !important;
|
| 131 |
+
box-shadow: var(--shadow-sm);
|
| 132 |
+
transition: box-shadow 0.2s;
|
| 133 |
+
}
|
| 134 |
+
[data-testid="stChatMessageContent"]:hover {
|
| 135 |
+
box-shadow: var(--shadow-md);
|
| 136 |
+
}
|
| 137 |
+
|
| 138 |
+
/* User message — distinct color */
|
| 139 |
+
[data-testid="stChatMessage"]:has([aria-label="user"]) [data-testid="stChatMessageContent"] {
|
| 140 |
+
background: linear-gradient(135deg, var(--qdb-primary) 0%, var(--qdb-primary-light) 100%);
|
| 141 |
+
color: white;
|
| 142 |
+
border: none;
|
| 143 |
+
}
|
| 144 |
+
[data-testid="stChatMessage"]:has([aria-label="user"]) [data-testid="stChatMessageContent"] * {
|
| 145 |
+
color: white !important;
|
| 146 |
+
}
|
| 147 |
+
|
| 148 |
+
/* Avatar styling */
|
| 149 |
+
[data-testid="stChatMessageAvatarUser"],
|
| 150 |
+
[data-testid="stChatMessageAvatarAssistant"] {
|
| 151 |
+
background: var(--qdb-primary) !important;
|
| 152 |
+
border: 2px solid var(--qdb-gold);
|
| 153 |
+
}
|
| 154 |
+
|
| 155 |
+
/* ============== Chat Input ============== */
|
| 156 |
+
[data-testid="stChatInput"] {
|
| 157 |
+
border-radius: var(--radius-xl) !important;
|
| 158 |
+
border: 2px solid var(--qdb-border) !important;
|
| 159 |
+
box-shadow: var(--shadow-md);
|
| 160 |
+
padding: 4px !important;
|
| 161 |
+
transition: border-color 0.2s, box-shadow 0.2s;
|
| 162 |
+
}
|
| 163 |
+
[data-testid="stChatInput"]:focus-within {
|
| 164 |
+
border-color: var(--qdb-primary) !important;
|
| 165 |
+
box-shadow: 0 0 0 4px rgba(139, 21, 56, 0.1), var(--shadow-md);
|
| 166 |
+
}
|
| 167 |
+
[data-testid="stChatInput"] textarea {
|
| 168 |
+
font-size: 15px !important;
|
| 169 |
+
padding: 12px 16px !important;
|
| 170 |
+
}
|
| 171 |
+
|
| 172 |
+
/* ============== Citations ============== */
|
| 173 |
+
.qdb-citations {
|
| 174 |
+
margin-top: 12px;
|
| 175 |
+
padding: 12px 14px;
|
| 176 |
+
background: var(--qdb-surface-2);
|
| 177 |
+
border-left: 3px solid var(--qdb-gold);
|
| 178 |
+
border-radius: var(--radius-sm);
|
| 179 |
+
}
|
| 180 |
+
.qdb-citation-item {
|
| 181 |
+
display: flex;
|
| 182 |
+
align-items: center;
|
| 183 |
+
gap: 8px;
|
| 184 |
+
padding: 6px 0;
|
| 185 |
+
font-size: 13px;
|
| 186 |
+
color: var(--qdb-text);
|
| 187 |
+
}
|
| 188 |
+
.qdb-citation-num {
|
| 189 |
+
background: var(--qdb-primary);
|
| 190 |
+
color: white;
|
| 191 |
+
width: 20px; height: 20px;
|
| 192 |
+
border-radius: 50%;
|
| 193 |
+
display: inline-flex;
|
| 194 |
+
align-items: center;
|
| 195 |
+
justify-content: center;
|
| 196 |
+
font-size: 11px;
|
| 197 |
+
font-weight: 600;
|
| 198 |
+
flex-shrink: 0;
|
| 199 |
+
}
|
| 200 |
+
.qdb-citation-source { font-weight: 600; }
|
| 201 |
+
.qdb-citation-section { color: var(--qdb-text-muted); }
|
| 202 |
+
.qdb-citation-score {
|
| 203 |
+
margin-left: auto;
|
| 204 |
+
background: var(--qdb-success);
|
| 205 |
+
color: white;
|
| 206 |
+
padding: 2px 8px;
|
| 207 |
+
border-radius: 999px;
|
| 208 |
+
font-size: 11px;
|
| 209 |
+
font-weight: 600;
|
| 210 |
+
}
|
| 211 |
+
|
| 212 |
+
/* ============== Stat Cards (sidebar) ============== */
|
| 213 |
+
.qdb-stat {
|
| 214 |
+
background: var(--qdb-surface);
|
| 215 |
+
border: 1px solid var(--qdb-border);
|
| 216 |
+
border-radius: var(--radius-md);
|
| 217 |
+
padding: 14px;
|
| 218 |
+
margin-bottom: 8px;
|
| 219 |
+
transition: transform 0.15s, box-shadow 0.15s;
|
| 220 |
+
}
|
| 221 |
+
.qdb-stat:hover {
|
| 222 |
+
transform: translateY(-1px);
|
| 223 |
+
box-shadow: var(--shadow-md);
|
| 224 |
+
}
|
| 225 |
+
.qdb-stat-label {
|
| 226 |
+
font-size: 11px;
|
| 227 |
+
color: var(--qdb-text-muted);
|
| 228 |
+
font-weight: 500;
|
| 229 |
+
text-transform: uppercase;
|
| 230 |
+
letter-spacing: 0.5px;
|
| 231 |
+
margin-bottom: 4px;
|
| 232 |
+
}
|
| 233 |
+
.qdb-stat-value {
|
| 234 |
+
font-size: 22px;
|
| 235 |
+
font-weight: 700;
|
| 236 |
+
color: var(--qdb-primary);
|
| 237 |
+
line-height: 1;
|
| 238 |
+
}
|
| 239 |
+
.qdb-stat-icon {
|
| 240 |
+
float: right;
|
| 241 |
+
color: var(--qdb-gold);
|
| 242 |
+
font-size: 20px;
|
| 243 |
+
margin-top: -4px;
|
| 244 |
+
}
|
| 245 |
+
|
| 246 |
+
/* ============== User Badge (sidebar) ============== */
|
| 247 |
+
.qdb-user-badge {
|
| 248 |
+
display: flex;
|
| 249 |
+
align-items: center;
|
| 250 |
+
gap: 12px;
|
| 251 |
+
padding: 12px;
|
| 252 |
+
background: var(--qdb-surface);
|
| 253 |
+
border: 1px solid var(--qdb-border);
|
| 254 |
+
border-radius: var(--radius-md);
|
| 255 |
+
margin-bottom: 12px;
|
| 256 |
+
}
|
| 257 |
+
.qdb-user-avatar {
|
| 258 |
+
width: 40px; height: 40px;
|
| 259 |
+
border-radius: 50%;
|
| 260 |
+
background: linear-gradient(135deg, var(--qdb-primary), var(--qdb-primary-light));
|
| 261 |
+
color: white;
|
| 262 |
+
display: flex;
|
| 263 |
+
align-items: center;
|
| 264 |
+
justify-content: center;
|
| 265 |
+
font-weight: 700;
|
| 266 |
+
font-size: 16px;
|
| 267 |
+
flex-shrink: 0;
|
| 268 |
+
}
|
| 269 |
+
.qdb-user-name { font-weight: 600; font-size: 14px; line-height: 1.2; }
|
| 270 |
+
.qdb-user-email { font-size: 11px; color: var(--qdb-text-muted); margin-top: 2px; }
|
| 271 |
+
.qdb-user-role {
|
| 272 |
+
display: inline-block;
|
| 273 |
+
background: var(--qdb-gold);
|
| 274 |
+
color: var(--qdb-primary-dark);
|
| 275 |
+
padding: 1px 8px;
|
| 276 |
+
border-radius: 999px;
|
| 277 |
+
font-size: 10px;
|
| 278 |
+
font-weight: 700;
|
| 279 |
+
margin-top: 4px;
|
| 280 |
+
text-transform: uppercase;
|
| 281 |
+
letter-spacing: 0.5px;
|
| 282 |
+
}
|
| 283 |
+
|
| 284 |
+
/* ============== Buttons ============== */
|
| 285 |
+
.stButton > button {
|
| 286 |
+
border-radius: var(--radius-sm) !important;
|
| 287 |
+
font-weight: 600 !important;
|
| 288 |
+
transition: all 0.15s !important;
|
| 289 |
+
border: 1px solid var(--qdb-border) !important;
|
| 290 |
+
padding: 8px 16px !important;
|
| 291 |
+
}
|
| 292 |
+
.stButton > button:hover {
|
| 293 |
+
transform: translateY(-1px);
|
| 294 |
+
box-shadow: var(--shadow-md);
|
| 295 |
+
}
|
| 296 |
+
.stButton > button[kind="primary"] {
|
| 297 |
+
background: linear-gradient(135deg, var(--qdb-primary) 0%, var(--qdb-primary-dark) 100%) !important;
|
| 298 |
+
color: white !important;
|
| 299 |
+
border: none !important;
|
| 300 |
+
}
|
| 301 |
+
.stButton > button[kind="primary"]:hover {
|
| 302 |
+
background: linear-gradient(135deg, var(--qdb-primary-dark) 0%, var(--qdb-primary) 100%) !important;
|
| 303 |
+
box-shadow: 0 6px 16px rgba(139, 21, 56, 0.25);
|
| 304 |
+
}
|
| 305 |
+
|
| 306 |
+
/* Link button (Microsoft sign in) */
|
| 307 |
+
a[data-testid="stLinkButton"] {
|
| 308 |
+
background: linear-gradient(135deg, #2F2F2F 0%, #1A1A1A 100%) !important;
|
| 309 |
+
color: white !important;
|
| 310 |
+
border: none !important;
|
| 311 |
+
border-radius: var(--radius-sm) !important;
|
| 312 |
+
padding: 14px 24px !important;
|
| 313 |
+
font-weight: 600 !important;
|
| 314 |
+
font-size: 15px !important;
|
| 315 |
+
box-shadow: var(--shadow-md) !important;
|
| 316 |
+
transition: all 0.15s !important;
|
| 317 |
+
text-decoration: none !important;
|
| 318 |
+
}
|
| 319 |
+
a[data-testid="stLinkButton"]:hover {
|
| 320 |
+
transform: translateY(-1px);
|
| 321 |
+
box-shadow: var(--shadow-lg) !important;
|
| 322 |
+
}
|
| 323 |
+
|
| 324 |
+
/* ============== Tabs ============== */
|
| 325 |
+
.stTabs [data-baseweb="tab-list"] {
|
| 326 |
+
gap: 4px;
|
| 327 |
+
background: var(--qdb-surface-2);
|
| 328 |
+
padding: 4px;
|
| 329 |
+
border-radius: var(--radius-md);
|
| 330 |
+
}
|
| 331 |
+
.stTabs [data-baseweb="tab"] {
|
| 332 |
+
border-radius: var(--radius-sm) !important;
|
| 333 |
+
padding: 8px 16px !important;
|
| 334 |
+
font-weight: 500 !important;
|
| 335 |
+
color: var(--qdb-text-muted) !important;
|
| 336 |
+
background: transparent !important;
|
| 337 |
+
}
|
| 338 |
+
.stTabs [data-baseweb="tab"][aria-selected="true"] {
|
| 339 |
+
background: var(--qdb-surface) !important;
|
| 340 |
+
color: var(--qdb-primary) !important;
|
| 341 |
+
box-shadow: var(--shadow-sm);
|
| 342 |
+
}
|
| 343 |
+
|
| 344 |
+
/* ============== File Uploader ============== */
|
| 345 |
+
[data-testid="stFileUploader"] section {
|
| 346 |
+
border: 2px dashed var(--qdb-border) !important;
|
| 347 |
+
border-radius: var(--radius-md) !important;
|
| 348 |
+
background: var(--qdb-surface-2) !important;
|
| 349 |
+
transition: all 0.2s;
|
| 350 |
+
}
|
| 351 |
+
[data-testid="stFileUploader"] section:hover {
|
| 352 |
+
border-color: var(--qdb-primary) !important;
|
| 353 |
+
background: var(--qdb-surface) !important;
|
| 354 |
+
}
|
| 355 |
+
|
| 356 |
+
/* ============== Expander ============== */
|
| 357 |
+
.streamlit-expanderHeader {
|
| 358 |
+
background: var(--qdb-surface-2) !important;
|
| 359 |
+
border-radius: var(--radius-sm) !important;
|
| 360 |
+
font-weight: 600 !important;
|
| 361 |
+
color: var(--qdb-text) !important;
|
| 362 |
+
}
|
| 363 |
+
|
| 364 |
+
/* ============== Login Hero ============== */
|
| 365 |
+
.qdb-login-hero {
|
| 366 |
+
text-align: center;
|
| 367 |
+
padding: 3rem 2rem;
|
| 368 |
+
background: linear-gradient(135deg, var(--qdb-primary) 0%, var(--qdb-primary-dark) 100%);
|
| 369 |
+
border-radius: var(--radius-xl);
|
| 370 |
+
color: white;
|
| 371 |
+
margin-bottom: 2rem;
|
| 372 |
+
box-shadow: var(--shadow-lg);
|
| 373 |
+
position: relative;
|
| 374 |
+
overflow: hidden;
|
| 375 |
+
}
|
| 376 |
+
.qdb-login-hero::before {
|
| 377 |
+
content: "";
|
| 378 |
+
position: absolute;
|
| 379 |
+
top: -50%; right: -10%;
|
| 380 |
+
width: 300px; height: 300px;
|
| 381 |
+
background: radial-gradient(circle, var(--qdb-gold) 0%, transparent 70%);
|
| 382 |
+
opacity: 0.15;
|
| 383 |
+
}
|
| 384 |
+
.qdb-login-logo {
|
| 385 |
+
width: 80px; height: 80px;
|
| 386 |
+
background: var(--qdb-gold);
|
| 387 |
+
border-radius: 20px;
|
| 388 |
+
display: inline-flex;
|
| 389 |
+
align-items: center;
|
| 390 |
+
justify-content: center;
|
| 391 |
+
font-weight: 800;
|
| 392 |
+
color: var(--qdb-primary-dark);
|
| 393 |
+
font-size: 32px;
|
| 394 |
+
margin-bottom: 1.5rem;
|
| 395 |
+
box-shadow: 0 8px 24px rgba(0,0,0,0.2);
|
| 396 |
+
}
|
| 397 |
+
.qdb-login-title {
|
| 398 |
+
font-size: 32px;
|
| 399 |
+
font-weight: 800;
|
| 400 |
+
margin-bottom: 8px;
|
| 401 |
+
color: white;
|
| 402 |
+
}
|
| 403 |
+
.qdb-login-subtitle {
|
| 404 |
+
color: rgba(255,255,255,0.85);
|
| 405 |
+
font-size: 15px;
|
| 406 |
+
margin-bottom: 0;
|
| 407 |
+
}
|
| 408 |
+
.qdb-login-feature {
|
| 409 |
+
display: flex;
|
| 410 |
+
gap: 12px;
|
| 411 |
+
align-items: flex-start;
|
| 412 |
+
padding: 16px;
|
| 413 |
+
background: var(--qdb-surface-2);
|
| 414 |
+
border-radius: var(--radius-md);
|
| 415 |
+
margin-bottom: 8px;
|
| 416 |
+
}
|
| 417 |
+
.qdb-login-feature-icon {
|
| 418 |
+
font-size: 20px;
|
| 419 |
+
flex-shrink: 0;
|
| 420 |
+
}
|
| 421 |
+
.qdb-login-feature-title { font-weight: 600; font-size: 14px; }
|
| 422 |
+
.qdb-login-feature-desc { font-size: 12px; color: var(--qdb-text-muted); margin-top: 2px; }
|
| 423 |
+
|
| 424 |
+
/* ============== Source Card (admin) ============== */
|
| 425 |
+
.qdb-source-card {
|
| 426 |
+
background: var(--qdb-surface);
|
| 427 |
+
border: 1px solid var(--qdb-border);
|
| 428 |
+
border-radius: var(--radius-md);
|
| 429 |
+
padding: 14px 16px;
|
| 430 |
+
margin-bottom: 8px;
|
| 431 |
+
display: flex;
|
| 432 |
+
align-items: center;
|
| 433 |
+
gap: 12px;
|
| 434 |
+
transition: all 0.15s;
|
| 435 |
+
}
|
| 436 |
+
.qdb-source-card:hover {
|
| 437 |
+
border-color: var(--qdb-primary-light);
|
| 438 |
+
box-shadow: var(--shadow-md);
|
| 439 |
+
}
|
| 440 |
+
.qdb-source-icon {
|
| 441 |
+
width: 40px; height: 40px;
|
| 442 |
+
background: linear-gradient(135deg, var(--qdb-primary), var(--qdb-primary-light));
|
| 443 |
+
border-radius: var(--radius-sm);
|
| 444 |
+
display: flex;
|
| 445 |
+
align-items: center;
|
| 446 |
+
justify-content: center;
|
| 447 |
+
color: white;
|
| 448 |
+
font-size: 18px;
|
| 449 |
+
flex-shrink: 0;
|
| 450 |
+
}
|
| 451 |
+
.qdb-source-name { font-weight: 600; font-size: 14px; line-height: 1.3; word-break: break-word; }
|
| 452 |
+
.qdb-source-meta { font-size: 12px; color: var(--qdb-text-muted); margin-top: 2px; }
|
| 453 |
+
|
| 454 |
+
/* ============== Loading dots ============== */
|
| 455 |
+
@keyframes qdb-pulse {
|
| 456 |
+
0%, 80%, 100% { opacity: 0.3; transform: scale(0.8); }
|
| 457 |
+
40% { opacity: 1; transform: scale(1); }
|
| 458 |
+
}
|
| 459 |
+
.qdb-loading {
|
| 460 |
+
display: inline-flex;
|
| 461 |
+
gap: 4px;
|
| 462 |
+
}
|
| 463 |
+
.qdb-loading span {
|
| 464 |
+
width: 8px; height: 8px;
|
| 465 |
+
border-radius: 50%;
|
| 466 |
+
background: var(--qdb-primary);
|
| 467 |
+
animation: qdb-pulse 1.4s infinite ease-in-out;
|
| 468 |
+
}
|
| 469 |
+
.qdb-loading span:nth-child(2) { animation-delay: 0.2s; }
|
| 470 |
+
.qdb-loading span:nth-child(3) { animation-delay: 0.4s; }
|
| 471 |
+
|
| 472 |
+
/* ============== Toast / Alerts ============== */
|
| 473 |
+
div[data-testid="stAlert"] {
|
| 474 |
+
border-radius: var(--radius-md) !important;
|
| 475 |
+
border: none !important;
|
| 476 |
+
box-shadow: var(--shadow-sm);
|
| 477 |
+
}
|
| 478 |
+
|
| 479 |
+
/* ============== Section divider ============== */
|
| 480 |
+
.qdb-divider {
|
| 481 |
+
height: 1px;
|
| 482 |
+
background: linear-gradient(90deg, transparent, var(--qdb-border), transparent);
|
| 483 |
+
margin: 1.5rem 0;
|
| 484 |
+
}
|
| 485 |
+
|
| 486 |
+
/* ============== Empty state ============== */
|
| 487 |
+
.qdb-empty {
|
| 488 |
+
text-align: center;
|
| 489 |
+
padding: 3rem 2rem;
|
| 490 |
+
background: var(--qdb-surface);
|
| 491 |
+
border: 2px dashed var(--qdb-border);
|
| 492 |
+
border-radius: var(--radius-lg);
|
| 493 |
+
color: var(--qdb-text-muted);
|
| 494 |
+
}
|
| 495 |
+
.qdb-empty-icon { font-size: 48px; opacity: 0.4; margin-bottom: 1rem; }
|
| 496 |
+
.qdb-empty-title { font-weight: 600; font-size: 16px; color: var(--qdb-text); margin-bottom: 4px; }
|
| 497 |
+
.qdb-empty-desc { font-size: 13px; }
|
| 498 |
+
|
| 499 |
+
/* ============== Mobile responsive ============== */
|
| 500 |
+
@media (max-width: 768px) {
|
| 501 |
+
.main .block-container { padding: 1rem; }
|
| 502 |
+
.qdb-page-title { font-size: 22px; }
|
| 503 |
+
.qdb-login-title { font-size: 24px; }
|
| 504 |
+
.qdb-login-hero { padding: 2rem 1.5rem; }
|
| 505 |
+
}
|
| 506 |
+
</style>
|
| 507 |
+
"""
|
| 508 |
+
|
| 509 |
+
|
| 510 |
+
def inject_styles() -> None:
|
| 511 |
+
"""Inject the QDB design system CSS into the current Streamlit page."""
|
| 512 |
+
st.markdown(_CSS, unsafe_allow_html=True)
|
| 513 |
+
|
| 514 |
+
|
| 515 |
+
def brand_header() -> None:
|
| 516 |
+
"""Render the QDB sidebar brand block."""
|
| 517 |
+
st.markdown(
|
| 518 |
+
"""
|
| 519 |
+
<div class="qdb-brand">
|
| 520 |
+
<div class="qdb-brand-logo">QDB</div>
|
| 521 |
+
<div>
|
| 522 |
+
<div class="qdb-brand-text">Knowledge Bot</div>
|
| 523 |
+
<div class="qdb-brand-subtitle">مساعد المعرفة الداخلي</div>
|
| 524 |
+
</div>
|
| 525 |
+
</div>
|
| 526 |
+
""",
|
| 527 |
+
unsafe_allow_html=True,
|
| 528 |
+
)
|
| 529 |
+
|
| 530 |
+
|
| 531 |
+
def page_header(title: str, subtitle: str = "") -> None:
|
| 532 |
+
"""Render a styled page title + subtitle."""
|
| 533 |
+
st.markdown(
|
| 534 |
+
f"""
|
| 535 |
+
<div class="qdb-page-title">{title}</div>
|
| 536 |
+
<div class="qdb-page-subtitle">{subtitle}</div>
|
| 537 |
+
""",
|
| 538 |
+
unsafe_allow_html=True,
|
| 539 |
+
)
|
| 540 |
+
|
| 541 |
+
|
| 542 |
+
def stat_card(label: str, value: str | int, icon: str = "") -> None:
|
| 543 |
+
"""Render a single stat card."""
|
| 544 |
+
st.markdown(
|
| 545 |
+
f"""
|
| 546 |
+
<div class="qdb-stat">
|
| 547 |
+
<span class="qdb-stat-icon">{icon}</span>
|
| 548 |
+
<div class="qdb-stat-label">{label}</div>
|
| 549 |
+
<div class="qdb-stat-value">{value}</div>
|
| 550 |
+
</div>
|
| 551 |
+
""",
|
| 552 |
+
unsafe_allow_html=True,
|
| 553 |
+
)
|
| 554 |
+
|
| 555 |
+
|
| 556 |
+
def user_badge(name: str, email: str, is_admin: bool) -> None:
|
| 557 |
+
"""Render the user badge in the sidebar."""
|
| 558 |
+
initial = (name or email)[0].upper()
|
| 559 |
+
role_html = '<div class="qdb-user-role">مشرف</div>' if is_admin else ""
|
| 560 |
+
st.markdown(
|
| 561 |
+
f"""
|
| 562 |
+
<div class="qdb-user-badge">
|
| 563 |
+
<div class="qdb-user-avatar">{initial}</div>
|
| 564 |
+
<div style="min-width:0; flex:1;">
|
| 565 |
+
<div class="qdb-user-name">{name}</div>
|
| 566 |
+
<div class="qdb-user-email">{email}</div>
|
| 567 |
+
{role_html}
|
| 568 |
+
</div>
|
| 569 |
+
</div>
|
| 570 |
+
""",
|
| 571 |
+
unsafe_allow_html=True,
|
| 572 |
+
)
|
| 573 |
+
|
| 574 |
+
|
| 575 |
+
def citation_block(citations: list[dict]) -> None:
|
| 576 |
+
"""Render a styled citations block."""
|
| 577 |
+
if not citations:
|
| 578 |
+
return
|
| 579 |
+
items = []
|
| 580 |
+
for i, c in enumerate(citations, 1):
|
| 581 |
+
sec = c.get("section", "")
|
| 582 |
+
score = c.get("score", 0)
|
| 583 |
+
section_html = f'<span class="qdb-citation-section"> — {sec}</span>' if sec else ""
|
| 584 |
+
items.append(
|
| 585 |
+
f"""
|
| 586 |
+
<div class="qdb-citation-item">
|
| 587 |
+
<span class="qdb-citation-num">{i}</span>
|
| 588 |
+
<span class="qdb-citation-source">{c['source_file']}</span>{section_html}
|
| 589 |
+
<span class="qdb-citation-score">{score:.0%}</span>
|
| 590 |
+
</div>
|
| 591 |
+
"""
|
| 592 |
+
)
|
| 593 |
+
st.markdown(
|
| 594 |
+
f'<div class="qdb-citations">{"".join(items)}</div>',
|
| 595 |
+
unsafe_allow_html=True,
|
| 596 |
+
)
|
| 597 |
+
|
| 598 |
+
|
| 599 |
+
def empty_state(icon: str, title: str, description: str) -> None:
|
| 600 |
+
"""Render a friendly empty state."""
|
| 601 |
+
st.markdown(
|
| 602 |
+
f"""
|
| 603 |
+
<div class="qdb-empty">
|
| 604 |
+
<div class="qdb-empty-icon">{icon}</div>
|
| 605 |
+
<div class="qdb-empty-title">{title}</div>
|
| 606 |
+
<div class="qdb-empty-desc">{description}</div>
|
| 607 |
+
</div>
|
| 608 |
+
""",
|
| 609 |
+
unsafe_allow_html=True,
|
| 610 |
+
)
|
| 611 |
+
|
| 612 |
+
|
| 613 |
+
def divider() -> None:
|
| 614 |
+
st.markdown('<div class="qdb-divider"></div>', unsafe_allow_html=True)
|
app/ui/admin.py
CHANGED
|
@@ -1,99 +1,232 @@
|
|
| 1 |
-
"""Admin panel for managing the knowledge base.
|
| 2 |
-
|
| 3 |
-
Run:
|
| 4 |
-
streamlit run app/ui/admin.py
|
| 5 |
-
"""
|
| 6 |
from pathlib import Path
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
import streamlit as st
|
| 9 |
|
| 10 |
-
from app.core.auth import
|
| 11 |
from app.core.config import settings
|
| 12 |
from app.ingestion.pipeline import ingest_directory, ingest_file
|
| 13 |
from app.rag.retriever import delete_source, list_sources
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
|
| 16 |
-
st.set_page_config(page_title="QDB Admin", page_icon="⚙️", layout="
|
|
|
|
| 17 |
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
with st.sidebar:
|
| 23 |
-
render_user_badge()
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
settings.documents_dir.mkdir(parents=True, exist_ok=True)
|
| 29 |
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
)
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
)
|
| 43 |
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
for file in uploaded:
|
| 46 |
target = settings.documents_dir / file.name
|
| 47 |
target.write_bytes(file.getbuffer())
|
| 48 |
-
with st.spinner(f"فهرسة {file.name}..."):
|
| 49 |
try:
|
| 50 |
-
n = ingest_file(target, category=
|
| 51 |
-
st.success(f"✅ {file.name}: تم فهرسة {n} مقطع")
|
| 52 |
except Exception as e:
|
| 53 |
-
st.error(f"❌ {file.name}: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
with tab_reindex:
|
| 83 |
-
st.subheader("إعادة فهرسة كل الملفات")
|
| 84 |
-
st.warning("هيعيد فهرسة كل الملفات الموجودة في `data/documents/` من البداية.")
|
| 85 |
-
files_in_dir = [
|
| 86 |
-
p for p in settings.documents_dir.iterdir() if p.suffix.lower() in {".docx", ".pdf"}
|
| 87 |
-
]
|
| 88 |
-
st.info(f"عدد الملفات في الفولدر: {len(files_in_dir)}")
|
| 89 |
-
if st.button("🚀 ابدأ الفهرسة الكاملة", type="primary"):
|
| 90 |
-
with st.spinner("جاري الفهرسة..."):
|
| 91 |
results = ingest_directory()
|
| 92 |
for fname, count in results.items():
|
| 93 |
if count > 0:
|
| 94 |
st.success(f"✅ {fname}: {count} مقطع")
|
| 95 |
else:
|
| 96 |
st.error(f"❌ {fname}: فشل")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
|
| 99 |
if __name__ == "__main__":
|
|
|
|
| 1 |
+
"""Admin panel for managing the knowledge base."""
|
| 2 |
+
import sys
|
|
|
|
|
|
|
|
|
|
| 3 |
from pathlib import Path
|
| 4 |
|
| 5 |
+
_PROJECT_ROOT = Path(__file__).resolve().parents[2]
|
| 6 |
+
if str(_PROJECT_ROOT) not in sys.path:
|
| 7 |
+
sys.path.insert(0, str(_PROJECT_ROOT))
|
| 8 |
+
|
| 9 |
import streamlit as st
|
| 10 |
|
| 11 |
+
from app.core.auth import logout, require_login
|
| 12 |
from app.core.config import settings
|
| 13 |
from app.ingestion.pipeline import ingest_directory, ingest_file
|
| 14 |
from app.rag.retriever import delete_source, list_sources
|
| 15 |
+
from app.ui._styles import (
|
| 16 |
+
brand_header,
|
| 17 |
+
divider,
|
| 18 |
+
empty_state,
|
| 19 |
+
inject_styles,
|
| 20 |
+
page_header,
|
| 21 |
+
stat_card,
|
| 22 |
+
user_badge,
|
| 23 |
+
)
|
| 24 |
|
| 25 |
|
| 26 |
+
st.set_page_config(page_title="QDB Admin", page_icon="⚙️", layout="centered")
|
| 27 |
+
inject_styles()
|
| 28 |
|
| 29 |
|
| 30 |
+
CATEGORIES = [
|
| 31 |
+
"📋 عام",
|
| 32 |
+
"👥 موارد بشرية",
|
| 33 |
+
"💰 مالية",
|
| 34 |
+
"💻 تقني",
|
| 35 |
+
"📐 تعليمات تشغيل",
|
| 36 |
+
"📜 سياسات",
|
| 37 |
+
"📊 تقارير",
|
| 38 |
+
]
|
| 39 |
|
|
|
|
|
|
|
| 40 |
|
| 41 |
+
def _file_icon(name: str) -> str:
|
| 42 |
+
if name.lower().endswith(".pdf"):
|
| 43 |
+
return "📕"
|
| 44 |
+
if name.lower().endswith(".docx"):
|
| 45 |
+
return "📘"
|
| 46 |
+
return "📄"
|
| 47 |
|
|
|
|
| 48 |
|
| 49 |
+
def _human_size(b: int) -> str:
|
| 50 |
+
if b < 1024:
|
| 51 |
+
return f"{b} B"
|
| 52 |
+
if b < 1024**2:
|
| 53 |
+
return f"{b/1024:.1f} KB"
|
| 54 |
+
return f"{b/1024**2:.1f} MB"
|
| 55 |
|
| 56 |
+
|
| 57 |
+
def _render_sidebar(user) -> None:
|
| 58 |
+
with st.sidebar:
|
| 59 |
+
brand_header()
|
| 60 |
+
user_badge(user.name, user.email, user.is_admin)
|
| 61 |
+
if st.button("🚪 تسجيل الخروج", use_container_width=True):
|
| 62 |
+
logout()
|
| 63 |
+
divider()
|
| 64 |
+
sources = list_sources()
|
| 65 |
+
col1, col2 = st.columns(2)
|
| 66 |
+
with col1:
|
| 67 |
+
stat_card("ملفات", len(sources), "📚")
|
| 68 |
+
with col2:
|
| 69 |
+
stat_card("مقاطع", sum(s["chunks"] for s in sources), "🧩")
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
def _tab_upload() -> None:
|
| 73 |
+
st.markdown(
|
| 74 |
+
'<div style="font-size:18px; font-weight:600; margin-bottom:1rem;">'
|
| 75 |
+
'📤 رفع ملف جديد للمعرفة</div>',
|
| 76 |
+
unsafe_allow_html=True,
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
col1, col2 = st.columns([1, 2])
|
| 80 |
+
with col1:
|
| 81 |
+
category = st.selectbox("التصنيف", CATEGORIES, label_visibility="visible")
|
| 82 |
+
with col2:
|
| 83 |
+
st.markdown(
|
| 84 |
+
'<div style="background:#FFF8E7; border:1px solid #E5C97D; padding:8px 12px; '
|
| 85 |
+
'border-radius:8px; font-size:12px; color:#6B5817; margin-top:28px;">'
|
| 86 |
+
'💡 اختار تصنيف مناسب عشان البوت يفلتر النتائج بشكل أدق</div>',
|
| 87 |
+
unsafe_allow_html=True,
|
| 88 |
)
|
| 89 |
|
| 90 |
+
uploaded = st.file_uploader(
|
| 91 |
+
"اختر ملفات Word أو PDF (تقدر ترفع أكتر من ملف)",
|
| 92 |
+
type=["docx", "pdf"],
|
| 93 |
+
accept_multiple_files=True,
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
if uploaded:
|
| 97 |
+
st.markdown(
|
| 98 |
+
f'<div style="margin-top:8px; font-size:13px; color:#6B7280;">'
|
| 99 |
+
f'📋 جاهز للرفع: <b style="color:#8B1538;">{len(uploaded)} ملف</b></div>',
|
| 100 |
+
unsafe_allow_html=True,
|
| 101 |
+
)
|
| 102 |
+
if st.button("🚀 رفع وفهرسة الكل", type="primary", use_container_width=True):
|
| 103 |
+
settings.documents_dir.mkdir(parents=True, exist_ok=True)
|
| 104 |
+
cat_clean = category.split(" ", 1)[1] if " " in category else category
|
| 105 |
for file in uploaded:
|
| 106 |
target = settings.documents_dir / file.name
|
| 107 |
target.write_bytes(file.getbuffer())
|
| 108 |
+
with st.spinner(f"⏳ جاري فهرسة **{file.name}**..."):
|
| 109 |
try:
|
| 110 |
+
n = ingest_file(target, category=cat_clean)
|
| 111 |
+
st.success(f"✅ **{file.name}**: تم فهرسة {n} مقطع")
|
| 112 |
except Exception as e:
|
| 113 |
+
st.error(f"❌ **{file.name}**: {e}")
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
def _tab_manage() -> None:
|
| 117 |
+
st.markdown(
|
| 118 |
+
'<div style="font-size:18px; font-weight:600; margin-bottom:1rem;">'
|
| 119 |
+
'📋 الملفات المفهرسة</div>',
|
| 120 |
+
unsafe_allow_html=True,
|
| 121 |
+
)
|
| 122 |
+
|
| 123 |
+
sources = list_sources()
|
| 124 |
+
if not sources:
|
| 125 |
+
empty_state(
|
| 126 |
+
"📂",
|
| 127 |
+
"لا توجد ملفات مفهرسة",
|
| 128 |
+
"ارفع ملفاتك من تبويب 'رفع ملفات' لتبدأ",
|
| 129 |
+
)
|
| 130 |
+
return
|
| 131 |
+
|
| 132 |
+
for s in sources:
|
| 133 |
+
col1, col2, col3, col4 = st.columns([6, 1.2, 1, 1])
|
| 134 |
+
with col1:
|
| 135 |
+
icon = _file_icon(s["source_file"])
|
| 136 |
+
st.markdown(
|
| 137 |
+
f"""
|
| 138 |
+
<div class="qdb-source-card">
|
| 139 |
+
<div class="qdb-source-icon">{icon}</div>
|
| 140 |
+
<div style="flex:1; min-width:0;">
|
| 141 |
+
<div class="qdb-source-name">{s['source_file']}</div>
|
| 142 |
+
<div class="qdb-source-meta">{s['chunks']} مقطع مفهرس</div>
|
| 143 |
+
</div>
|
| 144 |
+
</div>
|
| 145 |
+
""",
|
| 146 |
+
unsafe_allow_html=True,
|
| 147 |
+
)
|
| 148 |
+
with col2:
|
| 149 |
+
st.markdown('<div style="margin-top:14px;"></div>', unsafe_allow_html=True)
|
| 150 |
+
st.caption(f"{s['chunks']} chunks")
|
| 151 |
+
with col3:
|
| 152 |
+
st.markdown('<div style="margin-top:10px;"></div>', unsafe_allow_html=True)
|
| 153 |
+
if st.button("🔄", key=f"r_{s['source_file']}", help="إعادة فهرسة"):
|
| 154 |
+
path = settings.documents_dir / s["source_file"]
|
| 155 |
+
if path.exists():
|
| 156 |
+
with st.spinner(f"إعادة فهرسة {s['source_file']}..."):
|
| 157 |
+
ingest_file(path)
|
| 158 |
+
st.rerun()
|
| 159 |
+
else:
|
| 160 |
+
st.error("الملف غير موجود في المجلد")
|
| 161 |
+
with col4:
|
| 162 |
+
st.markdown('<div style="margin-top:10px;"></div>', unsafe_allow_html=True)
|
| 163 |
+
if st.button("🗑️", key=f"d_{s['source_file']}", help="حذف"):
|
| 164 |
+
delete_source(s["source_file"])
|
| 165 |
+
path = settings.documents_dir / s["source_file"]
|
| 166 |
+
if path.exists():
|
| 167 |
+
path.unlink()
|
| 168 |
+
st.rerun()
|
| 169 |
+
|
| 170 |
+
|
| 171 |
+
def _tab_reindex() -> None:
|
| 172 |
+
st.markdown(
|
| 173 |
+
'<div style="font-size:18px; font-weight:600; margin-bottom:1rem;">'
|
| 174 |
+
'🔄 إعادة الفهرسة الكاملة</div>',
|
| 175 |
+
unsafe_allow_html=True,
|
| 176 |
+
)
|
| 177 |
|
| 178 |
+
settings.documents_dir.mkdir(parents=True, exist_ok=True)
|
| 179 |
+
files_in_dir = [
|
| 180 |
+
p for p in settings.documents_dir.iterdir() if p.suffix.lower() in {".docx", ".pdf"}
|
| 181 |
+
]
|
| 182 |
+
|
| 183 |
+
st.markdown(
|
| 184 |
+
f'<div style="background:#FEF3C7; border-left:3px solid #F59E0B; '
|
| 185 |
+
f'padding:12px 16px; border-radius:8px; margin-bottom:1rem;">'
|
| 186 |
+
f'<b>⚠️ تحذير:</b> هيعيد فهرسة كل الملفات (<b>{len(files_in_dir)}</b> ملف) '
|
| 187 |
+
f'من البداية. ده ممكن ياخد وقت.</div>',
|
| 188 |
+
unsafe_allow_html=True,
|
| 189 |
+
)
|
| 190 |
+
|
| 191 |
+
if files_in_dir:
|
| 192 |
+
for f in files_in_dir:
|
| 193 |
+
st.markdown(
|
| 194 |
+
f'<div style="padding:6px 12px; background:#F5F5F7; border-radius:6px; '
|
| 195 |
+
f'margin-bottom:4px; font-size:13px;">'
|
| 196 |
+
f'{_file_icon(f.name)} {f.name} '
|
| 197 |
+
f'<span style="color:#6B7280; float:left;">{_human_size(f.stat().st_size)}</span>'
|
| 198 |
+
f'</div>',
|
| 199 |
+
unsafe_allow_html=True,
|
| 200 |
+
)
|
| 201 |
+
|
| 202 |
+
if st.button("🚀 ابدأ إعادة الفهرسة", type="primary", use_container_width=True):
|
| 203 |
+
with st.spinner("جاري إعادة فهرسة كل الملفات..."):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 204 |
results = ingest_directory()
|
| 205 |
for fname, count in results.items():
|
| 206 |
if count > 0:
|
| 207 |
st.success(f"✅ {fname}: {count} مقطع")
|
| 208 |
else:
|
| 209 |
st.error(f"❌ {fname}: فشل")
|
| 210 |
+
else:
|
| 211 |
+
empty_state("📁", "لا توجد ملفات في المجلد", "ارفع ملفات من تبويب 'رفع ملفات' أولاً")
|
| 212 |
+
|
| 213 |
+
|
| 214 |
+
def main() -> None:
|
| 215 |
+
user = require_login(require_admin=True)
|
| 216 |
+
_render_sidebar(user)
|
| 217 |
+
|
| 218 |
+
page_header(
|
| 219 |
+
"⚙️ لوحة الإدارة",
|
| 220 |
+
f"إدارة قاعدة المعرفة — مسجل دخولك كـ {user.email}"
|
| 221 |
+
)
|
| 222 |
+
|
| 223 |
+
tab1, tab2, tab3 = st.tabs(["📤 رفع ملفات", "📋 إدارة الملفات", "🔄 إعادة الفهرسة"])
|
| 224 |
+
with tab1:
|
| 225 |
+
_tab_upload()
|
| 226 |
+
with tab2:
|
| 227 |
+
_tab_manage()
|
| 228 |
+
with tab3:
|
| 229 |
+
_tab_reindex()
|
| 230 |
|
| 231 |
|
| 232 |
if __name__ == "__main__":
|
app/ui/streamlit_app.py
CHANGED
|
@@ -1,9 +1,4 @@
|
|
| 1 |
-
"""Main chat interface for employees.
|
| 2 |
-
|
| 3 |
-
Run:
|
| 4 |
-
streamlit run app/ui/streamlit_app.py
|
| 5 |
-
"""
|
| 6 |
-
# Ensure the project root is on sys.path so `from app...` works regardless of cwd.
|
| 7 |
import sys
|
| 8 |
from pathlib import Path
|
| 9 |
|
|
@@ -13,121 +8,169 @@ if str(_PROJECT_ROOT) not in sys.path:
|
|
| 13 |
|
| 14 |
import streamlit as st
|
| 15 |
|
| 16 |
-
from app.core.auth import
|
| 17 |
from app.rag.generator import answer_question
|
| 18 |
from app.rag.retriever import list_sources
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
|
| 21 |
st.set_page_config(
|
| 22 |
page_title="QDB Knowledge Bot",
|
| 23 |
-
page_icon="
|
| 24 |
-
layout="
|
| 25 |
initial_sidebar_state="expanded",
|
| 26 |
)
|
| 27 |
-
|
| 28 |
-
st.markdown(
|
| 29 |
-
"""
|
| 30 |
-
<style>
|
| 31 |
-
.stApp { direction: ltr; }
|
| 32 |
-
.arabic { direction: rtl; text-align: right; }
|
| 33 |
-
.citation-box {
|
| 34 |
-
background: #f0f2f6;
|
| 35 |
-
border-left: 3px solid #1f77b4;
|
| 36 |
-
padding: 8px 12px;
|
| 37 |
-
margin: 4px 0;
|
| 38 |
-
border-radius: 4px;
|
| 39 |
-
font-size: 0.85em;
|
| 40 |
-
}
|
| 41 |
-
</style>
|
| 42 |
-
""",
|
| 43 |
-
unsafe_allow_html=True,
|
| 44 |
-
)
|
| 45 |
|
| 46 |
|
| 47 |
def _is_arabic(text: str) -> bool:
|
| 48 |
-
return any("" <= c <= "ۿ" for c in text)
|
| 49 |
|
| 50 |
|
| 51 |
-
def
|
| 52 |
-
with st.chat_message(
|
| 53 |
if _is_arabic(content):
|
| 54 |
st.markdown(f'<div class="arabic">{content}</div>', unsafe_allow_html=True)
|
| 55 |
else:
|
| 56 |
st.markdown(content)
|
| 57 |
if citations:
|
| 58 |
-
|
| 59 |
-
for i, c in enumerate(citations, 1):
|
| 60 |
-
sec = f" — {c['section']}" if c.get("section") else ""
|
| 61 |
-
st.markdown(
|
| 62 |
-
f'<div class="citation-box"><b>{i}.</b> {c["source_file"]}{sec} '
|
| 63 |
-
f'<span style="color:#888">(تطابق: {c["score"]:.2f})</span></div>',
|
| 64 |
-
unsafe_allow_html=True,
|
| 65 |
-
)
|
| 66 |
|
| 67 |
|
| 68 |
-
def
|
| 69 |
-
user =
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
-
st.title("🤖 بوت المعرفة الداخلي")
|
| 72 |
-
st.caption("اسأل أي سؤال عن سياسات وإجراءات الشركة — البوت يجاوب من الملفات الرسمية مباشرة")
|
| 73 |
|
|
|
|
| 74 |
with st.sidebar:
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
sources = list_sources()
|
| 79 |
-
st.metric("الملفات المفهرسة", len(sources))
|
| 80 |
total_chunks = sum(s["chunks"] for s in sources)
|
| 81 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
if sources:
|
| 84 |
-
with st.expander("الم
|
| 85 |
for s in sources:
|
| 86 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
-
|
| 89 |
-
|
|
|
|
| 90 |
st.session_state.messages = []
|
| 91 |
st.rerun()
|
| 92 |
|
| 93 |
if user.is_admin:
|
| 94 |
-
st.
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
if "messages" not in st.session_state:
|
| 98 |
st.session_state.messages = []
|
| 99 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
for msg in st.session_state.messages:
|
| 101 |
-
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 105 |
-
|
| 106 |
|
| 107 |
-
with st.chat_message("assistant"):
|
| 108 |
-
with st.spinner("جاري البحث في المراجع..."):
|
| 109 |
try:
|
| 110 |
ans = answer_question(prompt)
|
| 111 |
if _is_arabic(ans.text):
|
| 112 |
st.markdown(f'<div class="arabic">{ans.text}</div>', unsafe_allow_html=True)
|
| 113 |
else:
|
| 114 |
st.markdown(ans.text)
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
f'<div class="citation-box"><b>{i}.</b> {c["source_file"]}{sec} '
|
| 122 |
-
f'<span style="color:#888">(تطابق: {c["score"]:.2f})</span></div>',
|
| 123 |
-
unsafe_allow_html=True,
|
| 124 |
-
)
|
| 125 |
-
|
| 126 |
-
st.session_state.messages.append(
|
| 127 |
-
{"role": "assistant", "content": ans.text, "citations": ans.citations}
|
| 128 |
-
)
|
| 129 |
except Exception as e:
|
| 130 |
-
err = f"حدث خطأ: {e}"
|
| 131 |
st.error(err)
|
| 132 |
st.session_state.messages.append({"role": "assistant", "content": err})
|
| 133 |
|
|
|
|
| 1 |
+
"""Main chat interface for employees — QDB Knowledge Bot."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import sys
|
| 3 |
from pathlib import Path
|
| 4 |
|
|
|
|
| 8 |
|
| 9 |
import streamlit as st
|
| 10 |
|
| 11 |
+
from app.core.auth import logout, require_login
|
| 12 |
from app.rag.generator import answer_question
|
| 13 |
from app.rag.retriever import list_sources
|
| 14 |
+
from app.ui._styles import (
|
| 15 |
+
brand_header,
|
| 16 |
+
citation_block,
|
| 17 |
+
divider,
|
| 18 |
+
empty_state,
|
| 19 |
+
inject_styles,
|
| 20 |
+
page_header,
|
| 21 |
+
stat_card,
|
| 22 |
+
user_badge,
|
| 23 |
+
)
|
| 24 |
|
| 25 |
|
| 26 |
st.set_page_config(
|
| 27 |
page_title="QDB Knowledge Bot",
|
| 28 |
+
page_icon="🏛️",
|
| 29 |
+
layout="centered",
|
| 30 |
initial_sidebar_state="expanded",
|
| 31 |
)
|
| 32 |
+
inject_styles()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
|
| 35 |
def _is_arabic(text: str) -> bool:
|
| 36 |
+
return any("" <= c <= "ۿ" for c in (text or ""))
|
| 37 |
|
| 38 |
|
| 39 |
+
def _render_assistant_message(content: str, citations: list[dict] | None = None) -> None:
|
| 40 |
+
with st.chat_message("assistant", avatar="🏛️"):
|
| 41 |
if _is_arabic(content):
|
| 42 |
st.markdown(f'<div class="arabic">{content}</div>', unsafe_allow_html=True)
|
| 43 |
else:
|
| 44 |
st.markdown(content)
|
| 45 |
if citations:
|
| 46 |
+
citation_block(citations)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
|
| 49 |
+
def _render_user_message(content: str) -> None:
|
| 50 |
+
with st.chat_message("user", avatar="👤"):
|
| 51 |
+
if _is_arabic(content):
|
| 52 |
+
st.markdown(f'<div class="arabic">{content}</div>', unsafe_allow_html=True)
|
| 53 |
+
else:
|
| 54 |
+
st.markdown(content)
|
| 55 |
|
|
|
|
|
|
|
| 56 |
|
| 57 |
+
def _render_sidebar(user) -> None:
|
| 58 |
with st.sidebar:
|
| 59 |
+
brand_header()
|
| 60 |
+
user_badge(user.name, user.email, user.is_admin)
|
| 61 |
+
|
| 62 |
+
if st.button("🚪 تسجيل الخروج", use_container_width=True):
|
| 63 |
+
logout()
|
| 64 |
+
|
| 65 |
+
divider()
|
| 66 |
+
|
| 67 |
+
# Stats
|
| 68 |
sources = list_sources()
|
|
|
|
| 69 |
total_chunks = sum(s["chunks"] for s in sources)
|
| 70 |
+
col1, col2 = st.columns(2)
|
| 71 |
+
with col1:
|
| 72 |
+
stat_card("ملفات", len(sources), "📚")
|
| 73 |
+
with col2:
|
| 74 |
+
stat_card("مقاطع", total_chunks, "🧩")
|
| 75 |
|
| 76 |
if sources:
|
| 77 |
+
with st.expander(f"📂 المراجع المتاحة ({len(sources)})"):
|
| 78 |
for s in sources:
|
| 79 |
+
st.markdown(
|
| 80 |
+
f'<div style="padding:6px 0; font-size:13px;">'
|
| 81 |
+
f'<span style="color:#8B1538;">📄</span> {s["source_file"]} '
|
| 82 |
+
f'<span style="color:#6B7280;font-size:11px;">— {s["chunks"]} مقطع</span>'
|
| 83 |
+
f'</div>',
|
| 84 |
+
unsafe_allow_html=True,
|
| 85 |
+
)
|
| 86 |
|
| 87 |
+
divider()
|
| 88 |
+
|
| 89 |
+
if st.button("🗑️ بدء محادثة جديدة", use_container_width=True):
|
| 90 |
st.session_state.messages = []
|
| 91 |
st.rerun()
|
| 92 |
|
| 93 |
if user.is_admin:
|
| 94 |
+
st.caption("🛡️ صفحة الإدارة متاحة من القائمة الجانبية")
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
def _render_welcome_state() -> None:
|
| 98 |
+
"""Show suggested questions when chat is empty."""
|
| 99 |
+
empty_state(
|
| 100 |
+
icon="💬",
|
| 101 |
+
title="ابدأ محادثة جديدة",
|
| 102 |
+
description="اسأل عن أي سياسة أو إجراء أو معلومة في مراجع الشركة الرسمية"
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
st.markdown('<div style="margin-top: 1.5rem;"></div>', unsafe_allow_html=True)
|
| 106 |
+
st.markdown(
|
| 107 |
+
'<div style="font-size:13px; color:#6B7280; font-weight:600; '
|
| 108 |
+
'text-transform:uppercase; letter-spacing:0.5px; margin-bottom:8px;">'
|
| 109 |
+
'💡 اقتراحات</div>',
|
| 110 |
+
unsafe_allow_html=True,
|
| 111 |
+
)
|
| 112 |
+
|
| 113 |
+
suggestions = [
|
| 114 |
+
"ما هي إجراءات إنشاء التقييم؟",
|
| 115 |
+
"ما هي السياسات الرئيسية في الشركة؟",
|
| 116 |
+
"اشرح لي الـ workflow بالتفصيل",
|
| 117 |
+
"What are the key procedures?",
|
| 118 |
+
]
|
| 119 |
+
cols = st.columns(2)
|
| 120 |
+
for i, q in enumerate(suggestions):
|
| 121 |
+
with cols[i % 2]:
|
| 122 |
+
if st.button(q, key=f"sug_{i}", use_container_width=True):
|
| 123 |
+
st.session_state.pending_question = q
|
| 124 |
+
st.rerun()
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
def main() -> None:
|
| 128 |
+
user = require_login()
|
| 129 |
+
_render_sidebar(user)
|
| 130 |
+
|
| 131 |
+
page_header(
|
| 132 |
+
"🏛️ مساعد المعرفة",
|
| 133 |
+
f"مرحباً {user.name.split()[0] if user.name else ''} — اسأل أي سؤال عن سياسات وإجراءات الشركة"
|
| 134 |
+
)
|
| 135 |
|
| 136 |
if "messages" not in st.session_state:
|
| 137 |
st.session_state.messages = []
|
| 138 |
|
| 139 |
+
# Show welcome state if no messages
|
| 140 |
+
if not st.session_state.messages:
|
| 141 |
+
_render_welcome_state()
|
| 142 |
+
|
| 143 |
+
# Render existing messages
|
| 144 |
for msg in st.session_state.messages:
|
| 145 |
+
if msg["role"] == "user":
|
| 146 |
+
_render_user_message(msg["content"])
|
| 147 |
+
else:
|
| 148 |
+
_render_assistant_message(msg["content"], msg.get("citations"))
|
| 149 |
|
| 150 |
+
# Handle pending question from suggestion click
|
| 151 |
+
pending = st.session_state.pop("pending_question", None)
|
| 152 |
+
prompt = pending or st.chat_input("✍️ اسأل سؤالك هنا...")
|
| 153 |
+
|
| 154 |
+
if prompt:
|
| 155 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 156 |
+
_render_user_message(prompt)
|
| 157 |
|
| 158 |
+
with st.chat_message("assistant", avatar="🏛️"):
|
| 159 |
+
with st.spinner("🔍 جاري البحث في المراجع..."):
|
| 160 |
try:
|
| 161 |
ans = answer_question(prompt)
|
| 162 |
if _is_arabic(ans.text):
|
| 163 |
st.markdown(f'<div class="arabic">{ans.text}</div>', unsafe_allow_html=True)
|
| 164 |
else:
|
| 165 |
st.markdown(ans.text)
|
| 166 |
+
citation_block(ans.citations)
|
| 167 |
+
st.session_state.messages.append({
|
| 168 |
+
"role": "assistant",
|
| 169 |
+
"content": ans.text,
|
| 170 |
+
"citations": ans.citations,
|
| 171 |
+
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
except Exception as e:
|
| 173 |
+
err = f"⚠️ حدث خطأ: {e}"
|
| 174 |
st.error(err)
|
| 175 |
st.session_state.messages.append({"role": "assistant", "content": err})
|
| 176 |
|