AlauStone commited on
Commit
616dfd4
·
verified ·
1 Parent(s): 29f4858

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -10
app.py CHANGED
@@ -1032,14 +1032,21 @@ def _check_and_sync():
1032
  _check_and_sync()
1033
 
1034
  # --- 登录用户自动模式切换:根据知识库内容决定默认模式 ---
1035
- if not IS_GUEST and "sel_web" not in st.session_state:
1036
- # 首次加载时,检查私有库是否有内容
 
 
 
 
1037
  priv_count = st.session_state.get("_sync_count_private")
1038
  if priv_count is None:
1039
  priv_count = _count_chunks(PRIVATE_SCOPE)
1040
  st.session_state["_sync_count_private"] = priv_count
1041
- # 有内容则默认知识库模式,否则联网模式
1042
- st.session_state["sel_web"] = (priv_count == 0)
 
 
 
1043
 
1044
 
1045
  # =========================
@@ -1375,6 +1382,9 @@ with st.sidebar:
1375
  _delete_uploaded_file_from_storage(PUBLIC_SCOPE, delete_target)
1376
  _list_uploaded_files_db.clear()
1377
  st.session_state["_sync_count_public"] = _count_chunks(PUBLIC_SCOPE)
 
 
 
1378
  st.toast(f"已删除 {delete_target}")
1379
  st.rerun()
1380
 
@@ -1403,6 +1413,8 @@ with st.sidebar:
1403
  # 检查是否有待处理的上传(通过 on_change 标记)
1404
  if pub_files and st.session_state.pop("_pending_upload_public", False):
1405
  process_upload(pub_files, "public", PUBLIC_SCOPE)
 
 
1406
 
1407
  if pub_chunk_count > 0 and len(pub_file_list) >= 2:
1408
  if st.button("🗑️ 清空公共库", use_container_width=True, type="secondary", key="clear_pub"):
@@ -1410,6 +1422,9 @@ with st.sidebar:
1410
  _clear_uploaded_files_storage(PUBLIC_SCOPE)
1411
  _list_uploaded_files_db.clear()
1412
  st.session_state["_sync_count_public"] = 0
 
 
 
1413
  st.toast("公共知识库已清空")
1414
  st.rerun()
1415
  else:
@@ -1435,10 +1450,9 @@ with st.sidebar:
1435
  _delete_chunks_by_file(PRIVATE_SCOPE, delete_target)
1436
  _delete_uploaded_file_from_storage(PRIVATE_SCOPE, delete_target)
1437
  _list_uploaded_files_db.clear()
1438
- new_count = _count_chunks(PRIVATE_SCOPE)
1439
- st.session_state["_sync_count_private"] = new_count
1440
- # 删除后如果私有库为空,自动切换到联网模式
1441
- if new_count == 0:
1442
  st.session_state["sel_web"] = True
1443
  st.toast(f"已删除 {delete_target}")
1444
  st.rerun()
@@ -1472,8 +1486,9 @@ with st.sidebar:
1472
  _clear_all_chunks(PRIVATE_SCOPE)
1473
  _clear_uploaded_files_storage(PRIVATE_SCOPE)
1474
  st.session_state["_sync_count_private"] = 0
1475
- # 清空后自动切换到联网模式
1476
- st.session_state["sel_web"] = True
 
1477
  st.toast("私有知识库已清空")
1478
  st.rerun()
1479
 
 
1032
  _check_and_sync()
1033
 
1034
  # --- 登录用户自动模式切换:根据知识库内容决定默认模式 ---
1035
+ def _has_any_kb_content():
1036
+ """检查是否有任何知识库内容(公共库或私有库)"""
1037
+ pub_count = st.session_state.get("_sync_count_public")
1038
+ if pub_count is None:
1039
+ pub_count = _count_chunks(PUBLIC_SCOPE)
1040
+ st.session_state["_sync_count_public"] = pub_count
1041
  priv_count = st.session_state.get("_sync_count_private")
1042
  if priv_count is None:
1043
  priv_count = _count_chunks(PRIVATE_SCOPE)
1044
  st.session_state["_sync_count_private"] = priv_count
1045
+ return (pub_count + priv_count) > 0
1046
+
1047
+ if not IS_GUEST and "sel_web" not in st.session_state:
1048
+ # 首次加载时,有任何知识库内容则默认知识库模式,否则联网模式
1049
+ st.session_state["sel_web"] = not _has_any_kb_content()
1050
 
1051
 
1052
  # =========================
 
1382
  _delete_uploaded_file_from_storage(PUBLIC_SCOPE, delete_target)
1383
  _list_uploaded_files_db.clear()
1384
  st.session_state["_sync_count_public"] = _count_chunks(PUBLIC_SCOPE)
1385
+ # 删除后如果所有知识库为空,自动切换到联网模式
1386
+ if not _has_any_kb_content():
1387
+ st.session_state["sel_web"] = True
1388
  st.toast(f"已删除 {delete_target}")
1389
  st.rerun()
1390
 
 
1413
  # 检查是否有待处理的上传(通过 on_change 标记)
1414
  if pub_files and st.session_state.pop("_pending_upload_public", False):
1415
  process_upload(pub_files, "public", PUBLIC_SCOPE)
1416
+ # 上传文件后自动切换到知识库模式
1417
+ st.session_state["sel_web"] = False
1418
 
1419
  if pub_chunk_count > 0 and len(pub_file_list) >= 2:
1420
  if st.button("🗑️ 清空公共库", use_container_width=True, type="secondary", key="clear_pub"):
 
1422
  _clear_uploaded_files_storage(PUBLIC_SCOPE)
1423
  _list_uploaded_files_db.clear()
1424
  st.session_state["_sync_count_public"] = 0
1425
+ # 清空后如果所有知识库为空,自动切换到联网模式
1426
+ if not _has_any_kb_content():
1427
+ st.session_state["sel_web"] = True
1428
  st.toast("公共知识库已清空")
1429
  st.rerun()
1430
  else:
 
1450
  _delete_chunks_by_file(PRIVATE_SCOPE, delete_target)
1451
  _delete_uploaded_file_from_storage(PRIVATE_SCOPE, delete_target)
1452
  _list_uploaded_files_db.clear()
1453
+ st.session_state["_sync_count_private"] = _count_chunks(PRIVATE_SCOPE)
1454
+ # 删除后如果所有知识库为空,自动切换到联网模式
1455
+ if not _has_any_kb_content():
 
1456
  st.session_state["sel_web"] = True
1457
  st.toast(f"已删除 {delete_target}")
1458
  st.rerun()
 
1486
  _clear_all_chunks(PRIVATE_SCOPE)
1487
  _clear_uploaded_files_storage(PRIVATE_SCOPE)
1488
  st.session_state["_sync_count_private"] = 0
1489
+ # 清空后如果所有知识库为空,自动切换到联网模式
1490
+ if not _has_any_kb_content():
1491
+ st.session_state["sel_web"] = True
1492
  st.toast("私有知识库已清空")
1493
  st.rerun()
1494