Spaces:
Sleeping
Sleeping
Download app.py from UNIQ-Project-AI/uniq_ui: direct link, hf CLI and curl.
- Browser
- Download file 32 kB
-
https://huggingface.co/spaces/UNIQ-Project-AI/uniq_ui/resolve/main/app.py
- Command line
-
hf download hf://spaces/UNIQ-Project-AI/uniq_ui/app.py
-
curl -L -o app.py https://huggingface.co/spaces/UNIQ-Project-AI/uniq_ui/resolve/main/app.py
32 kB
| import streamlit as st | |
| import requests | |
| import uuid | |
| import re | |
| # ========================= | |
| # ⚙️ CONFIGURATION | |
| # ========================= | |
| st.set_page_config( | |
| page_title="UNIQ AI Assistant", | |
| page_icon="🤖", | |
| layout="wide", | |
| initial_sidebar_state="expanded" | |
| ) | |
| BASE_URL = "https://uniq-project-ai-uniq-backendd.hf.space" | |
| CHAT_URL = f"{BASE_URL}/chat" | |
| CALC_URL = f"{BASE_URL}/calculate-gpa" | |
| PLAN_URL = f"{BASE_URL}/plan-gpa" | |
| # ========================= | |
| # 🎨 GLOBAL CSS | |
| # ========================= | |
| st.markdown(""" | |
| <style> | |
| @import url('https://fonts.googleapis.com/css2?family=Cairo:wght@400;500;600;700&family=JetBrains+Mono:wght@400;500&display=swap'); | |
| :root { | |
| --bg: #0a0f1e; | |
| --bg2: #0f1629; | |
| --bg3: #141c35; | |
| --surface: #1a2340; | |
| --surface2: #1f2a4a; | |
| --border: rgba(99,130,255,0.15); | |
| --border2: rgba(99,130,255,0.28); | |
| --accent: #4f7dff; | |
| --accent2: #7b9fff; | |
| --accent-glow: rgba(79,125,255,0.18); | |
| --text: #e8edf8; | |
| --text2: #8a96b8; | |
| --text3: #5a6585; | |
| --success: #22c55e; | |
| --danger: #ef4444; | |
| --radius: 14px; | |
| --radius-sm: 8px; | |
| --font: 'Cairo', sans-serif; | |
| --mono: 'JetBrains Mono', monospace; | |
| } | |
| /* ── HIDE STREAMLIT CHROME ── */ | |
| #MainMenu, footer, header { visibility: hidden; } | |
| .stDeployButton { display: none; } | |
| /* ── GLOBAL RESET ── */ | |
| html, body, [class*="css"] { | |
| font-family: var(--font) !important; | |
| background-color: var(--bg) !important; | |
| color: var(--text) !important; | |
| } | |
| .block-container { | |
| padding-top: 0 !important; | |
| padding-bottom: 1rem !important; | |
| max-width: 100% !important; | |
| } | |
| /* ── SIDEBAR ── */ | |
| [data-testid="stSidebar"] { | |
| background-color: var(--bg2) !important; | |
| border-right: 1px solid var(--border) !important; | |
| } | |
| [data-testid="stSidebar"] > div:first-child { | |
| padding: 24px 16px !important; | |
| } | |
| [data-testid="stSidebar"] .stMarkdown p, | |
| [data-testid="stSidebar"] .stMarkdown div, | |
| [data-testid="stSidebar"] label, | |
| [data-testid="stSidebar"] small { | |
| color: var(--text) !important; | |
| font-family: var(--font) !important; | |
| } | |
| /* sidebar selectbox */ | |
| [data-testid="stSidebar"] .stSelectbox > div > div { | |
| background: var(--surface) !important; | |
| border: 1px solid var(--border) !important; | |
| border-radius: var(--radius-sm) !important; | |
| color: var(--text) !important; | |
| font-family: var(--font) !important; | |
| } | |
| [data-testid="stSidebar"] .stSelectbox > div > div:focus-within { | |
| border-color: var(--accent) !important; | |
| box-shadow: none !important; | |
| } | |
| [data-testid="stSidebar"] .stSelectbox svg { fill: var(--text2) !important; } | |
| /* sidebar button */ | |
| [data-testid="stSidebar"] .stButton > button { | |
| width: 100% !important; | |
| background: transparent !important; | |
| border: 1px solid var(--border2) !important; | |
| color: var(--text2) !important; | |
| font-family: var(--font) !important; | |
| font-size: 13px !important; | |
| padding: 9px !important; | |
| border-radius: var(--radius-sm) !important; | |
| transition: all .2s !important; | |
| } | |
| [data-testid="stSidebar"] .stButton > button:hover { | |
| background: rgba(239,68,68,0.08) !important; | |
| border-color: var(--danger) !important; | |
| color: var(--danger) !important; | |
| } | |
| /* ── MAIN AREA ── */ | |
| .uniq-header { | |
| text-align: center; | |
| padding: 20px 32px 16px; | |
| border-bottom: 1px solid var(--border); | |
| margin-bottom: 0; | |
| } | |
| .uniq-header h1 { | |
| font-size: 22px; | |
| font-weight: 700; | |
| color: var(--text); | |
| margin-bottom: 4px; | |
| } | |
| .uniq-header p { font-size: 13px; color: var(--text3); margin: 0; } | |
| /* ── MESSAGES ── */ | |
| .msg-user-wrap { | |
| display: flex; | |
| justify-content: flex-end; | |
| gap: 10px; | |
| margin-bottom: 16px; | |
| animation: fadeUp .25s ease; | |
| } | |
| .msg-bot-wrap { | |
| display: flex; | |
| justify-content: flex-start; | |
| gap: 10px; | |
| margin-bottom: 16px; | |
| animation: fadeUp .25s ease; | |
| } | |
| @keyframes fadeUp { | |
| from { opacity:0; transform:translateY(10px); } | |
| to { opacity:1; transform:translateY(0); } | |
| } | |
| .avatar-bot { | |
| width: 34px; height: 34px; border-radius: 50%; | |
| background: linear-gradient(135deg, var(--accent), #8b5cf6); | |
| display: flex; align-items: center; justify-content: center; | |
| font-size: 15px; flex-shrink: 0; | |
| } | |
| .avatar-user { | |
| width: 34px; height: 34px; border-radius: 50%; | |
| background: var(--surface2); | |
| border: 1px solid var(--border2); | |
| display: flex; align-items: center; justify-content: center; | |
| font-size: 15px; flex-shrink: 0; | |
| } | |
| .bubble-user { | |
| max-width: 72%; | |
| padding: 12px 16px; | |
| border-radius: var(--radius); | |
| border-top-left-radius: 4px; | |
| background: linear-gradient(135deg, #1e3a8a, #1d3070); | |
| border: 1px solid rgba(79,125,255,0.3); | |
| font-size: 14px; line-height: 1.8; | |
| color: var(--text); | |
| direction: rtl; | |
| } | |
| .bubble-bot { | |
| max-width: 78%; | |
| padding: 12px 16px; | |
| border-radius: var(--radius); | |
| border-top-right-radius: 4px; | |
| background: var(--surface); | |
| border: 1px solid var(--border); | |
| font-size: 14px; line-height: 1.8; | |
| direction: rtl; | |
| color: var(--text); | |
| } | |
| .bubble-bot h1,.bubble-bot h2,.bubble-bot h3 { | |
| color: var(--accent2); margin: 10px 0 6px; font-size: 14px; | |
| } | |
| .bubble-bot ul,.bubble-bot ol { padding-right: 18px; margin: 6px 0; } | |
| .bubble-bot li { margin-bottom: 4px; } | |
| .bubble-bot code { | |
| background: var(--bg); color: var(--accent2); | |
| padding: 1px 6px; border-radius: 4px; | |
| font-family: var(--mono); font-size: 12px; | |
| } | |
| .bubble-bot strong { color: var(--accent2); } | |
| /* ── GPA RESULT BUBBLE ── */ | |
| /* bubble خاص بنتيجة الـ GPA تظهر كرد بوت ثابت في الشات */ | |
| .bubble-gpa-result { | |
| max-width: 78%; | |
| padding: 14px 16px; | |
| border-radius: var(--radius); | |
| border-top-right-radius: 4px; | |
| background: var(--surface); | |
| border: 1px solid var(--border2); | |
| font-size: 13px; | |
| direction: rtl; | |
| color: var(--text); | |
| } | |
| .gpa-result-big { | |
| font-size: 32px; | |
| font-weight: 700; | |
| color: var(--accent2); | |
| text-align: center; | |
| margin-bottom: 2px; | |
| } | |
| .gpa-result-sub { | |
| font-size: 11px; | |
| color: var(--text3); | |
| text-align: center; | |
| margin-bottom: 10px; | |
| } | |
| .gpa-grade-row { | |
| display: flex; | |
| align-items: center; | |
| justify-content: space-between; | |
| padding: 6px 10px; | |
| background: var(--bg); | |
| border-radius: var(--radius-sm); | |
| font-size: 12px; | |
| margin-bottom: 5px; | |
| } | |
| .gpa-grade-label { color: var(--text2); } | |
| .gpa-grade-badge { | |
| background: var(--accent-glow); | |
| color: var(--accent2); | |
| border: 1px solid rgba(79,125,255,0.25); | |
| padding: 2px 10px; | |
| border-radius: 20px; | |
| font-size: 11px; | |
| font-weight: 600; | |
| } | |
| .gpa-impossible { | |
| text-align: center; | |
| padding: 10px; | |
| background: rgba(239,68,68,0.08); | |
| border: 1px solid rgba(239,68,68,0.25); | |
| border-radius: var(--radius-sm); | |
| color: var(--danger); | |
| font-size: 13px; | |
| } | |
| /* ── SUGGESTIONS ── */ | |
| .sug-title { | |
| font-size: 12px; color: var(--text3); | |
| margin-bottom: 10px; margin-top: 8px; | |
| } | |
| .sug-btn-div button { | |
| width: 100% !important; | |
| background: var(--surface) !important; | |
| border: 1px solid var(--border) !important; | |
| color: var(--text2) !important; | |
| font-family: var(--font) !important; | |
| font-size: 13px !important; | |
| padding: 10px 14px !important; | |
| border-radius: var(--radius-sm) !important; | |
| text-align: right !important; | |
| line-height: 1.5 !important; | |
| transition: all .2s !important; | |
| min-height: 56px !important; | |
| } | |
| .sug-btn-div button:hover { | |
| border-color: var(--accent) !important; | |
| color: var(--text) !important; | |
| background: var(--accent-glow) !important; | |
| transform: translateY(-1px) !important; | |
| } | |
| /* ── CHAT INPUT ── */ | |
| [data-testid="stChatInput"] { | |
| background-color: var(--surface) !important; | |
| border: 1px solid var(--border) !important; | |
| border-radius: var(--radius) !important; | |
| padding: 4px !important; | |
| } | |
| [data-testid="stChatInput"] textarea { | |
| background-color: var(--bg2) !important; | |
| color: var(--text) !important; | |
| -webkit-text-fill-color: var(--text) !important; | |
| opacity: 1 !important; | |
| caret-color: var(--accent) !important; | |
| font-family: var(--font) !important; | |
| font-size: 15px !important; | |
| line-height: 1.6 !important; | |
| direction: rtl !important; | |
| border-radius: var(--radius-sm) !important; | |
| } | |
| [data-testid="stChatInput"] textarea:focus { | |
| color: var(--text) !important; | |
| -webkit-text-fill-color: var(--text) !important; | |
| background-color: var(--bg3) !important; | |
| } | |
| [data-testid="stChatInput"] textarea::placeholder { | |
| color: var(--text3) !important; | |
| -webkit-text-fill-color: var(--text3) !important; | |
| opacity: 0.8 !important; | |
| } | |
| [data-testid="stChatInput"] div { | |
| background-color: transparent !important; | |
| color: var(--text) !important; | |
| } | |
| [data-testid="stChatInput"] section { | |
| background-color: transparent !important; | |
| } | |
| /* ── COMPACT FORM (chatbot-sized) ── */ | |
| /* الـ expander نفسه */ | |
| .stExpander { | |
| background: var(--bg2) !important; | |
| border: 1px solid var(--border2) !important; | |
| border-radius: var(--radius) !important; | |
| /* محدودية عرض تناسب نافذة شات صغيرة */ | |
| max-width: 480px !important; | |
| } | |
| .stExpander summary { | |
| background: var(--surface) !important; | |
| color: var(--text) !important; | |
| font-family: var(--font) !important; | |
| font-size: 13px !important; | |
| font-weight: 600 !important; | |
| border-radius: var(--radius) !important; | |
| padding: 10px 14px !important; | |
| } | |
| .stExpander summary:hover { background: var(--surface2) !important; } | |
| .stExpander [data-testid="stExpanderDetails"] { | |
| background: var(--bg2) !important; | |
| padding: 12px !important; | |
| } | |
| /* labels داخل الفورم — أصغر */ | |
| .stExpander label, | |
| .stExpander .stSelectbox label, | |
| .stExpander .stTextInput label, | |
| .stExpander .stNumberInput label { | |
| color: var(--text3) !important; | |
| font-size: 11px !important; | |
| font-family: var(--font) !important; | |
| margin-bottom: 2px !important; | |
| } | |
| /* inputs داخل الفورم — compact */ | |
| .stExpander .stTextInput input, | |
| .stExpander .stNumberInput input, | |
| .stExpander .stSelectbox > div > div { | |
| background: var(--surface2) !important; | |
| border: 1px solid var(--border) !important; | |
| color: var(--text) !important; | |
| font-family: var(--font) !important; | |
| font-size: 13px !important; | |
| border-radius: var(--radius-sm) !important; | |
| padding: 6px 10px !important; | |
| min-height: 34px !important; | |
| } | |
| .stExpander .stTextInput input:focus, | |
| .stExpander .stNumberInput input:focus, | |
| .stExpander .stSelectbox > div > div:focus-within { | |
| border-color: var(--accent) !important; | |
| box-shadow: none !important; | |
| } | |
| /* primary btn داخل الفورم */ | |
| .stExpander .stButton > button[kind="primary"], | |
| .stExpander .stFormSubmitButton > button { | |
| background: var(--accent) !important; | |
| border: none !important; | |
| color: #fff !important; | |
| font-family: var(--font) !important; | |
| font-size: 12px !important; | |
| font-weight: 600 !important; | |
| padding: 7px 14px !important; | |
| border-radius: var(--radius-sm) !important; | |
| } | |
| .stExpander .stButton > button[kind="primary"]:hover, | |
| .stExpander .stFormSubmitButton > button:hover { | |
| background: var(--accent2) !important; | |
| } | |
| /* secondary btn داخل الفورم */ | |
| .stExpander .stButton > button[kind="secondary"] { | |
| background: transparent !important; | |
| border: 1px solid var(--border2) !important; | |
| color: var(--text2) !important; | |
| font-family: var(--font) !important; | |
| font-size: 12px !important; | |
| font-weight: 600 !important; | |
| padding: 7px 14px !important; | |
| border-radius: var(--radius-sm) !important; | |
| } | |
| .stExpander .stButton > button[kind="secondary"]:hover { | |
| border-color: var(--accent) !important; | |
| color: var(--text) !important; | |
| } | |
| /* alert boxes */ | |
| .stAlert { | |
| border-radius: var(--radius-sm) !important; | |
| font-family: var(--font) !important; | |
| font-size: 12px !important; | |
| padding: 8px 12px !important; | |
| } | |
| [data-testid="stAlertContentInfo"] { | |
| background: var(--accent-glow) !important; | |
| border: 1px solid rgba(79,125,255,0.25) !important; | |
| color: var(--accent2) !important; | |
| } | |
| [data-testid="stAlertContentSuccess"] { | |
| background: rgba(34,197,94,0.1) !important; | |
| border: 1px solid rgba(34,197,94,0.3) !important; | |
| color: var(--success) !important; | |
| } | |
| [data-testid="stAlertContentError"] { | |
| background: rgba(239,68,68,0.08) !important; | |
| border: 1px solid rgba(239,68,68,0.25) !important; | |
| color: var(--danger) !important; | |
| } | |
| /* ── SPINNER ── */ | |
| .stSpinner > div { border-top-color: var(--accent) !important; } | |
| /* user-card in sidebar */ | |
| .user-card { | |
| background: var(--surface); | |
| border: 1px solid var(--border); | |
| border-radius: var(--radius-sm); | |
| padding: 12px; | |
| margin: 8px 0; | |
| } | |
| .user-card-label { font-size: 11px; color: var(--text3); margin-bottom: 6px; } | |
| .user-id-val { | |
| font-family: var(--mono); | |
| font-size: 10px; | |
| color: var(--accent2); | |
| word-break: break-all; | |
| line-height: 1.6; | |
| } | |
| /* logo */ | |
| .sidebar-logo { | |
| display: flex; align-items: center; gap: 10px; | |
| padding: 4px 0 16px; | |
| border-bottom: 1px solid var(--border); | |
| margin-bottom: 4px; | |
| } | |
| .logo-icon { | |
| width: 36px; height: 36px; | |
| background: linear-gradient(135deg, var(--accent), #8b5cf6); | |
| border-radius: 10px; | |
| display: flex; align-items: center; justify-content: center; | |
| font-size: 18px; | |
| } | |
| .logo-text { font-size: 20px; font-weight: 700; color: var(--text); } | |
| .logo-text span { color: var(--accent2); } | |
| .sidebar-section-label { | |
| font-size: 11px; font-weight: 600; color: var(--text3); | |
| text-transform: uppercase; letter-spacing: 1.2px; | |
| margin-bottom: 6px; margin-top: 4px; | |
| } | |
| /* typing dots */ | |
| .typing-indicator { | |
| display: flex; gap: 5px; align-items: center; padding: 6px 2px; | |
| } | |
| .typing-indicator span { | |
| width: 7px; height: 7px; border-radius: 50%; | |
| background: var(--text3); | |
| animation: blink 1.2s infinite; | |
| display: inline-block; | |
| } | |
| .typing-indicator span:nth-child(2) { animation-delay: .2s; } | |
| .typing-indicator span:nth-child(3) { animation-delay: .4s; } | |
| @keyframes blink { | |
| 0%,80%,100% { opacity:.3; transform:scale(1); } | |
| 40% { opacity:1; transform:scale(1.25); } | |
| } | |
| /* number input arrows hide */ | |
| input[type=number]::-webkit-outer-spin-button, | |
| input[type=number]::-webkit-inner-spin-button { -webkit-appearance:none; } | |
| input[type=number] { -moz-appearance:textfield; } | |
| hr { border-color: var(--border) !important; } | |
| </style> | |
| """, unsafe_allow_html=True) | |
| # ========================= | |
| # 👤 USERS | |
| # ========================= | |
| DB_USERS = { | |
| "مستخدم عشوائي (Guest)": "random", | |
| "أحمد (user_01)": "user_01", | |
| "كنزي (user_02)": "user_02", | |
| "مريم (user_04)": "user_04", | |
| } | |
| # ========================= | |
| # 💡 SUGGESTIONS | |
| # ========================= | |
| SUGGESTIONS = [ | |
| "اسجل Convex Optimization ولا Bayesian Statistics؟", | |
| "عايز احسب الـ GPA", | |
| "عايز أوصل GPA 3.5 وأنا دلوقتي 2", | |
| "الـ GPA عبارة عن ايه؟", | |
| "ايه ال prerequisite لـ Data Structure؟", | |
| "رشحلي مواد سهلة الترم الجاي", | |
| ] | |
| # ========================= | |
| # 🧠 SESSION STATE | |
| # ========================= | |
| def _init_state(): | |
| if "selected_user_label" not in st.session_state: | |
| st.session_state.selected_user_label = "مستخدم عشوائي (Guest)" | |
| st.session_state.user_id = str(uuid.uuid4()) | |
| if "messages" not in st.session_state: | |
| st.session_state.messages = [] | |
| if "gpa_rows" not in st.session_state: | |
| st.session_state.gpa_rows = 1 | |
| if "current_intent" not in st.session_state: | |
| st.session_state.current_intent = None | |
| if "last_bot_answer" not in st.session_state: | |
| st.session_state.last_bot_answer = "" | |
| if "pending_prompt" not in st.session_state: | |
| st.session_state.pending_prompt = None | |
| if "calc_result" not in st.session_state: | |
| st.session_state.calc_result = None | |
| if "plan_result" not in st.session_state: | |
| st.session_state.plan_result = None | |
| if "invalid_grades" not in st.session_state: | |
| st.session_state.invalid_grades = [] | |
| _init_state() | |
| # ========================= | |
| # 🛠️ HELPER: build gpa_calc result bubble HTML | |
| # ========================= | |
| def _build_calc_bubble(gpa_val: float) -> str: | |
| return f""" | |
| <div class="bubble-gpa-result"> | |
| <div class="gpa-result-big">{gpa_val}</div> | |
| <div class="gpa-result-sub">📊 المعدل التراكمي المحسوب</div> | |
| </div>""" | |
| # ========================= | |
| # 🛠️ HELPER: build gpa_plan result bubble HTML | |
| # ========================= | |
| def _build_plan_bubble(pr: dict) -> str: | |
| if not pr.get("possible"): | |
| return """ | |
| <div class="bubble-gpa-result"> | |
| <div class="gpa-impossible">❌ صعب توصل للمعدل المطلوب بالساعات المتبقية</div> | |
| </div>""" | |
| grades_html = "".join( | |
| f'<div class="gpa-grade-row">' | |
| f'<span class="gpa-grade-label">{m["hours"]} ساعات</span>' | |
| f'<span class="gpa-grade-badge">{m["grade"]}</span>' | |
| f'</div>' | |
| for m in (pr.get("min_grades") or []) | |
| ) | |
| return f""" | |
| <div class="bubble-gpa-result"> | |
| <div class="gpa-result-big">{pr["required_avg_gpa"]}</div> | |
| <div class="gpa-result-sub">🎯 المعدل الفصلي المطلوب لتحقيق هدفك</div> | |
| <div style="font-size:11px;color:var(--text3);margin-bottom:6px;text-align:right"> | |
| أقل التقديرات المطلوبة لكل ساعة | |
| </div> | |
| {grades_html} | |
| </div>""" | |
| # ========================= | |
| # ⚙️ SIDEBAR | |
| # ========================= | |
| with st.sidebar: | |
| st.markdown(""" | |
| <div class="sidebar-logo"> | |
| <div class="logo-icon">🤖</div> | |
| <div class="logo-text">UNIQ <span>AI</span></div> | |
| </div> | |
| """, unsafe_allow_html=True) | |
| st.markdown('<div class="sidebar-section-label">المستخدم</div>', unsafe_allow_html=True) | |
| selected_label = st.selectbox( | |
| "user_select", | |
| options=list(DB_USERS.keys()), | |
| index=list(DB_USERS.keys()).index(st.session_state.selected_user_label), | |
| label_visibility="collapsed", | |
| ) | |
| if selected_label != st.session_state.selected_user_label: | |
| st.session_state.selected_user_label = selected_label | |
| st.session_state.user_id = ( | |
| str(uuid.uuid4()) | |
| if DB_USERS[selected_label] == "random" | |
| else DB_USERS[selected_label] | |
| ) | |
| st.session_state.messages = [] | |
| st.session_state.current_intent = None | |
| st.session_state.calc_result = None | |
| st.session_state.plan_result = None | |
| st.session_state.invalid_grades = [] | |
| st.rerun() | |
| st.markdown(f""" | |
| <div class="user-card"> | |
| <div class="user-card-label">🆔 User ID</div> | |
| <div class="user-id-val">{st.session_state.user_id}</div> | |
| </div> | |
| """, unsafe_allow_html=True) | |
| st.markdown("<br>", unsafe_allow_html=True) | |
| if st.button("🗑️ مسح المحادثة", use_container_width=True): | |
| st.session_state.messages = [] | |
| st.session_state.current_intent = None | |
| st.session_state.gpa_rows = 1 | |
| st.session_state.calc_result = None | |
| st.session_state.plan_result = None | |
| st.session_state.invalid_grades = [] | |
| st.rerun() | |
| # ========================= | |
| # 🤖 TOP HEADER | |
| # ========================= | |
| st.markdown(""" | |
| <div class="uniq-header"> | |
| <h1>🤖 UNIQ AI</h1> | |
| <p>مساعدك الأكاديمي الذكي للمواد والمعدل والخطة الدراسية</p> | |
| </div> | |
| """, unsafe_allow_html=True) | |
| st.markdown("<div style='height:12px'></div>", unsafe_allow_html=True) | |
| # ========================= | |
| # 💬 CHAT HISTORY | |
| # ========================= | |
| for msg in st.session_state.messages: | |
| if msg["role"] == "user": | |
| st.markdown(f""" | |
| <div class="msg-user-wrap"> | |
| <div class="bubble-user">{msg["content"]}</div> | |
| <div class="avatar-user">👤</div> | |
| </div>""", unsafe_allow_html=True) | |
| elif msg["role"] == "assistant": | |
| st.markdown(f""" | |
| <div class="msg-bot-wrap"> | |
| <div class="avatar-bot">🤖</div> | |
| <div class="bubble-bot">{msg["content"]}</div> | |
| </div>""", unsafe_allow_html=True) | |
| elif msg["role"] == "result": | |
| # ✅ نتيجة الـ GPA تظهر كبابل bot ثابت في الشات | |
| st.markdown(f""" | |
| <div class="msg-bot-wrap"> | |
| <div class="avatar-bot">🤖</div> | |
| {msg["content"]} | |
| </div>""", unsafe_allow_html=True) | |
| # ========================= | |
| # 💡 SUGGESTIONS (empty state) | |
| # ========================= | |
| if len(st.session_state.messages) == 0: | |
| st.markdown('<div class="sug-title">💡 اقتراحات سريعة</div>', unsafe_allow_html=True) | |
| col_a, col_b = st.columns(2) | |
| for i, sug in enumerate(SUGGESTIONS): | |
| target = col_a if i % 2 == 0 else col_b | |
| with target: | |
| st.markdown('<div class="sug-btn-div">', unsafe_allow_html=True) | |
| if st.button(sug, key=f"sug_{i}", use_container_width=True): | |
| st.session_state.pending_prompt = sug | |
| st.rerun() | |
| st.markdown('</div>', unsafe_allow_html=True) | |
| # ========================= | |
| # 🎯 GPA CALCULATOR PANEL | |
| # ========================= | |
| if st.session_state.current_intent == "gpa_calc": | |
| with st.expander("🧮 GPA Calculator", expanded=True): | |
| st.info(st.session_state.last_bot_answer or "أدخل درجاتك لحساب المعدل") | |
| subjects_data = [] | |
| for i in range(st.session_state.gpa_rows): | |
| c1, c2 = st.columns([3, 1]) | |
| with c1: | |
| grade = st.text_input( | |
| f"الدرجة / التقدير (مادة {i+1})", | |
| key=f"grade_{i}" | |
| ) | |
| with c2: | |
| hours = st.selectbox( | |
| "الساعات", | |
| [1, 2, 3, 4], | |
| index=2, | |
| key=f"hours_{i}" | |
| ) | |
| if grade: | |
| subjects_data.append({"grade": grade, "credit_hours": hours}) | |
| st.markdown("<div style='height:4px'></div>", unsafe_allow_html=True) | |
| btn_c1, btn_c2, btn_c3 = st.columns(3) | |
| # ➕ ADD ROW | |
| with btn_c1: | |
| if st.button("➕ إضافة مادة", key="add_row"): | |
| st.session_state.gpa_rows += 1 | |
| st.session_state.calc_result = None | |
| st.rerun() | |
| # 🧮 CALCULATE | |
| with btn_c2: | |
| if st.button("🧮 احسب الآن", key="calc_now", type="primary"): | |
| valid_letters = [ | |
| "A+","A","A-","B+","B","B-", | |
| "C+","C","C-","D+","D","F" | |
| ] | |
| validated_subjects = [] | |
| error_found = False | |
| st.session_state.invalid_grades = [] | |
| for i in range(st.session_state.gpa_rows): | |
| grade_raw = st.session_state.get(f"grade_{i}", "").strip() | |
| hours = st.session_state.get(f"hours_{i}", 3) | |
| if grade_raw != "": | |
| cleaned_grade = grade_raw.upper() | |
| is_valid = False | |
| if cleaned_grade in valid_letters: | |
| is_valid = True | |
| else: | |
| try: | |
| numeric_grade = float(cleaned_grade) | |
| if 0 <= numeric_grade <= 100: | |
| is_valid = True | |
| except: | |
| pass | |
| if is_valid: | |
| validated_subjects.append({ | |
| "grade": cleaned_grade, | |
| "credit_hours": hours | |
| }) | |
| else: | |
| st.error(f"⚠️ القيمة '{grade_raw}' غير صحيحة!") | |
| st.session_state.invalid_grades.append(i) | |
| error_found = True | |
| if error_found: | |
| st.warning("❌ يوجد أخطاء في الدرجات، صححها أولاً.") | |
| elif validated_subjects: | |
| try: | |
| res = requests.post(CALC_URL, json={"subjects": validated_subjects}) | |
| if res.status_code == 200: | |
| gpa_val = res.json().get("gpa") | |
| st.session_state.calc_result = gpa_val | |
| # ✅ أضف النتيجة كـ "result" message في الشات | |
| bubble_html = _build_calc_bubble(gpa_val) | |
| st.session_state.messages.append({ | |
| "role": "result", | |
| "content": bubble_html | |
| }) | |
| # ✅ أغلق الفورم تلقائياً بعد الحساب | |
| st.session_state.current_intent = None | |
| st.session_state.gpa_rows = 1 | |
| st.session_state.invalid_grades = [] | |
| st.rerun() | |
| else: | |
| st.error(f"❌ خطأ في الحساب: {res.text}") | |
| except Exception as e: | |
| st.error(f"❌ تعذر الاتصال بالسيرفر: {e}") | |
| else: | |
| st.warning("⚠️ أدخل مادة واحدة على الأقل") | |
| # ❌ CLOSE PANEL | |
| with btn_c3: | |
| if st.button("❌ إغلاق", key="calc_close"): | |
| st.session_state.current_intent = None | |
| st.session_state.gpa_rows = 1 | |
| st.session_state.calc_result = None | |
| st.session_state.invalid_grades = [] | |
| st.rerun() | |
| # ========================= | |
| # 🎯 GPA PLANNER PANEL | |
| # ========================= | |
| elif st.session_state.current_intent == "gpa_plan": | |
| with st.expander("🎯 GPA Planner", expanded=True): | |
| st.info(st.session_state.last_bot_answer or "حدد بياناتك لحساب الخطة") | |
| pc1, pc2 = st.columns(2) | |
| with pc1: | |
| target = st.number_input("المعدل المستهدف", value=3.5, min_value=0.0, max_value=4.0, step=0.1, key="plan_target") | |
| done = st.number_input("الساعات المنجزة", value=60, min_value=0, key="plan_done") | |
| with pc2: | |
| current = st.number_input("المعدل الحالي", value=2.5, min_value=0.0, max_value=4.0, step=0.1, key="plan_current") | |
| remain = st.number_input("الساعات المتبقية", value=15, min_value=1, key="plan_remain") | |
| st.markdown("<div style='height:4px'></div>", unsafe_allow_html=True) | |
| pb1, pb2 = st.columns(2) | |
| with pb1: | |
| if st.button("احسب الخطة", key="calc_plan", type="primary"): | |
| try: | |
| res = requests.post(PLAN_URL, json={ | |
| "target_gpa": target, | |
| "current_gpa": current, | |
| "completed_hours": done, | |
| "remaining_hours": remain, | |
| }) | |
| if res.status_code == 200: | |
| pr = res.json() | |
| # ✅ أضف النتيجة كـ "result" message في الشات | |
| bubble_html = _build_plan_bubble(pr) | |
| st.session_state.messages.append({ | |
| "role": "result", | |
| "content": bubble_html | |
| }) | |
| # ✅ أغلق الفورم تلقائياً بعد الحساب | |
| st.session_state.current_intent = None | |
| st.session_state.plan_result = None | |
| st.rerun() | |
| else: | |
| st.error(f"❌ خطأ في الحساب: {res.text}") | |
| except Exception as e: | |
| st.error(f"❌ تعذر الاتصال بالسيرفر: {e}") | |
| with pb2: | |
| if st.button("❌ إغلاق الخطة", key="plan_close"): | |
| st.session_state.current_intent = None | |
| st.session_state.plan_result = None | |
| st.rerun() | |
| # ========================= | |
| # 💬 CHAT INPUT | |
| # ========================= | |
| user_input = st.chat_input("اسأل UNIQ عن موادك أو معدلك أو خطتك الدراسية...") | |
| if st.session_state.pending_prompt: | |
| user_input = st.session_state.pending_prompt | |
| st.session_state.pending_prompt = None | |
| # ========================= | |
| # 🚀 SEND & RECEIVE | |
| # ========================= | |
| if user_input: | |
| st.session_state.messages.append({"role": "user", "content": user_input}) | |
| with st.spinner("🤖 جاري التفكير..."): | |
| try: | |
| res = requests.post( | |
| CHAT_URL, | |
| json={"user_id": st.session_state.user_id, "query": user_input}, | |
| timeout=30, | |
| ) | |
| if res.status_code == 200: | |
| data = res.json() | |
| answer = data.get("answer", "") | |
| intent = data.get("intent") | |
| st.session_state.last_bot_answer = answer | |
| blocked = any( | |
| m in answer for m in ["متاحة فقط للطلاب", "للطلاب المسجلين"] | |
| ) | |
| st.session_state.current_intent = None if blocked else intent | |
| st.session_state.calc_result = None | |
| st.session_state.plan_result = None | |
| html = answer | |
| html = html.replace("&", "&").replace("<", "<").replace(">", ">") | |
| html = re.sub(r'\*\*(.+?)\*\*', r'<strong>\1</strong>', html) | |
| html = re.sub(r'\*(.+?)\*', r'<em>\1</em>', html) | |
| html = re.sub(r'`(.+?)`', r'<code>\1</code>', html) | |
| html = re.sub(r'^### (.+)$', r'<h3>\1</h3>', html, flags=re.M) | |
| html = re.sub(r'^## (.+)$', r'<h2>\1</h2>', html, flags=re.M) | |
| html = re.sub(r'^# (.+)$', r'<h1>\1</h1>', html, flags=re.M) | |
| html = re.sub(r'^- (.+)$', r'<li>\1</li>', html, flags=re.M) | |
| html = re.sub(r'(<li>.*?</li>)', r'<ul>\1</ul>', html, flags=re.S) | |
| html = html.replace("\n", "<br>") | |
| st.session_state.messages.append({"role": "assistant", "content": html}) | |
| else: | |
| st.session_state.messages.append({ | |
| "role": "assistant", | |
| "content": f"❌ خطأ من السيرفر: {res.status_code}" | |
| }) | |
| except Exception as e: | |
| st.session_state.messages.append({ | |
| "role": "assistant", | |
| "content": "❌ تعذر الاتصال بالسيرفر" | |
| }) | |
| st.rerun() |