AlauStone commited on
Commit
eaa4f9c
·
verified ·
1 Parent(s): 42e1cea

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -19
app.py CHANGED
@@ -119,9 +119,25 @@ st.markdown(
119
  unsafe_allow_html=True,
120
  )
121
 
122
- # 未登录时显示简洁引导(登录后由 chat_box 内的欢迎页接管
 
123
  if not st.session_state.get("current_user"):
124
- st.caption("👈 请在左侧面板**登录**或**注册**后开始使用")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
125
 
126
  # =========================
127
  # 1.5 Supabase 客户端初始化
@@ -295,24 +311,31 @@ with st.sidebar:
295
  input_username = st.text_input("用户名", key="login_user")
296
  input_password = st.text_input("密码", type="password", key="login_pass")
297
 
298
- if input_username == "" or input_password == "":
299
- st.stop()
300
-
301
- ok, role, reason = verify_user(input_username, input_password)
302
- if not ok:
303
- st.session_state.login_attempts += 1
304
- remaining = MAX_LOGIN_ATTEMPTS - st.session_state.login_attempts
305
- if reason == "not_found":
306
- st.warning(f"⚠️ 用户不存在,请先注册(剩余 {remaining} 次)")
 
 
 
 
 
307
  else:
308
- st.warning(f"⚠️ 密码错误(剩余 {remaining} 次)")
309
- st.stop()
 
 
 
 
310
  else:
311
- st.session_state.login_attempts = 0
312
- st.session_state.current_user = input_username
313
- st.session_state.current_role = role
314
- role_label = "管理员" if role == "admin" else "普通用户"
315
- st.success(f"✅ {input_username}({role_label})")
316
 
317
  else: # 注册
318
  reg_user = st.text_input("用户名", key="reg_user")
@@ -337,6 +360,25 @@ with st.sidebar:
337
 
338
  CURRENT_USER = st.session_state.current_user
339
  IS_ADMIN = st.session_state.current_role == "admin"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
340
 
341
 
342
  # =========================
@@ -932,7 +974,7 @@ with st.sidebar:
932
  st.rerun()
933
 
934
  # --- 模型设置 ---
935
- with st.expander("⚙️ 模型设置", expanded=True):
936
  selected_display_name = st.selectbox(
937
  "模型", list(model_mapping.keys()), index=0, label_visibility="collapsed",
938
  key="sel_model"
 
119
  unsafe_allow_html=True,
120
  )
121
 
122
+ # 未登录时显示欢迎引导(登录后清除
123
+ _welcome_hero = st.empty()
124
  if not st.session_state.get("current_user"):
125
+ _welcome_hero.markdown(
126
+ """
127
+ <div style="text-align:center; color:#888; padding:80px 20px 20px;">
128
+ <div style="font-size:52px; margin-bottom:16px;">🤖</div>
129
+ <div style="font-size:20px; font-weight:600; margin-bottom:10px;">欢迎使用智答 AI 助手</div>
130
+ <div style="font-size:15px; line-height:2;">
131
+ 🌐 <b>联网模式</b> —— 大模型 + 网络搜索,实时回答<br>
132
+ 📚 <b>知识库模式</b> —— 上传文档,基于私有知识回答<br>
133
+ </div>
134
+ <div style="margin-top:18px; font-size:13px; color:#aaa;">
135
+ 👈 请在左侧面板<b>登录</b>或<b>注册</b>后开始使用
136
+ </div>
137
+ </div>
138
+ """,
139
+ unsafe_allow_html=True,
140
+ )
141
 
142
  # =========================
143
  # 1.5 Supabase 客户端初始化
 
311
  input_username = st.text_input("用户名", key="login_user")
312
  input_password = st.text_input("密码", type="password", key="login_pass")
313
 
314
+ if st.button("🔓 登录", use_container_width=True, type="primary", key="btn_login"):
315
+ if input_username == "" or input_password == "":
316
+ st.warning("请输入用户名和密码")
317
+ st.stop()
318
+
319
+ ok, role, reason = verify_user(input_username, input_password)
320
+ if not ok:
321
+ st.session_state.login_attempts += 1
322
+ remaining = MAX_LOGIN_ATTEMPTS - st.session_state.login_attempts
323
+ if reason == "not_found":
324
+ st.warning(f"⚠️ 用户不存在,请先注册(剩余 {remaining} 次)")
325
+ else:
326
+ st.warning(f"⚠️ 密码错误(剩余 {remaining} 次)")
327
+ st.stop()
328
  else:
329
+ st.session_state.login_attempts = 0
330
+ st.session_state.current_user = input_username
331
+ st.session_state.current_role = role
332
+ role_label = "管理员" if role == "admin" else "普通用户"
333
+ st.success(f"✅ {input_username}({role_label})")
334
+ st.rerun()
335
  else:
336
+ # 未点击登录按钮时,检查是否已登录
337
+ if not st.session_state.get("current_user"):
338
+ st.stop()
 
 
339
 
340
  else: # 注册
341
  reg_user = st.text_input("用户名", key="reg_user")
 
360
 
361
  CURRENT_USER = st.session_state.current_user
362
  IS_ADMIN = st.session_state.current_role == "admin"
363
+ _welcome_hero.empty() # 登录成功,清除欢迎页
364
+
365
+ # 登录后自动收起侧边栏(通过 JS 模拟点击收起按钮)
366
+ if not st.session_state.get("_sidebar_collapsed_once"):
367
+ st.session_state["_sidebar_collapsed_once"] = True
368
+ import streamlit.components.v1 as components
369
+ components.html(
370
+ """
371
+ <script>
372
+ (function() {
373
+ const btn = window.parent.document.querySelector(
374
+ '[data-testid="stSidebarCollapseButton"] button'
375
+ );
376
+ if (btn) btn.click();
377
+ })();
378
+ </script>
379
+ """,
380
+ height=0,
381
+ )
382
 
383
 
384
  # =========================
 
974
  st.rerun()
975
 
976
  # --- 模型设置 ---
977
+ with st.expander("⚙️ 模型设置"):
978
  selected_display_name = st.selectbox(
979
  "模型", list(model_mapping.keys()), index=0, label_visibility="collapsed",
980
  key="sel_model"